Skip to content

Commit 6f6168e

Browse files
Merge pull request #67 from MihaiCristianCondrea/codex/create-mainuistate-data-class
Consolidate MainActivity LiveData into MainUiState
2 parents 2d2a02e + 43bd817 commit 6f6168e

File tree

3 files changed

+51
-34
lines changed

3 files changed

+51
-34
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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.d4rk.androidtutorials.java.ui.screens.main;
2+
3+
import com.google.android.material.navigation.NavigationBarView;
4+
5+
/**
6+
* 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.
9+
*/
10+
public class MainUiState {
11+
@NavigationBarView.LabelVisibility
12+
private final int bottomNavVisibility;
13+
private final int defaultNavDestination;
14+
private final boolean themeChanged;
15+
16+
public MainUiState(@NavigationBarView.LabelVisibility int bottomNavVisibility, int defaultNavDestination, boolean themeChanged) {
17+
this.bottomNavVisibility = bottomNavVisibility;
18+
this.defaultNavDestination = defaultNavDestination;
19+
this.themeChanged = themeChanged;
20+
}
21+
22+
@NavigationBarView.LabelVisibility
23+
public int getBottomNavVisibility() {
24+
return bottomNavVisibility;
25+
}
26+
27+
public int getDefaultNavDestination() {
28+
return defaultNavDestination;
29+
}
30+
31+
public boolean isThemeChanged() {
32+
return themeChanged;
33+
}
34+
}

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

Lines changed: 9 additions & 26 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,
@@ -63,8 +61,8 @@ public MainViewModel(ApplyThemeSettingsUseCase applyThemeSettingsUseCase,
6361
this.getAppUpdateManagerUseCase = getAppUpdateManagerUseCase;
6462
}
6563

66-
private static int getVisibilityMode(String labelVisibilityStr, String[] bottomNavBarLabelsValues) {
67-
int visibilityMode = NavigationBarView.LABEL_VISIBILITY_AUTO;
64+
private static @NavigationBarView.LabelVisibility int getVisibilityMode(String labelVisibilityStr, String[] bottomNavBarLabelsValues) {
65+
@NavigationBarView.LabelVisibility int visibilityMode = NavigationBarView.LABEL_VISIBILITY_AUTO;
6866
if (labelVisibilityStr.equals(bottomNavBarLabelsValues[0])) {
6967
visibilityMode = NavigationBarView.LABEL_VISIBILITY_LABELED;
7068
} else if (labelVisibilityStr.equals(bottomNavBarLabelsValues[1])) {
@@ -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();
89-
int visibilityMode = getVisibilityMode(labelVisibilityStr, bottomNavBarLabelsValues);
90-
bottomNavLabelVisibility.setValue(visibilityMode);
86+
@NavigationBarView.LabelVisibility int visibilityMode = getVisibilityMode(labelVisibilityStr, bottomNavBarLabelsValues);
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)