From 6d9763d079388ce4efd7912d01f917e964f90824 Mon Sep 17 00:00:00 2001 From: Helium314 Date: Tue, 11 Feb 2025 22:43:29 +0100 Subject: [PATCH] add save color options to new colors screen --- .../latin/settings/ColorsSettingsFragment.kt | 3 -- .../keyboard/latin/utils/CustomLayoutUtils.kt | 4 +- .../keyboard/settings/screens/ColorsScreen.kt | 40 +++++++++++++++++++ 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/helium314/keyboard/latin/settings/ColorsSettingsFragment.kt b/app/src/main/java/helium314/keyboard/latin/settings/ColorsSettingsFragment.kt index 689d6809f..5397b2140 100644 --- a/app/src/main/java/helium314/keyboard/latin/settings/ColorsSettingsFragment.kt +++ b/app/src/main/java/helium314/keyboard/latin/settings/ColorsSettingsFragment.kt @@ -439,9 +439,6 @@ class ColorsNightSettingsFragment : ColorsSettingsFragment() { // override val titleResId = R.string.select_user_colors_night } -@Serializable -data class SaveThoseColors(val moreColors: Int, val colors: Map>) - val colorPrefsAndResIds = listOf( KeyboardTheme.COLOR_BACKGROUND to R.string.select_color_background, KeyboardTheme.COLOR_KEYS to R.string.select_color_key_background, diff --git a/app/src/main/java/helium314/keyboard/latin/utils/CustomLayoutUtils.kt b/app/src/main/java/helium314/keyboard/latin/utils/CustomLayoutUtils.kt index 35b48c18c..0e82de36f 100644 --- a/app/src/main/java/helium314/keyboard/latin/utils/CustomLayoutUtils.kt +++ b/app/src/main/java/helium314/keyboard/latin/utils/CustomLayoutUtils.kt @@ -252,9 +252,9 @@ private fun findMatchingLayout(layoutNames: List, mainLayoutName: String ?: layoutNames.firstOrNull { it.count { it == '.' } == 2 } } -private fun encodeBase36(string: String): String = BigInteger(string.toByteArray()).toString(36) +fun encodeBase36(string: String): String = BigInteger(string.toByteArray()).toString(36) -private fun decodeBase36(string: String) = BigInteger(string, 36).toByteArray().decodeToString() +fun decodeBase36(string: String) = BigInteger(string, 36).toByteArray().decodeToString() // this goes into prefs and file names, so do not change! const val CUSTOM_LAYOUT_PREFIX = "custom." diff --git a/app/src/main/java/helium314/keyboard/settings/screens/ColorsScreen.kt b/app/src/main/java/helium314/keyboard/settings/screens/ColorsScreen.kt index 617683169..07f6c0422 100644 --- a/app/src/main/java/helium314/keyboard/settings/screens/ColorsScreen.kt +++ b/app/src/main/java/helium314/keyboard/settings/screens/ColorsScreen.kt @@ -1,6 +1,14 @@ // SPDX-License-Identifier: GPL-3.0-only package helium314.keyboard.settings.screens +import android.app.Activity +import android.content.ClipData +import android.content.ClipboardManager +import android.content.Context +import android.content.Intent +import android.content.SharedPreferences +import androidx.activity.compose.rememberLauncherForActivityResult +import androidx.activity.result.contract.ActivityResultContracts import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Column @@ -50,12 +58,15 @@ import helium314.keyboard.latin.settings.colorPrefsAndResIds import helium314.keyboard.latin.settings.getColorPrefsToHideInitially import helium314.keyboard.latin.utils.Log import helium314.keyboard.latin.utils.ResourceUtils +import helium314.keyboard.latin.utils.encodeBase36 import helium314.keyboard.latin.utils.getActivity import helium314.keyboard.latin.utils.prefs import helium314.keyboard.settings.SearchScreen import helium314.keyboard.settings.SettingsActivity import helium314.keyboard.settings.Theme import helium314.keyboard.settings.dialogs.ColorPickerDialog +import kotlinx.serialization.Serializable +import kotlinx.serialization.json.Json @Composable fun ColorsScreen( @@ -114,6 +125,11 @@ fun ColorsScreen( var newThemeName by rememberSaveable(stateSaver = TextFieldValue.Saver) { mutableStateOf(TextFieldValue(themeName)) } var chosenColor: ColorSetting? by remember { mutableStateOf(null) } + val saveLauncher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { + if (it.resultCode != Activity.RESULT_OK) return@rememberLauncherForActivityResult + val uri = it.data?.data ?: return@rememberLauncherForActivityResult + ctx.getActivity()?.contentResolver?.openOutputStream(uri)?.writer()?.use { it.write(getColorString(prefs, newThemeName.text)) } + } SearchScreen( title = { var nameValid by rememberSaveable { mutableStateOf(true) } @@ -144,6 +160,17 @@ fun ColorsScreen( stringResource(R.string.main_colors) to { KeyboardTheme.writeUserMoreColors(prefs, newThemeName.text, 0) }, stringResource(R.string.more_colors) to { KeyboardTheme.writeUserMoreColors(prefs, newThemeName.text, 1) }, stringResource(R.string.all_colors) to { KeyboardTheme.writeUserMoreColors(prefs, newThemeName.text, 2) }, + stringResource(R.string.save) to { + val intent = Intent(Intent.ACTION_CREATE_DOCUMENT) + .addCategory(Intent.CATEGORY_OPENABLE) + .putExtra(Intent.EXTRA_TITLE,"theme.json") + .setType("application/json") + saveLauncher.launch(intent) + }, + stringResource(R.string.copy_to_clipboard) to { + val cm = ctx.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager + cm.setPrimaryClip(ClipData.newPlainText("HeliBoard theme", getColorString(prefs, newThemeName.text))) + }, ), onClickBack = onClickBack, filteredItems = { search -> shownColors.filter { @@ -204,6 +231,19 @@ fun ColorsScreen( } } +private fun getColorString(prefs: SharedPreferences, themeName: String): String { + val moreColors = KeyboardTheme.readUserMoreColors(prefs, themeName) + if (moreColors == 2) { + val colors = KeyboardTheme.readUserAllColors(prefs, themeName).map { it.key.name to it.value } + return Json.encodeToString((colors + (encodeBase36(themeName) to 0)).toMap()) // put theme name in here too + } + val colors = KeyboardTheme.readUserColors(prefs, themeName).associate { it.name to (it.color to (it.auto == true)) } + return Json.encodeToString(SaveThoseColors(themeName, moreColors, colors)) +} + +@Serializable +data class SaveThoseColors(val name: String? = null, val moreColors: Int, val colors: Map>) + @Preview @Composable private fun Preview() {