-
-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replace LocationLiveData with MapLocationFlow
- Loading branch information
Showing
10 changed files
with
210 additions
and
192 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
95 changes: 0 additions & 95 deletions
95
project/app/src/gms/java/org/owntracks/android/ui/map/LocationLiveData.kt
This file was deleted.
Oops, something went wrong.
71 changes: 71 additions & 0 deletions
71
project/app/src/gms/java/org/owntracks/android/ui/map/MapLocationFlow.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,71 @@ | ||
package org.owntracks.android.ui.map | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Context | ||
import android.location.Location | ||
import android.os.Looper | ||
import com.google.android.gms.location.FusedLocationProviderClient | ||
import com.google.android.gms.location.LocationCallback | ||
import com.google.android.gms.location.LocationResult | ||
import com.google.android.gms.location.LocationServices | ||
import java.time.Duration | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.channels.awaitClose | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.SharingStarted | ||
import kotlinx.coroutines.flow.callbackFlow | ||
import kotlinx.coroutines.flow.shareIn | ||
import org.owntracks.android.gms.location.toGMSLocationRequest | ||
import org.owntracks.android.location.LocationRequest | ||
import org.owntracks.android.location.LocatorPriority | ||
import timber.log.Timber | ||
|
||
class MapLocationFlow(private val locationProviderClient: FusedLocationProviderClient) { | ||
constructor( | ||
context: Context, | ||
) : this(LocationServices.getFusedLocationProviderClient(context)) | ||
|
||
init { | ||
Timber.tag("LocationFlow").w("Init locationFlow") | ||
} | ||
|
||
@SuppressLint("MissingPermission") | ||
fun getLocationFlow(coroutineScope: CoroutineScope): Flow<Location> = | ||
callbackFlow { | ||
val locationCallback = | ||
object : LocationCallback() { | ||
override fun onLocationResult(location: LocationResult) { | ||
location.lastLocation?.run(::trySend) | ||
} | ||
} | ||
locationProviderClient.apply { | ||
requestLocationUpdates( | ||
LocationRequest( | ||
smallestDisplacement = 1f, | ||
priority = LocatorPriority.HighAccuracy, | ||
interval = Duration.ofSeconds(2), | ||
waitForAccurateLocation = false) | ||
.toGMSLocationRequest(), | ||
locationCallback, | ||
Looper.getMainLooper()) | ||
.addOnCompleteListener { | ||
Timber.d( | ||
"LocationLiveData location update request completed: " + | ||
"Success=${it.isSuccessful} Cancelled=${it.isCanceled}") | ||
Timber.tag("LocationFlow").w("Starting locationFlow") | ||
} | ||
awaitClose { | ||
locationProviderClient | ||
.removeLocationUpdates(locationCallback) | ||
.addOnCompleteListener { | ||
Timber.d( | ||
"LocationLiveData removing location updates completed: " + | ||
"Success=${it.isSuccessful} Cancelled=${it.isCanceled}") | ||
Timber.tag("LocationFlow").w("Stopping locationFlow") | ||
} | ||
} | ||
} | ||
} | ||
.shareIn( | ||
coroutineScope, SharingStarted.WhileSubscribed(stopTimeoutMillis = 2000), replay = 0) | ||
} |
19 changes: 19 additions & 0 deletions
19
project/app/src/main/java/org/owntracks/android/di/CurrentLocationFlowProviderModule.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,19 @@ | ||
package org.owntracks.android.di | ||
|
||
import android.content.Context | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.components.ViewModelComponent | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.android.scopes.ViewModelScoped | ||
import org.owntracks.android.ui.map.MapLocationFlow | ||
|
||
@InstallIn(ViewModelComponent::class) | ||
@Module | ||
class CurrentLocationFlowProviderModule { | ||
@Provides | ||
@ViewModelScoped | ||
fun getCurrentLocationFlow(@ApplicationContext applicationContext: Context) = | ||
MapLocationFlow(applicationContext) | ||
} |
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
Oops, something went wrong.