Skip to content

Commit

Permalink
fix(ui): "Sync in background" preference never show the time and stat…
Browse files Browse the repository at this point in the history
…us of last sync

- refactor preference keys
- polish string values and their translations
  • Loading branch information
WhiredPlanck committed Aug 6, 2022
1 parent fa4dbbb commit c6db387
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 84 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/com/osfans/trime/data/AppPrefs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ class AppPrefs(
var lastSyncStatus: Boolean
get() = prefs.getPref(LAST_SYNC_STATUS, false)
set(v) = prefs.setPref(LAST_SYNC_STATUS, v)
var lastBackgroundSync: Long
get() = prefs.getPref(LAST_BACKGROUND_SYNC, 0L)
var lastBackgroundSync: String
get() = prefs.getPref(LAST_BACKGROUND_SYNC, "")
set(v) = prefs.setPref(LAST_BACKGROUND_SYNC, v)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class ResetAssetsDialog(private val context: Context) :
/** 彈出對話框 */
fun show() {
AlertDialog.Builder(context)
.setTitle(R.string.conf__reset_title)
.setTitle(R.string.data_reset)
.setNegativeButton(android.R.string.cancel, null)
.setMultiChoiceItems(
assets, checkedList
Expand Down
58 changes: 21 additions & 37 deletions app/src/main/java/com/osfans/trime/ui/fragments/UserDataFragment.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.osfans.trime.ui.fragments

import android.content.Context
import android.os.Bundle
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
Expand All @@ -15,8 +14,8 @@ import com.osfans.trime.ui.components.ResetAssetsDialog
import com.osfans.trime.ui.main.MainViewModel
import com.osfans.trime.util.formatDateTime
import com.osfans.trime.util.withLoadingDialog
import kotlinx.coroutines.withContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class UserDataFragment : PreferenceFragmentCompat() {

Expand All @@ -26,7 +25,7 @@ class UserDataFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.user_data_preference)
with(preferenceScreen) {
get<Preference>("data_synchronize")?.setOnPreferenceClickListener {
get<Preference>("data_sync_user_data")?.setOnPreferenceClickListener {
lifecycleScope.withLoadingDialog(context, 200L, R.string.sync_progress) {
withContext(Dispatchers.IO) {
Rime.sync_user_data()
Expand All @@ -36,9 +35,25 @@ class UserDataFragment : PreferenceFragmentCompat() {
}
true
}
get<Preference>("data_synchronize_background")?.setOnPreferenceClickListener {
setBackgroundSyncSummary(context)
true
get<SwitchPreferenceCompat>("data_sync_in_background")?.apply {
val lastBackgroundSync = prefs.userData.lastBackgroundSync
summaryOn =
if (lastBackgroundSync.isBlank()) {
context.getString(R.string.data_never_sync_in_background)
} else {
context.getString(
R.string.data_last_sync_in_background,
formatDateTime(lastBackgroundSync.toLong()),
context.getString(
if (prefs.userData.lastSyncStatus) {
R.string.success
} else {
R.string.failure
}
)
)
}
summaryOff = context.getString(R.string.data_enable_syncing_in_background)
}
get<Preference>("data_reset")?.setOnPreferenceClickListener {
ResetAssetsDialog(context).show()
Expand All @@ -51,36 +66,5 @@ class UserDataFragment : PreferenceFragmentCompat() {
super.onResume()
viewModel.setToolbarTitle(getString(R.string.pref_user_data))
viewModel.disableTopOptionsMenu()
setBackgroundSyncSummary(context)
}

private fun setBackgroundSyncSummary(context: Context?) {
val syncBgPref = findPreference<SwitchPreferenceCompat>("pref_sync_bg")
if (context == null) {
if (syncBgPref?.isChecked == true) {
syncBgPref.setSummaryOn(R.string.pref_sync_bg_never)
} else {
syncBgPref?.setSummaryOff(R.string.conf__synchronize_background_summary)
}
} else {
var summary: String
if (syncBgPref?.isChecked == true) {
val lastResult = prefs.userData.lastSyncStatus
val lastTime = prefs.userData.lastBackgroundSync
summary = if (lastResult) {
context.getString(R.string.pref_sync_bg_success)
} else {
context.getString(R.string.pref_sync_bg_failure)
}
summary = if (lastTime == 0L) {
context.getString(R.string.conf__synchronize_background_summary)
} else {
summary.format(formatDateTime(lastTime))
}
syncBgPref.summaryOn = summary
} else {
syncBgPref?.setSummaryOff(R.string.conf__synchronize_background_summary)
}
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/osfans/trime/util/ShortcutUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ object ShortcutUtils {

fun syncInBackground(context: Context) {
val prefs = AppPrefs.defaultInstance()
prefs.userData.lastBackgroundSync = Date().time
prefs.userData.lastBackgroundSync = Date().time.toString()
prefs.userData.lastSyncStatus = Rime.syncUserData(context)
}

Expand Down
26 changes: 15 additions & 11 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@
<string name="pref_schemas_summary">从已安装的方案中选择方案</string>
<string name="looks__selected_color_title">配色</string>
<string name="looks__selected_color_summary">选取键盘配色</string>
<string name="conf__reset_title">恢复默认设置</string>
<string name="conf__reset_summary">使用默认设置覆盖共享文件夹下同名文件</string>
<string name="settings__keyboard_preferences_title">偏好设置</string>
<string name="keyboard__key_sound_title">按键声音</string>
<string name="keyboard__key_sound_volume_title">按键音量</string>
Expand Down Expand Up @@ -83,8 +81,8 @@
<string name="keyboard__soft_cursor_title">悬浮窗编码区使用插入符号(^)</string>
<string name="pref_deploy">部署</string>
<string name="pref_deploy_summary">修改设置后需要再次部署同文输入法平台</string>
<string name="conf__synchronize_title">同步</string>
<string name="conf__synchronize_summary">同步用户字典至sync文件夹</string>
<string name="data_sync_user_data">同步</string>
<string name="data_sync_user_data_summary">同步用户文件夹下的文件至 sync 文件夹</string>
<string name="reset_success">重置成功</string>
<string name="reset_failure">重置失败</string>
<string name="deploy_progress">正在部署…</string>
Expand All @@ -100,13 +98,10 @@
<string name="other__destroy_on_quit_title">退出时清除缓存</string>
<string name="pref_themes_name_tongwenfeng">同文风</string>
<string name="pref_themes_name_trime">默认</string>
<string name="conf__shared_data_dir_title">共享文件夹</string>
<string name="conf__user_data_dir_title">用户文件夹</string>
<string name="conf__synchronize_background_title">后台同步</string>
<string name="pref_sync_bg_success">上次同步于 %s,成功</string>
<string name="pref_sync_bg_failure">上次同步于 %s,失败</string>
<string name="pref_sync_bg_never">后台同步未运行</string>
<string name="conf__synchronize_background_summary">点击以开启后台同步</string>
<string name="data_shared_data_dir">共享文件夹</string>
<string name="data_user_data_dir">用户文件夹</string>
<string name="data_sync_in_background">后台同步</string>
<string name="data_enable_syncing_in_background">点击以开启后台同步</string>
<string name="pref_help__marketplace">应用市场</string>
<string name="pref_help__user_community">用户社区</string>
<string name="pref_keyboard__effect">按键效果</string>
Expand Down Expand Up @@ -245,4 +240,13 @@
<string name="pref_theme_and_color">主题与配色</string>
<string name="pref_user_data">用户数据</string>
<string name="pref_user_data_summary">用于管理您的数据的设置</string>
<string name="data_never_sync_in_background">从未在后台同步过</string>
<string name="storage">存储</string>
<string name="maintenance">维护</string>
<string name="loading">正在加载</string>
<string name="data_reset">恢复默认设置</string>
<string name="data_reset_summary">用默认的内置配置覆盖共享目录中的相同文件</string>
<string name="data_last_sync_in_background">上次后台同步时间:%s\n状态:%s</string>
<string name="success">成功</string>
<string name="failure">失败</string>
</resources>
26 changes: 15 additions & 11 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
<string name="pref_schemas_summary">從已安裝的方案中選擇要使用的方案</string>
<string name="looks__selected_color_title">配色</string>
<string name="looks__selected_color_summary">選取想要的鍵盤配色</string>
<string name="conf__reset_title">回廠</string>
<string name="conf__reset_summary">使用預設設定覆蓋共享資料夾下同名檔案</string>
<string name="data_reset">回廠</string>
<string name="data_reset_summary">使用預設的內建設置覆蓋共享資料夾下相同檔案</string>
<string name="settings__keyboard_preferences_title">偏好設定</string>
<string name="keyboard__key_sound_title">按鍵聲音</string>
<string name="keyboard__key_sound_volume_title">按鍵音量</string>
Expand Down Expand Up @@ -84,8 +84,8 @@
<string name="keyboard__soft_cursor_title">懸浮窗編碼區使用插入符號(^)</string>
<string name="pref_deploy">部署</string>
<string name="pref_deploy_summary">修改設定後需要再次部署同文輸入法平臺</string>
<string name="conf__synchronize_title">同步</string>
<string name="conf__synchronize_summary">同步使用者字典至sync資料夾</string>
<string name="data_sync_user_data">同步</string>
<string name="data_sync_user_data_summary">同步使用者資料夾下的檔案至 sync 資料夾</string>
<string name="reset_success">回廠成功</string>
<string name="reset_failure">回廠失敗</string>
<string name="deploy_progress">正在部署…</string>
Expand All @@ -101,13 +101,10 @@
<string name="other__destroy_on_quit_title">離開時清理記憶體</string>
<string name="pref_themes_name_tongwenfeng">同文風</string>
<string name="pref_themes_name_trime">默認</string>
<string name="conf__shared_data_dir_title">共享資料夾</string>
<string name="conf__user_data_dir_title">使用者資料夾</string>
<string name="conf__synchronize_background_title">後台同步</string>
<string name="pref_sync_bg_success">上次同步於 %s,成功</string>
<string name="pref_sync_bg_failure">上次同步於 %s,失敗</string>
<string name="pref_sync_bg_never">後台同步未運行</string>
<string name="conf__synchronize_background_summary">點擊以啟用後台同步</string>
<string name="data_shared_data_dir">共享資料夾</string>
<string name="data_user_data_dir">使用者資料夾</string>
<string name="data_never_sync_in_background">從未在後臺同步過</string>
<string name="data_enable_syncing_in_background">點擊以啟用後台同步</string>
<string name="pref_help__marketplace">應用市場</string>
<string name="pref_help__user_community">使用者社群</string>
<string name="pref_keyboard__function">檢視</string>
Expand Down Expand Up @@ -246,4 +243,11 @@
<string name="pref_user_data">使用者資料</string>
<string name="pref_user_data_summary">用於管理你的資料的設定</string>
<string name="pref_keyboard">鍵盤設定</string>
<string name="storage">儲存</string>
<string name="maintenance">維護</string>
<string name="loading">正在載入</string>
<string name="data_sync_in_background">後臺同步</string>
<string name="data_last_sync_in_background">上次後臺同步時間:%s\n狀態:%s</string>
<string name="success">成功</string>
<string name="failure">失敗</string>
</resources>
23 changes: 12 additions & 11 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
<string name="pref_schemas_summary">Select Installed Schemas</string>
<string name="looks__selected_color_title">Colors</string>
<string name="looks__selected_color_summary">Select Desired Color for Keyboard</string>
<string name="conf__reset_title">Reset</string>
<string name="conf__reset_summary">Use Default Settings to Replace the contents in shared directory</string>
<string name="data_reset">Reset</string>
<string name="data_reset_summary">Overwrite the same files in shared directory with default built-in assets</string>
<string name="settings__keyboard_preferences_title">Preferences</string>
<string name="keyboard__key_sound_title">Sound on keypress</string>
<string name="keyboard__key_sound_volume_title">Sound volume on key press</string>
Expand Down Expand Up @@ -86,8 +86,8 @@
<string name="keyboard__soft_cursor_title">Use soft cursor(^) in composition</string>
<string name="pref_deploy">Deploy</string>
<string name="pref_deploy_summary">Gotta Deploy after Modifying Settings </string>
<string name="conf__synchronize_title">Synchronize</string>
<string name="conf__synchronize_summary">Synchronize User Dictionary to sync directory</string>
<string name="data_sync_user_data">Synchronize</string>
<string name="data_sync_user_data_summary">Synchronize files in user dictionary to sync directory</string>
<string name="reset_success">Reset Succeeded!</string>
<string name="reset_failure">Reset Failed!</string>
<string name="deploy_progress">Deploying…</string>
Expand All @@ -101,13 +101,12 @@
<string name="pref_other">Others</string>
<string name="other__show_status_bar_icon_title">Show icon in notification bar</string>
<string name="other__destroy_on_quit_title">Free memory on quit</string>
<string name="conf__shared_data_dir_title">Shared directory</string>
<string name="conf__user_data_dir_title">User directory</string>
<string name="conf__synchronize_background_title">Synchronize background</string>
<string name="pref_sync_bg_success">Last background synchronized successfully at %s</string>
<string name="pref_sync_bg_failure">Last background synchronized failed at %s</string>
<string name="pref_sync_bg_never">Never synchronized background</string>
<string name="conf__synchronize_background_summary">Click to enable</string>
<string name="data_shared_data_dir">Shared directory</string>
<string name="data_user_data_dir">User directory</string>
<string name="data_sync_in_background">Sync in background</string>
<string name="data_last_sync_in_background">Last sync in background: %s\nStatus: %s</string>
<string name="data_never_sync_in_background">Never synced in the background</string>
<string name="data_enable_syncing_in_background">Click to enable</string>
<string name="pref_help__marketplace">Marketplace</string>
<string name="pref_help__user_community">User Community</string>
<string name="pref_keyboard__function">View</string>
Expand Down Expand Up @@ -252,4 +251,6 @@
<string name="storage">Storage</string>
<string name="maintenance">Maintenance</string>
<string name="loading">Loading</string>
<string name="success">success</string>
<string name="failure">failure</string>
</resources>
19 changes: 9 additions & 10 deletions app/src/main/res/xml/user_data_preference.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,30 @@
app:iconSpaceReserved="false" >
<EditTextPreference android:key="data_shared_data_dir"
app:iconSpaceReserved="false"
android:title="@string/conf__shared_data_dir_title"
android:title="@string/data_shared_data_dir"
app:useSimpleSummaryProvider="true"/>

<EditTextPreference android:key="data_user_data_dir"
app:iconSpaceReserved="false"
android:title="@string/conf__user_data_dir_title"
android:title="@string/data_user_data_dir"
app:useSimpleSummaryProvider="true"/>
</PreferenceCategory>

<PreferenceCategory android:title="@string/maintenance"
app:iconSpaceReserved="false" >
<Preference android:key="data_synchronize"
<Preference android:key="data_sync_user_data"
app:iconSpaceReserved="false"
android:title="@string/conf__synchronize_title"
android:summary="@string/conf__synchronize_summary"/>
android:title="@string/data_sync_user_data"
android:summary="@string/data_sync_user_data_summary"/>

<SwitchPreferenceCompat android:key="data_synchronize_background"
<SwitchPreferenceCompat android:key="data_sync_in_background"
app:iconSpaceReserved="false"
android:title="@string/conf__synchronize_background_title"
android:summary="@string/conf__synchronize_background_summary"
android:title="@string/data_sync_in_background"
android:persistent="true" />

<Preference android:key="data_reset"
app:iconSpaceReserved="false"
android:title="@string/conf__reset_title"
android:summary="@string/conf__reset_summary"/>
android:title="@string/data_reset"
android:summary="@string/data_reset_summary"/>
</PreferenceCategory>
</PreferenceScreen>

0 comments on commit c6db387

Please sign in to comment.