Add font override in debug menu
This commit is contained in:
@@ -19,12 +19,14 @@ package app.lawnchair.font.googlefonts
|
||||
|
||||
import android.content.Context
|
||||
import android.content.res.Resources
|
||||
import app.lawnchair.preferences2.PreferenceManager2
|
||||
import app.lawnchair.util.toArrayList
|
||||
import com.android.launcher3.util.MainThreadInitializedObject
|
||||
import kotlinx.coroutines.CoroutineName
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.flow.first
|
||||
import org.json.JSONObject
|
||||
|
||||
class GoogleFontsListing private constructor(private val context: Context) {
|
||||
@@ -33,12 +35,19 @@ class GoogleFontsListing private constructor(private val context: Context) {
|
||||
private val dataProvider = MockDataProvider(context.resources)
|
||||
private val fonts by lazy { scope.async(Dispatchers.IO) { loadFontListing() } }
|
||||
|
||||
private fun loadFontListing(): List<GoogleFontInfo> {
|
||||
private suspend fun loadFontListing(): List<GoogleFontInfo> {
|
||||
val json = dataProvider.getFontListing()
|
||||
return parseFontListing(json)
|
||||
}
|
||||
|
||||
private fun parseFontListing(json: JSONObject): List<GoogleFontInfo> {
|
||||
private suspend fun getAdditionalFonts(): List<String> {
|
||||
val prefs = PreferenceManager2.getInstance(context)
|
||||
val userFontsString = prefs.additionalFonts.get().first()
|
||||
val userFonts = if (userFontsString.isEmpty()) emptyList() else userFontsString.split(",")
|
||||
return listOf("Inter") + userFonts
|
||||
}
|
||||
|
||||
private suspend fun parseFontListing(json: JSONObject): List<GoogleFontInfo> {
|
||||
val fonts = ArrayList<GoogleFontInfo>()
|
||||
val items = json.getJSONArray(KEY_ITEMS)
|
||||
for (i in (0 until items.length())) {
|
||||
@@ -47,7 +56,9 @@ class GoogleFontsListing private constructor(private val context: Context) {
|
||||
val variants = font.getJSONArray(KEY_VARIANTS).toArrayList<String>()
|
||||
fonts.add(GoogleFontInfo(family, variants))
|
||||
}
|
||||
fonts.add(GoogleFontInfo("Inter", listOf("regular", "italic", "500", "500italic", "700", "700italic")))
|
||||
getAdditionalFonts().forEach {
|
||||
fonts.add(GoogleFontInfo(it, listOf("regular", "italic", "500", "500italic", "700", "700italic")))
|
||||
}
|
||||
fonts.sort()
|
||||
return fonts
|
||||
}
|
||||
|
||||
@@ -242,6 +242,11 @@ class PreferenceManager2(private val context: Context) : PreferenceManager {
|
||||
onSet = { reloadHelper.reloadGrid() },
|
||||
)
|
||||
|
||||
val additionalFonts = preference(
|
||||
key = stringPreferencesKey(name = "additional_fonts"),
|
||||
defaultValue = "",
|
||||
)
|
||||
|
||||
init {
|
||||
initializeIconShape(iconShape.firstBlocking())
|
||||
iconShape.get()
|
||||
|
||||
@@ -12,10 +12,7 @@ import app.lawnchair.preferences.getAdapter
|
||||
import app.lawnchair.preferences.preferenceManager
|
||||
import app.lawnchair.preferences2.PreferenceManager2
|
||||
import app.lawnchair.preferences2.preferenceManager2
|
||||
import app.lawnchair.ui.preferences.components.ClickablePreference
|
||||
import app.lawnchair.ui.preferences.components.PreferenceGroup
|
||||
import app.lawnchair.ui.preferences.components.PreferenceLayout
|
||||
import app.lawnchair.ui.preferences.components.SwitchPreference
|
||||
import app.lawnchair.ui.preferences.components.*
|
||||
import com.android.launcher3.settings.DeveloperOptionsFragment
|
||||
import com.android.launcher3.settings.SettingsActivity
|
||||
import com.patrykmichalik.preferencemanager.Preference
|
||||
@@ -36,6 +33,8 @@ fun DebugMenuPreferences() {
|
||||
val prefs = preferenceManager()
|
||||
val prefs2 = preferenceManager2()
|
||||
val flags = remember { prefs.getDebugFlags() }
|
||||
val flags2 = remember { prefs2.debugFlags }
|
||||
val textFlags = remember { prefs2.textFlags }
|
||||
val context = LocalContext.current
|
||||
PreferenceLayout(label = "Debug Menu") {
|
||||
PreferenceGroup {
|
||||
@@ -53,7 +52,7 @@ fun DebugMenuPreferences() {
|
||||
)
|
||||
}
|
||||
PreferenceGroup(heading = "Debug Flags") {
|
||||
prefs2.debugFlags.forEach {
|
||||
flags2.forEach {
|
||||
SwitchPreference(
|
||||
adapter = it.getAdapter(),
|
||||
label = it.key.name
|
||||
@@ -65,6 +64,12 @@ fun DebugMenuPreferences() {
|
||||
label = it.key,
|
||||
)
|
||||
}
|
||||
textFlags.forEach {
|
||||
TextPreference(
|
||||
adapter = it.getAdapter(),
|
||||
label = it.key.name
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -72,6 +77,9 @@ fun DebugMenuPreferences() {
|
||||
private val PreferenceManager2.debugFlags: List<Preference<Boolean, Boolean>>
|
||||
get() = listOf(showComponentNames)
|
||||
|
||||
private val PreferenceManager2.textFlags: List<Preference<String, String>>
|
||||
get() = listOf(additionalFonts)
|
||||
|
||||
private fun PreferenceManager.getDebugFlags() =
|
||||
listOf(
|
||||
deviceSearch,
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
package app.lawnchair.ui.preferences.components
|
||||
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.requiredWidth
|
||||
import androidx.compose.material.OutlinedTextField
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.OutlinedButton
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import app.lawnchair.preferences.PreferenceAdapter
|
||||
import app.lawnchair.ui.AlertBottomSheetContent
|
||||
import app.lawnchair.ui.util.bottomSheetHandler
|
||||
|
||||
@Composable
|
||||
fun TextPreference(
|
||||
adapter: PreferenceAdapter<String>,
|
||||
label: String,
|
||||
description: String? = null,
|
||||
enabled: Boolean = true,
|
||||
) {
|
||||
val value = adapter.state.value
|
||||
TextPreference(
|
||||
value = value,
|
||||
onChange = adapter::onChange,
|
||||
label = label,
|
||||
description = description,
|
||||
enabled = enabled,
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun TextPreference(
|
||||
value: String,
|
||||
onChange: (String) -> Unit,
|
||||
label: String,
|
||||
description: String? = null,
|
||||
enabled: Boolean = true,
|
||||
) {
|
||||
val bottomSheetHandler = bottomSheetHandler
|
||||
PreferenceTemplate(
|
||||
title = { Text(text = label) },
|
||||
description = { description?.let { Text(text = it) } },
|
||||
modifier = Modifier
|
||||
.clickable(enabled) {
|
||||
bottomSheetHandler.show {
|
||||
TextPreferenceDialog(
|
||||
title = label,
|
||||
initialValue = value,
|
||||
onDismissRequest = { bottomSheetHandler.hide() },
|
||||
onConfirm = onChange
|
||||
)
|
||||
}
|
||||
},
|
||||
enabled = enabled
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun TextPreferenceDialog(
|
||||
title: String,
|
||||
initialValue: String,
|
||||
onDismissRequest: () -> Unit,
|
||||
onConfirm: (String) -> Unit
|
||||
) {
|
||||
var value by remember { mutableStateOf(initialValue) }
|
||||
AlertBottomSheetContent(
|
||||
title = { Text(text = title) },
|
||||
text = {
|
||||
OutlinedTextField(
|
||||
value = value,
|
||||
onValueChange = { value = it },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
singleLine = true
|
||||
)
|
||||
},
|
||||
buttons = {
|
||||
OutlinedButton(
|
||||
onClick = onDismissRequest
|
||||
) {
|
||||
Text(text = stringResource(id = android.R.string.cancel))
|
||||
}
|
||||
Spacer(modifier = Modifier.requiredWidth(8.dp))
|
||||
Button(
|
||||
onClick = {
|
||||
onDismissRequest()
|
||||
onConfirm(value)
|
||||
}
|
||||
) {
|
||||
Text(text = stringResource(id = android.R.string.ok))
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user