commit 0d8bf10049f0f19e6c5b96f710b5b85aeffed317
parent c8bfd14c3f9581b2098f620592b2498d3a5c21d0
Author: rhunk <101876869+rhunk@users.noreply.github.com>
Date:   Fri, 23 Jun 2023 17:53:33 +0200

refactor: debug settings

Diffstat:
Mapp/src/main/assets/lang/en_US.json | 4++--
Mapp/src/main/assets/lang/fr_FR.json | 4++--
Aapp/src/main/kotlin/me/rhunk/snapenhance/ui/download/DebugSettingsLayoutInflater.kt | 110+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mapp/src/main/kotlin/me/rhunk/snapenhance/ui/download/DownloadManagerActivity.kt | 4++--
Dapp/src/main/kotlin/me/rhunk/snapenhance/ui/download/SettingLayoutInflater.kt | 105-------------------------------------------------------------------------------
Aapp/src/main/res/drawable/debug_settings_icon.xml | 4++++
Mapp/src/main/res/layout/download_manager_activity.xml | 4++--
7 files changed, 122 insertions(+), 113 deletions(-)

diff --git a/app/src/main/assets/lang/en_US.json b/app/src/main/assets/lang/en_US.json @@ -375,8 +375,8 @@ "story_category": "Stories", "spotlight_category": "Spotlight" }, - "settings": "Settings", - "settings_page": { + "debug_settings": "Debug Settings", + "debug_settings_page": { "clear_file_title": "Clear {file_name} file", "clear_file_confirmation": "Are you sure you want to clear the {file_name} file?", "clear_cache_title": "Clear Cache", diff --git a/app/src/main/assets/lang/fr_FR.json b/app/src/main/assets/lang/fr_FR.json @@ -375,8 +375,8 @@ "story_category": "Story", "spotlight_category": "Spotlight" }, - "settings": "Paramètres", - "settings_page": { + "debug_settings": "Paramètres de débogage", + "debug_settings_page": { "clear_file_title": "Supprimer le fichier {file_name}", "clear_file_confirmation": "Êtes-vous sûr de vouloir supprimer le fichier {file_name} ?", "clear_cache_title": "Supprimer le cache", diff --git a/app/src/main/kotlin/me/rhunk/snapenhance/ui/download/DebugSettingsLayoutInflater.kt b/app/src/main/kotlin/me/rhunk/snapenhance/ui/download/DebugSettingsLayoutInflater.kt @@ -0,0 +1,109 @@ +package me.rhunk.snapenhance.ui.download + +import android.app.AlertDialog +import android.content.Intent +import android.view.View +import android.view.ViewGroup +import android.widget.ArrayAdapter +import android.widget.ImageButton +import android.widget.ListView +import android.widget.TextView +import android.widget.Toast +import me.rhunk.snapenhance.R +import me.rhunk.snapenhance.SharedContext +import me.rhunk.snapenhance.bridge.common.impl.file.BridgeFileType +import me.rhunk.snapenhance.ui.config.ConfigActivity +import java.io.File + +class ActionListAdapter( + private val activity: DownloadManagerActivity, + private val layoutId: Int, + private val actions: Array<Pair<String, () -> Unit>> +) : ArrayAdapter<Pair<String, () -> Unit>>(activity, layoutId, actions) { + override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { + val view = convertView ?: activity.layoutInflater.inflate(layoutId, parent, false) + val action = actions[position] + view.isClickable = true + + view.findViewById<TextView>(R.id.feature_text).text = action.first + view.setOnClickListener { + action.second() + } + + return view + } +} + +class DebugSettingsLayoutInflater( + private val activity: DownloadManagerActivity +) { + private fun confirmAction(title: String, message: String, action: () -> Unit) { + activity.runOnUiThread { + AlertDialog.Builder(activity) + .setTitle(title) + .setMessage(message) + .setPositiveButton(SharedContext.translation["button.positive"]) { _, _ -> + action() + } + .setNegativeButton(SharedContext.translation["button.negative"]) { _, _ -> } + .show() + } + } + + private fun showSuccessToast() { + Toast.makeText(activity, "Success", Toast.LENGTH_SHORT).show() + } + + fun inflate(parent: ViewGroup) { + val debugSettingsLayout = activity.layoutInflater.inflate(R.layout.debug_settings_page, parent, false) + + val debugSettingsTranslation = activity.translation.getCategory("debug_settings_page") + + debugSettingsLayout.findViewById<ImageButton>(R.id.back_button).setOnClickListener { + parent.removeView(debugSettingsLayout) + } + + debugSettingsLayout.findViewById<TextView>(R.id.title).text = activity.translation["debug_settings"] + + debugSettingsLayout.findViewById<ListView>(R.id.setting_page_list).apply { + adapter = ActionListAdapter(activity, R.layout.debug_setting_item, mutableListOf<Pair<String, () -> Unit>>().apply { + add(SharedContext.translation["config_activity.title"] to { + activity.startActivity(Intent(activity, ConfigActivity::class.java)) + }) + add(debugSettingsTranslation["clear_cache_title"] to { + context.cacheDir.listFiles()?.forEach { + it.deleteRecursively() + } + showSuccessToast() + }) + + BridgeFileType.values().forEach { fileType -> + val actionName = debugSettingsTranslation.format("clear_file_title", "file_name" to fileType.displayName) + add(actionName to { + confirmAction(actionName, debugSettingsTranslation.format("clear_file_confirmation", "file_name" to fileType.displayName)) { + fileType.resolve(context).deleteRecursively() + showSuccessToast() + } + }) + } + + add(debugSettingsTranslation["reset_all_title"] to { + confirmAction(debugSettingsTranslation["reset_all_title"], debugSettingsTranslation["reset_all_confirmation"]) { + arrayOf(context.cacheDir, context.filesDir, File(context.dataDir, "databases"), File(context.dataDir, "shared_prefs")).forEach { + it.listFiles()?.forEach { file -> + file.deleteRecursively() + } + } + showSuccessToast() + } + }) + }.toTypedArray()) + } + + activity.registerBackCallback { + parent.removeView(debugSettingsLayout) + } + + parent.addView(debugSettingsLayout) + } +}+ \ No newline at end of file diff --git a/app/src/main/kotlin/me/rhunk/snapenhance/ui/download/DownloadManagerActivity.kt b/app/src/main/kotlin/me/rhunk/snapenhance/ui/download/DownloadManagerActivity.kt @@ -74,8 +74,8 @@ class DownloadManagerActivity : Activity() { findViewById<TextView>(R.id.title).text = resources.getString(R.string.app_name) + " " + BuildConfig.VERSION_NAME - findViewById<ImageButton>(R.id.settings_button).setOnClickListener { - SettingLayoutInflater(this).inflate(findViewById(android.R.id.content)) + findViewById<ImageButton>(R.id.debug_settings_button).setOnClickListener { + DebugSettingsLayoutInflater(this).inflate(findViewById(android.R.id.content)) } with(findViewById<RecyclerView>(R.id.download_list)) { diff --git a/app/src/main/kotlin/me/rhunk/snapenhance/ui/download/SettingLayoutInflater.kt b/app/src/main/kotlin/me/rhunk/snapenhance/ui/download/SettingLayoutInflater.kt @@ -1,104 +0,0 @@ -package me.rhunk.snapenhance.ui.download - -import android.app.AlertDialog -import android.view.View -import android.view.ViewGroup -import android.widget.ArrayAdapter -import android.widget.ImageButton -import android.widget.ListView -import android.widget.TextView -import android.widget.Toast -import me.rhunk.snapenhance.R -import me.rhunk.snapenhance.SharedContext -import me.rhunk.snapenhance.bridge.common.impl.file.BridgeFileType -import java.io.File - -class SettingAdapter( - private val activity: DownloadManagerActivity, - private val layoutId: Int, - private val actions: Array<Pair<String, () -> Unit>> -) : ArrayAdapter<Pair<String, () -> Unit>>(activity, layoutId, actions) { - override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { - val view = convertView ?: activity.layoutInflater.inflate(layoutId, parent, false) - val action = actions[position] - view.isClickable = true - - view.findViewById<TextView>(R.id.feature_text).text = action.first - view.setOnClickListener { - action.second() - } - - return view - } -} - -class SettingLayoutInflater( - private val activity: DownloadManagerActivity -) { - private fun confirmAction(title: String, message: String, action: () -> Unit) { - activity.runOnUiThread { - AlertDialog.Builder(activity) - .setTitle(title) - .setMessage(message) - .setPositiveButton(SharedContext.translation["button.positive"]) { _, _ -> - action() - } - .setNegativeButton(SharedContext.translation["button.negative"]) { _, _ -> } - .show() - } - } - - private fun showSuccessToast() { - Toast.makeText(activity, "Success", Toast.LENGTH_SHORT).show() - } - - fun inflate(parent: ViewGroup) { - val settingsView = activity.layoutInflater.inflate(R.layout.debug_settings_page, parent, false) - - val settingTranslation = activity.translation.getCategory("settings_page") - - settingsView.findViewById<ImageButton>(R.id.back_button).setOnClickListener { - parent.removeView(settingsView) - } - - settingsView.findViewById<TextView>(R.id.title).text = activity.translation["settings"] - - settingsView.findViewById<ListView>(R.id.setting_page_list).apply { - adapter = SettingAdapter(activity, R.layout.debug_setting_item, mutableListOf<Pair<String, () -> Unit>>().apply { - add(settingTranslation["clear_cache_title"] to { - context.cacheDir.listFiles()?.forEach { - it.deleteRecursively() - } - showSuccessToast() - }) - - BridgeFileType.values().forEach { fileType -> - val actionName = settingTranslation.format("clear_file_title", "file_name" to fileType.displayName) - add(actionName to { - confirmAction(actionName, settingTranslation.format("clear_file_confirmation", "file_name" to fileType.displayName)) { - fileType.resolve(context).deleteRecursively() - showSuccessToast() - } - }) - } - - add(settingTranslation["reset_all_title"] to { - confirmAction(settingTranslation["reset_all_title"], settingTranslation["reset_all_confirmation"]) { - arrayOf(context.cacheDir, context.filesDir, File(context.dataDir, "databases"), File(context.dataDir, "shared_prefs")).forEach { - it.listFiles()?.forEach { file -> - file.deleteRecursively() - } - } - showSuccessToast() - } - }) - }.toTypedArray()) - } - - activity.registerBackCallback { - parent.removeView(settingsView) - } - - parent.addView(settingsView) - } -}- \ No newline at end of file diff --git a/app/src/main/res/drawable/debug_settings_icon.xml b/app/src/main/res/drawable/debug_settings_icon.xml @@ -0,0 +1,4 @@ +<vector android:height="30dp" android:viewportHeight="960" + android:viewportWidth="960" android:width="30dp" xmlns:android="http://schemas.android.com/apk/res/android"> + <path android:fillColor="#FFFFFF" android:pathData="M480,840q-65,0 -121,-31t-83,-89L160,720v-60h92q-7,-26 -7,-52.5L245,554h-86v-60h86q0,-29 0.5,-57.5T254,380h-94v-60h120q14,-28 37,-49t51,-35l-77,-76 40,-40 94,94q28,-10 56.5,-10t56.5,10l94,-94 40,40 -76,76q28,14 49.5,35.5T683,320h118v60h-95q9,28 8.5,56.5T714,494h87v60h-87q0,27 0.5,53.5T708,660h93v60L685,720q-26,59 -82.5,89.5T480,840ZM480,780q72,0 123,-50.5T654,607v-167q0,-72 -51,-122.5T480,267q-72,0 -123,50.5T306,440v167q0,72 51,122.5T480,780ZM400,640h160v-60L400,580v60ZM400,467h160v-60L400,407v60ZM480,524h0.5,-0.5 0.5,-0.5 0.5,-0.5 0.5,-0.5Z"/> +</vector> diff --git a/app/src/main/res/layout/download_manager_activity.xml b/app/src/main/res/layout/download_manager_activity.xml @@ -27,11 +27,11 @@ android:fontFamily="@font/avenir_next_bold" /> <ImageButton - android:id="@+id/settings_button" + android:id="@+id/debug_settings_button" android:layout_width="45dp" android:layout_height="match_parent" android:background="@null" - android:src="@drawable/settings_icon" + android:src="@drawable/debug_settings_icon" android:layout_gravity="center_vertical|end" android:padding="8dp" android:contentDescription="@null" />