Skip to content

Commit

Permalink
fix: copy theme files to correct directory
Browse files Browse the repository at this point in the history
List and show themes with user/share directory.  Sort themes with default theme at the top and "share" themes
at the bottom.  Copy themes to the correct directory after selected.
  • Loading branch information
goofyz committed Jun 7, 2024
1 parent b6da8b6 commit 87870d2
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 22 deletions.
58 changes: 48 additions & 10 deletions app/src/main/java/com/osfans/trime/data/theme/ThemeManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ object ThemeManager {
fun onThemeChange(theme: Theme)
}

private const val SHARE_TYPE = "SHARE"
private const val USER_TYPE = "USER"
const val DEFAULT_THEME = "trime"
const val TONGWENFENG_THEME = "tongwenfeng"

/**
* Update sharedThemes and userThemes.
*/
Expand All @@ -32,27 +37,56 @@ object ThemeManager {
private fun listThemes(path: File): MutableList<String> {
return path.listFiles { _, name -> name.endsWith("trime.yaml") }
?.mapNotNull { f ->
if (f.name == "trime.yaml") "trime" else f.name.substringBeforeLast(".trime.yaml")
getThemeName(f)
}
?.toMutableList() ?: mutableListOf()
}

private val sharedThemes: MutableList<String> get() = listThemes(DataManager.sharedDataDir)
private fun getThemeName(f: File): String {
val name = f.name.substringBeforeLast(".trime.yaml")
return if (name == "trime.yaml") {
"trime"
} else {
name
}
}

private fun getSharedThemes(): List<String> = listThemes(DataManager.sharedDataDir).sorted()

private val userThemes: MutableList<String> get() = listThemes(DataManager.userDataDir)
private fun getUserThemes(): List<String> = listThemes(DataManager.userDataDir).sorted()

fun getAllThemes(): List<String> {
fun getAllThemes(): List<Pair<String, String>> {
val userThemes = getUserThemes()
if (DataManager.sharedDataDir.absolutePath == DataManager.userDataDir.absolutePath) {
return userThemes
return userThemes.map { Pair(it, USER_TYPE) }
}

// if same theme exists in both user & share dir, user dir will be used
val allThemes = userThemes.map { Pair(it, USER_TYPE) }.toMutableList()
getSharedThemes().forEach {
if (!userThemes.contains(it)) {
allThemes.add(Pair(it, SHARE_TYPE))
}
}

return allThemes.also {
moveThemeToFirst(it, TONGWENFENG_THEME)
moveThemeToFirst(it, DEFAULT_THEME)
}
}

private fun moveThemeToFirst(
themes: MutableList<Pair<String, String>>,
themeName: String,
) {
val defaultThemeIdx = themes.indexOfFirst { it.first == themeName }
if (defaultThemeIdx > 0) {
val pair = themes.removeAt(defaultThemeIdx)
themes.add(0, pair)
}
return sharedThemes + userThemes
}

private fun refreshThemes() {
sharedThemes.clear()
userThemes.clear()
sharedThemes.addAll(listThemes(DataManager.sharedDataDir))
userThemes.addAll(listThemes(DataManager.userDataDir))
}

// 在初始化 ColorManager 时会被赋值
Expand All @@ -76,4 +110,8 @@ object ThemeManager {
TabManager.refresh()
}
}

fun isUserTheme(theme: Pair<String, String>): Boolean {
return theme.second == USER_TYPE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@ object ThemePickerDialog {
}
val allNames =
all.map {
when (it) {
"trime" -> context.getString(R.string.theme_trime)
"tongwenfeng" -> context.getString(R.string.theme_tongwenfeng)
else -> it
when (val themeName = it.first) {
ThemeManager.DEFAULT_THEME -> context.getString(R.string.theme_trime)
ThemeManager.TONGWENFENG_THEME -> context.getString(R.string.theme_tongwenfeng)
else ->
if (ThemeManager.isUserTheme(it)) {
themeName
} else {
"[${context.getString(R.string.share)}] $themeName"
}
}
}
val current =
AppPrefs.defaultInstance().theme.selectedTheme
val currentIndex = all.indexOfFirst { it == current }.coerceAtLeast(0)
val current = AppPrefs.defaultInstance().theme.selectedTheme
val currentIndex = all.indexOfFirst { it.first == current }.coerceAtLeast(0)
return AlertDialog.Builder(context).apply {
setTitle(R.string.looks__selected_theme_title)
if (allNames.isEmpty()) {
Expand All @@ -48,7 +52,7 @@ object ThemePickerDialog {
dialog.dismiss()
withContext(Dispatchers.IO) {
copyThemeFile(context, all[which])
ThemeManager.setNormalTheme(all[which])
ThemeManager.setNormalTheme(all[which].first)
}
}
}
Expand All @@ -59,14 +63,35 @@ object ThemePickerDialog {

private suspend fun copyThemeFile(
context: Context,
selectedName: String,
selectedTheme: Pair<String, String>,
) {
val fileNameWithoutExt = if (selectedName == "trime") selectedName else "$selectedName.trime"
val themeName = selectedTheme.first
val fileNameWithoutExt =
if (themeName == "trime") {
"trime"
} else {
"$themeName.trime"
}

val profile = AppPrefs.defaultInstance().profile
val uri =
if (ThemeManager.isUserTheme(selectedTheme)) {
profile.userDataDirUri
} else {
profile.sharedDataDirUri
}

val targetPath =
if (ThemeManager.isUserTheme(selectedTheme)) {
profile.getAppUserDir()
} else {
profile.getAppShareDir()
}

val sync = FolderSync(context, AppPrefs.defaultInstance().profile.userDataDirUri)
val sync = FolderSync(context, uri)
sync.copyFiles(
arrayOf("$fileNameWithoutExt.yaml", "$fileNameWithoutExt.custom.yaml"),
AppPrefs.Profile.getAppPath(),
targetPath,
)
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,5 @@ SPDX-License-Identifier: GPL-3.0-or-later
<string name="theme_trime">默认</string>
<string name="theme_tongwenfeng">同文风</string>
<string name="pref__clear">清除</string>
<string name="share">共享</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,5 @@ SPDX-License-Identifier: GPL-3.0-or-later
<string name="theme_trime">預設</string>
<string name="theme_tongwenfeng">同文風</string>
<string name="pref__clear">清除</string>
<string name="share">共享</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,5 @@ SPDX-License-Identifier: GPL-3.0-or-later
<string name="theme_trime">default</string>
<string name="theme_tongwenfeng">tongwenfeng</string>
<string name="pref__clear">Clear</string>
<string name="share">Share</string>
</resources>

0 comments on commit 87870d2

Please sign in to comment.