launchInVM
This commit is contained in:
1 parent
ebb3eb1ab3
commit
4cbb0eca4f
5 files changed
+27
-20
No files matched your search
@@ -259,7 +259,7 @@ fun GalleryListScreen(lub: ListUrlBuilder, navigator: DestinationsNavigator) {
|
||||
}
|
||||
}.flow.cachedIn(viewModelScope)
|
||||
}.collectAsLazyPagingItems()
|
||||
FavouriteStatusRouter.observe(data)
|
||||
FavouriteStatusRouter.Observe(data)
|
||||
val listMode by Settings.listMode.collectAsState()
|
||||
|
||||
val quickSearchList = remember { mutableStateListOf<QuickSearch>() }
|
||||
|
||||
@@ -13,11 +13,7 @@ import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.material3.TopAppBarDefaults
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.input.nestedscroll.nestedScroll
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
@@ -53,6 +49,7 @@ import com.hippo.ehviewer.ui.tools.foldToLoadResult
|
||||
import com.hippo.ehviewer.ui.tools.getClippedRefreshKey
|
||||
import com.hippo.ehviewer.ui.tools.getLimit
|
||||
import com.hippo.ehviewer.ui.tools.getOffset
|
||||
import com.hippo.ehviewer.ui.tools.launchInVM
|
||||
import com.hippo.ehviewer.ui.tools.rememberInVM
|
||||
import com.ramcosta.composedestinations.annotation.Destination
|
||||
import eu.kanade.tachiyomi.util.lang.withIOContext
|
||||
@@ -60,7 +57,7 @@ import moe.tarsin.coroutines.runSuspendCatching
|
||||
|
||||
@Destination
|
||||
@Composable
|
||||
fun GalleryPreviewScreen(galleryDetail: GalleryDetail, toNextPageArg: Boolean, navigator: NavController) {
|
||||
fun GalleryPreviewScreen(galleryDetail: GalleryDetail, toNextPage: Boolean, navigator: NavController) {
|
||||
LockDrawer(true)
|
||||
val context = LocalContext.current
|
||||
val scrollBehaviour = TopAppBarDefaults.pinnedScrollBehavior()
|
||||
@@ -68,11 +65,9 @@ fun GalleryPreviewScreen(galleryDetail: GalleryDetail, toNextPageArg: Boolean, n
|
||||
val pages = galleryDetail.pages
|
||||
val pgSize = galleryDetail.previewList.size
|
||||
val thumbColumns by Settings.thumbColumns.collectAsState()
|
||||
var toNextPage by rememberSaveable { mutableStateOf(toNextPageArg) }
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
if (toNextPage) state.scrollToItem(pgSize)
|
||||
toNextPage = false
|
||||
if (toNextPage) {
|
||||
launchInVM { state.scrollToItem(pgSize) }
|
||||
}
|
||||
|
||||
val data = rememberInVM {
|
||||
|
||||
@@ -67,7 +67,7 @@ fun HistoryScreen(navigator: DestinationsNavigator) {
|
||||
EhDB.historyLazyList
|
||||
}.flow.cachedIn(viewModelScope)
|
||||
}.collectAsLazyPagingItems()
|
||||
FavouriteStatusRouter.observe(historyData)
|
||||
FavouriteStatusRouter.Observe(historyData)
|
||||
Scaffold(
|
||||
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
|
||||
topBar = {
|
||||
|
||||
@@ -9,7 +9,13 @@ import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberUpdatedState
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
import kotlin.coroutines.EmptyCoroutineContext
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.CoroutineStart
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class StateMapViewModel : ViewModel() {
|
||||
val states: MutableMap<Int, ArrayDeque<Any>> = mutableMapOf()
|
||||
@@ -36,6 +42,15 @@ inline fun <T : Any> rememberInVM(
|
||||
return value
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun launchInVM(
|
||||
context: CoroutineContext = EmptyCoroutineContext,
|
||||
start: CoroutineStart = CoroutineStart.DEFAULT,
|
||||
block: suspend CoroutineScope.() -> Unit,
|
||||
) = rememberInVM {
|
||||
viewModelScope.launch(context, start, block)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun <T> rememberUpdatedStateInVM(newValue: T): State<T> = rememberInVM {
|
||||
mutableStateOf(newValue)
|
||||
|
||||
@@ -21,11 +21,10 @@ import androidx.compose.runtime.Stable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.paging.compose.LazyPagingItems
|
||||
import com.hippo.ehviewer.EhDB
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.ui.tools.rememberInVM
|
||||
import com.hippo.ehviewer.ui.tools.launchInVM
|
||||
import com.hippo.ehviewer.ui.tools.rememberUpdatedStateInVM
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -58,14 +57,12 @@ object FavouriteStatusRouter {
|
||||
}.collectAsState(transform(initial.favoriteSlot))
|
||||
|
||||
@Composable
|
||||
fun observe(list: LazyPagingItems<out GalleryInfo>) {
|
||||
fun Observe(list: LazyPagingItems<out GalleryInfo>) {
|
||||
val realList by rememberUpdatedStateInVM(newValue = list.itemSnapshotList.items)
|
||||
rememberInVM {
|
||||
viewModelScope.launch {
|
||||
globalFlow.collect { (gid, newSlot) ->
|
||||
realList.forEach { info ->
|
||||
if (info.gid == gid) info.favoriteSlot = newSlot
|
||||
}
|
||||
launchInVM {
|
||||
globalFlow.collect { (gid, newSlot) ->
|
||||
realList.forEach { info ->
|
||||
if (info.gid == gid) info.favoriteSlot = newSlot
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user