Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ package org.fossasia.openevent.general.order

import android.os.Bundle
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.navigation.Navigation.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.dialog_filter_order.view.completedOrdersCheckBox
import kotlinx.android.synthetic.main.dialog_filter_order.view.pendingOrdersCheckBox
import kotlinx.android.synthetic.main.dialog_filter_order.view.placedOrdersCheckBox
import kotlinx.android.synthetic.main.dialog_filter_order.view.dateRadioButton
import kotlinx.android.synthetic.main.dialog_filter_order.view.orderStatusRadioButton
import kotlinx.android.synthetic.main.fragment_expired_order.view.ordersRecycler
import kotlinx.android.synthetic.main.fragment_expired_order.view.noTicketsScreen
import kotlinx.android.synthetic.main.fragment_expired_order.view.shimmerSearch
import kotlinx.android.synthetic.main.fragment_expired_order.view.filterToolbar
import kotlinx.android.synthetic.main.fragment_expired_order.view.toolbar
import org.fossasia.openevent.general.R
import org.fossasia.openevent.general.event.EventUtils
import org.fossasia.openevent.general.utils.Utils.setToolbar
import org.fossasia.openevent.general.utils.extensions.nonNull
import org.jetbrains.anko.design.longSnackbar
Expand All @@ -29,9 +35,12 @@ class ExpiredOrderFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
rootView = inflater.inflate(R.layout.fragment_expired_order, container, false)
setToolbar(activity, getString(R.string.past_tickets))
setHasOptionsMenu(true)
setToolbar(activity, show = false)
rootView.toolbar.setNavigationOnClickListener {
activity?.onBackPressed()
}

ordersRecyclerAdapter.setShowExpired(true)
val linearLayoutManager = LinearLayoutManager(context)
linearLayoutManager.orientation = RecyclerView.VERTICAL
rootView.ordersRecycler.layoutManager = linearLayoutManager
Expand All @@ -53,21 +62,24 @@ class ExpiredOrderFragment : Fragment() {
ordersUnderUserVM.noTickets
.nonNull()
.observe(viewLifecycleOwner, Observer {
rootView.noTicketsScreen.isVisible = it
showNoTicketsScreen(it)
})

ordersUnderUserVM.eventAndOrder
.nonNull()
.observe(viewLifecycleOwner, Observer {
val list = it.sortedByDescending {
EventUtils.getTimeInMilliSeconds(it.first.startsAt, null)
}
ordersRecyclerAdapter.addAllPairs(list, true)
ordersRecyclerAdapter.notifyDataSetChanged()
ordersRecyclerAdapter.setSavedEventAndOrder(it)
applyFilter()
Timber.d("Fetched events of size %s", ordersRecyclerAdapter.itemCount)
})

ordersUnderUserVM.ordersUnderUser(true)
val currentOrdersAndEvent = ordersUnderUserVM.eventAndOrder.value
if (currentOrdersAndEvent == null) {
ordersUnderUserVM.ordersUnderUser(true)
} else {
ordersRecyclerAdapter.setSavedEventAndOrder(currentOrdersAndEvent)
applyFilter()
}

return rootView
}
Expand All @@ -81,15 +93,45 @@ class ExpiredOrderFragment : Fragment() {
}
}
ordersRecyclerAdapter.setListener(recyclerViewClickListener)
rootView.filterToolbar.setOnClickListener {
showFilterDialog()
}
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> {
activity?.onBackPressed()
true
}
else -> super.onOptionsItemSelected(item)
}
private fun showFilterDialog() {
val filterLayout = layoutInflater.inflate(R.layout.dialog_filter_order, null)
filterLayout.completedOrdersCheckBox.isChecked = ordersUnderUserVM.isShowingCompletedOrders
filterLayout.pendingOrdersCheckBox.isChecked = ordersUnderUserVM.isShowingPendingOrders
filterLayout.placedOrdersCheckBox.isChecked = ordersUnderUserVM.isShowingPlacedOrders
if (ordersUnderUserVM.isSortingOrdersByDate)
filterLayout.dateRadioButton.isChecked = true
else
filterLayout.orderStatusRadioButton.isChecked = true

AlertDialog.Builder(requireContext())
.setView(filterLayout)
.setNegativeButton(getString(R.string.cancel)) { dialog, _ ->
dialog.cancel()
}.setPositiveButton(getString(R.string.apply)) { _, _ ->
ordersUnderUserVM.isShowingCompletedOrders = filterLayout.completedOrdersCheckBox.isChecked
ordersUnderUserVM.isShowingPendingOrders = filterLayout.pendingOrdersCheckBox.isChecked
ordersUnderUserVM.isShowingPlacedOrders = filterLayout.placedOrdersCheckBox.isChecked
ordersUnderUserVM.isSortingOrdersByDate = filterLayout.dateRadioButton.isChecked
applyFilter()
}.create().show()
}

private fun applyFilter() {
ordersRecyclerAdapter.setFilter(
completed = ordersUnderUserVM.isShowingCompletedOrders,
placed = ordersUnderUserVM.isShowingPlacedOrders,
pending = ordersUnderUserVM.isShowingPendingOrders,
sortByDate = ordersUnderUserVM.isSortingOrdersByDate
)
showNoTicketsScreen(ordersRecyclerAdapter.itemCount == 0)
}

private fun showNoTicketsScreen(show: Boolean) {
rootView.noTicketsScreen.isVisible = show
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,72 @@ package org.fossasia.openevent.general.order
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import org.fossasia.openevent.general.attendees.ORDER_STATUS_COMPLETED
import org.fossasia.openevent.general.attendees.ORDER_STATUS_PENDING
import org.fossasia.openevent.general.attendees.ORDER_STATUS_PLACED
import org.fossasia.openevent.general.event.Event
import org.fossasia.openevent.general.databinding.ItemCardOrderBinding
import org.fossasia.openevent.general.event.EventUtils

class OrdersRecyclerAdapter : RecyclerView.Adapter<OrdersViewHolder>() {

private val savedEventAndOrder = ArrayList<Pair<Event, Order>>()
private val eventAndOrderIdentifier = ArrayList<Pair<Event, Order>>()
private var showExpired = false
private var clickListener: OrderClickListener? = null

private var isShowingCompletedOrders = true
private var isShowingPendingOrders = true
private var isShowingPlacedOrders = true
private var isSortingOrdersByDate = true

fun setListener(listener: OrderClickListener?) {
clickListener = listener
}

fun addAllPairs(list: List<Pair<Event, Order>>, showExpired: Boolean) {
fun setShowExpired(expired: Boolean) {
showExpired = expired
}

fun setSavedEventAndOrder(list: List<Pair<Event, Order>>) {
if (savedEventAndOrder.isNotEmpty())
savedEventAndOrder.clear()
savedEventAndOrder.addAll(list)
}

private fun addAllPairs() {
if (eventAndOrderIdentifier.isNotEmpty())
this.eventAndOrderIdentifier.clear()
eventAndOrderIdentifier.addAll(list)
this.showExpired = showExpired
val filteredList = ArrayList<Pair<Event, Order>>()
if (isShowingCompletedOrders) {
filteredList.addAll(savedEventAndOrder.filter { it.second.status == ORDER_STATUS_COMPLETED })
}
if (isShowingPendingOrders) {
filteredList.addAll(savedEventAndOrder.filter { it.second.status == ORDER_STATUS_PENDING })
}
if (isShowingPlacedOrders) {
filteredList.addAll(savedEventAndOrder.filter { it.second.status == ORDER_STATUS_PLACED })
}
if (isSortingOrdersByDate) {
filteredList.sortedByDescending { eventAndOrder ->
EventUtils.getTimeInMilliSeconds(eventAndOrder.first.startsAt, null)
}.also { eventAndOrderIdentifier.addAll(it) }
} else {
filteredList.sortedBy { eventAndOrder ->
eventAndOrder.second.status
}.also {
eventAndOrderIdentifier.addAll(it)
}
}
notifyDataSetChanged()
}

fun setFilter(completed: Boolean, pending: Boolean, placed: Boolean, sortByDate: Boolean) {
isShowingPlacedOrders = placed
isShowingPendingOrders = pending
isShowingCompletedOrders = completed
isSortingOrdersByDate = sortByDate
addAllPairs()
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OrdersViewHolder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import androidx.core.view.isVisible
import androidx.core.widget.NestedScrollView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.navigation.Navigation.findNavController
import kotlinx.android.synthetic.main.dialog_filter_order.view.orderStatusRadioButton
import kotlinx.android.synthetic.main.dialog_filter_order.view.dateRadioButton
import kotlinx.android.synthetic.main.dialog_filter_order.view.completedOrdersCheckBox
import kotlinx.android.synthetic.main.dialog_filter_order.view.pendingOrdersCheckBox
import kotlinx.android.synthetic.main.dialog_filter_order.view.placedOrdersCheckBox
import kotlinx.android.synthetic.main.fragment_orders_under_user.view.findMyTickets
import kotlinx.android.synthetic.main.fragment_orders_under_user.view.noTicketsScreen
import kotlinx.android.synthetic.main.fragment_orders_under_user.view.ordersRecycler
Expand All @@ -21,9 +27,10 @@ import kotlinx.android.synthetic.main.fragment_orders_under_user.view.pastEvent
import kotlinx.android.synthetic.main.fragment_orders_under_user.view.ticketsNumber
import kotlinx.android.synthetic.main.fragment_orders_under_user.view.toolbarLayout
import kotlinx.android.synthetic.main.fragment_orders_under_user.view.ticketsTitle
import kotlinx.android.synthetic.main.fragment_orders_under_user.view.filterToolbar
import kotlinx.android.synthetic.main.fragment_orders_under_user.view.filter
import org.fossasia.openevent.general.R
import org.fossasia.openevent.general.BottomIconDoubleClick
import org.fossasia.openevent.general.event.EventUtils
import org.fossasia.openevent.general.utils.Utils
import org.fossasia.openevent.general.utils.extensions.nonNull
import org.koin.androidx.viewmodel.ext.android.viewModel
Expand Down Expand Up @@ -56,15 +63,21 @@ class OrdersUnderUserFragment : Fragment(), BottomIconDoubleClick {
rootView = inflater.inflate(R.layout.fragment_orders_under_user, container, false)
setToolbar(activity, show = false)

ordersRecyclerAdapter.setShowExpired(false)
rootView.ordersRecycler.adapter = ordersRecyclerAdapter
rootView.ordersRecycler.isNestedScrollingEnabled = false

val linearLayoutManager = LinearLayoutManager(context)
linearLayoutManager.orientation = RecyclerView.VERTICAL
rootView.ordersRecycler.layoutManager = linearLayoutManager

if (ordersUnderUserVM.eventAndOrder.value == null)
val currentEventAndOrder = ordersUnderUserVM.eventAndOrder.value
if (currentEventAndOrder == null)
ordersUnderUserVM.ordersUnderUser(false)
else {
ordersRecyclerAdapter.setSavedEventAndOrder(currentEventAndOrder)
applyFilter()
}

ordersUnderUserVM.showShimmerResults
.nonNull()
Expand All @@ -87,12 +100,9 @@ class OrdersUnderUserFragment : Fragment(), BottomIconDoubleClick {
ordersUnderUserVM.eventAndOrder
.nonNull()
.observe(viewLifecycleOwner, Observer {
val list = it.sortedByDescending {
EventUtils.getTimeInMilliSeconds(it.first.startsAt, null)
}
rootView.ticketsNumber.text = "${it.size} orders"
ordersRecyclerAdapter.addAllPairs(list, false)
ordersRecyclerAdapter.notifyDataSetChanged()
ordersRecyclerAdapter.setSavedEventAndOrder(it)
applyFilter()
Timber.d("Fetched events of size %s", ordersRecyclerAdapter.itemCount)
})

Expand Down Expand Up @@ -123,6 +133,46 @@ class OrdersUnderUserFragment : Fragment(), BottomIconDoubleClick {
rootView.toolbarLayout.hideWithFading()
}
}
rootView.filter.setOnClickListener {
showFilterDialog()
}
rootView.filterToolbar.setOnClickListener {
showFilterDialog()
}
}

private fun showFilterDialog() {
val filterLayout = layoutInflater.inflate(R.layout.dialog_filter_order, null)
filterLayout.completedOrdersCheckBox.isChecked = ordersUnderUserVM.isShowingCompletedOrders
filterLayout.pendingOrdersCheckBox.isChecked = ordersUnderUserVM.isShowingPendingOrders
filterLayout.placedOrdersCheckBox.isChecked = ordersUnderUserVM.isShowingPlacedOrders
if (ordersUnderUserVM.isSortingOrdersByDate)
filterLayout.dateRadioButton.isChecked = true
else
filterLayout.orderStatusRadioButton.isChecked = true

AlertDialog.Builder(requireContext())
.setView(filterLayout)
.setNegativeButton(getString(R.string.cancel)) { dialog, _ ->
dialog.cancel()
}.setPositiveButton(getString(R.string.apply)) { _, _ ->
ordersUnderUserVM.isShowingCompletedOrders = filterLayout.completedOrdersCheckBox.isChecked
ordersUnderUserVM.isShowingPendingOrders = filterLayout.pendingOrdersCheckBox.isChecked
ordersUnderUserVM.isShowingPlacedOrders = filterLayout.placedOrdersCheckBox.isChecked
ordersUnderUserVM.isSortingOrdersByDate = filterLayout.dateRadioButton.isChecked
applyFilter()
}.create().show()
}

private fun applyFilter() {
ordersRecyclerAdapter.setFilter(
completed = ordersUnderUserVM.isShowingCompletedOrders,
placed = ordersUnderUserVM.isShowingPlacedOrders,
pending = ordersUnderUserVM.isShowingPendingOrders,
sortByDate = ordersUnderUserVM.isSortingOrdersByDate
)
rootView.ticketsNumber.text = "${ordersRecyclerAdapter.itemCount} orders"
showNoTicketsScreen(ordersRecyclerAdapter.itemCount == 0)
}

override fun onDestroyView() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ class OrdersUnderUserViewModel(
private val mutableNoTickets = MutableLiveData<Boolean>()
val noTickets: LiveData<Boolean> = mutableNoTickets

// Retain filter options
var isShowingCompletedOrders = true
var isShowingPendingOrders = true
var isShowingPlacedOrders = true
var isSortingOrdersByDate = true

fun getId() = authHolder.getId()

fun isLoggedIn() = authHolder.isLoggedIn()
Expand Down
Loading