-
-
Notifications
You must be signed in to change notification settings - Fork 5
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 #12 from Jintin/feature/state
add state flow type support
- Loading branch information
Showing
7 changed files
with
81 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
48 changes: 48 additions & 0 deletions
48
lib/src/main/java/com/jintin/fancylocation/LocationStateFlow.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,48 @@ | ||
package com.jintin.fancylocation | ||
|
||
import android.Manifest | ||
import android.content.Context | ||
import android.location.Location | ||
import androidx.annotation.RequiresPermission | ||
import com.google.android.gms.location.LocationRequest | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
import kotlin.coroutines.coroutineContext | ||
|
||
class LocationStateFlow( | ||
private val locationProvider: ILocationProvider | ||
) { | ||
private val _value = MutableStateFlow<LocationData>(LocationData.Init) | ||
val value: StateFlow<LocationData> get() = _value | ||
|
||
constructor( | ||
context: Context, | ||
locationRequest: LocationRequest | ||
) : this(LocationProvider(context, locationRequest)) | ||
|
||
@RequiresPermission(anyOf = [Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION]) | ||
suspend fun start() { | ||
_value.value = LocationData.Loading | ||
locationProvider.requestLocationUpdates(object : ILocationObserver { | ||
override fun onLocationResult(location: Location) { | ||
_value.value = LocationData.Success(location) | ||
} | ||
|
||
override fun onLocationFailed() { | ||
_value.value = LocationData.Fail | ||
} | ||
}) | ||
with(CoroutineScope(coroutineContext)) { // spawns and uses parent scope! | ||
launch { | ||
suspendCancellableCoroutine<Unit> { | ||
it.invokeOnCancellation { | ||
locationProvider.removeLocationUpdates() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |