From 16f6e3c0cd40c9233f89baec4c084384423c5eb2 Mon Sep 17 00:00:00 2001 From: Smarshal21 Date: Fri, 29 Sep 2023 22:55:54 +0530 Subject: [PATCH] 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 ed8fb5048..3b03a8443 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 1591e023b..b5597e86b 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) + } } } }