Skip to content

Commit 4c9f719

Browse files
Ensure settings updates trigger main UI recreation
1 parent 28afd9b commit 4c9f719

File tree

5 files changed

+21
-8
lines changed

5 files changed

+21
-8
lines changed

app/src/main/java/com/d4rk/androidtutorials/java/data/repository/DefaultMainRepository.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,23 @@ public void markStartupScreenShown() {
100100
/**
101101
* Applies the language setting from SharedPreferences.
102102
*/
103-
public void applyLanguageSettings() {
103+
public boolean applyLanguageSettings() {
104104
String languageKey = context.getString(R.string.key_language);
105105
String defaultLanguageValue = context.getString(R.string.default_value_language);
106106
String languageCode = defaultSharedPrefs.getString(languageKey, defaultLanguageValue);
107107

108+
if (languageCode == null) {
109+
languageCode = defaultLanguageValue;
110+
}
111+
112+
LocaleListCompat currentLocales = AppCompatDelegate.getApplicationLocales();
113+
String currentLanguageTags = currentLocales.toLanguageTags();
114+
if (languageCode.equals(currentLanguageTags)) {
115+
return false;
116+
}
117+
108118
AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags(languageCode));
119+
return true;
109120
}
110121

111122
/**

app/src/main/java/com/d4rk/androidtutorials/java/data/repository/MainRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public interface MainRepository {
1313

1414
void markStartupScreenShown();
1515

16-
void applyLanguageSettings();
16+
boolean applyLanguageSettings();
1717

1818
AppUpdateManager getAppUpdateManager();
1919
}

app/src/main/java/com/d4rk/androidtutorials/java/domain/main/ApplyLanguageSettingsUseCase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public ApplyLanguageSettingsUseCase(MainRepository repository) {
1212
this.repository = repository;
1313
}
1414

15-
public void invoke() {
16-
repository.applyLanguageSettings();
15+
public boolean invoke() {
16+
return repository.applyLanguageSettings();
1717
}
1818
}

app/src/main/java/com/d4rk/androidtutorials/java/ui/screens/main/MainUiState.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
/**
66
* UI state for {@link MainActivity}. Holds values related to the main screen such as
7-
* bottom navigation visibility, the default navigation destination, and whether the theme
8-
* has changed requiring a recreation of the activity.
7+
* bottom navigation visibility, the default navigation destination, and whether the configuration
8+
* has changed (e.g., theme or language) requiring a recreation of the activity.
99
*/
1010
public record MainUiState(@NavigationBarView.LabelVisibility int bottomNavVisibility,
1111
int defaultNavDestination, boolean themeChanged) {

app/src/main/java/com/d4rk/androidtutorials/java/ui/screens/main/MainViewModel.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,10 @@ public void applySettings(String[] themeValues,
9191
startFragmentId = R.id.navigation_home;
9292
}
9393

94-
uiState.setValue(new MainUiState(visibilityMode, startFragmentId, changedTheme));
95-
applyLanguageSettingsUseCase.invoke();
94+
boolean changedLanguage = applyLanguageSettingsUseCase.invoke();
95+
boolean requiresRecreation = changedTheme || changedLanguage;
96+
97+
uiState.setValue(new MainUiState(visibilityMode, startFragmentId, requiresRecreation));
9698
isLoading.setValue(false);
9799
}
98100

0 commit comments

Comments
 (0)