1
0
mirror of https://github.com/wulkanowy/wulkanowy.git synced 2025-01-31 19:52:46 +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.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-adapters:$moshi"

View File

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

View File

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

View File

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

View File

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

View File

@ -20,7 +20,7 @@ class AnalyticsHelper @Inject constructor(
params.forEach {
if (it.second == null) return@forEach
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 Boolean, is Boolean? -> putBoolean(it.first, it.second as Boolean)
}