Skip to content

Commit 7e7ea6d

Browse files
Torsten Groteluca020400
andcommitted
Glimpse: Fix trashed SECURE_REVIEW items not getting removed from screen
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
1 parent dde8aa8 commit 7e7ea6d

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

app/src/main/java/org/lineageos/glimpse/viewmodels/LocalPlayerViewModel.kt

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import androidx.media3.common.MediaItem
1515
import androidx.media3.exoplayer.ExoPlayer
1616
import kotlinx.coroutines.Dispatchers
1717
import kotlinx.coroutines.ExperimentalCoroutinesApi
18+
import kotlinx.coroutines.flow.Flow
1819
import kotlinx.coroutines.flow.MutableStateFlow
1920
import kotlinx.coroutines.flow.SharingStarted
2021
import kotlinx.coroutines.flow.asStateFlow
@@ -113,6 +114,58 @@ class LocalPlayerViewModel(
113114
initialValue = RequestStatus.Loading(),
114115
)
115116

117+
private fun <T, E> List<Flow<RequestStatus<T, E>>>.toFlowOfRequestStatusList() = flow {
118+
if (isEmpty()) {
119+
emit(RequestStatus.Success(emptyList()))
120+
return@flow
121+
}
122+
123+
emit(RequestStatus.Loading())
124+
125+
combine(this@toFlowOfRequestStatusList) { statusArray ->
126+
// Check if any item is still loading
127+
if (statusArray.any { it is RequestStatus.Loading }) {
128+
return@combine RequestStatus.Loading<List<T>, E>()
129+
}
130+
131+
// Collect all successful data, ignoring errors
132+
statusArray
133+
.filterIsInstance<RequestStatus.Success<T, E>>()
134+
.map { it.data }
135+
.let {
136+
RequestStatus.Success(it)
137+
}
138+
}.collect { combinedStatus ->
139+
emit(combinedStatus)
140+
}
141+
}
142+
143+
/**
144+
* Collect secure media via Uri to update its list after deletion/restore.
145+
* Needed because we can't use the album to observe for changes.
146+
*
147+
* NOTE: This _will_ be slow for large amounts of media.
148+
*/
149+
@OptIn(ExperimentalCoroutinesApi::class)
150+
val secureMedias = parsedIntent
151+
.flatMapLatest {
152+
when (it) {
153+
is IntentsViewModel.ParsedIntent.SecureReviewIntent -> {
154+
it.medias.map { media ->
155+
mediaRepository.media(media.uri)
156+
}.toFlowOfRequestStatusList()
157+
}
158+
159+
else -> flowOf(RequestStatus.Loading())
160+
}
161+
}
162+
.flowOn(Dispatchers.IO)
163+
.stateIn(
164+
viewModelScope,
165+
started = SharingStarted.WhileSubscribed(),
166+
initialValue = RequestStatus.Loading(),
167+
)
168+
116169
@OptIn(ExperimentalCoroutinesApi::class)
117170
val medias = parsedIntent
118171
.flatMapLatest {
@@ -123,9 +176,7 @@ class LocalPlayerViewModel(
123176

124177
is IntentsViewModel.ParsedIntent.ReviewIntent -> album
125178

126-
is IntentsViewModel.ParsedIntent.SecureReviewIntent -> {
127-
flowOf(RequestStatus.Success(it.medias))
128-
}
179+
is IntentsViewModel.ParsedIntent.SecureReviewIntent -> secureMedias
129180

130181
else -> flowOf(RequestStatus.Loading())
131182
}

0 commit comments

Comments
 (0)