~gardenapple/mitch

78c799b4d2f4eb2633aebed99ed12ce12918919e — gardenapple a month ago 5bded8b
Add dialog to manually restart app on lang change :(
M app/src/main/java/ua/gardenapple/itchupdater/Mitch.kt => app/src/main/java/ua/gardenapple/itchupdater/Mitch.kt +23 -13
@@ 8,10 8,13 @@ import android.content.Context
import android.content.SharedPreferences
import android.os.Build
import android.util.Log
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.edit
import androidx.preference.PreferenceManager
import androidx.work.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import okhttp3.Cache
import okhttp3.OkHttpClient
import org.acra.ACRA


@@ 20,8 23,11 @@ import org.acra.config.DialogConfigurationBuilder
import org.acra.config.MailSenderConfigurationBuilder
import org.acra.data.StringFormat
import ua.gardenapple.itchupdater.client.UpdateChecker
import ua.gardenapple.itchupdater.database.AppDatabase
import ua.gardenapple.itchupdater.database.DatabaseCleanup
import ua.gardenapple.itchupdater.database.installation.Installation
import ua.gardenapple.itchupdater.files.*
import ua.gardenapple.itchupdater.install.Installations
import ua.gardenapple.itchupdater.install.InstallerDatabaseHandler
import ua.gardenapple.itchupdater.ui.CrashDialog
import ua.gardenapple.itchupdater.ui.MitchContextWrapper


@@ 60,6 66,9 @@ const val PREF_PREFIX_PALESTINE_LINK = "mitch.palestine_"
const val PREF_PREFIX_PALESTINE_LAST_CHECK = "mitch.palestinetimestamp_"
const val PREF_WEB_ANDROID_FILTER = "ua.gardenapple.itchupdater.web_android_filter"
const val PREF_LANG = "mitch.lang"
/**
 * Locale is not controlled directly by the user; instead, it is managed automatically in Mitch.kt
 */
const val PREF_LANG_LOCALE = "mitch.lang_locale"
const val PREF_LANG_SITE_LOCALE = "mitch.lang_site_locale"



@@ 99,7 108,6 @@ class Mitch : Application() {

    private val preferenceChangeListener =
        SharedPreferences.OnSharedPreferenceChangeListener { prefs, key ->
            Log.d(LOGGING_TAG, "Changed $key !")
            when (key) {
                "preference_update_check_if_metered" -> {
                    registerUpdateCheckTask(prefs.getBoolean(key, false),


@@ 222,19 230,21 @@ class Mitch : Application() {
    }

    private fun setLangFromPreferences(prefs: SharedPreferences) {
        prefs.edit(true) {
            val newLocale = when (prefs.getString(PREF_LANG, "default")) {
                "system" -> Utils.getPreferredLocale(applicationContext).toLanguageTag()
                "site" -> prefs.getString(PREF_LANG_SITE_LOCALE, "en")
                else -> {
                    val siteLocale = prefs.getString(PREF_LANG_SITE_LOCALE, "en")
                    if (siteLocale == "en")
                        Utils.getPreferredLocale(applicationContext).toLanguageTag()
                    else
                        siteLocale
                }
        val systemLocale = Utils.getPreferredLocale(applicationContext).toLanguageTag()
        val newLocale = when (prefs.getString(PREF_LANG, "default")) {
            "system" -> systemLocale
            "site" -> prefs.getString(PREF_LANG_SITE_LOCALE, "en")
            else -> {
                val siteLocale = prefs.getString(PREF_LANG_SITE_LOCALE, "en")
                if (siteLocale == "en")
                    systemLocale
                else
                    siteLocale
            }
            putString(PREF_LANG_LOCALE, newLocale)
        }
        prefs.edit(true) {
            if (newLocale != prefs.getString(PREF_LANG_LOCALE, systemLocale))
                putString(PREF_LANG_LOCALE, newLocale)
        }
    }


M app/src/main/java/ua/gardenapple/itchupdater/ui/MainActivity.kt => app/src/main/java/ua/gardenapple/itchupdater/ui/MainActivity.kt +61 -1
@@ 1,17 1,24 @@
package ua.gardenapple.itchupdater.ui

import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.graphics.drawable.ColorDrawable
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.edit
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.preference.PreferenceManager
import androidx.work.ExistingPeriodicWorkPolicy
import com.google.android.material.bottomnavigation.BottomNavigationView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking


@@ 20,7 27,9 @@ import ua.gardenapple.itchupdater.database.AppDatabase
import ua.gardenapple.itchupdater.database.DatabaseCleanup
import ua.gardenapple.itchupdater.databinding.ActivityMainBinding


/**
 * The MainActiviry handles a lot of things, including day/night themes and languages
 */
class MainActivity : MitchActivity(), ActivityCompat.OnRequestPermissionsResultCallback {
    private lateinit var browseFragment: BrowseFragment
    private lateinit var currentFragmentTag: String


@@ 28,7 37,45 @@ class MainActivity : MitchActivity(), ActivityCompat.OnRequestPermissionsResultC
    lateinit var binding: ActivityMainBinding
    private set

    /**
     * The restart dialog is handled here.
     * Yes, this means that if the language changes outside of MainActivity,
     * then there will be no dialog.
     */
    private val langChangeListener =
        SharedPreferences.OnSharedPreferenceChangeListener langChange@{ prefs, key ->
            if (key != PREF_LANG_LOCALE)
                return@langChange

            Log.d(LOGGING_TAG, "lang changed to ${prefs.getString(PREF_LANG_LOCALE, "null?")}")

            AlertDialog.Builder(this).run {
                setTitle(R.string.dialog_lang_restart_title)
                setMessage(R.string.dialog_lang_restart)
                setIcon(R.drawable.ic_baseline_warning_24)

                setPositiveButton(android.R.string.ok) { _, _ ->
                    //Restart app https://stackoverflow.com/a/22345538/5701177
                    val intent = Intent(
                        Intent.ACTION_VIEW,
                        Uri.parse(browseFragment.webView.url),
                        context,
                        MainActivity::class.java
                    )
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    context.startActivity(intent)
                    finish()
                    Runtime.getRuntime().exit(0);
                }

                setCancelable(false)
                show()
            }
        }

    companion object {
        const val LOGGING_TAG = "MainActivity"

        const val EXTRA_SHOULD_OPEN_LIBRARY = "SHOULD_OPEN_LIBRARY"
        
        private const val ACTIVE_FRAGMENT_KEY: String = "fragment"


@@ 130,6 177,18 @@ class MainActivity : MitchActivity(), ActivityCompat.OnRequestPermissionsResultC
        }
    }

    override fun onResume() {
        super.onResume()
        PreferenceManager.getDefaultSharedPreferences(this)
            .registerOnSharedPreferenceChangeListener(langChangeListener)
    }

    override fun onPause() {
        super.onPause()
        PreferenceManager.getDefaultSharedPreferences(this)
            .unregisterOnSharedPreferenceChangeListener(langChangeListener)
    }

    override fun onBackPressed() {
        if (browseFragment.isVisible) {
            val cantGoBack = browseFragment.onBackPressed()


@@ 288,6 347,7 @@ class MainActivity : MitchActivity(), ActivityCompat.OnRequestPermissionsResultC
    }



    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        when (requestCode) {

M app/src/main/java/ua/gardenapple/itchupdater/ui/SettingsFragment.kt => app/src/main/java/ua/gardenapple/itchupdater/ui/SettingsFragment.kt +4 -4
@@ 1,20 1,21 @@
package ua.gardenapple.itchupdater.ui

import android.app.NotificationManager
import android.content.SharedPreferences
import android.content.res.Configuration
import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.core.app.NotificationManagerCompat
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.work.ExistingPeriodicWorkPolicy
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import ua.gardenapple.itchupdater.Mitch
import ua.gardenapple.itchupdater.NOTIFICATION_CHANNEL_ID_INSTALLING
import ua.gardenapple.itchupdater.R
import ua.gardenapple.itchupdater.*
import ua.gardenapple.itchupdater.database.AppDatabase
import ua.gardenapple.itchupdater.database.DatabaseCleanup
import ua.gardenapple.itchupdater.database.installation.Installation


@@ 63,7 64,6 @@ class SettingsFragment : PreferenceFragmentCompat() {
        return false
    }


    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)


M app/src/main/res/values-ru/strings.xml => app/src/main/res/values-ru/strings.xml +2 -0
@@ 170,4 170,6 @@
    <string name="settings_lang_site">Как у сайта</string>
    <string name="settings_lang_system">Как у системы</string>
    <string name="settings_lang">Язык</string>
    <string name="dialog_lang_restart_title">Смена языка</string>
    <string name="dialog_lang_restart">Приложение перезапустится, чтобы сменить язык</string>
</resources>
\ No newline at end of file

M app/src/main/res/values-uk/strings.xml => app/src/main/res/values-uk/strings.xml +2 -0
@@ 166,4 166,6 @@
    <string name="settings_lang_site">Як на сайті</string>
    <string name="settings_lang_system">Як у системі</string>
    <string name="settings_lang">Мова</string>
    <string name="dialog_lang_restart_title">Зміна мови</string>
    <string name="dialog_lang_restart">Додаток перезапуститься з новою мовою</string>
</resources>
\ No newline at end of file

M app/src/main/res/values/strings.xml => app/src/main/res/values/strings.xml +2 -0
@@ 166,4 166,6 @@
    <string name="settings_lang_site">Depends on site</string>
    <string name="settings_lang_system">Depends on system</string>
    <string name="settings_lang">Language</string>
    <string name="dialog_lang_restart_title">Language changed</string>
    <string name="dialog_lang_restart">The app will restart to apply the new language</string>
</resources>