Implemented LiveData

This commit is contained in:
Smarshal21 2023-09-28 04:01:28 +05:30
parent 807fdd31fd
commit 4d4eb2149c
3 changed files with 83 additions and 3 deletions

View file

@ -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<Address>) :
class GeocodingAdapter(private val context: Context, private var dataSource: List<Address>) :
RecyclerView.Adapter<GeocodingAdapter.ViewHolder>() {
interface OnItemClickListener {
fun onItemClick(position: Int)
}
fun updateData(data: List<Address>) {
this.dataSource = data
notifyDataSetChanged()
}
private var listener: OnItemClickListener? = null
fun setOnItemClickListener(listener: OnItemClickListener) {

View file

@ -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<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)
@ -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
}

View file

@ -0,0 +1,61 @@
/*
* Nextcloud Talk application
*
* @author Samanwith KSN
* Copyright (C) 2023 Samanwith KSN <samanwith21@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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<List<Address>>()
fun getGeocodingResultsLiveData(): LiveData<List<Address>> {
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<Address>
geocodingResultsLiveData.postValue(results)
} catch (e: Exception) {
Log.e(TAG, "Failed to get geocoded addresses", e)
}
}
}
}