@@ -31,52 +31,59 @@ class WhatsNewViewModel(
3131 private val _hasHistory = MutableStateFlow (false )
3232 val hasHistory: StateFlow <Boolean > = _hasHistory .asStateFlow()
3333
34+ @Volatile
35+ private var lastLanguageTag: String? = null
36+
3437 init {
3538 // Re-load whenever the user's selected app language changes.
36- // The loader resolves locale per call, but cached state on this
37- // VM would otherwise serve the previous language's text after
38- // the user switches in Tweaks. distinctUntilChanged guards
39- // against the initial replay-emit firing the load twice.
39+ // The tag is threaded explicitly into the loader so we beat
40+ // the race against MainActivity's setActiveLanguageTag — both
41+ // collectors fan out from the same flow with no ordering
42+ // guarantee, so reading Locale.getDefault() inside the loader
43+ // can return the previous language. distinctUntilChanged
44+ // guards against the initial replay-emit firing the load
45+ // twice.
4046 viewModelScope.launch {
4147 try {
4248 tweaksRepository
4349 .getAppLanguage()
4450 .distinctUntilChanged()
45- .collect {
46- reloadHistory()
47- reloadPending()
51+ .collect { tag ->
52+ lastLanguageTag = tag
53+ reloadHistory(tag)
54+ reloadPending(tag)
4855 }
4956 } catch (t: Throwable ) {
5057 logger.e(t) { " Failed to observe app-language for what's-new reloads" }
5158 }
5259 }
5360 }
5461
55- private suspend fun reloadHistory () {
62+ private suspend fun reloadHistory (languageTag : String? ) {
5663 try {
57- val entries = whatsNewLoader.loadAll()
64+ val entries = whatsNewLoader.loadAll(languageTag )
5865 _historyEntries .value = entries
5966 _hasHistory .value = entries.size > 1
6067 } catch (t: Throwable ) {
6168 logger.e(t) { " Failed to load what's-new history" }
6269 }
6370 }
6471
65- private suspend fun reloadPending () {
72+ private suspend fun reloadPending (languageTag : String? ) {
6673 try {
67- evaluate()
74+ evaluate(languageTag )
6875 } catch (t: Throwable ) {
6976 logger.e(t) { " Failed to evaluate what's-new state" }
7077 }
7178 }
7279
73- private suspend fun evaluate () {
80+ private suspend fun evaluate (languageTag : String? = lastLanguageTag ) {
7481 val current = appVersionInfo.versionCode
7582 val lastSeen = tweaksRepository.getLastSeenWhatsNewVersionCode().first() ? : Int .MIN_VALUE
7683
7784 if (lastSeen >= current) return
7885
79- val entry = whatsNewLoader.forVersionCode(current)
86+ val entry = whatsNewLoader.forVersionCode(current, languageTag )
8087 if (entry == null || ! entry.showAsSheet) {
8188 tweaksRepository.setLastSeenWhatsNewVersionCode(current)
8289 return
@@ -100,10 +107,11 @@ class WhatsNewViewModel(
100107 fun forceShowLatest () {
101108 viewModelScope.launch {
102109 try {
110+ val tag = lastLanguageTag
103111 val current = appVersionInfo.versionCode
104112 val entry =
105- whatsNewLoader.forVersionCode(current)
106- ? : whatsNewLoader.loadAll().firstOrNull()
113+ whatsNewLoader.forVersionCode(current, tag )
114+ ? : whatsNewLoader.loadAll(tag ).firstOrNull()
107115 ? : return @launch
108116 _pendingEntry .value = entry
109117 } catch (t: Throwable ) {
0 commit comments