mirror of
https://github.com/nextcloud/talk-android.git
synced 2024-11-22 13:05:31 +03:00
Fixed the Suggested Changes
Signed-off-by: Smarshal21 <lcb2021048@iiitl.ac.in>
This commit is contained in:
parent
25e2cabbdf
commit
16f6e3c0cd
2 changed files with 74 additions and 51 deletions
|
@ -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<Address> = 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<Address> = ArrayList()
|
||||
try {
|
||||
results = nominatimClient!!.search(query) as ArrayList<Address>
|
||||
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<Address>) {
|
||||
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"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<List<Address>>()
|
||||
private val queryLiveData = MutableLiveData<String>()
|
||||
private val nominatimClient: TalkJsonNominatimClient
|
||||
private val okHttpClient: OkHttpClient = OkHttpClient.Builder().build()
|
||||
private var geocodingResults: List<Address> = ArrayList()
|
||||
private var query: String = ""
|
||||
fun getGeocodingResultsLiveData(): LiveData<List<Address>> {
|
||||
return geocodingResultsLiveData
|
||||
}
|
||||
|
||||
private val nominatimClient: TalkJsonNominatimClient
|
||||
private val okHttpClient: OkHttpClient = OkHttpClient.Builder().build()
|
||||
fun getQueryLiveData(): LiveData<String> {
|
||||
return queryLiveData
|
||||
}
|
||||
|
||||
fun getQuery(): String {
|
||||
return query
|
||||
}
|
||||
|
||||
fun setQuery(query: String) {
|
||||
if (queryLiveData.value.isNullOrEmpty()) {
|
||||
queryLiveData.value = query
|
||||
}
|
||||
}
|
||||
|
||||
fun getGeocodingResults(): List<Address> {
|
||||
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<Address>
|
||||
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<Address>
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue