Implement icon picker ui
This commit is contained in:
1 parent
6d9acab484
commit
02a8ad7dc4
10 files changed
+236
-36
No files matched your search
@@ -55,6 +55,7 @@ apply plugin: 'com.android.application'
|
||||
apply plugin: 'kotlin-android'
|
||||
apply plugin: 'com.google.protobuf'
|
||||
apply plugin: 'com.google.android.gms.oss-licenses-plugin'
|
||||
apply plugin: 'kotlin-parcelize'
|
||||
|
||||
final def commitHash = { ->
|
||||
final def stdout = new ByteArrayOutputStream()
|
||||
|
||||
@@ -199,4 +199,5 @@
|
||||
<string name="all_apps_device_search_hint">Search</string>
|
||||
<string name="dark_status_bar_label">Dark Status Bar</string>
|
||||
<string name="lawnicons_not_installed_description">Lawnicons not installed.</string>
|
||||
<string name="icon_picker_default_category">Icons</string>
|
||||
</resources>
|
||||
@@ -6,11 +6,19 @@ import android.content.pm.PackageManager
|
||||
import android.content.res.Resources
|
||||
import android.content.res.XmlResourceParser
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.util.Log
|
||||
import android.util.Xml
|
||||
import com.android.launcher3.R
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.flowOn
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.xmlpull.v1.XmlPullParser
|
||||
import org.xmlpull.v1.XmlPullParserException
|
||||
import org.xmlpull.v1.XmlPullParserFactory
|
||||
import java.io.IOException
|
||||
import java.lang.Exception
|
||||
|
||||
class CustomIconPack(context: Context, packPackageName: String) :
|
||||
IconPack(context, packPackageName) {
|
||||
@@ -110,6 +118,47 @@ class CustomIconPack(context: Context, packPackageName: String) :
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
override fun getAllIcons(): Flow<List<IconPickerCategory>> = flow {
|
||||
val result = mutableListOf<IconPickerCategory>()
|
||||
|
||||
var currentTitle: String? = null
|
||||
val currentItems = mutableListOf<IconPickerItem>()
|
||||
|
||||
suspend fun endCategory() {
|
||||
if (currentItems.isEmpty()) return
|
||||
val title = currentTitle ?: context.getString(R.string.icon_picker_default_category)
|
||||
result.add(IconPickerCategory(title, ArrayList(currentItems)))
|
||||
currentTitle = null
|
||||
currentItems.clear()
|
||||
emit(ArrayList(result))
|
||||
}
|
||||
|
||||
try {
|
||||
val parser = getXml("drawable")
|
||||
while (parser != null && parser.next() != XmlPullParser.END_DOCUMENT) {
|
||||
if (parser.eventType != XmlPullParser.START_TAG) continue
|
||||
when (parser.name) {
|
||||
"category" -> {
|
||||
endCategory()
|
||||
currentTitle = parser["title"]!!
|
||||
}
|
||||
"item" -> {
|
||||
val drawableName = parser["drawable"]!!
|
||||
val resId = getDrawableId(drawableName)
|
||||
if (resId != 0) {
|
||||
val item = IconPickerItem(packPackageName, drawableName, drawableName, resId)
|
||||
currentItems.add(item)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
endCategory()
|
||||
} catch (e: Exception) {
|
||||
|
||||
}
|
||||
}.flowOn(Dispatchers.IO)
|
||||
|
||||
private fun getDrawableId(name: String) = idCache.getOrPut(name) {
|
||||
packResources.getIdentifier(name, "drawable", packPackageName)
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.graphics.drawable.Drawable
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import java.util.concurrent.Semaphore
|
||||
|
||||
abstract class IconPack(
|
||||
@@ -43,6 +44,8 @@ abstract class IconPack(
|
||||
|
||||
abstract fun getIcon(iconEntry: IconEntry, iconDpi: Int): Drawable?
|
||||
|
||||
abstract fun getAllIcons(): Flow<List<IconPickerCategory>>
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
protected abstract fun loadInternal()
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package app.lawnchair.icons
|
||||
|
||||
data class IconPickerCategory(
|
||||
val title: String,
|
||||
val items: List<IconPickerItem>
|
||||
)
|
||||
|
||||
fun IconPickerCategory.filter(searchQuery: String): IconPickerCategory {
|
||||
return IconPickerCategory(
|
||||
title = title,
|
||||
items = items
|
||||
.filter { it.label.lowercase().contains(searchQuery.lowercase()) }
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package app.lawnchair.icons
|
||||
|
||||
import android.os.Parcelable
|
||||
import kotlinx.parcelize.Parcelize
|
||||
|
||||
@Parcelize
|
||||
data class IconPickerItem(
|
||||
val packPackageName: String,
|
||||
val drawableName: String,
|
||||
val label: String,
|
||||
val resId: Int
|
||||
) : Parcelable {
|
||||
fun toIconEntry(): IconEntry {
|
||||
return IconEntry(
|
||||
packPackageName = packPackageName,
|
||||
name = drawableName
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,29 +1,33 @@
|
||||
package app.lawnchair.ui.preferences
|
||||
|
||||
import android.graphics.drawable.Drawable
|
||||
import androidx.activity.compose.LocalOnBackPressedDispatcherOwner
|
||||
import androidx.compose.animation.ExperimentalAnimationApi
|
||||
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.LocalLayoutDirection
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.navigation.NavGraphBuilder
|
||||
import androidx.navigation.NavType
|
||||
import androidx.navigation.navArgument
|
||||
import app.lawnchair.icons.IconPack
|
||||
import app.lawnchair.icons.IconPackProvider
|
||||
import app.lawnchair.theme.surfaceColorAtElevation
|
||||
import app.lawnchair.icons.*
|
||||
import app.lawnchair.ui.preferences.components.*
|
||||
import app.lawnchair.ui.util.resultSender
|
||||
import app.lawnchair.util.value
|
||||
import com.google.accompanist.drawablepainter.rememberDrawablePainter
|
||||
import com.google.accompanist.insets.ui.LocalScaffoldPadding
|
||||
import com.google.accompanist.navigation.animation.composable
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import java.util.*
|
||||
|
||||
@ExperimentalAnimationApi
|
||||
@@ -42,7 +46,6 @@ fun NavGraphBuilder.iconPickerGraph(route: String) {
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalFoundationApi::class)
|
||||
@ExperimentalAnimationApi
|
||||
@Composable
|
||||
fun IconPickerPreference(packageName: String) {
|
||||
@@ -58,6 +61,8 @@ fun IconPickerPreference(packageName: String) {
|
||||
val iconPack = optionalIconPack?.value
|
||||
var searchQuery by remember { mutableStateOf("") }
|
||||
|
||||
val onClickItem = resultSender<IconPickerItem>()
|
||||
|
||||
PreferenceSearchScaffold(
|
||||
searchInput = {
|
||||
SearchTextField(
|
||||
@@ -79,36 +84,15 @@ fun IconPickerPreference(packageName: String) {
|
||||
val topPadding = scaffoldPadding.calculateTopPadding()
|
||||
|
||||
CompositionLocalProvider(LocalScaffoldPadding provides innerPadding) {
|
||||
PreferenceLazyColumn(
|
||||
modifier = Modifier
|
||||
.padding(horizontal = 16.dp)
|
||||
.padding(top = topPadding)
|
||||
) {
|
||||
for (i in (0 until 4)) {
|
||||
stickyHeader {
|
||||
Text(
|
||||
text = "Category $i",
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.background(MaterialTheme.colorScheme.background)
|
||||
.padding(vertical = 16.dp),
|
||||
style = MaterialTheme.typography.titleSmall,
|
||||
color = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
}
|
||||
verticalGridItems(
|
||||
count = 18,
|
||||
numColumns = 5,
|
||||
gap = 16.dp
|
||||
) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.aspectRatio(1f)
|
||||
.clip(shape = CircleShape)
|
||||
.background(MaterialTheme.colorScheme.surfaceColorAtElevation(3.dp))
|
||||
)
|
||||
}
|
||||
}
|
||||
if (iconPack != null) {
|
||||
IconPickerGrid(
|
||||
iconPack = iconPack,
|
||||
searchQuery = searchQuery,
|
||||
onClickItem = onClickItem,
|
||||
modifier = Modifier
|
||||
.padding(horizontal = 16.dp)
|
||||
.padding(top = topPadding)
|
||||
)
|
||||
}
|
||||
}
|
||||
Spacer(
|
||||
@@ -120,6 +104,75 @@ fun IconPickerPreference(packageName: String) {
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalFoundationApi::class)
|
||||
@Composable
|
||||
fun IconPickerGrid(
|
||||
iconPack: IconPack,
|
||||
searchQuery: String,
|
||||
onClickItem: (item: IconPickerItem) -> Unit,
|
||||
modifier: Modifier = Modifier
|
||||
) {
|
||||
val categories by iconPack.getAllIcons().collectAsState(emptyList())
|
||||
val filteredCategories by derivedStateOf {
|
||||
categories
|
||||
.map { it.filter(searchQuery) }
|
||||
.filter { it.items.isNotEmpty() }
|
||||
}
|
||||
|
||||
PreferenceLazyColumn(modifier = modifier) {
|
||||
filteredCategories.forEach { category ->
|
||||
stickyHeader {
|
||||
Text(
|
||||
text = category.title,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.background(MaterialTheme.colorScheme.background)
|
||||
.padding(vertical = 16.dp),
|
||||
style = MaterialTheme.typography.titleSmall,
|
||||
color = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
}
|
||||
verticalGridItems(
|
||||
items = category.items,
|
||||
numColumns = 5,
|
||||
gap = 16.dp
|
||||
) { _, item ->
|
||||
IconPreview(
|
||||
iconPack = iconPack,
|
||||
iconItem = item,
|
||||
onClick = {
|
||||
onClickItem(item)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun IconPreview(
|
||||
iconPack: IconPack,
|
||||
iconItem: IconPickerItem,
|
||||
onClick: () -> Unit
|
||||
) {
|
||||
val drawable by produceState<Drawable?>(initialValue = null, iconPack, iconItem) {
|
||||
launch(Dispatchers.IO) {
|
||||
value = iconPack.getIcon(iconItem.toIconEntry(), 0)
|
||||
}
|
||||
}
|
||||
Image(
|
||||
painter = rememberDrawablePainter(drawable),
|
||||
contentDescription = iconItem.drawableName,
|
||||
modifier = Modifier
|
||||
.aspectRatio(1f)
|
||||
.clickable(
|
||||
interactionSource = remember { MutableInteractionSource() },
|
||||
indication = null,
|
||||
onClick = onClick
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun loadIconPack(packPackageName: String): Optional<IconPack>? {
|
||||
val context = LocalContext.current
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package app.lawnchair.ui.preferences
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.content.pm.LauncherApps
|
||||
import android.util.Log
|
||||
import androidx.compose.animation.ExperimentalAnimationApi
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
@@ -13,9 +15,11 @@ import androidx.core.graphics.drawable.toBitmap
|
||||
import androidx.navigation.NavGraphBuilder
|
||||
import androidx.navigation.NavType
|
||||
import androidx.navigation.navArgument
|
||||
import app.lawnchair.icons.IconPickerItem
|
||||
import app.lawnchair.ui.preferences.components.AppItem
|
||||
import app.lawnchair.ui.preferences.components.PreferenceLayoutLazyColumn
|
||||
import app.lawnchair.ui.preferences.components.preferenceGroupItems
|
||||
import app.lawnchair.ui.util.OnResult
|
||||
import com.android.launcher3.util.ComponentKey
|
||||
import com.google.accompanist.navigation.animation.composable
|
||||
|
||||
@@ -51,6 +55,11 @@ fun SelectIconPreference(componentKey: ComponentKey) {
|
||||
val iconPacks by LocalPreferenceInteractor.current.iconPacks.collectAsState()
|
||||
val navController = LocalNavController.current
|
||||
|
||||
OnResult<IconPickerItem> {
|
||||
Log.d("SelectIconPreference", "result: $it")
|
||||
(context as Activity).finish()
|
||||
}
|
||||
|
||||
PreferenceLayoutLazyColumn(label = label) {
|
||||
preferenceGroupItems(
|
||||
heading = { "Choose icon from" },
|
||||
|
||||
@@ -37,3 +37,18 @@ fun LazyListScope.verticalGridItems(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> LazyListScope.verticalGridItems(
|
||||
items: List<T>,
|
||||
numColumns: Int,
|
||||
gap: Dp,
|
||||
itemContent: @Composable (index: Int, item: T) -> Unit
|
||||
) {
|
||||
verticalGridItems(
|
||||
count = items.size,
|
||||
numColumns = numColumns,
|
||||
gap = gap
|
||||
) { index ->
|
||||
itemContent(index, items[index])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package app.lawnchair.ui.util
|
||||
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.runtime.livedata.observeAsState
|
||||
import app.lawnchair.ui.preferences.LocalNavController
|
||||
|
||||
@Composable
|
||||
fun <T> resultSender(): (T) -> Unit {
|
||||
val navController = LocalNavController.current
|
||||
return { item: T ->
|
||||
navController.previousBackStackEntry
|
||||
?.savedStateHandle
|
||||
?.set("result", item)
|
||||
navController.popBackStack()
|
||||
Unit
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun <T> OnResult(callback: (result: T) -> Unit) {
|
||||
val currentCallback = rememberUpdatedState(callback)
|
||||
var fired by remember { mutableStateOf(false) }
|
||||
|
||||
val handle = LocalNavController.current.currentBackStackEntry?.savedStateHandle
|
||||
val result = handle?.getLiveData<T>("result")?.observeAsState()
|
||||
|
||||
SideEffect {
|
||||
result?.value?.let {
|
||||
if (fired) return@let
|
||||
fired = true
|
||||
currentCallback.value(it)
|
||||
handle.remove<T>("result")
|
||||
Unit
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user