mirror of
https://github.com/Helium314/HeliBoard.git
synced 2025-05-14 05:52:47 +00:00
make custom font setting work
This commit is contained in:
parent
ca89eaa51c
commit
534bfb2f13
5 changed files with 84 additions and 15 deletions
|
@ -17,7 +17,6 @@ import kotlinx.coroutines.flow.MutableStateFlow
|
|||
// make all prefs actually work
|
||||
// appearance
|
||||
// click on bg image does nothing when already set (but works after reload)
|
||||
// custom font loading not implemented
|
||||
// have large bg image, and first-time load the keyboard on new search field -> bg image expands full size
|
||||
// advanced
|
||||
// preferences
|
||||
|
@ -27,6 +26,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
|
|||
// more similar dialog style args (for all dialogs, or for none)
|
||||
// check whether dialogs have the same colors, i think currently it's a bit inconsistent
|
||||
// see all the properties for each alertDialog -> any way to set it in a single place?
|
||||
// yes/no/default can now be confirmDialog
|
||||
// title too huge for bg image and text on spacebar dialogs, also maybe somewhere else -> where to set in one place?
|
||||
// check dark and light theme (don't have dynamic)
|
||||
// rename both settingsActivities
|
||||
|
|
|
@ -26,23 +26,26 @@ fun ConfirmationDialog(
|
|||
text: @Composable (() -> Unit)? = null,
|
||||
confirmButtonText: String = stringResource(android.R.string.ok),
|
||||
cancelButtonText: String = stringResource(android.R.string.cancel),
|
||||
neutralButtonText: String? = null,
|
||||
onNeutral: () -> Unit = { },
|
||||
shape: Shape = MaterialTheme.shapes.medium,
|
||||
backgroundColor: Color = MaterialTheme.colorScheme.surface,
|
||||
contentColor: Color = contentColorFor(backgroundColor),
|
||||
properties: DialogProperties = DialogProperties(),
|
||||
) {
|
||||
AlertDialog(
|
||||
ThreeButtonAlertDialog(
|
||||
onDismissRequest = onDismissRequest,
|
||||
confirmButton = {
|
||||
TextButton(onClick = { onConfirmed(); onDismissRequest() }) { Text(confirmButtonText) }
|
||||
},
|
||||
onConfirmed = onConfirmed,
|
||||
confirmButtonText = confirmButtonText,
|
||||
cancelButtonText = cancelButtonText,
|
||||
neutralButtonText = neutralButtonText,
|
||||
onNeutral = onNeutral,
|
||||
modifier = modifier,
|
||||
dismissButton = { TextButton(onClick = onDismissRequest) { Text(cancelButtonText) } },
|
||||
title = title,
|
||||
text = text,
|
||||
shape = shape,
|
||||
containerColor = backgroundColor,
|
||||
textContentColor = contentColor,
|
||||
backgroundColor = backgroundColor,
|
||||
contentColor = contentColor,
|
||||
properties = properties,
|
||||
)
|
||||
}
|
||||
|
@ -53,6 +56,7 @@ private fun PreviewConfirmDialog() {
|
|||
ConfirmationDialog(
|
||||
onDismissRequest = { },
|
||||
onConfirmed = {},
|
||||
neutralButtonText = "hi",
|
||||
confirmButtonText = "I don't care",
|
||||
text = { Text(stringResource(R.string.disable_personalized_dicts_message)) }
|
||||
)
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
package helium314.keyboard.settings.dialogs
|
||||
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
|
||||
@Composable
|
||||
fun InfoDialog(
|
||||
message: String,
|
||||
onDismissRequest: () -> Unit
|
||||
) {
|
||||
ThreeButtonAlertDialog(
|
||||
onDismissRequest = onDismissRequest,
|
||||
text = { Text(message) },
|
||||
onConfirmed = { },
|
||||
confirmButtonText = null
|
||||
)
|
||||
}
|
|
@ -23,7 +23,7 @@ fun ThreeButtonAlertDialog(
|
|||
text: @Composable (() -> Unit)? = null,
|
||||
onNeutral: () -> Unit = { },
|
||||
checkOk: () -> Boolean = { true },
|
||||
confirmButtonText: String = stringResource(android.R.string.ok),
|
||||
confirmButtonText: String? = stringResource(android.R.string.ok),
|
||||
cancelButtonText: String = stringResource(android.R.string.cancel),
|
||||
neutralButtonText: String? = null,
|
||||
shape: Shape = MaterialTheme.shapes.medium,
|
||||
|
@ -41,6 +41,7 @@ fun ThreeButtonAlertDialog(
|
|||
) { Text(neutralButtonText) }
|
||||
Spacer(modifier.weight(1f))
|
||||
TextButton(onClick = onDismissRequest) { Text(cancelButtonText) }
|
||||
if (confirmButtonText != null)
|
||||
TextButton(
|
||||
enabled = checkOk(),
|
||||
onClick = { onDismissRequest(); onConfirmed() },
|
||||
|
|
|
@ -5,6 +5,7 @@ import android.app.Activity
|
|||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.graphics.BitmapFactory
|
||||
import android.graphics.Typeface
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
|
@ -31,9 +32,11 @@ import helium314.keyboard.latin.settings.ColorsNightSettingsFragment
|
|||
import helium314.keyboard.latin.settings.ColorsSettingsFragment
|
||||
import helium314.keyboard.latin.settings.Settings
|
||||
import helium314.keyboard.latin.settings.SettingsValues
|
||||
import helium314.keyboard.latin.utils.DeviceProtectedUtils
|
||||
import helium314.keyboard.latin.utils.Log
|
||||
import helium314.keyboard.latin.utils.getActivity
|
||||
import helium314.keyboard.latin.utils.getStringResourceOrName
|
||||
import helium314.keyboard.latin.utils.infoDialog
|
||||
import helium314.keyboard.latin.utils.prefs
|
||||
import helium314.keyboard.latin.utils.switchTo
|
||||
import helium314.keyboard.settings.AllPrefs
|
||||
|
@ -47,11 +50,14 @@ import helium314.keyboard.settings.SettingsActivity2
|
|||
import helium314.keyboard.settings.SliderPreference
|
||||
import helium314.keyboard.settings.SwitchPreference
|
||||
import helium314.keyboard.settings.Theme
|
||||
import helium314.keyboard.settings.dialogs.ConfirmationDialog
|
||||
import helium314.keyboard.settings.dialogs.CustomizeIconsDialog
|
||||
import helium314.keyboard.settings.dialogs.InfoDialog
|
||||
import helium314.keyboard.settings.dialogs.TextInputDialog
|
||||
import helium314.keyboard.settings.keyboardNeedsReload
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import java.io.File
|
||||
|
||||
@Composable
|
||||
fun AppearanceScreen(
|
||||
|
@ -338,11 +344,52 @@ fun createAppearancePrefs(context: Context) = listOf(
|
|||
}
|
||||
},
|
||||
PrefDef(context, NonSettingsPrefs.CUSTOM_FONT, R.string.custom_font) { def ->
|
||||
val ctx = LocalContext.current
|
||||
var showDialog by remember { mutableStateOf(false) }
|
||||
var showErrorDialog by remember { mutableStateOf(false) }
|
||||
val fontFile = Settings.getCustomFontFile(ctx)
|
||||
val launcher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) {
|
||||
if (it.resultCode != Activity.RESULT_OK) return@rememberLauncherForActivityResult
|
||||
val uri = it.data?.data ?: return@rememberLauncherForActivityResult
|
||||
val tempFile = File(DeviceProtectedUtils.getFilesDir(context), "temp_file")
|
||||
FileUtils.copyContentUriToNewFile(uri, ctx, tempFile)
|
||||
try {
|
||||
val typeface = Typeface.createFromFile(tempFile)
|
||||
fontFile.delete()
|
||||
tempFile.renameTo(fontFile)
|
||||
Settings.clearCachedTypeface()
|
||||
keyboardNeedsReload = true
|
||||
} catch (_: Exception) {
|
||||
showErrorDialog = true
|
||||
tempFile.delete()
|
||||
}
|
||||
}
|
||||
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
|
||||
.addCategory(Intent.CATEGORY_OPENABLE)
|
||||
.setType("*/*")
|
||||
Preference(
|
||||
name = def.title,
|
||||
onClick = { showDialog = true },
|
||||
) // todo: create and show the dialog
|
||||
onClick = {
|
||||
if (fontFile.exists())
|
||||
showDialog = true
|
||||
else launcher.launch(intent)
|
||||
},
|
||||
)
|
||||
if (showDialog)
|
||||
ConfirmationDialog(
|
||||
onDismissRequest = { showDialog = false },
|
||||
onConfirmed = { launcher.launch(intent) },
|
||||
onNeutral = {
|
||||
fontFile.delete()
|
||||
Settings.clearCachedTypeface()
|
||||
keyboardNeedsReload = true
|
||||
},
|
||||
neutralButtonText = stringResource(R.string.delete),
|
||||
confirmButtonText = stringResource(R.string.load),
|
||||
title = { Text(stringResource(R.string.custom_font)) }
|
||||
)
|
||||
if (showErrorDialog)
|
||||
InfoDialog(stringResource(R.string.file_read_error)) { showErrorDialog = false }
|
||||
},
|
||||
)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue