Skip to content

Commit

Permalink
avoid usage of instanceState (use viewmodel instead)
Browse files Browse the repository at this point in the history
use viewModel to keep track of query

change handling for textChanged listener

minor changes

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
  • Loading branch information
mahibi committed Oct 2, 2023
1 parent 16f6e3c commit 8f15c3c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package com.nextcloud.talk.adapters

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
Expand All @@ -35,6 +36,8 @@ class GeocodingAdapter(private val context: Context, private var dataSource: Lis
interface OnItemClickListener {
fun onItemClick(position: Int)
}

@SuppressLint("NotifyDataSetChanged")
fun updateData(data: List<Address>) {
this.dataSource = data
notifyDataSetChanged()
Expand Down
97 changes: 36 additions & 61 deletions app/src/main/java/com/nextcloud/talk/location/GeocodingActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ import android.view.Menu
import android.view.MenuItem
import android.view.inputmethod.EditorInfo
import androidx.appcompat.widget.SearchView
import androidx.core.view.MenuItemCompat
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.LinearLayoutManager
Expand All @@ -55,8 +53,7 @@ import javax.inject.Inject

@AutoInjector(NextcloudTalkApplication::class)
class GeocodingActivity :
BaseActivity(),
SearchView.OnQueryTextListener {
BaseActivity() {

private lateinit var binding: ActivityGeocodingBinding

Expand All @@ -67,11 +64,10 @@ class GeocodingActivity :
lateinit var okHttpClient: OkHttpClient

lateinit var roomToken: String
var nominatimClient: TalkJsonNominatimClient? = null
private var nominatimClient: TalkJsonNominatimClient? = null

var searchItem: MenuItem? = null
private var searchItem: MenuItem? = null
var searchView: SearchView? = null
var query: String? = null

lateinit var adapter: GeocodingAdapter
private var geocodingResults: List<Address> = ArrayList()
Expand All @@ -90,34 +86,24 @@ class GeocodingActivity :
Configuration.getInstance().load(context, PreferenceManager.getDefaultSharedPreferences(context))

roomToken = intent.getStringExtra(BundleKeys.KEY_ROOM_TOKEN)!!
query = intent.getStringExtra(BundleKeys.KEY_GEOCODING_QUERY)

recyclerView = findViewById(R.id.geocoding_results)
recyclerView.layoutManager = LinearLayoutManager(this)
adapter = GeocodingAdapter(this, geocodingResults)
recyclerView.adapter = adapter
viewModel = ViewModelProvider(this).get(GeoCodingViewModel::class.java)
viewModel = ViewModelProvider(this)[GeoCodingViewModel::class.java]

query = viewModel.getQuery()
if (query.isNullOrEmpty()) {
query = intent.getStringExtra(BundleKeys.KEY_GEOCODING_QUERY)
viewModel.setQuery(query ?: "")
var query = viewModel.getQuery()
if (query.isEmpty() && intent.hasExtra(BundleKeys.KEY_GEOCODING_QUERY)) {
query = intent.getStringExtra(BundleKeys.KEY_GEOCODING_QUERY).orEmpty()
viewModel.setQuery(query)
}
val savedResults = viewModel.getGeocodingResults()
initAdapter(savedResults)
viewModel.getGeocodingResultsLiveData().observe(
this,
Observer { results ->
geocodingResults = results
adapter.updateData(results)
}
)
viewModel.getQueryLiveData().observe(
this,
Observer { newQuery ->
query = newQuery
searchView?.setQuery(query, false)
}
)
viewModel.getGeocodingResultsLiveData().observe(this) { results ->
geocodingResults = results
adapter.updateData(results)
}
val baseUrl = getString(R.string.osm_geocoder_url)
val email = context.getString(R.string.osm_geocoder_contact)
nominatimClient = TalkJsonNominatimClient(baseUrl, okHttpClient, email)
Expand All @@ -132,8 +118,8 @@ class GeocodingActivity :
override fun onResume() {
super.onResume()

if (!query.isNullOrEmpty()) {
viewModel.searchLocation(query!!)
if (viewModel.getQuery().isNotEmpty() && adapter.itemCount == 0) {
viewModel.searchLocation()
} else {
Log.e(TAG, "search string that was passed to GeocodingController was null or empty")
}
Expand All @@ -148,7 +134,7 @@ class GeocodingActivity :
startActivity(intent)
}
})
searchView?.setQuery(query, false)
searchView?.setQuery(viewModel.getQuery(), false)
}

private fun setupActionBar() {
Expand Down Expand Up @@ -184,43 +170,17 @@ class GeocodingActivity :
menuInflater.inflate(R.menu.menu_geocoding, menu)
searchItem = menu.findItem(R.id.geocoding_action_search)
initSearchView()

searchItem?.expandActionView()
searchView?.setQuery(query, false)
searchView?.clearFocus()
return true
}

override fun onQueryTextSubmit(query: String?): Boolean {
this.query = query ?: ""
viewModel.setQuery(this.query!!)
if (query != null) {
viewModel.searchLocation(query)
}
searchView?.setQuery(viewModel.getQuery(), false)
searchView?.clearFocus()
return true
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
query?.let { viewModel.setQuery(it) }
outState.putString(KEY_SEARCH_QUERY, query)
}

override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
query = viewModel.getQuery()
query = savedInstanceState.getString(KEY_SEARCH_QUERY)
}

override fun onQueryTextChange(newText: String?): Boolean {
return true
}

private fun initSearchView() {
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
if (searchItem != null) {
searchView = MenuItemCompat.getActionView(searchItem) as SearchView
searchView = searchItem!!.actionView as SearchView?

searchView?.maxWidth = Int.MAX_VALUE
searchView?.inputType = InputType.TYPE_TEXT_VARIATION_FILTER
var imeOptions = EditorInfo.IME_ACTION_DONE or EditorInfo.IME_FLAG_NO_FULLSCREEN
Expand All @@ -230,7 +190,23 @@ class GeocodingActivity :
searchView?.imeOptions = imeOptions
searchView?.queryHint = resources!!.getString(R.string.nc_search)
searchView?.setSearchableInfo(searchManager.getSearchableInfo(componentName))
searchView?.setOnQueryTextListener(this)
searchView?.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String): Boolean {
viewModel.setQuery(query)
viewModel.searchLocation()
searchView?.clearFocus()
return true
}

override fun onQueryTextChange(query: String): Boolean {
// This is a workaround to not set viewModel data when onQueryTextChange is triggered on startup
// Otherwise it would be set to an empty string.
if (searchView?.width!! > 0) {
viewModel.setQuery(query)
}
return true
}
})

searchItem?.setOnActionExpandListener(object : MenuItem.OnActionExpandListener {
override fun onMenuItemActionExpand(menuItem: MenuItem): Boolean {
Expand All @@ -256,6 +232,5 @@ class GeocodingActivity :

companion object {
val TAG = GeocodingActivity::class.java.simpleName
const val KEY_SEARCH_QUERY = "search_query"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import java.io.IOException

class GeoCodingViewModel : ViewModel() {
private val geocodingResultsLiveData = MutableLiveData<List<Address>>()
private val queryLiveData = MutableLiveData<String>()
private val nominatimClient: TalkJsonNominatimClient
private val okHttpClient: OkHttpClient = OkHttpClient.Builder().build()
private var geocodingResults: List<Address> = ArrayList()
Expand All @@ -45,18 +44,12 @@ class GeoCodingViewModel : ViewModel() {
return geocodingResultsLiveData
}

fun getQueryLiveData(): LiveData<String> {
return queryLiveData
}

fun getQuery(): String {
return query
}

fun setQuery(query: String) {
if (queryLiveData.value.isNullOrEmpty()) {
queryLiveData.value = query
}
this.query = query
}

fun getGeocodingResults(): List<Address> {
Expand All @@ -71,7 +64,7 @@ class GeoCodingViewModel : ViewModel() {
)
}

fun searchLocation(query: String) {
fun searchLocation() {
if (query.isNotEmpty()) {
CoroutineScope(Dispatchers.IO).launch {
try {
Expand Down

0 comments on commit 8f15c3c

Please sign in to comment.