mirror of https://github.com/tasks/tasks
Add LocationProvider interface
parent
29fdba597b
commit
a57040119b
@ -0,0 +1,5 @@
|
||||
package org.tasks.location
|
||||
|
||||
interface LocationProvider {
|
||||
suspend fun currentLocation(): MapPosition
|
||||
}
|
||||
@ -0,0 +1,30 @@
|
||||
package org.tasks.location
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import com.mapbox.android.core.location.LocationEngineCallback
|
||||
import com.mapbox.android.core.location.LocationEngineProvider
|
||||
import com.mapbox.android.core.location.LocationEngineResult
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlin.coroutines.suspendCoroutine
|
||||
|
||||
class MapboxLocationProvider(private val context: Context) : LocationProvider {
|
||||
@SuppressLint("MissingPermission")
|
||||
override suspend fun currentLocation(): MapPosition = withContext(Dispatchers.IO) {
|
||||
suspendCoroutine { cont ->
|
||||
LocationEngineProvider.getBestLocationEngine(context)
|
||||
.getLastLocation(
|
||||
object : LocationEngineCallback<LocationEngineResult> {
|
||||
override fun onSuccess(result: LocationEngineResult) {
|
||||
val location = result.lastLocation!!
|
||||
cont.resumeWith(Result.success(MapPosition(location.latitude, location.longitude)))
|
||||
}
|
||||
|
||||
override fun onFailure(exception: Exception) {
|
||||
cont.resumeWith(Result.failure(exception))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue