Skip to content

Commit e5d5713

Browse files
Introduce consolidated MainUiState
1 parent 2d2a02e commit e5d5713

File tree

3 files changed

+44
-31
lines changed

3 files changed

+44
-31
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,17 @@ private void launcherShortcuts() {
185185
}
186186

187187
private void observeViewModel() {
188-
mainViewModel.getBottomNavVisibility().observe(this, visibilityMode -> {
188+
mainViewModel.getUiState().observe(this, uiState -> {
189+
if (uiState == null) {
190+
return;
191+
}
192+
189193
EdgeToEdgeDelegate edgeToEdgeDelegate = new EdgeToEdgeDelegate(this);
190194
if (mBinding.navView instanceof BottomNavigationView) {
191195

192196
edgeToEdgeDelegate.applyEdgeToEdgeBottomBar(mBinding.container, mBinding.navView);
193197

194-
((BottomNavigationView) mBinding.navView).setLabelVisibilityMode(visibilityMode);
198+
((BottomNavigationView) mBinding.navView).setLabelVisibilityMode(uiState.getBottomNavVisibility());
195199
if (mBinding.adView != null) {
196200
if (ConsentUtils.canShowAds(this)) {
197201
MobileAds.initialize(this);
@@ -204,15 +208,13 @@ private void observeViewModel() {
204208
} else {
205209
edgeToEdgeDelegate.applyEdgeToEdge(mBinding.container);
206210
}
207-
});
208211

209-
mainViewModel.getDefaultNavDestination().observe(this, startFragmentId -> {
210212
NavHostFragment navHostFragment = (NavHostFragment)
211213
getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment_activity_main);
212214
if (navHostFragment != null) {
213215
navController = navHostFragment.getNavController();
214216
NavGraph navGraph = navController.getNavInflater().inflate(R.navigation.mobile_navigation);
215-
navGraph.setStartDestination(startFragmentId);
217+
navGraph.setStartDestination(uiState.getDefaultNavDestination());
216218
navController.setGraph(navGraph);
217219

218220
if (mBinding.navView instanceof BottomNavigationView bottomNav) {
@@ -228,10 +230,8 @@ private void observeViewModel() {
228230
bottomSheet.show(getSupportFragmentManager(), bottomSheet.getTag());
229231
});
230232
}
231-
});
232233

233-
mainViewModel.getThemeChanged().observe(this, changed -> {
234-
if (Boolean.TRUE.equals(changed)) {
234+
if (uiState.isThemeChanged()) {
235235
recreate();
236236
}
237237
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.d4rk.androidtutorials.java.ui.screens.main;
2+
3+
/**
4+
* UI state for {@link MainActivity}. Holds values related to the main screen such as
5+
* bottom navigation visibility, the default navigation destination, and whether the theme
6+
* has changed requiring a recreation of the activity.
7+
*/
8+
public class MainUiState {
9+
private final int bottomNavVisibility;
10+
private final int defaultNavDestination;
11+
private final boolean themeChanged;
12+
13+
public MainUiState(int bottomNavVisibility, int defaultNavDestination, boolean themeChanged) {
14+
this.bottomNavVisibility = bottomNavVisibility;
15+
this.defaultNavDestination = defaultNavDestination;
16+
this.themeChanged = themeChanged;
17+
}
18+
19+
public int getBottomNavVisibility() {
20+
return bottomNavVisibility;
21+
}
22+
23+
public int getDefaultNavDestination() {
24+
return defaultNavDestination;
25+
}
26+
27+
public boolean isThemeChanged() {
28+
return themeChanged;
29+
}
30+
}

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

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ public class MainViewModel extends ViewModel {
3838
private final IsAppInstalledUseCase isAppInstalledUseCase;
3939
private final BuildShortcutIntentUseCase buildShortcutIntentUseCase;
4040
private final GetAppUpdateManagerUseCase getAppUpdateManagerUseCase;
41-
private final MutableLiveData<Integer> bottomNavLabelVisibility = new MutableLiveData<>();
42-
private final MutableLiveData<Integer> defaultNavDestination = new MutableLiveData<>();
43-
private final MutableLiveData<Boolean> themeChanged = new MutableLiveData<>();
41+
private final MutableLiveData<MainUiState> uiState = new MutableLiveData<>();
4442

4543
@Inject
4644
public MainViewModel(ApplyThemeSettingsUseCase applyThemeSettingsUseCase,
@@ -83,11 +81,9 @@ public void applySettings(String[] themeValues,
8381
String[] bottomNavBarLabelsValues,
8482
String[] defaultTabValues) {
8583
boolean changedTheme = applyThemeSettingsUseCase.invoke(themeValues);
86-
themeChanged.setValue(changedTheme);
8784

8885
String labelVisibilityStr = getBottomNavLabelVisibilityUseCase.invoke();
8986
int visibilityMode = getVisibilityMode(labelVisibilityStr, bottomNavBarLabelsValues);
90-
bottomNavLabelVisibility.setValue(visibilityMode);
9187

9288
String startFragmentIdValue = getDefaultTabPreferenceUseCase.invoke();
9389
int startFragmentId;
@@ -100,7 +96,8 @@ public void applySettings(String[] themeValues,
10096
} else {
10197
startFragmentId = R.id.navigation_home;
10298
}
103-
defaultNavDestination.setValue(startFragmentId);
99+
100+
uiState.setValue(new MainUiState(visibilityMode, startFragmentId, changedTheme));
104101
applyLanguageSettingsUseCase.invoke();
105102
}
106103

@@ -133,24 +130,10 @@ public Intent getShortcutIntent(boolean isInstalled) {
133130
}
134131

135132
/**
136-
* Expose the bottom nav visibility as LiveData, so MainActivity can observe it.
137-
*/
138-
public LiveData<Integer> getBottomNavVisibility() {
139-
return bottomNavLabelVisibility;
140-
}
141-
142-
/**
143-
* Expose the default nav destination as LiveData, so MainActivity can observe it.
144-
*/
145-
public LiveData<Integer> getDefaultNavDestination() {
146-
return defaultNavDestination;
147-
}
148-
149-
/**
150-
* This tells the UI whether the theme changed so it can decide to recreate if necessary.
133+
* Expose the consolidated UI state so MainActivity can observe it.
151134
*/
152-
public LiveData<Boolean> getThemeChanged() {
153-
return themeChanged;
135+
public LiveData<MainUiState> getUiState() {
136+
return uiState;
154137
}
155138

156139
/**

0 commit comments

Comments
 (0)