(Absolutely awful) picker for any icon from any pack

This commit is contained in:
MM20
2022-07-28 22:48:12 +02:00
parent a2a22bd257
commit 6152c566f0
11 changed files with 185 additions and 58 deletions
@@ -6,9 +6,13 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyItemScope
import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyGridScope
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.paging.compose.itemsIndexed
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.OutlinedTextField
@@ -24,6 +28,8 @@ import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.paging.compose.LazyPagingItems
import androidx.paging.compose.collectAsLazyPagingItems
import de.mm20.launcher2.badges.Badge
import de.mm20.launcher2.search.data.Searchable
import de.mm20.launcher2.ui.R
@@ -108,6 +114,15 @@ fun CustomizeSearchableSheet(
val suggestions by
remember { viewModel.getIconSuggestions(iconSizePx.toInt()) }
.observeAsState(emptyList())
val iconPackIcons by remember {
viewModel.getAllIconsFromAllIconPacks()
}.observeAsState(emptyList())
val pagingItems = iconPackIcons.map {
it.flow.collectAsLazyPagingItems()
}
LazyVerticalGrid(columns = GridCells.Fixed(LocalGridColumns.current)) {
items(suggestions) {
Box(
@@ -116,15 +131,49 @@ fun CustomizeSearchableSheet(
) {
ShapedLauncherIcon(
size = iconSize,
icon = it.icon,
icon = it.preview,
onClick = {
viewModel.pickIcon(it.data)
viewModel.pickIcon(it.customIcon)
}
)
}
}
for (pager in pagingItems) {
itemsIndexed(pager) { index, item ->
Box(
contentAlignment = Alignment.Center,
modifier = Modifier.padding(vertical = 8.dp)
) {
ShapedLauncherIcon(
size = iconSize,
icon = item?.preview,
onClick = {
viewModel.pickIcon(item?.customIcon)
}
)
}
}
}
}
}
}
}
fun <T : Any> LazyGridScope.itemsIndexed(
items: LazyPagingItems<T>,
key: ((index: Int, item: T) -> Any)? = null,
itemContent: @Composable LazyGridScope.(index: Int, value: T?) -> Unit
) {
items(
count = items.itemCount,
key = if (key == null) null else { index ->
val item = items.peek(index)
if (item == null) {
} else {
key(index, item)
}
}
) { index ->
this@itemsIndexed.itemContent(index, items[index])
}
}
@@ -1,15 +1,14 @@
package de.mm20.launcher2.ui.launcher.search.common.customattrs
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.liveData
import androidx.paging.Pager
import androidx.paging.PagingConfig
import de.mm20.launcher2.customattrs.CustomIcon
import de.mm20.launcher2.icons.CustomIconSuggestion
import de.mm20.launcher2.icons.IconRepository
import de.mm20.launcher2.icons.LauncherIcon
import de.mm20.launcher2.search.data.Searchable
import kotlinx.coroutines.flow.Flow
import org.koin.androidx.compose.inject
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
@@ -40,4 +39,20 @@ class CustomizeSearchableSheetVM(
iconRepository.setCustomIcon(searchable, icon)
closeIconPicker()
}
fun getAllIconsFromAllIconPacks() = liveData {
emit(emptyList())
val iconPacks = iconRepository.getInstalledIconPacks()
emit(iconPacks.map {
val source = iconRepository.getAllIconsFromPack(it.packageName)
Pager(
PagingConfig(pageSize = 20, enablePlaceholders = false, maxSize = 200),
) {
source
}
})
}
}