diff --git a/app/src/main/java/cc/chenhe/qqnotifyevo/preference/AdvancedFr.kt b/app/src/main/java/cc/chenhe/qqnotifyevo/preference/AdvancedFr.kt index b9b6a26..f2964a1 100644 --- a/app/src/main/java/cc/chenhe/qqnotifyevo/preference/AdvancedFr.kt +++ b/app/src/main/java/cc/chenhe/qqnotifyevo/preference/AdvancedFr.kt @@ -29,34 +29,36 @@ class AdvancedFr : PreferenceFragmentCompat() { val newWrapper: String = new as? String ?: "" if (newWrapper.indexOf("\$n") == -1) { AlertDialog.Builder(requireContext()) - .setTitle(R.string.tip) - .setMessage(R.string.pref_advanced_nickname_wrapper_invalid_message) - .setPositiveButton(R.string.confirm, null) - .show() + .setTitle(R.string.tip) + .setMessage(R.string.pref_advanced_nickname_wrapper_invalid_message) + .setPositiveButton(R.string.confirm, null) + .show() false } else { true } } } - findPreference("avatar_cache_period")!!.summaryProvider = AvatarCachePeriodSummaryProvider() + findPreference("avatar_cache_period")!!.summaryProvider = + AvatarCachePeriodSummaryProvider() findPreference("log")!!.setOnPreferenceChangeListener { pref, new -> if (new as Boolean) { AlertDialog.Builder(requireContext()) - .setTitle(R.string.tip) - .setMessage(R.string.pref_log_dialog_message) - .setCancelable(false) - .setPositiveButton(R.string.confirm) { _, _ -> - (pref as SwitchPreferenceCompat).isChecked = true - } - .setNegativeButton(R.string.cancel, null) - .show() + .setTitle(R.string.tip) + .setMessage(R.string.pref_log_dialog_message) + .setCancelable(false) + .setPositiveButton(R.string.confirm) { _, _ -> + (pref as SwitchPreferenceCompat).isChecked = true + } + .setNegativeButton(R.string.cancel, null) + .show() false } else { true } } - findPreference("show_in_recent")!!.summaryProvider = ShowInRecentSummaryProvider() + findPreference("show_in_recent")!!.summaryProvider = + ShowInRecentSummaryProvider() deleteLog = findPreference("delete_log")!! refreshLogSize() } @@ -67,21 +69,25 @@ class AdvancedFr : PreferenceFragmentCompat() { nevoMultiMsgTip(requireContext(), true) Toast.makeText(requireContext(), R.string.done, Toast.LENGTH_SHORT).show() if (NotificationManagerCompat.from(requireContext()) - .getNotificationChannel(NOTIFY_SELF_TIPS_CHANNEL_ID)?.importance == - NotificationManagerCompat.IMPORTANCE_NONE) { + .getNotificationChannel(NOTIFY_SELF_TIPS_CHANNEL_ID)?.importance == + NotificationManagerCompat.IMPORTANCE_NONE + ) { AlertDialog.Builder(requireContext()) - .setTitle(R.string.tip) - .setMessage(R.string.pref_reset_tips_notify_dialog) - .setPositiveButton(R.string.confirm) { _, _ -> - openTipsNotificationSetting() - } - .setNegativeButton(R.string.cancel, null) - .show() + .setTitle(R.string.tip) + .setMessage(R.string.pref_reset_tips_notify_dialog) + .setPositiveButton(R.string.confirm) { _, _ -> + openTipsNotificationSetting() + } + .setNegativeButton(R.string.cancel, null) + .show() } } "delete_avatar_cache" -> { - AvatarManager.get(getAvatarDiskCacheDir(requireContext()), getAvatarCachePeriod(requireContext())) - .clearCache() + AvatarManager.get( + getAvatarDiskCacheDir(requireContext()), + getAvatarCachePeriod(requireContext()) + ) + .clearCache() Toast.makeText(requireContext(), R.string.done, Toast.LENGTH_SHORT).show() } "delete_nevo_channel" -> { @@ -91,15 +97,15 @@ class AdvancedFr : PreferenceFragmentCompat() { } "delete_log" -> { AlertDialog.Builder(requireContext()) - .setTitle(R.string.tip) - .setMessage(R.string.pref_delete_log_dialog_message) - .setPositiveButton(R.string.confirm) { _, _ -> - (requireContext().applicationContext as MyApplication).deleteLog() - Toast.makeText(requireContext(), R.string.done, Toast.LENGTH_SHORT).show() - refreshLogSize() - } - .setNegativeButton(R.string.cancel, null) - .show() + .setTitle(R.string.tip) + .setMessage(R.string.pref_delete_log_dialog_message) + .setPositiveButton(R.string.confirm) { _, _ -> + (requireContext().applicationContext as MyApplication).deleteLog() + Toast.makeText(requireContext(), R.string.done, Toast.LENGTH_SHORT).show() + refreshLogSize() + } + .setNegativeButton(R.string.cancel, null) + .show() } } return super.onPreferenceTreeClick(preference) @@ -124,12 +130,16 @@ class AdvancedFr : PreferenceFragmentCompat() { private fun refreshLogSize() { val files = getLogDir(requireContext()).listFiles { f -> f.isFile } - val size = files?.sumByDouble { f -> f.length().toDouble() } ?: 0.0 - deleteLog.summary = getString(R.string.pref_delete_log_summary, - files?.size ?: 0, describeFileSize(size.toLong())) + val size = files?.sumOf { f -> f.length() } ?: 0 + deleteLog.summary = getString( + R.string.pref_delete_log_summary, + files?.size ?: 0, + describeFileSize(size) + ) } - private inner class AvatarCachePeriodSummaryProvider : Preference.SummaryProvider { + private inner class AvatarCachePeriodSummaryProvider : + Preference.SummaryProvider { override fun provideSummary(preference: ListPreference): CharSequence { val period: Long = preference.value.toLong() / 1000 val day = (period / (24 * 3600)).toInt() @@ -158,9 +168,11 @@ class AdvancedFr : PreferenceFragmentCompat() { } } - private inner class ShowInRecentSummaryProvider : Preference.SummaryProvider { + private inner class ShowInRecentSummaryProvider : + Preference.SummaryProvider { - private val summaries = requireContext().resources.getStringArray(R.array.pref_show_in_recent_summaries) + private val summaries = + requireContext().resources.getStringArray(R.array.pref_show_in_recent_summaries) override fun provideSummary(preference: SwitchPreferenceCompat): CharSequence { return if (preference.isChecked) summaries[0] else summaries[1] diff --git a/app/src/main/java/cc/chenhe/qqnotifyevo/preference/MainPreferenceFr.kt b/app/src/main/java/cc/chenhe/qqnotifyevo/preference/MainPreferenceFr.kt index ebd68f8..e8a187b 100644 --- a/app/src/main/java/cc/chenhe/qqnotifyevo/preference/MainPreferenceFr.kt +++ b/app/src/main/java/cc/chenhe/qqnotifyevo/preference/MainPreferenceFr.kt @@ -51,7 +51,8 @@ class MainPreferenceFr : PreferenceFragmentCompat() { findPreference("permission")?.fragment = PermissionFr::class.java.name findPreference("advanced")?.fragment = AdvancedFr::class.java.name - findPreference("version_code")?.summary = getString(R.string.pref_version_code, getVersion(ctx)) + findPreference("version_code")?.summary = + getString(R.string.pref_version_code, getVersion(ctx)) } fun setMode(@Mode mode: Int) { @@ -64,10 +65,14 @@ class MainPreferenceFr : PreferenceFragmentCompat() { lifecycleScope.launch { checkServiceRunning() } model.mode.observe(viewLifecycleOwner) { mode -> - notification.setTitle(if (mode == MODE_NEVO) - R.string.pref_notify_nevo else R.string.pref_notify_system) - notification.setSummary(if (mode == MODE_NEVO) - R.string.pref_notify_nevo_summary else R.string.pref_notify_system_summary) + notification.setTitle( + if (mode == MODE_NEVO) + R.string.pref_notify_nevo else R.string.pref_notify_system + ) + notification.setSummary( + if (mode == MODE_NEVO) + R.string.pref_notify_nevo_summary else R.string.pref_notify_system_summary + ) } model.serviceRunning.observe(viewLifecycleOwner) { serviceRunning -> @@ -94,7 +99,8 @@ class MainPreferenceFr : PreferenceFragmentCompat() { override fun onPreferenceTreeClick(preference: Preference): Boolean { when (preference.key) { "system_notify_setting" -> { - val pkgName = if (model.mode.value == MODE_NEVO) "com.oasisfeng.nevo" else requireContext().packageName + val pkgName = + if (model.mode.value == MODE_NEVO) "com.oasisfeng.nevo" else requireContext().packageName Intent().let { it.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS it.putExtra(Settings.EXTRA_APP_PACKAGE, pkgName) @@ -126,10 +132,10 @@ class MainPreferenceFr : PreferenceFragmentCompat() { } } catch (e: Exception) { AlertDialog.Builder(requireContext()) - .setTitle(R.string.tip) - .setMessage(R.string.main_nevo_not_install) - .setPositiveButton(R.string.confirm, null) - .show() + .setTitle(R.string.tip) + .setMessage(R.string.main_nevo_not_install) + .setPositiveButton(R.string.confirm, null) + .show() } } @@ -146,22 +152,22 @@ class MainPreferenceFr : PreferenceFragmentCompat() { private fun donate() { AlertDialog.Builder(requireContext()) - .setTitle(R.string.pref_donate_message) - .setSingleChoiceItems(R.array.pref_donate_options, -1) { _, i -> - startAliPay() - } - .show() + .setTitle(R.string.pref_donate_message) + .setSingleChoiceItems(R.array.pref_donate_options, -1) { _, _ -> + startAliPay() + } + .show() } private fun showInfo() { AlertDialog.Builder(requireContext()) - .setTitle(getString(R.string.about_dialog_title)) - .setMessage(getString(R.string.about_dialog_message)) - .setNeutralButton(R.string.about_dialog_github) { _, _ -> - openUrl(GITHUB_URL) - } - .setPositiveButton(R.string.confirm, null) - .show() + .setTitle(getString(R.string.about_dialog_title)) + .setMessage(getString(R.string.about_dialog_message)) + .setNeutralButton(R.string.about_dialog_github) { _, _ -> + openUrl(GITHUB_URL) + } + .setPositiveButton(R.string.confirm, null) + .show() } private fun startAliPay() { @@ -170,7 +176,8 @@ class MainPreferenceFr : PreferenceFragmentCompat() { val intent = Intent(Intent.ACTION_VIEW, uri) startActivity(intent) } catch (e: java.lang.Exception) { - Toast.makeText(requireContext(), R.string.pref_donate_alipay_error, Toast.LENGTH_SHORT).show() + Toast.makeText(requireContext(), R.string.pref_donate_alipay_error, Toast.LENGTH_SHORT) + .show() } }