Skip to content

Commit

Permalink
Glimpse: Fix trashed SECURE_REVIEW items not getting removed from screen
Browse files Browse the repository at this point in the history
Without this fix, when you review taken photos while the device is locked
and trash them, they would stay on screen as if they weren't trashed.

With this fix, they get removed/restored as they would when the device is unlocked.

Co-authored-by: Luca Stefani <luca.stefani.ge1@gmail.com>
Change-Id: Icc161597ca823255cc2c5d395381dccf10c33cbc
  • Loading branch information
Torsten Grote and luca020400 committed Feb 22, 2025
1 parent dde8aa8 commit 7e7ea6d
Showing 1 changed file with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.media3.common.MediaItem
import androidx.media3.exoplayer.ExoPlayer
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.asStateFlow
Expand Down Expand Up @@ -113,6 +114,58 @@ class LocalPlayerViewModel(
initialValue = RequestStatus.Loading(),
)

private fun <T, E> List<Flow<RequestStatus<T, E>>>.toFlowOfRequestStatusList() = flow {
if (isEmpty()) {
emit(RequestStatus.Success(emptyList()))
return@flow
}

emit(RequestStatus.Loading())

combine(this@toFlowOfRequestStatusList) { statusArray ->
// Check if any item is still loading
if (statusArray.any { it is RequestStatus.Loading }) {
return@combine RequestStatus.Loading<List<T>, E>()
}

// Collect all successful data, ignoring errors
statusArray
.filterIsInstance<RequestStatus.Success<T, E>>()
.map { it.data }
.let {
RequestStatus.Success(it)
}
}.collect { combinedStatus ->
emit(combinedStatus)
}
}

/**
* Collect secure media via Uri to update its list after deletion/restore.
* Needed because we can't use the album to observe for changes.
*
* NOTE: This _will_ be slow for large amounts of media.
*/
@OptIn(ExperimentalCoroutinesApi::class)
val secureMedias = parsedIntent
.flatMapLatest {
when (it) {
is IntentsViewModel.ParsedIntent.SecureReviewIntent -> {
it.medias.map { media ->
mediaRepository.media(media.uri)
}.toFlowOfRequestStatusList()
}

else -> flowOf(RequestStatus.Loading())
}
}
.flowOn(Dispatchers.IO)
.stateIn(
viewModelScope,
started = SharingStarted.WhileSubscribed(),
initialValue = RequestStatus.Loading(),
)

@OptIn(ExperimentalCoroutinesApi::class)
val medias = parsedIntent
.flatMapLatest {
Expand All @@ -123,9 +176,7 @@ class LocalPlayerViewModel(

is IntentsViewModel.ParsedIntent.ReviewIntent -> album

is IntentsViewModel.ParsedIntent.SecureReviewIntent -> {
flowOf(RequestStatus.Success(it.medias))
}
is IntentsViewModel.ParsedIntent.SecureReviewIntent -> secureMedias

else -> flowOf(RequestStatus.Loading())
}
Expand Down

0 comments on commit 7e7ea6d

Please sign in to comment.