Skip to content

Commit

Permalink
Removing use of Result<> since I am not doing something apart from lo…
Browse files Browse the repository at this point in the history
…gging the error and just using the default value
  • Loading branch information
itissid committed Dec 17, 2024
1 parent 5081115 commit c1f5ff1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 30 deletions.
21 changes: 10 additions & 11 deletions app/src/main/java/me/itissid/privyloci/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,12 @@ fun MainScreenWrapper(viewModel: MainViewModel) {
)
) { Logger.v(TAG, it.entries.joinToString(separator = "\n")) }

val userVisitedPermissionLauncherPreference =
viewModel.userVisitedPermissionLauncher.collectAsState().value.getOrDefault(false)
val userVisitedPermissionLauncherPreference by
viewModel.userVisitedPermissionLauncher.collectAsState()

val userPausedLocationCollection by
viewModel.userPausedLocationCollection.collectAsState()

val userPausedLocationCollection =
viewModel.userPausedLocationCollection.collectAsState().value.getOrDefault(
false
)
// location permission revocation is superflous to previous user setting to stop collection.
if (!foregroundLocationPermissionState.allPermissionsGranted) {
viewModel.setUserPausedLocationCollection(false)
Expand All @@ -157,7 +156,7 @@ fun MainScreenWrapper(viewModel: MainViewModel) {
LaunchedEffect(Unit) {
coroutineScope.launch {
viewModel.wasFGPersistentNotificationDismissed.collect { result ->
userDismissedFGNotification.value = result.getOrDefault(false)
userDismissedFGNotification.value = result
}
}
}
Expand Down Expand Up @@ -350,12 +349,12 @@ fun MainScreenWrapper(viewModel: MainViewModel) {
fun LaunchPrivyForeGroundService(
context: Context,
userDismissedForegroundNotification: Boolean,
userPausedLocationCollectionStateFlow: StateFlow<Result<Boolean>>,
userPausedLocationCollectionStateFlow: StateFlow<Boolean>,
isRunningStateFlow: StateFlow<Boolean>
) {
val userPausedLocationCollection =
userPausedLocationCollectionStateFlow.collectAsState().value.getOrDefault(false)
val isRunning = isRunningStateFlow.collectAsState().value
val userPausedLocationCollection by
userPausedLocationCollectionStateFlow.collectAsState()
val isRunning by isRunningStateFlow.collectAsState()
Logger.d(
TAG,
"User dismissed FG notification: $userDismissedForegroundNotification, User paused location collection: $userPausedLocationCollection, isRunning: $isRunning"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,20 @@ class UserPreferences @Inject constructor(
fun <T> DataStore<Preferences>.readAsResult(
key: Preferences.Key<T>,
defaultValue: T
): Flow<Result<T>> {
): Flow<T> {
return this.data
.map { preferences ->
Result.success(
preferences[key]
?: defaultValue
).also {
Logger.v("UserPreferences", "Read $key: ${it.getOrNull()}")
}
val result = preferences[key] ?: defaultValue
Logger.v("UserPreferences", "Read $key: $result")
result
}
.catch { exception ->
Logger.e(
"UserPreferences",
"Error reading preferences: Message: `${exception.message}`",
exception
)
emit(Result.failure(exception))
emit(defaultValue)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ class BlePermissionViewModel @Inject constructor(

// Flow indicating if the user visited the BLE permission launcher before
private val userVisitedBlePermissionLauncher = userPreferences.userVisitedBlePermissionLauncher
.map { it.getOrDefault(false) }
.stateIn(viewModelScope, SharingStarted.Eagerly, false)

// Flow or a boolean indicating if BLE permission is currently granted
Expand Down
20 changes: 10 additions & 10 deletions app/src/main/java/me/itissid/privyloci/viewmodels/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,43 +64,43 @@ class MainViewModel @Inject constructor(
)

// Expose preferences as StateFlow public variables.
val wasFGPermissionRationaleDismissed: StateFlow<Result<Boolean>> =
val wasFGPermissionRationaleDismissed =
repository.wasFGPermissionRationaleDismissed
.stateIn(
scope = viewModelScope,
started = SharingStarted.Lazily,
initialValue = Result.success(false)
initialValue = false
)

val wasFGPersistentNotificationDismissed: StateFlow<Result<Boolean>> =
val wasFGPersistentNotificationDismissed =
repository.wasFGPersistentNotificationDismissed
.stateIn(
scope = viewModelScope,
started = SharingStarted.Lazily,
initialValue = Result.success(false)
initialValue = false
)

val wasReactivateFGRationaleDismissed: StateFlow<Result<Boolean>> =
val wasReactivateFGRationaleDismissed: StateFlow<Boolean> =
repository.wasReactivateFGRationaleDismissed
.stateIn(
scope = viewModelScope,
started = SharingStarted.Lazily,
initialValue = Result.success(false)
initialValue = false
)

val userVisitedPermissionLauncher: StateFlow<Result<Boolean>> =
val userVisitedPermissionLauncher: StateFlow<Boolean> =
userPreferences.userVisitedPermissionLauncher
.stateIn(
scope = viewModelScope,
started = SharingStarted.Lazily,
initialValue = Result.success(false)
initialValue = false
)
val userPausedLocationCollection: StateFlow<Result<Boolean>> =
val userPausedLocationCollection: StateFlow<Boolean> =
userPreferences.userPausedLocationCollection
.stateIn(
scope = viewModelScope,
started = SharingStarted.Lazily,
initialValue = Result.success(false)
initialValue = false
)

fun setFGPermissionRationaleDismissed(dismissed: Boolean) {
Expand Down

0 comments on commit c1f5ff1

Please sign in to comment.