-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature|build] Support clear sticker provider thumbnail caches; upda…
…te dependencies
- Loading branch information
Showing
14 changed files
with
279 additions
and
12 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
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
14 changes: 11 additions & 3 deletions
14
app/src/main/java/com/skyd/rays/model/respository/DataRepository.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 |
---|---|---|
@@ -1,16 +1,24 @@ | ||
package com.skyd.rays.model.respository | ||
|
||
import com.skyd.rays.base.BaseRepository | ||
import com.skyd.rays.config.PROVIDER_THUMBNAIL_DIR | ||
import com.skyd.rays.model.db.dao.sticker.StickerDao | ||
import kotlinx.coroutines.flow.Flow | ||
import javax.inject.Inject | ||
import kotlin.system.measureTimeMillis | ||
|
||
class DataRepository @Inject constructor(private val stickerDao: StickerDao) : BaseRepository() { | ||
suspend fun requestDeleteAllData(): Flow<Long> { | ||
return flowOnIo { | ||
val startTime = System.currentTimeMillis() | ||
stickerDao.deleteAllStickerWithTags() | ||
emit(System.currentTimeMillis() - startTime) | ||
emit(measureTimeMillis { stickerDao.deleteAllStickerWithTags() }) | ||
} | ||
} | ||
|
||
suspend fun requestDeleteDocumentsProviderThumbnails(): Flow<Long> { | ||
return flowOnIo { | ||
emit(measureTimeMillis { | ||
PROVIDER_THUMBNAIL_DIR.deleteRecursively() | ||
}) | ||
} | ||
} | ||
} |
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
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/skyd/rays/ui/screen/settings/data/cache/CacheEvent.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,9 @@ | ||
package com.skyd.rays.ui.screen.settings.data.cache | ||
|
||
import com.skyd.rays.base.mvi.MviSingleEvent | ||
|
||
sealed interface CacheEvent : MviSingleEvent { | ||
sealed interface DeleteDocumentsProviderThumbnailsResultEvent : CacheEvent { | ||
data class Success(val time: Long) : DeleteDocumentsProviderThumbnailsResultEvent | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/com/skyd/rays/ui/screen/settings/data/cache/CacheIntent.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,8 @@ | ||
package com.skyd.rays.ui.screen.settings.data.cache | ||
|
||
import com.skyd.rays.base.mvi.MviIntent | ||
|
||
sealed interface CacheIntent : MviIntent { | ||
data object Init : CacheIntent | ||
data object DeleteDocumentsProviderThumbnails : CacheIntent | ||
} |
20 changes: 20 additions & 0 deletions
20
app/src/main/java/com/skyd/rays/ui/screen/settings/data/cache/CachePartialStateChange.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,20 @@ | ||
package com.skyd.rays.ui.screen.settings.data.cache | ||
|
||
internal sealed interface CachePartialStateChange { | ||
fun reduce(oldState: CacheState): CacheState | ||
|
||
data object LoadingDialog : CachePartialStateChange { | ||
override fun reduce(oldState: CacheState): CacheState = oldState.copy(loadingDialog = true) | ||
} | ||
|
||
data object Init : CachePartialStateChange { | ||
override fun reduce(oldState: CacheState) = oldState | ||
} | ||
|
||
sealed interface DeleteDocumentsProviderThumbnails : CachePartialStateChange { | ||
data class Success(val time: Long) : DeleteDocumentsProviderThumbnails { | ||
override fun reduce(oldState: CacheState): CacheState = | ||
oldState.copy(loadingDialog = false) | ||
} | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
app/src/main/java/com/skyd/rays/ui/screen/settings/data/cache/CacheScreen.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,84 @@ | ||
package com.skyd.rays.ui.screen.settings.data.cache | ||
|
||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.DeleteOutline | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.material3.SnackbarHost | ||
import androidx.compose.material3.SnackbarHostState | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TopAppBarDefaults | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.vector.rememberVectorPainter | ||
import androidx.compose.ui.input.nestedscroll.nestedScroll | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
import androidx.lifecycle.compose.collectAsStateWithLifecycle | ||
import com.skyd.rays.R | ||
import com.skyd.rays.base.mvi.getDispatcher | ||
import com.skyd.rays.ext.showSnackbarWithLaunchedEffect | ||
import com.skyd.rays.ui.component.BaseSettingsItem | ||
import com.skyd.rays.ui.component.RaysTopBar | ||
import com.skyd.rays.ui.component.RaysTopBarStyle | ||
import com.skyd.rays.ui.component.dialog.WaitingDialog | ||
|
||
const val CACHE_SCREEN_ROUTE = "cacheScreen" | ||
|
||
@Composable | ||
fun CacheScreen(viewModel: CacheViewModel = hiltViewModel()) { | ||
val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior() | ||
val context = LocalContext.current | ||
val snackbarHostState = remember { SnackbarHostState() } | ||
val uiState by viewModel.viewState.collectAsStateWithLifecycle() | ||
val uiEvent by viewModel.singleEvent.collectAsStateWithLifecycle(initialValue = null) | ||
|
||
val dispatch = viewModel.getDispatcher(startWith = CacheIntent.Init) | ||
|
||
Scaffold( | ||
snackbarHost = { SnackbarHost(hostState = snackbarHostState) }, | ||
topBar = { | ||
RaysTopBar( | ||
style = RaysTopBarStyle.Large, | ||
scrollBehavior = scrollBehavior, | ||
title = { Text(text = stringResource(R.string.cache_screen_name)) }, | ||
) | ||
} | ||
) { paddingValues -> | ||
LazyColumn( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.nestedScroll(scrollBehavior.nestedScrollConnection), | ||
contentPadding = paddingValues, | ||
) { | ||
item { | ||
BaseSettingsItem( | ||
icon = rememberVectorPainter(image = Icons.Default.DeleteOutline), | ||
text = stringResource(id = R.string.cache_screen_delete_provider_thumbnails), | ||
descriptionText = stringResource(id = R.string.cache_screen_delete_provider_thumbnails_description), | ||
onClick = { dispatch(CacheIntent.DeleteDocumentsProviderThumbnails) } | ||
) | ||
} | ||
} | ||
|
||
when (val event = uiEvent) { | ||
is CacheEvent.DeleteDocumentsProviderThumbnailsResultEvent.Success -> { | ||
snackbarHostState.showSnackbarWithLaunchedEffect( | ||
message = context.getString( | ||
R.string.cache_screen_delete_provider_thumbnails_success, | ||
event.time / 1000.0f | ||
), | ||
key2 = event, | ||
) | ||
} | ||
|
||
null -> Unit | ||
} | ||
|
||
WaitingDialog(visible = uiState.loadingDialog) | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
app/src/main/java/com/skyd/rays/ui/screen/settings/data/cache/CacheState.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,13 @@ | ||
package com.skyd.rays.ui.screen.settings.data.cache | ||
|
||
import com.skyd.rays.base.mvi.MviViewState | ||
|
||
data class CacheState( | ||
val loadingDialog: Boolean, | ||
) : MviViewState { | ||
companion object { | ||
fun initial() = CacheState( | ||
loadingDialog = false, | ||
) | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
app/src/main/java/com/skyd/rays/ui/screen/settings/data/cache/CacheViewModel.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,73 @@ | ||
package com.skyd.rays.ui.screen.settings.data.cache | ||
|
||
import androidx.lifecycle.viewModelScope | ||
import com.skyd.rays.base.mvi.AbstractMviViewModel | ||
import com.skyd.rays.ext.startWith | ||
import com.skyd.rays.model.respository.DataRepository | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.SharedFlow | ||
import kotlinx.coroutines.flow.SharingStarted | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.filterIsInstance | ||
import kotlinx.coroutines.flow.filterNot | ||
import kotlinx.coroutines.flow.flatMapConcat | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.merge | ||
import kotlinx.coroutines.flow.onEach | ||
import kotlinx.coroutines.flow.scan | ||
import kotlinx.coroutines.flow.stateIn | ||
import kotlinx.coroutines.flow.take | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class CacheViewModel @Inject constructor(private var dataRepo: DataRepository) : | ||
AbstractMviViewModel<CacheIntent, CacheState, CacheEvent>() { | ||
|
||
override val viewState: StateFlow<CacheState> | ||
|
||
init { | ||
val initialVS = CacheState.initial() | ||
|
||
viewState = merge( | ||
intentSharedFlow.filterIsInstance<CacheIntent.Init>().take(1), | ||
intentSharedFlow.filterNot { it is CacheIntent.Init } | ||
) | ||
.shareWhileSubscribed() | ||
.toPartialStateChangeFlow() | ||
.debugLog("CachePartialStateChange") | ||
.sendSingleEvent() | ||
.scan(initialVS) { vs, change -> change.reduce(vs) } | ||
.debugLog("ViewState") | ||
.stateIn( | ||
viewModelScope, | ||
SharingStarted.Eagerly, | ||
initialVS | ||
) | ||
} | ||
|
||
|
||
private fun Flow<CachePartialStateChange>.sendSingleEvent(): Flow<CachePartialStateChange> { | ||
return onEach { change -> | ||
val event = when (change) { | ||
is CachePartialStateChange.DeleteDocumentsProviderThumbnails.Success -> | ||
CacheEvent.DeleteDocumentsProviderThumbnailsResultEvent.Success(change.time) | ||
|
||
else -> return@onEach | ||
} | ||
sendEvent(event) | ||
} | ||
} | ||
|
||
private fun SharedFlow<CacheIntent>.toPartialStateChangeFlow(): Flow<CachePartialStateChange> { | ||
return merge( | ||
filterIsInstance<CacheIntent.Init>().map { CachePartialStateChange.Init }, | ||
|
||
filterIsInstance<CacheIntent.DeleteDocumentsProviderThumbnails>().flatMapConcat { | ||
dataRepo.requestDeleteDocumentsProviderThumbnails() | ||
.map { CachePartialStateChange.DeleteDocumentsProviderThumbnails.Success(it) } | ||
.startWith(CachePartialStateChange.LoadingDialog) | ||
}, | ||
) | ||
} | ||
} |
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
Oops, something went wrong.