Skip to content

Commit 90cf96e

Browse files
committed
Fix theme import
- write theme json file actually - allow overwriting same background image files
1 parent dd92ec8 commit 90cf96e

File tree

3 files changed

+56
-24
lines changed

3 files changed

+56
-24
lines changed

app/src/main/java/org/fcitx/fcitx5/android/data/theme/CustomThemeSerializer.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
package org.fcitx.fcitx5.android.data.theme
66

77
import arrow.core.compose
8-
import kotlinx.serialization.json.*
8+
import kotlinx.serialization.json.JsonElement
9+
import kotlinx.serialization.json.JsonObject
10+
import kotlinx.serialization.json.JsonPrimitive
11+
import kotlinx.serialization.json.JsonTransformingSerializer
12+
import kotlinx.serialization.json.boolean
13+
import kotlinx.serialization.json.jsonObject
14+
import kotlinx.serialization.json.jsonPrimitive
915
import org.fcitx.fcitx5.android.utils.NostalgicSerializer
10-
import org.fcitx.fcitx5.android.utils.identity
11-
import org.fcitx.fcitx5.android.utils.upcast
1216

1317
object CustomThemeSerializer : JsonTransformingSerializer<Theme.Custom>(Theme.Custom.serializer()) {
1418

@@ -35,17 +39,18 @@ object CustomThemeSerializer : JsonTransformingSerializer<Theme.Custom>(Theme.Cu
3539
private fun JsonObject.removeVersion() =
3640
JsonObject(this - VERSION)
3741

42+
private val EmptyTransform: (JsonObject) -> JsonObject = { it }
3843

3944
private fun applyStrategy(oldVersion: String, obj: JsonObject) =
4045
strategies
4146
.takeWhile { it.version != oldVersion }
42-
.foldRight(JsonObject::identity.upcast()) { f, acc -> f compose acc }
47+
.foldRight(EmptyTransform) { it, acc -> it.transformation compose acc }
4348
.invoke(obj)
4449

4550
data class MigrationStrategy(
4651
val version: String,
4752
val transformation: (JsonObject) -> JsonObject
48-
) : (JsonObject) -> JsonObject by transformation
53+
)
4954

5055
private val strategies: List<MigrationStrategy> =
5156
// Add migrations here
@@ -69,8 +74,7 @@ object CustomThemeSerializer : JsonTransformingSerializer<Theme.Custom>(Theme.Cu
6974
put("genericActiveForegroundColor", getValue("accentKeyTextColor"))
7075
}
7176
})
72-
},
73-
MigrationStrategy("1.0", JsonObject::identity),
77+
}
7478
)
7579

7680
private const val VERSION = "version"

app/src/main/java/org/fcitx/fcitx5/android/data/theme/ThemeFilesManager.kt

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,29 @@ object ThemeFilesManager {
123123
)
124124
if (ThemeManager.BuiltinThemes.find { it.name == decoded.name } != null)
125125
errorRuntime(R.string.exception_theme_name_clash)
126-
val exists = ThemeManager.getTheme(decoded.name) != null
126+
val oldTheme = ThemeManager.getTheme(decoded.name) as? Theme.Custom
127+
val newCreated = oldTheme == null
127128
val newTheme = if (decoded.backgroundImage != null) {
128129
val srcFile = File(dir, decoded.backgroundImage.srcFilePath)
129-
val croppedFile = File(dir, decoded.backgroundImage.croppedFilePath)
130-
extracted.find { it.name == srcFile.name }?.copyTo(srcFile)
130+
val oldSrcFile = oldTheme?.backgroundImage?.srcFilePath?.let { File(it) }
131+
val srcFileNameMatches = oldSrcFile?.name == srcFile.name
132+
extracted.find { it.name == srcFile.name }
133+
// allow overwriting background image files when theme and file names all are same
134+
?.copyTo(srcFile, overwrite = srcFileNameMatches)
131135
?: errorRuntime(R.string.exception_theme_src_image)
132-
extracted.find { it.name == croppedFile.name }?.copyTo(croppedFile)
136+
val croppedFile = File(dir, decoded.backgroundImage.croppedFilePath)
137+
val oldCroppedFile =
138+
oldTheme?.backgroundImage?.croppedFilePath?.let { File(it) }
139+
val croppedFileNameMatches = oldCroppedFile?.name == croppedFile.name
140+
extracted.find { it.name == croppedFile.name }
141+
?.copyTo(croppedFile, overwrite = croppedFileNameMatches)
133142
?: errorRuntime(R.string.exception_theme_cropped_image)
143+
if (!srcFileNameMatches) {
144+
oldSrcFile?.delete()
145+
}
146+
if (!croppedFileNameMatches) {
147+
oldCroppedFile?.delete()
148+
}
134149
decoded.copy(
135150
backgroundImage = decoded.backgroundImage.copy(
136151
croppedFilePath = croppedFile.path,
@@ -140,7 +155,8 @@ object ThemeFilesManager {
140155
} else {
141156
decoded
142157
}
143-
Triple(!exists, newTheme, migrated)
158+
saveThemeFiles(newTheme)
159+
Triple(newCreated, newTheme, migrated)
144160
}
145161
}
146162
}

app/src/main/java/org/fcitx/fcitx5/android/ui/main/settings/theme/ThemeListAdapter.kt

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,20 @@ abstract class ThemeListAdapter : RecyclerView.Adapter<ThemeListAdapter.ViewHold
5757
}
5858
}
5959

60+
private fun prependOffset(index: Int): Int {
61+
return if (index == -1) 0 else 1
62+
}
63+
6064
fun prependTheme(it: Theme) {
6165
entries.add(0, it)
62-
activeIndex += 1
63-
lightIndex += 1
64-
darkIndex += 1
66+
activeIndex += prependOffset(activeIndex)
67+
lightIndex += prependOffset(lightIndex)
68+
darkIndex += prependOffset(darkIndex)
6569
notifyItemInserted(OFFSET)
6670
}
6771

68-
fun replaceTheme(theme: Theme) {
69-
val index = entries.indexOfFirst { it.name == theme.name }
70-
entries[index] = theme
71-
notifyItemChanged(index + OFFSET)
72-
}
73-
74-
private fun removedOffset(removedIndex: Int, offsetIndex: Int): Int {
75-
val i = removedIndex - OFFSET - offsetIndex
76-
return i.sign
72+
private fun removedOffset(removedIndex: Int, index: Int): Int {
73+
return if (index == -1) 0 else (removedIndex - OFFSET - index).sign
7774
}
7875

7976
fun removeTheme(name: String) {
@@ -85,6 +82,21 @@ abstract class ThemeListAdapter : RecyclerView.Adapter<ThemeListAdapter.ViewHold
8582
darkIndex += removedOffset(index, darkIndex)
8683
}
8784

85+
private fun replaceIndex(replacedIndex: Int, index: Int): Int {
86+
return if (replacedIndex + OFFSET == index) OFFSET else index
87+
}
88+
89+
fun replaceTheme(theme: Theme) {
90+
val index = entries.indexOfFirst { it.name == theme.name }
91+
entries.removeAt(index)
92+
entries.add(0, theme)
93+
activeIndex = replaceIndex(index, activeIndex)
94+
lightIndex = replaceIndex(index, lightIndex)
95+
darkIndex = replaceIndex(index, darkIndex)
96+
notifyItemMoved(index + OFFSET, OFFSET)
97+
notifyItemChanged(OFFSET)
98+
}
99+
88100
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
89101
ViewHolder(
90102
when (viewType) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy