Skip to content

Commit

Permalink
- add search functionality to recyclerviews
Browse files Browse the repository at this point in the history
- version bump v1.2.0
  • Loading branch information
lmj0011 committed May 5, 2020
1 parent 6d58468 commit 91de985
Show file tree
Hide file tree
Showing 21 changed files with 430 additions and 65 deletions.
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ android {
applicationId "name.lmj0011.courierlocker"
minSdkVersion 19
targetSdkVersion 29
versionCode 47
versionCode 48
versionName "1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down Expand Up @@ -50,7 +50,7 @@ android {
productFlavors {
dev { // active development/testing
applicationIdSuffix ".dev"
versionNameSuffix ".1.3-dev"
versionNameSuffix ".2.0-dev"
resValue("string", "app_name", "Courier Locker (dev)")
manifestPlaceholders = [
appIcon: "@mipmap/ic_launcher_dev",
Expand All @@ -60,7 +60,7 @@ android {
}
beta { // public testing
applicationIdSuffix ".beta"
versionNameSuffix ".1.3-beta"
versionNameSuffix ".2.0-beta"
resValue("string", "app_name", "Courier Locker (beta)")
manifestPlaceholders = [
appIcon: "@mipmap/ic_launcher_beta",
Expand All @@ -70,7 +70,7 @@ android {
}
prod { // official release
applicationIdSuffix ".prod"
versionNameSuffix ".1.3"
versionNameSuffix ".2.0"
resValue("string", "app_name", "Courier Locker")
manifestPlaceholders = [
appIcon: "@mipmap/ic_launcher",
Expand Down
20 changes: 18 additions & 2 deletions app/src/main/java/name/lmj0011/courierlocker/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,28 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte

override fun onSupportNavigateUp(): Boolean {
when(navController.currentDestination?.id) {
// seems to be the only way to get the Maps recyclerview to show updated items when navigated
// from EditAptBuildingsMapsFragment
R.id.editTripFragment -> {
// resets the recyclerview to original state
navController.navigate(R.id.tripsFragment)
return true
}
R.id.editAptBuildingsMapsFragment -> {
// resets the recyclerview to original state
navController.navigate(R.id.mapsFragment)
return true
}
R.id.editGateCodeFragment -> {
// resets the recyclerview to original state
navController.navigate(R.id.gateCodesFragment)
return true
}
R.id.editCustomerFragment -> {
// resets the recyclerview to original state
navController.navigate(R.id.customersFragment)
return true
}

else -> { }
}

return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,16 @@ class CustomerListAdapter(private val clickListener: CustomerListener): ListAdap
return ViewHolder.from(parent)
}

fun filterBySearchQuery(query: String?, list: MutableList<Customer>): MutableList<Customer> {
if (query.isNullOrBlank()) return list

return list.filter {
val inName = it.name.contains(query, true)
val inAddress = it.address.contains(query, true)
val inNote = it.note.contains(query, true)

return@filter inName || inAddress || inNote
}.toMutableList()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,17 @@ class GateCodeListAdapter(private val clickListener: GateCodeListener): ListAdap
)
}.toMutableList()
}

fun filterBySearchQuery(query: String?, list: MutableList<GateCode>): MutableList<GateCode> {
if (query.isNullOrBlank()) return list

return list.filter {
val inAddress = it.address.contains(query, true)
val inCodes = it.codes.any { str ->
str.contains(query, true)
}

return@filter inAddress || inCodes
}.toMutableList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class MapListAdapter(private val clickListener: MapListener, private val parentF
return ViewHolder.from(parent, this.parentFragment)
}

fun filterByClosestGateCodeLocation(list: MutableList<Apartment>): MutableList<Apartment> {
fun filterByClosestLocation(list: MutableList<Apartment>): MutableList<Apartment> {
return list.sortedBy {
LocationHelper.calculateApproxDistanceBetweenMapPoints(
LocationHelper.lastLatitude.value!!,
Expand All @@ -139,5 +139,16 @@ class MapListAdapter(private val clickListener: MapListener, private val parentF
)
}.toMutableList()
}

fun filterBySearchQuery(query: String?, list: MutableList<Apartment>): MutableList<Apartment> {
if (query.isNullOrBlank()) return list

return list.filter {
val inName = it.name.contains(query, true)
val inAddress = it.address.contains(query, true)

return@filter inName || inAddress
}.toMutableList()
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,18 @@ class TripListAdapter(private val clickListener: TripListener): ListAdapter<Trip
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder.from(parent)
}

fun filterBySearchQuery(query: String?, list: MutableList<Trip>): MutableList<Trip> {
if (query.isNullOrBlank()) return list

return list.filter {
val inDate = getTripDate(it).contains(query, true)
val inPickupAddress = it.pickupAddress.contains(query, true)
val inDropOffAddress = it.dropOffAddress.contains(query, true)
val inPayAmount = it.payAmount.contains(query, true)
val inGigName = it.gigName.contains(query, true)

return@filter inGigName || inPickupAddress || inDropOffAddress|| inPayAmount || inDate
}.toMutableList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package name.lmj0011.courierlocker.fragments
import android.content.SharedPreferences
import android.os.Bundle
import android.view.*
import androidx.appcompat.widget.SearchView
import androidx.fragment.app.Fragment
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.DrawableCompat
Expand All @@ -14,28 +15,37 @@ import androidx.lifecycle.ViewModelProviders
import androidx.navigation.fragment.findNavController
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.DividerItemDecoration
import kotlinx.coroutines.*
import name.lmj0011.courierlocker.MainActivity

import name.lmj0011.courierlocker.databinding.FragmentCustomersBinding
import name.lmj0011.courierlocker.R
import name.lmj0011.courierlocker.adapters.CustomerListAdapter
import name.lmj0011.courierlocker.database.CourierLockerDatabase
import name.lmj0011.courierlocker.database.Customer
import name.lmj0011.courierlocker.factories.CustomerViewModelFactory
import name.lmj0011.courierlocker.fragments.dialogs.ClearAllCustomersDialogFragment
import name.lmj0011.courierlocker.helpers.interfaces.SearchableRecyclerView
import name.lmj0011.courierlocker.viewmodels.CustomerViewModel

/**
* A simple [Fragment] subclass.
*
*/
class CustomersFragment : Fragment(), ClearAllCustomersDialogFragment.NoticeDialogListener {
class CustomersFragment :
Fragment(),
ClearAllCustomersDialogFragment.NoticeDialogListener,
SearchableRecyclerView
{

private lateinit var binding: FragmentCustomersBinding
private lateinit var mainActivity: MainActivity
private lateinit var viewModelFactory: CustomerViewModelFactory
private lateinit var listAdapter: CustomerListAdapter
private lateinit var customerViewModel: CustomerViewModel
private lateinit var sharedPreferences: SharedPreferences
private var fragmentJob: Job = Job()
private val uiScope = CoroutineScope(Dispatchers.Main + fragmentJob)

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
Expand Down Expand Up @@ -73,6 +83,39 @@ class CustomersFragment : Fragment(), ClearAllCustomersDialogFragment.NoticeDial
binding.generateCustomerBtn.visibility = View.GONE
}

binding.customersSearchView.setOnQueryTextListener(object: SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
return false
}

override fun onQueryTextChange(newText: String?): Boolean {
var list = customerViewModel.customers.value

list?.let {
uiScope.launch {
val filteredList = withContext(Dispatchers.Default) {
listAdapter.filterBySearchQuery(newText, it)
}

this@CustomersFragment.submitListToAdapter(filteredList)
}
}
return false
}
})

binding.customersSearchView.setOnCloseListener {
this@CustomersFragment.toggleSearch(mainActivity, binding.customersSearchView, false)
false
}

binding.customersSearchView.setOnQueryTextFocusChangeListener { view, hasFocus ->
if (hasFocus) { } else{
binding.customersSearchView.setQuery("", true)
this@CustomersFragment.toggleSearch(mainActivity, binding.customersSearchView, false)
}
}

return binding.root
}

Expand All @@ -85,6 +128,11 @@ class CustomersFragment : Fragment(), ClearAllCustomersDialogFragment.NoticeDial
this.applyPreferences()
}

override fun onDestroy() {
super.onDestroy()
fragmentJob?.cancel()
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
inflater.inflate(R.menu.customers, menu)
Expand All @@ -95,6 +143,10 @@ class CustomersFragment : Fragment(), ClearAllCustomersDialogFragment.NoticeDial
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_maps_search -> {
this@CustomersFragment.toggleSearch(mainActivity, binding.customersSearchView, true)
true
}
R.id.action_customers_clear_all -> {
this.showClearAllCustomersDialog()
true
Expand Down Expand Up @@ -136,4 +188,9 @@ class CustomersFragment : Fragment(), ClearAllCustomersDialogFragment.NoticeDial
)
}

private fun submitListToAdapter (list: MutableList<Customer>) {
listAdapter.submitList(list)
listAdapter.notifyDataSetChanged()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -236,24 +236,6 @@ class EditAptBuildingsMapsFragment : Fragment(){
fragmentJob.cancel()
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
inflater.inflate(R.menu.edit_map_feeds, menu)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_edit_map_feeds -> {
this.findNavController().navigate(MapsFragmentDirections.actionMapsFragmentToEditMapFeedsFragment())
true
}
else -> super.onOptionsItemSelected(item)
}
}

private fun applyPreferences() { }


Expand Down
Loading

0 comments on commit 91de985

Please sign in to comment.