Skip to content

Commit

Permalink
enh : fix not properly load folders
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSluffy committed Jan 23, 2025
1 parent 486f361 commit 94d2ba5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
18 changes: 13 additions & 5 deletions lawnchair/src/app/lawnchair/data/folder/model/FolderViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock

class FolderViewModel(context: Context) : ViewModel() {

Expand All @@ -38,9 +40,13 @@ class FolderViewModel(context: Context) : ViewModel() {
val folderInfo = _folderInfo.asStateFlow()
private var tempTitle: String = ""

private val mutex = Mutex()

init {
viewModelScope.launch {
loadFolders()
mutex.withLock {
loadFolders()
}
}
}

Expand All @@ -53,7 +59,9 @@ class FolderViewModel(context: Context) : ViewModel() {

fun refreshFolders() {
viewModelScope.launch {
loadFolders()
mutex.withLock {
loadFolders()
}
}
}

Expand Down Expand Up @@ -82,22 +90,22 @@ class FolderViewModel(context: Context) : ViewModel() {
fun updateFolderInfo(folderInfo: FolderInfo, hide: Boolean) {
viewModelScope.launch {
repository.updateFolderInfo(folderInfo, hide)
loadFolders()
}
refreshFolders()
}

fun saveFolder(folderInfo: FolderInfo) {
viewModelScope.launch {
repository.saveFolderInfo(folderInfo)
loadFolders()
}
refreshFolders()
}

fun deleteFolderInfo(id: Int) {
viewModelScope.launch {
repository.deleteFolderInfo(id)
loadFolders()
}
refreshFolders()
}

fun updateFolderWithItems(id: Int, title: String, appInfos: List<AppInfo>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class FolderService(val context: Context) : SafeCloseable {
}
}

suspend fun getAllFolders(): List<FolderInfo> = withContext(Dispatchers.IO) {
suspend fun getAllFolders(): List<FolderInfo> = withContext(Dispatchers.Main) {
try {
val folderEntities = folderDao.getAllFolders().firstOrNull() ?: emptyList()
folderEntities.mapNotNull { folderEntity ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ fun DrawerFolderPreferences(
viewModel.setAction(Action.DEFAULT)
}

viewModel.refreshFolders()

LoadingScreen(obj = folders, modifier = modifier.fillMaxWidth()) { items ->
PreferenceLayoutLazyColumn(
modifier = Modifier.fillMaxSize(),
Expand Down

0 comments on commit 94d2ba5

Please sign in to comment.