From e98f5b9d54e78049f0d9db1f38f01dd9a884397c Mon Sep 17 00:00:00 2001 From: Koitharu Date: Mon, 24 Feb 2025 19:49:51 +0200 Subject: [PATCH] Fix locale changing --- .../org/koitharu/kotatsu/core/ui/BaseService.kt | 6 ++++++ .../kotlin/org/koitharu/kotatsu/core/AppModule.kt | 7 +++++++ .../koitharu/kotatsu/core/LocalizedAppContext.kt | 13 +++++++++++++ .../ui/worker/DownloadNotificationFactory.kt | 4 ++-- .../kotatsu/explore/data/MangaSourcesRepository.kt | 3 ++- .../kotatsu/main/ui/welcome/WelcomeViewModel.kt | 3 ++- .../koitharu/kotatsu/reader/domain/PageLoader.kt | 3 ++- .../koitharu/kotatsu/reader/ui/PageSaveHelper.kt | 3 ++- .../kotatsu/settings/search/SettingsSearchHelper.kt | 3 ++- .../settings/sources/manage/SourcesListProducer.kt | 3 ++- .../tracker/work/TrackerNotificationHelper.kt | 3 ++- .../org/koitharu/kotatsu/core/ui/BaseService.kt | 9 ++++++++- .../org/koitharu/kotatsu/core/ui/BaseService.kt | 9 ++++++++- 13 files changed, 58 insertions(+), 11 deletions(-) create mode 100644 app/src/main/kotlin/org/koitharu/kotatsu/core/LocalizedAppContext.kt diff --git a/app/src/debug/kotlin/org/koitharu/kotatsu/core/ui/BaseService.kt b/app/src/debug/kotlin/org/koitharu/kotatsu/core/ui/BaseService.kt index 6453ac7fe..b98c6cdb6 100644 --- a/app/src/debug/kotlin/org/koitharu/kotatsu/core/ui/BaseService.kt +++ b/app/src/debug/kotlin/org/koitharu/kotatsu/core/ui/BaseService.kt @@ -1,10 +1,16 @@ package org.koitharu.kotatsu.core.ui +import android.content.Context +import androidx.core.content.ContextCompat import androidx.lifecycle.LifecycleService import leakcanary.AppWatcher abstract class BaseService : LifecycleService() { + override fun attachBaseContext(newBase: Context) { + super.attachBaseContext(ContextCompat.getContextForLanguage(newBase)) + } + override fun onDestroy() { super.onDestroy() AppWatcher.objectWatcher.watch( diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/AppModule.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/AppModule.kt index 57ef8da27..10217fb26 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/AppModule.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/AppModule.kt @@ -6,6 +6,7 @@ import android.os.Build import android.provider.SearchRecentSuggestions import android.text.Html import androidx.collection.arraySetOf +import androidx.core.content.ContextCompat import androidx.room.InvalidationTracker import androidx.work.WorkManager import coil3.ImageLoader @@ -76,6 +77,12 @@ interface AppModule { companion object { + @Provides + @LocalizedAppContext + fun provideLocalizedContext( + @ApplicationContext context: Context, + ): Context = ContextCompat.getContextForLanguage(context) + @Provides @Singleton fun provideNetworkState( diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/LocalizedAppContext.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/LocalizedAppContext.kt new file mode 100644 index 000000000..59777a36f --- /dev/null +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/LocalizedAppContext.kt @@ -0,0 +1,13 @@ +package org.koitharu.kotatsu.core + +import javax.inject.Qualifier + +@Qualifier +@Target( + AnnotationTarget.FUNCTION, + AnnotationTarget.PROPERTY_GETTER, + AnnotationTarget.PROPERTY_SETTER, + AnnotationTarget.VALUE_PARAMETER, + AnnotationTarget.FIELD, +) +annotation class LocalizedAppContext diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadNotificationFactory.kt b/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadNotificationFactory.kt index f6bf39208..67a5ac01a 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadNotificationFactory.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadNotificationFactory.kt @@ -19,11 +19,11 @@ import coil3.size.Scale import dagger.assisted.Assisted import dagger.assisted.AssistedFactory import dagger.assisted.AssistedInject -import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.ErrorReporterReceiver +import org.koitharu.kotatsu.core.LocalizedAppContext import org.koitharu.kotatsu.core.model.LocalMangaSource import org.koitharu.kotatsu.core.nav.AppRouter import org.koitharu.kotatsu.core.util.ext.getDrawableOrThrow @@ -43,7 +43,7 @@ private const val CHANNEL_ID_SILENT = "download_bg" private const val GROUP_ID = "downloads" class DownloadNotificationFactory @AssistedInject constructor( - @ApplicationContext private val context: Context, + @LocalizedAppContext private val context: Context, private val workManager: WorkManager, private val coil: ImageLoader, @Assisted private val uuid: UUID, diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/explore/data/MangaSourcesRepository.kt b/app/src/main/kotlin/org/koitharu/kotatsu/explore/data/MangaSourcesRepository.kt index 818e76400..770890500 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/explore/data/MangaSourcesRepository.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/explore/data/MangaSourcesRepository.kt @@ -18,6 +18,7 @@ import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onStart import org.koitharu.kotatsu.BuildConfig +import org.koitharu.kotatsu.core.LocalizedAppContext import org.koitharu.kotatsu.core.db.MangaDatabase import org.koitharu.kotatsu.core.db.dao.MangaSourcesDao import org.koitharu.kotatsu.core.db.entity.MangaSourceEntity @@ -42,7 +43,7 @@ import javax.inject.Singleton @Singleton class MangaSourcesRepository @Inject constructor( - @ApplicationContext private val context: Context, + @LocalizedAppContext private val context: Context, private val db: MangaDatabase, private val settings: AppSettings, ) { diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/main/ui/welcome/WelcomeViewModel.kt b/app/src/main/kotlin/org/koitharu/kotatsu/main/ui/welcome/WelcomeViewModel.kt index 7ec601e12..7bbc1b3bb 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/main/ui/welcome/WelcomeViewModel.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/main/ui/welcome/WelcomeViewModel.kt @@ -7,6 +7,7 @@ import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.flow.MutableStateFlow +import org.koitharu.kotatsu.core.LocalizedAppContext import org.koitharu.kotatsu.core.ui.BaseViewModel import org.koitharu.kotatsu.core.util.LocaleComparator import org.koitharu.kotatsu.core.util.ext.mapSortedByCount @@ -25,7 +26,7 @@ import javax.inject.Inject @HiltViewModel class WelcomeViewModel @Inject constructor( private val repository: MangaSourcesRepository, - @ApplicationContext context: Context, + @LocalizedAppContext context: Context, ) : BaseViewModel() { private val allSources = repository.allMangaSources diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/reader/domain/PageLoader.kt b/app/src/main/kotlin/org/koitharu/kotatsu/reader/domain/PageLoader.kt index f891652f4..9dc2b9535 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/reader/domain/PageLoader.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/reader/domain/PageLoader.kt @@ -28,6 +28,7 @@ import okhttp3.OkHttpClient import okhttp3.Request import okio.use import org.jetbrains.annotations.Blocking +import org.koitharu.kotatsu.core.LocalizedAppContext import org.koitharu.kotatsu.core.image.BitmapDecoderCompat import org.koitharu.kotatsu.core.network.CommonHeaders import org.koitharu.kotatsu.core.network.MangaHttpClient @@ -71,7 +72,7 @@ import kotlin.coroutines.CoroutineContext @ActivityRetainedScoped class PageLoader @Inject constructor( - @ApplicationContext private val context: Context, + @LocalizedAppContext private val context: Context, lifecycle: ActivityRetainedLifecycle, @MangaHttpClient private val okHttp: OkHttpClient, private val cache: PagesCache, diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/PageSaveHelper.kt b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/PageSaveHelper.kt index d2b393a3f..cf0e6aa2e 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/PageSaveHelper.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/PageSaveHelper.kt @@ -25,6 +25,7 @@ import okio.buffer import okio.openZip import okio.sink import okio.source +import org.koitharu.kotatsu.core.LocalizedAppContext import org.koitharu.kotatsu.core.image.BitmapDecoderCompat import org.koitharu.kotatsu.core.os.OpenDocumentTreeHelper import org.koitharu.kotatsu.core.prefs.AppSettings @@ -46,7 +47,7 @@ import kotlin.coroutines.resume class PageSaveHelper @AssistedInject constructor( @Assisted activityResultCaller: ActivityResultCaller, - @ApplicationContext private val context: Context, + @LocalizedAppContext private val context: Context, private val settings: AppSettings, private val pageLoaderProvider: Provider, ) : ActivityResultCallback { diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/search/SettingsSearchHelper.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/search/SettingsSearchHelper.kt index 03a444eb2..41441a77e 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/search/SettingsSearchHelper.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/search/SettingsSearchHelper.kt @@ -10,6 +10,7 @@ import androidx.preference.get import dagger.Reusable import dagger.hilt.android.qualifiers.ApplicationContext import org.koitharu.kotatsu.R +import org.koitharu.kotatsu.core.LocalizedAppContext import org.koitharu.kotatsu.settings.AppearanceSettingsFragment import org.koitharu.kotatsu.settings.DownloadsSettingsFragment import org.koitharu.kotatsu.settings.NetworkSettingsFragment @@ -28,7 +29,7 @@ import javax.inject.Inject @Reusable @SuppressLint("RestrictedApi") class SettingsSearchHelper @Inject constructor( - @ApplicationContext private val context: Context, + @LocalizedAppContext private val context: Context, ) { fun inflatePreferences(): List { diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/manage/SourcesListProducer.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/manage/SourcesListProducer.kt index c10147f1f..2901f6bc1 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/manage/SourcesListProducer.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/manage/SourcesListProducer.kt @@ -15,6 +15,7 @@ import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import org.koitharu.kotatsu.R +import org.koitharu.kotatsu.core.LocalizedAppContext import org.koitharu.kotatsu.core.db.TABLE_SOURCES import org.koitharu.kotatsu.core.model.getTitle import org.koitharu.kotatsu.core.model.isNsfw @@ -31,7 +32,7 @@ import javax.inject.Inject @ViewModelScoped class SourcesListProducer @Inject constructor( lifecycle: ViewModelLifecycle, - @ApplicationContext private val context: Context, + @LocalizedAppContext private val context: Context, private val repository: MangaSourcesRepository, private val settings: AppSettings, ) : InvalidationTracker.Observer(TABLE_SOURCES) { diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/tracker/work/TrackerNotificationHelper.kt b/app/src/main/kotlin/org/koitharu/kotatsu/tracker/work/TrackerNotificationHelper.kt index daab52ec1..f2f166b45 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/tracker/work/TrackerNotificationHelper.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/tracker/work/TrackerNotificationHelper.kt @@ -16,6 +16,7 @@ import coil3.ImageLoader import coil3.request.ImageRequest import dagger.hilt.android.qualifiers.ApplicationContext import org.koitharu.kotatsu.R +import org.koitharu.kotatsu.core.LocalizedAppContext import org.koitharu.kotatsu.core.nav.AppRouter import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.util.ext.checkNotificationPermission @@ -26,7 +27,7 @@ import org.koitharu.kotatsu.parsers.model.MangaChapter import javax.inject.Inject class TrackerNotificationHelper @Inject constructor( - @ApplicationContext private val applicationContext: Context, + @LocalizedAppContext private val applicationContext: Context, private val settings: AppSettings, private val coil: ImageLoader, ) { diff --git a/app/src/nightly/kotlin/org/koitharu/kotatsu/core/ui/BaseService.kt b/app/src/nightly/kotlin/org/koitharu/kotatsu/core/ui/BaseService.kt index 7a8f1463c..c01174ef1 100644 --- a/app/src/nightly/kotlin/org/koitharu/kotatsu/core/ui/BaseService.kt +++ b/app/src/nightly/kotlin/org/koitharu/kotatsu/core/ui/BaseService.kt @@ -1,5 +1,12 @@ package org.koitharu.kotatsu.core.ui +import android.content.Context +import androidx.core.content.ContextCompat import androidx.lifecycle.LifecycleService -abstract class BaseService : LifecycleService() +abstract class BaseService : LifecycleService() { + + override fun attachBaseContext(newBase: Context) { + super.attachBaseContext(ContextCompat.getContextForLanguage(newBase)) + } +} diff --git a/app/src/release/kotlin/org/koitharu/kotatsu/core/ui/BaseService.kt b/app/src/release/kotlin/org/koitharu/kotatsu/core/ui/BaseService.kt index 7a8f1463c..c01174ef1 100644 --- a/app/src/release/kotlin/org/koitharu/kotatsu/core/ui/BaseService.kt +++ b/app/src/release/kotlin/org/koitharu/kotatsu/core/ui/BaseService.kt @@ -1,5 +1,12 @@ package org.koitharu.kotatsu.core.ui +import android.content.Context +import androidx.core.content.ContextCompat import androidx.lifecycle.LifecycleService -abstract class BaseService : LifecycleService() +abstract class BaseService : LifecycleService() { + + override fun attachBaseContext(newBase: Context) { + super.attachBaseContext(ContextCompat.getContextForLanguage(newBase)) + } +}