Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Fix bug and Remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
SanmerDev committed Nov 25, 2023
1 parent 3d0a904 commit 62c7836
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -104,16 +103,16 @@ fun ModulesScreen(
)

DisposableEffect(viewModel) {
onDispose { viewModel.closeSearch() }
onDispose(viewModel::closeSearch)
}

Scaffold(
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
topBar = {
TopBar(
isSearch = viewModel.isSearch,
onQueryChange = { viewModel.search(it) },
onOpenSearch = { viewModel.isSearch = true },
onQueryChange = viewModel::search,
onOpenSearch = viewModel::openSearch,
onCloseSearch = viewModel::closeSearch,
setMenu = viewModel::setModulesMenu,
scrollBehavior = scrollBehavior
Expand Down Expand Up @@ -187,7 +186,10 @@ private fun TopBar(
setMenu: (ModulesMenuExt) -> Unit,
scrollBehavior: TopAppBarScrollBehavior,
) {
var query by rememberSaveable { mutableStateOf("") }
var query by remember { mutableStateOf("") }
DisposableEffect(isSearch) {
onDispose { query = "" }
}

SearchTopBar(
isSearch = isSearch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -60,16 +60,16 @@ fun RepositoryScreen(
)

DisposableEffect(viewModel) {
onDispose { viewModel.closeSearch() }
onDispose(viewModel::closeSearch)
}

Scaffold(
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
topBar = {
TopBar(
isSearch = viewModel.isSearch,
onQueryChange = { viewModel.search(it)},
onOpenSearch = { viewModel.isSearch = true },
onQueryChange = viewModel::search,
onOpenSearch = viewModel::openSearch,
onCloseSearch = viewModel::closeSearch,
setMenu = viewModel::setRepositoryMenu,
scrollBehavior = scrollBehavior
Expand Down Expand Up @@ -122,7 +122,10 @@ private fun TopBar(
setMenu: (RepositoryMenuExt) -> Unit,
scrollBehavior: TopAppBarScrollBehavior,
) {
var query by rememberSaveable { mutableStateOf("") }
var query by remember { mutableStateOf("") }
DisposableEffect(isSearch) {
onDispose { query = "" }
}

SearchTopBar(
isSearch = isSearch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject
Expand All @@ -51,14 +50,15 @@ class ModulesViewModel @Inject constructor(
.map { it.modulesMenu }

var isSearch by mutableStateOf(false)
private set
private val keyFlow = MutableStateFlow("")

private val valuesFlow = MutableStateFlow(
listOf<Pair<LocalState, LocalModule>>()
)
val local get() = valuesFlow.asStateFlow()

var isLoading by mutableStateOf(false)
var isLoading by mutableStateOf(true)
private set
var isRefreshing by mutableStateOf(false)
private set
Expand All @@ -77,7 +77,7 @@ class ModulesViewModel @Inject constructor(

private fun dataObserver() {
combine(
localRepository.getLocalAllAsFlow().onStart { isLoading = true },
localRepository.getLocalAllAsFlow(),
modulesMenu,
keyFlow,
) { list, menu, key ->
Expand All @@ -103,7 +103,7 @@ class ModulesViewModel @Inject constructor(

}.toMutableStateList()

if (isLoading) isLoading = false
isLoading = false

}.launchIn(viewModelScope)
}
Expand All @@ -130,6 +130,10 @@ class ModulesViewModel @Inject constructor(
keyFlow.value = key
}

fun openSearch() {
isSearch = true
}

fun closeSearch() {
isSearch = false
keyFlow.value = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject
Expand All @@ -35,14 +34,15 @@ class RepositoryViewModel @Inject constructor(
.map { it.repositoryMenu }

var isSearch by mutableStateOf(false)
private set
private val keyFlow = MutableStateFlow("")

private val valuesFlow = MutableStateFlow(
listOf<Pair<OnlineState, OnlineModule>>()
)
val online get() = valuesFlow.asStateFlow()

var isLoading by mutableStateOf(false)
var isLoading by mutableStateOf(true)
private set
var isRefreshing by mutableStateOf(false)
private set
Expand All @@ -59,7 +59,7 @@ class RepositoryViewModel @Inject constructor(

private fun dataObserver() {
combine(
localRepository.getOnlineAllAsFlow().onStart { isLoading = true },
localRepository.getOnlineAllAsFlow(),
repositoryMenu,
keyFlow,
) { list, menu, key ->
Expand Down Expand Up @@ -90,7 +90,7 @@ class RepositoryViewModel @Inject constructor(

}.toMutableStateList()

if (isLoading) isLoading = false
isLoading = false

}.launchIn(viewModelScope)
}
Expand All @@ -117,6 +117,10 @@ class RepositoryViewModel @Inject constructor(
keyFlow.value = key
}

fun openSearch() {
isSearch = true
}

fun closeSearch() {
isSearch = false
keyFlow.value = ""
Expand Down

0 comments on commit 62c7836

Please sign in to comment.