-
-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3349 from nextcloud/LIveData
use MVVM for Geocoding
- Loading branch information
Showing
3 changed files
with
141 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
app/src/main/java/com/nextcloud/talk/viewmodels/GeoCodingViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Nextcloud Talk application | ||
* | ||
* @author Samanwith KSN | ||
* Copyright (C) 2023 Samanwith KSN <[email protected]> | ||
* | ||
* 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 com.nextcloud.talk.location.GeocodingActivity | ||
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 | ||
import java.io.IOException | ||
|
||
class GeoCodingViewModel : ViewModel() { | ||
private val geocodingResultsLiveData = MutableLiveData<List<Address>>() | ||
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 | ||
} | ||
|
||
fun getQuery(): String { | ||
return query | ||
} | ||
|
||
fun setQuery(query: String) { | ||
this.query = query | ||
} | ||
|
||
fun getGeocodingResults(): List<Address> { | ||
return geocodingResults | ||
} | ||
|
||
init { | ||
nominatimClient = TalkJsonNominatimClient( | ||
"https://nominatim.openstreetmap.org/", | ||
okHttpClient, | ||
" [email protected]" | ||
) | ||
} | ||
|
||
fun searchLocation() { | ||
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) | ||
} | ||
} | ||
} | ||
} | ||
} |