Skip to content

Commit

Permalink
enh : Exclude apps already in other folders from container list
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSluffy committed Jan 23, 2025
1 parent 792d7ed commit 30bde62
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lawnchair/src/app/lawnchair/data/folder/model/FolderViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class FolderViewModel(context: Context) : ViewModel() {
private val _foldersMutable = MutableLiveData<List<FolderInfo>>()
val foldersMutable: LiveData<List<FolderInfo>> = _foldersMutable

private val _items = MutableStateFlow<Set<String>>(setOf())
val items: StateFlow<Set<String>> = _items.asStateFlow()

private val _folderInfo = MutableStateFlow<FolderInfo?>(null)
val folderInfo = _folderInfo.asStateFlow()
private var tempTitle: String = ""
Expand All @@ -54,6 +57,13 @@ class FolderViewModel(context: Context) : ViewModel() {
}
}

fun setItems(id: Int) {
viewModelScope.launch {
val items = repository.getItems(id)
_items.value = items
}
}

fun updateCurrentTitle(title: String) {
if (action.value == Action.EDIT) {
tempTitle = title
Expand Down
4 changes: 4 additions & 0 deletions lawnchair/src/app/lawnchair/data/folder/service/FolderDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ interface FolderDao {
@Transaction
suspend fun getFolderWithItems(folderId: Int): FolderWithItems?

@Query("SELECT * FROM FolderItems WHERE folderId IS NOT :folderId")
@Transaction
suspend fun getItems(folderId: Int): List<FolderItemEntity>

@Query("SELECT * FROM Folders")
fun getAllFolders(): Flow<List<FolderInfoEntity>>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ class FolderService(val context: Context) : SafeCloseable {
return null
}

suspend fun getItems(id: Int): Set<String> = withContext(Dispatchers.IO) {
return@withContext try {
folderDao.getItems(id).mapNotNull { it.componentKey }.toSet()
} catch (e: Exception) {
Log.e("FolderService", "Failed to get all items", e)
setOf()
}
}

suspend fun getAllFolders(): List<FolderInfo> = withContext(Dispatchers.IO) {
try {
val folderEntities = folderDao.getAllFolders().firstOrNull() ?: emptyList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import app.lawnchair.LawnchairLauncher
import app.lawnchair.data.Converters
import app.lawnchair.data.factory.ViewModelFactory
import app.lawnchair.data.folder.model.FolderViewModel
import app.lawnchair.launcher
Expand Down Expand Up @@ -62,6 +63,7 @@ fun AppListToFolderPreferences(
val loading = folderInfo == null

val selectedAppsState = remember { mutableStateOf(setOf<ItemInfo>()) }
val dbItems = viewModel.items.collectAsState()

LaunchedEffect(folderInfoId) {
viewModel.setFolderInfo(folderInfoId, false)
Expand All @@ -70,9 +72,12 @@ fun AppListToFolderPreferences(
LaunchedEffect(folderInfo) {
val folderContents = folderInfo?.contents?.toMutableSet() ?: mutableSetOf()
selectedAppsState.value = folderContents
viewModel.setItems(folderInfoId)
}

val apps = launcher.mAppsView.appsStore.apps.toList()
val apps = launcher.mAppsView.appsStore.apps
.toList()
.filterNot { app -> dbItems.value.contains(Converters().fromComponentKey(app.componentKey)) }
.sortedBySelection(selectedAppsState.value)

val state = rememberLazyListState()
Expand Down

0 comments on commit 30bde62

Please sign in to comment.