1
0
mirror of https://github.com/wulkanowy/wulkanowy.git synced 2025-01-31 22:52:44 +01:00

Fix follow system language setting (#1113)

This commit is contained in:
Rafał Borcz 2021-01-31 22:38:30 +01:00 committed by GitHub
parent 4984cb9b26
commit 82b207b03a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 45 additions and 24 deletions

View File

@ -184,7 +184,7 @@ dependencies {
implementation "com.aurelhubert:ahbottomnavigation:2.3.4" implementation "com.aurelhubert:ahbottomnavigation:2.3.4"
implementation "com.ncapdevi:frag-nav:3.3.0" implementation "com.ncapdevi:frag-nav:3.3.0"
implementation "com.github.YarikSOffice:lingver:1.2.2" implementation "com.github.YarikSOffice:lingver:1.3.0"
implementation "com.squareup.moshi:moshi:$moshi" implementation "com.squareup.moshi:moshi:$moshi"
implementation "com.squareup.moshi:moshi-adapters:$moshi" implementation "com.squareup.moshi:moshi-adapters:$moshi"

View File

@ -5,6 +5,7 @@ import android.content.Context
import android.util.Log.DEBUG import android.util.Log.DEBUG
import android.util.Log.INFO import android.util.Log.INFO
import android.util.Log.VERBOSE import android.util.Log.VERBOSE
import android.webkit.WebView
import androidx.hilt.work.HiltWorkerFactory import androidx.hilt.work.HiltWorkerFactory
import androidx.multidex.MultiDex import androidx.multidex.MultiDex
import androidx.work.Configuration import androidx.work.Configuration
@ -47,17 +48,18 @@ class WulkanowyApp : Application(), Configuration.Provider {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
Lingver.init(this)
themeManager.applyDefaultTheme()
initializeAppLanguage()
themeManager.applyDefaultTheme()
initLogging() initLogging()
logCurrentLanguage() fixWebViewLocale()
} }
private fun initLogging() { private fun initLogging() {
if (appInfo.isDebug) { if (appInfo.isDebug) {
Timber.plant(DebugLogTree()) Timber.plant(DebugLogTree())
Timber.plant(FileLoggerTree.Builder() Timber.plant(
FileLoggerTree.Builder()
.withFileName("wulkanowy.%g.log") .withFileName("wulkanowy.%g.log")
.withDirName(applicationContext.filesDir.absolutePath) .withDirName(applicationContext.filesDir.absolutePath)
.withFileLimit(10) .withFileLimit(10)
@ -71,14 +73,20 @@ class WulkanowyApp : Application(), Configuration.Provider {
registerActivityLifecycleCallbacks(ActivityLifecycleLogger()) registerActivityLifecycleCallbacks(ActivityLifecycleLogger())
} }
private fun logCurrentLanguage() { private fun initializeAppLanguage() {
val newLang = if (preferencesRepository.appLanguage == "system") { Lingver.init(this)
appInfo.systemLanguage
if (preferencesRepository.appLanguage == "system") {
Lingver.getInstance().setFollowSystemLocale(this)
analyticsHelper.logEvent("language", "startup" to appInfo.systemLanguage)
} else { } else {
preferencesRepository.appLanguage analyticsHelper.logEvent("language", "startup" to preferencesRepository.appLanguage)
}
} }
analyticsHelper.logEvent("language", "startup" to newLang) private fun fixWebViewLocale() {
//https://stackoverflow.com/questions/40398528/android-webview-language-changes-abruptly-on-android-7-0-and-above
WebView(this).destroy()
} }
override fun getWorkManagerConfiguration() = Configuration.Builder() override fun getWorkManagerConfiguration() = Configuration.Builder()

View File

@ -79,6 +79,10 @@ class SettingsFragment : PreferenceFragmentCompat(),
lingver.setLocale(requireContext(), langCode) lingver.setLocale(requireContext(), langCode)
} }
override fun updateLanguageToFollowSystem() {
lingver.setFollowSystemLocale(requireContext())
}
override fun setServicesSuspended(serviceEnablesKey: String, isHolidays: Boolean) { override fun setServicesSuspended(serviceEnablesKey: String, isHolidays: Boolean) {
findPreference<Preference>(serviceEnablesKey)?.run { findPreference<Preference>(serviceEnablesKey)?.run {
summary = if (isHolidays) getString(R.string.pref_services_suspended) else "" summary = if (isHolidays) getString(R.string.pref_services_suspended) else ""

View File

@ -42,14 +42,18 @@ class SettingsPresenter @Inject constructor(
when (key) { when (key) {
serviceEnableKey -> with(syncManager) { if (isServiceEnabled) startPeriodicSyncWorker() else stopSyncWorker() } serviceEnableKey -> with(syncManager) { if (isServiceEnabled) startPeriodicSyncWorker() else stopSyncWorker() }
servicesIntervalKey, servicesOnlyWifiKey -> syncManager.startPeriodicSyncWorker(true) servicesIntervalKey, servicesOnlyWifiKey -> syncManager.startPeriodicSyncWorker(true)
isDebugNotificationEnableKey -> chuckerCollector.showNotification = isDebugNotificationEnable isDebugNotificationEnableKey -> chuckerCollector.showNotification =
isDebugNotificationEnable
appThemeKey -> view?.recreateView() appThemeKey -> view?.recreateView()
isUpcomingLessonsNotificationsEnableKey -> if (!isUpcomingLessonsNotificationsEnable) timetableNotificationHelper.cancelNotification() isUpcomingLessonsNotificationsEnableKey -> if (!isUpcomingLessonsNotificationsEnable) timetableNotificationHelper.cancelNotification()
appLanguageKey -> view?.run { appLanguageKey -> view?.run {
val newLang = if (appLanguage == "system") appInfo.systemLanguage else appLanguage if (appLanguage == "system") {
analytics.logEvent("language", "setting_changed" to newLang) updateLanguageToFollowSystem()
analytics.logEvent("language", "setting_changed" to appInfo.systemLanguage)
updateLanguage(newLang) } else {
updateLanguage(appLanguage)
analytics.logEvent("language", "setting_changed" to appLanguage)
}
recreateView() recreateView()
} }
} }
@ -71,7 +75,10 @@ class SettingsPresenter @Inject constructor(
analytics.logEvent("sync_now", "status" to "success") analytics.logEvent("sync_now", "status" to "success")
} }
WorkInfo.State.FAILED -> { WorkInfo.State.FAILED -> {
showError(syncFailedString, Throwable(workInfo.outputData.getString("error"))) showError(
syncFailedString,
Throwable(workInfo.outputData.getString("error"))
)
analytics.logEvent("sync_now", "status" to "failed") analytics.logEvent("sync_now", "status" to "failed")
} }
else -> Timber.d("Sync now state: ${workInfo.state}") else -> Timber.d("Sync now state: ${workInfo.state}")

View File

@ -14,6 +14,8 @@ interface SettingsView : BaseView {
fun updateLanguage(langCode: String) fun updateLanguage(langCode: String)
fun updateLanguageToFollowSystem()
fun setServicesSuspended(serviceEnablesKey: String, isHolidays: Boolean) fun setServicesSuspended(serviceEnablesKey: String, isHolidays: Boolean)
fun setSyncInProgress(inProgress: Boolean) fun setSyncInProgress(inProgress: Boolean)

View File

@ -20,7 +20,7 @@ class AnalyticsHelper @Inject constructor(
params.forEach { params.forEach {
if (it.second == null) return@forEach if (it.second == null) return@forEach
when (it.second) { when (it.second) {
is String, is String? -> putString(it.first, it.second as String) is String, is String? -> putString(it.first, it.second.toString())
is Int, is Int? -> putInt(it.first, it.second as Int) is Int, is Int? -> putInt(it.first, it.second as Int)
is Boolean, is Boolean? -> putBoolean(it.first, it.second as Boolean) is Boolean, is Boolean? -> putBoolean(it.first, it.second as Boolean)
} }