Skip to content

Commit

Permalink
Implemented LiveData
Browse files Browse the repository at this point in the history
  • Loading branch information
Smarshal21 committed Sep 27, 2023
1 parent 807fdd3 commit 4d4eb21
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 17 additions & 2 deletions app/src/main/java/com/nextcloud/talk/location/GeocodingActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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() {
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}

0 comments on commit 4d4eb21

Please sign in to comment.