From 4d4eb2149c4ef2a0baff099378a44e32595b481d Mon Sep 17 00:00:00 2001 From: Smarshal21 Date: Thu, 28 Sep 2023 04:01:28 +0530 Subject: [PATCH 1/4] Implemented LiveData --- .../talk/adapters/GeocodingAdapter.kt | 6 +- .../talk/location/GeocodingActivity.kt | 19 +++++- .../talk/viewmodels/GeoCodingViewModel.kt | 61 +++++++++++++++++++ 3 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/com/nextcloud/talk/viewmodels/GeoCodingViewModel.kt diff --git a/app/src/main/java/com/nextcloud/talk/adapters/GeocodingAdapter.kt b/app/src/main/java/com/nextcloud/talk/adapters/GeocodingAdapter.kt index 8350da0b0f..6431290031 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/GeocodingAdapter.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/GeocodingAdapter.kt @@ -29,12 +29,16 @@ import androidx.recyclerview.widget.RecyclerView import com.nextcloud.talk.R import fr.dudie.nominatim.model.Address -class GeocodingAdapter(private val context: Context, private val dataSource: List
) : +class GeocodingAdapter(private val context: Context, private var dataSource: List
) : RecyclerView.Adapter() { interface OnItemClickListener { fun onItemClick(position: Int) } + fun updateData(data: List
) { + this.dataSource = data + notifyDataSetChanged() + } private var listener: OnItemClickListener? = null fun setOnItemClickListener(listener: OnItemClickListener) { diff --git a/app/src/main/java/com/nextcloud/talk/location/GeocodingActivity.kt b/app/src/main/java/com/nextcloud/talk/location/GeocodingActivity.kt index ff08ef6e8e..00323ba8be 100644 --- a/app/src/main/java/com/nextcloud/talk/location/GeocodingActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/location/GeocodingActivity.kt @@ -33,6 +33,8 @@ 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 import androidx.recyclerview.widget.RecyclerView @@ -45,6 +47,7 @@ import com.nextcloud.talk.api.NcApi import com.nextcloud.talk.application.NextcloudTalkApplication import com.nextcloud.talk.databinding.ActivityGeocodingBinding import com.nextcloud.talk.utils.bundle.BundleKeys +import com.nextcloud.talk.viewmodels.GeoCodingViewModel import fr.dudie.nominatim.client.TalkJsonNominatimClient import fr.dudie.nominatim.model.Address import kotlinx.coroutines.CoroutineScope @@ -79,7 +82,7 @@ class GeocodingActivity : lateinit var adapter: GeocodingAdapter private var geocodingResults: List
= ArrayList() private lateinit var recyclerView: RecyclerView - + private lateinit var viewModel: GeoCodingViewModel override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this) @@ -97,6 +100,16 @@ class GeocodingActivity : recyclerView.layoutManager = LinearLayoutManager(this) adapter = GeocodingAdapter(this, geocodingResults) recyclerView.adapter = adapter + viewModel = ViewModelProvider(this).get(GeoCodingViewModel::class.java) + + // Observe geocoding results LiveData + viewModel.getGeocodingResultsLiveData().observe(this, Observer { results -> + // Update the adapter with the new 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) } override fun onStart() { @@ -169,7 +182,9 @@ class GeocodingActivity : override fun onQueryTextSubmit(query: String?): Boolean { this.query = query - searchLocation() + if (query != null) { + viewModel.searchLocation(query) + } searchView?.clearFocus() return true } diff --git a/app/src/main/java/com/nextcloud/talk/viewmodels/GeoCodingViewModel.kt b/app/src/main/java/com/nextcloud/talk/viewmodels/GeoCodingViewModel.kt new file mode 100644 index 0000000000..98830755e1 --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/viewmodels/GeoCodingViewModel.kt @@ -0,0 +1,61 @@ +/* + * Nextcloud Talk application + * + * @author Samanwith KSN + * Copyright (C) 2023 Samanwith KSN + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.nextcloud.talk.viewmodels + +import android.util.Log +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel +import com.nextcloud.talk.activities.CallActivity.Companion.TAG +import fr.dudie.nominatim.client.TalkJsonNominatimClient +import fr.dudie.nominatim.model.Address +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import okhttp3.OkHttpClient + +class GeoCodingViewModel : ViewModel() { + private val geocodingResultsLiveData = MutableLiveData>() + fun getGeocodingResultsLiveData(): LiveData> { + return geocodingResultsLiveData + } + + private val nominatimClient: TalkJsonNominatimClient + private val okHttpClient: OkHttpClient = OkHttpClient.Builder().build() + + init { + nominatimClient = TalkJsonNominatimClient( + "https://nominatim.openstreetmap.org/", + okHttpClient, + " android@nextcloud.com" + ) + } + + fun searchLocation(query: String) { + CoroutineScope(Dispatchers.IO).launch { + try { + val results = nominatimClient.search(query) as ArrayList
+ geocodingResultsLiveData.postValue(results) + } catch (e: Exception) { + Log.e(TAG, "Failed to get geocoded addresses", e) + } + } + } +} \ No newline at end of file From 25e2cabbdf64ce20528b8182cff38c9525fff584 Mon Sep 17 00:00:00 2001 From: Smarshal21 Date: Thu, 28 Sep 2023 11:21:33 +0530 Subject: [PATCH 2/4] Fixed Codacy Static and ktlint errors --- .../com/nextcloud/talk/adapters/GeocodingAdapter.kt | 2 +- .../com/nextcloud/talk/location/GeocodingActivity.kt | 11 ++++++----- .../nextcloud/talk/viewmodels/GeoCodingViewModel.kt | 6 ++++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/adapters/GeocodingAdapter.kt b/app/src/main/java/com/nextcloud/talk/adapters/GeocodingAdapter.kt index 6431290031..2c3731fe2d 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/GeocodingAdapter.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/GeocodingAdapter.kt @@ -36,7 +36,7 @@ class GeocodingAdapter(private val context: Context, private var dataSource: Lis fun onItemClick(position: Int) } fun updateData(data: List
) { - this.dataSource = data + this.dataSource = data notifyDataSetChanged() } diff --git a/app/src/main/java/com/nextcloud/talk/location/GeocodingActivity.kt b/app/src/main/java/com/nextcloud/talk/location/GeocodingActivity.kt index 00323ba8be..ed8fb5048d 100644 --- a/app/src/main/java/com/nextcloud/talk/location/GeocodingActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/location/GeocodingActivity.kt @@ -103,10 +103,11 @@ class GeocodingActivity : viewModel = ViewModelProvider(this).get(GeoCodingViewModel::class.java) // Observe geocoding results LiveData - viewModel.getGeocodingResultsLiveData().observe(this, Observer { results -> - // Update the adapter with the new results - adapter.updateData(results) - }) + viewModel.getGeocodingResultsLiveData().observe( + this, + Observer { results -> // Update the adapter with the new 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) @@ -200,7 +201,7 @@ class GeocodingActivity : searchView?.maxWidth = Int.MAX_VALUE searchView?.inputType = InputType.TYPE_TEXT_VARIATION_FILTER var imeOptions = EditorInfo.IME_ACTION_DONE or EditorInfo.IME_FLAG_NO_FULLSCREEN - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && appPreferences!!.isKeyboardIncognito) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && appPreferences.isKeyboardIncognito) { imeOptions = imeOptions or EditorInfo.IME_FLAG_NO_PERSONALIZED_LEARNING } searchView?.imeOptions = imeOptions diff --git a/app/src/main/java/com/nextcloud/talk/viewmodels/GeoCodingViewModel.kt b/app/src/main/java/com/nextcloud/talk/viewmodels/GeoCodingViewModel.kt index 98830755e1..1591e023b9 100644 --- a/app/src/main/java/com/nextcloud/talk/viewmodels/GeoCodingViewModel.kt +++ b/app/src/main/java/com/nextcloud/talk/viewmodels/GeoCodingViewModel.kt @@ -17,6 +17,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ + package com.nextcloud.talk.viewmodels import android.util.Log @@ -30,6 +31,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import okhttp3.OkHttpClient +import java.io.IOException class GeoCodingViewModel : ViewModel() { private val geocodingResultsLiveData = MutableLiveData>() @@ -53,9 +55,9 @@ class GeoCodingViewModel : ViewModel() { try { val results = nominatimClient.search(query) as ArrayList
geocodingResultsLiveData.postValue(results) - } catch (e: Exception) { + } catch (e: IOException) { Log.e(TAG, "Failed to get geocoded addresses", e) } } } -} \ No newline at end of file +} From 16f6e3c0cd40c9233f89baec4c084384423c5eb2 Mon Sep 17 00:00:00 2001 From: Smarshal21 Date: Fri, 29 Sep 2023 22:55:54 +0530 Subject: [PATCH 3/4] Fixed the Suggested Changes Signed-off-by: Smarshal21 --- .../talk/location/GeocodingActivity.kt | 80 +++++++++---------- .../talk/viewmodels/GeoCodingViewModel.kt | 45 +++++++++-- 2 files changed, 74 insertions(+), 51 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/location/GeocodingActivity.kt b/app/src/main/java/com/nextcloud/talk/location/GeocodingActivity.kt index ed8fb5048d..3b03a84430 100644 --- a/app/src/main/java/com/nextcloud/talk/location/GeocodingActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/location/GeocodingActivity.kt @@ -39,7 +39,6 @@ import androidx.preference.PreferenceManager import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import autodagger.AutoInjector -import com.google.android.material.snackbar.Snackbar import com.nextcloud.talk.R import com.nextcloud.talk.activities.BaseActivity import com.nextcloud.talk.adapters.GeocodingAdapter @@ -50,11 +49,6 @@ import com.nextcloud.talk.utils.bundle.BundleKeys import com.nextcloud.talk.viewmodels.GeoCodingViewModel import fr.dudie.nominatim.client.TalkJsonNominatimClient import fr.dudie.nominatim.model.Address -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers.IO -import kotlinx.coroutines.Dispatchers.Main -import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext import okhttp3.OkHttpClient import org.osmdroid.config.Configuration import javax.inject.Inject @@ -83,6 +77,7 @@ class GeocodingActivity : private var geocodingResults: List
= ArrayList() private lateinit var recyclerView: RecyclerView private lateinit var viewModel: GeoCodingViewModel + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this) @@ -102,12 +97,27 @@ class GeocodingActivity : recyclerView.adapter = adapter viewModel = ViewModelProvider(this).get(GeoCodingViewModel::class.java) - // Observe geocoding results LiveData + query = viewModel.getQuery() + if (query.isNullOrEmpty()) { + query = intent.getStringExtra(BundleKeys.KEY_GEOCODING_QUERY) + viewModel.setQuery(query ?: "") + } + val savedResults = viewModel.getGeocodingResults() + initAdapter(savedResults) viewModel.getGeocodingResultsLiveData().observe( this, - Observer { results -> // Update the adapter with the new results + Observer { results -> + geocodingResults = results adapter.updateData(results) - }) + } + ) + viewModel.getQueryLiveData().observe( + this, + Observer { newQuery -> + query = newQuery + searchView?.setQuery(query, false) + } + ) val baseUrl = getString(R.string.osm_geocoder_url) val email = context.getString(R.string.osm_geocoder_contact) nominatimClient = TalkJsonNominatimClient(baseUrl, okHttpClient, email) @@ -123,11 +133,10 @@ class GeocodingActivity : super.onResume() if (!query.isNullOrEmpty()) { - searchLocation() + viewModel.searchLocation(query!!) } else { Log.e(TAG, "search string that was passed to GeocodingController was null or empty") } - adapter.setOnItemClickListener(object : GeocodingAdapter.OnItemClickListener { override fun onItemClick(position: Int) { val address: Address = adapter.getItem(position) as Address @@ -139,6 +148,7 @@ class GeocodingActivity : startActivity(intent) } }) + searchView?.setQuery(query, false) } private fun setupActionBar() { @@ -182,7 +192,8 @@ class GeocodingActivity : } override fun onQueryTextSubmit(query: String?): Boolean { - this.query = query + this.query = query ?: "" + viewModel.setQuery(this.query!!) if (query != null) { viewModel.searchLocation(query) } @@ -190,6 +201,18 @@ class GeocodingActivity : 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 } @@ -231,37 +254,8 @@ class GeocodingActivity : nominatimClient = TalkJsonNominatimClient(baseUrl, okHttpClient, email) } - private fun searchLocation(): Boolean { - CoroutineScope(IO).launch { - executeGeocodingRequest() - } - return true - } - - @Suppress("Detekt.TooGenericExceptionCaught") - private suspend fun executeGeocodingRequest() { - var results: ArrayList
= ArrayList() - try { - results = nominatimClient!!.search(query) as ArrayList
- for (address in results) { - Log.d(TAG, address.displayName) - Log.d(TAG, address.latitude.toString()) - Log.d(TAG, address.longitude.toString()) - } - } catch (e: Exception) { - Log.e(TAG, "Failed to get geocoded addresses", e) - Snackbar.make(binding.root, R.string.nc_common_error_sorry, Snackbar.LENGTH_LONG).show() - } - updateResultsOnMainThread(results) - } - - private suspend fun updateResultsOnMainThread(results: ArrayList
) { - withContext(Main) { - initAdapter(results) - } - } - companion object { - private val TAG = GeocodingActivity::class.java.simpleName + val TAG = GeocodingActivity::class.java.simpleName + const val KEY_SEARCH_QUERY = "search_query" } } diff --git a/app/src/main/java/com/nextcloud/talk/viewmodels/GeoCodingViewModel.kt b/app/src/main/java/com/nextcloud/talk/viewmodels/GeoCodingViewModel.kt index 1591e023b9..b5597e86b8 100644 --- a/app/src/main/java/com/nextcloud/talk/viewmodels/GeoCodingViewModel.kt +++ b/app/src/main/java/com/nextcloud/talk/viewmodels/GeoCodingViewModel.kt @@ -25,6 +25,7 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import com.nextcloud.talk.activities.CallActivity.Companion.TAG +import com.nextcloud.talk.location.GeocodingActivity import fr.dudie.nominatim.client.TalkJsonNominatimClient import fr.dudie.nominatim.model.Address import kotlinx.coroutines.CoroutineScope @@ -35,12 +36,32 @@ import java.io.IOException class GeoCodingViewModel : ViewModel() { private val geocodingResultsLiveData = MutableLiveData>() + private val queryLiveData = MutableLiveData() + private val nominatimClient: TalkJsonNominatimClient + private val okHttpClient: OkHttpClient = OkHttpClient.Builder().build() + private var geocodingResults: List
= ArrayList() + private var query: String = "" fun getGeocodingResultsLiveData(): LiveData> { return geocodingResultsLiveData } - private val nominatimClient: TalkJsonNominatimClient - private val okHttpClient: OkHttpClient = OkHttpClient.Builder().build() + fun getQueryLiveData(): LiveData { + return queryLiveData + } + + fun getQuery(): String { + return query + } + + fun setQuery(query: String) { + if (queryLiveData.value.isNullOrEmpty()) { + queryLiveData.value = query + } + } + + fun getGeocodingResults(): List
{ + return geocodingResults + } init { nominatimClient = TalkJsonNominatimClient( @@ -51,12 +72,20 @@ class GeoCodingViewModel : ViewModel() { } fun searchLocation(query: String) { - CoroutineScope(Dispatchers.IO).launch { - try { - val results = nominatimClient.search(query) as ArrayList
- geocodingResultsLiveData.postValue(results) - } catch (e: IOException) { - Log.e(TAG, "Failed to get geocoded addresses", e) + if (query.isNotEmpty()) { + CoroutineScope(Dispatchers.IO).launch { + try { + val results = nominatimClient.search(query) as ArrayList
+ for (address in results) { + Log.d(GeocodingActivity.TAG, address.displayName) + Log.d(GeocodingActivity.TAG, address.latitude.toString()) + Log.d(GeocodingActivity.TAG, address.longitude.toString()) + } + geocodingResults = results + geocodingResultsLiveData.postValue(results) + } catch (e: IOException) { + Log.e(TAG, "Failed to get geocoded addresses", e) + } } } } From 8f15c3c1e76ec1057f434fcacb7f1d27b9f4f5bd Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Mon, 2 Oct 2023 15:07:32 +0200 Subject: [PATCH 4/4] avoid usage of instanceState (use viewmodel instead) use viewModel to keep track of query change handling for textChanged listener minor changes Signed-off-by: Marcel Hibbe --- .../talk/adapters/GeocodingAdapter.kt | 3 + .../talk/location/GeocodingActivity.kt | 97 +++++++------------ .../talk/viewmodels/GeoCodingViewModel.kt | 11 +-- 3 files changed, 41 insertions(+), 70 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/adapters/GeocodingAdapter.kt b/app/src/main/java/com/nextcloud/talk/adapters/GeocodingAdapter.kt index 2c3731fe2d..603c73537e 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/GeocodingAdapter.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/GeocodingAdapter.kt @@ -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 @@ -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
) { this.dataSource = data notifyDataSetChanged() diff --git a/app/src/main/java/com/nextcloud/talk/location/GeocodingActivity.kt b/app/src/main/java/com/nextcloud/talk/location/GeocodingActivity.kt index 3b03a84430..123b8ee10b 100644 --- a/app/src/main/java/com/nextcloud/talk/location/GeocodingActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/location/GeocodingActivity.kt @@ -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 @@ -55,8 +53,7 @@ import javax.inject.Inject @AutoInjector(NextcloudTalkApplication::class) class GeocodingActivity : - BaseActivity(), - SearchView.OnQueryTextListener { + BaseActivity() { private lateinit var binding: ActivityGeocodingBinding @@ -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
= ArrayList() @@ -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) @@ -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") } @@ -148,7 +134,7 @@ class GeocodingActivity : startActivity(intent) } }) - searchView?.setQuery(query, false) + searchView?.setQuery(viewModel.getQuery(), false) } private fun setupActionBar() { @@ -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 @@ -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 { @@ -256,6 +232,5 @@ class GeocodingActivity : companion object { val TAG = GeocodingActivity::class.java.simpleName - const val KEY_SEARCH_QUERY = "search_query" } } diff --git a/app/src/main/java/com/nextcloud/talk/viewmodels/GeoCodingViewModel.kt b/app/src/main/java/com/nextcloud/talk/viewmodels/GeoCodingViewModel.kt index b5597e86b8..d66d9059c8 100644 --- a/app/src/main/java/com/nextcloud/talk/viewmodels/GeoCodingViewModel.kt +++ b/app/src/main/java/com/nextcloud/talk/viewmodels/GeoCodingViewModel.kt @@ -36,7 +36,6 @@ import java.io.IOException class GeoCodingViewModel : ViewModel() { private val geocodingResultsLiveData = MutableLiveData>() - private val queryLiveData = MutableLiveData() private val nominatimClient: TalkJsonNominatimClient private val okHttpClient: OkHttpClient = OkHttpClient.Builder().build() private var geocodingResults: List
= ArrayList() @@ -45,18 +44,12 @@ class GeoCodingViewModel : ViewModel() { return geocodingResultsLiveData } - fun getQueryLiveData(): LiveData { - return queryLiveData - } - fun getQuery(): String { return query } fun setQuery(query: String) { - if (queryLiveData.value.isNullOrEmpty()) { - queryLiveData.value = query - } + this.query = query } fun getGeocodingResults(): List
{ @@ -71,7 +64,7 @@ class GeoCodingViewModel : ViewModel() { ) } - fun searchLocation(query: String) { + fun searchLocation() { if (query.isNotEmpty()) { CoroutineScope(Dispatchers.IO).launch { try {