44import android .content .Intent ;
55import android .net .Uri ;
66
7- import androidx .lifecycle .ViewModel ;
87import androidx .lifecycle .LiveData ;
98import androidx .lifecycle .MutableLiveData ;
9+ import androidx .lifecycle .ViewModel ;
1010
1111import com .d4rk .androidtutorials .java .R ;
1212import com .d4rk .androidtutorials .java .data .model .PromotedApp ;
@@ -29,10 +29,7 @@ public class HomeViewModel extends ViewModel {
2929 private final GetDailyTipUseCase getDailyTipUseCase ;
3030 private final GetPromotedAppsUseCase getPromotedAppsUseCase ;
3131
32- private final MutableLiveData <String > announcementTitle = new MutableLiveData <>();
33- private final MutableLiveData <String > announcementSubtitle = new MutableLiveData <>();
34- private final MutableLiveData <String > dailyTip = new MutableLiveData <>();
35- private final MutableLiveData <List <PromotedApp >> promotedApps = new MutableLiveData <>(new ArrayList <>());
32+ private final MutableLiveData <HomeUiState > uiState = new MutableLiveData <>();
3633
3734 @ Inject
3835 public HomeViewModel (Application application ,
@@ -44,43 +41,45 @@ public HomeViewModel(Application application,
4441 this .getDailyTipUseCase = getDailyTipUseCase ;
4542 this .getPromotedAppsUseCase = getPromotedAppsUseCase ;
4643
47- announcementTitle .setValue (application .getString (R .string .announcement_title ));
48- announcementSubtitle .setValue (application .getString (R .string .announcement_subtitle ));
49- dailyTip .setValue (getDailyTipUseCase .invoke ());
44+ HomeUiState initialState = new HomeUiState (
45+ application .getString (R .string .announcement_title ),
46+ application .getString (R .string .announcement_subtitle ),
47+ getDailyTipUseCase .invoke (),
48+ new ArrayList <>()
49+ );
50+ uiState .setValue (initialState );
5051
5152 getPromotedAppsUseCase .invoke (apps -> {
53+ List <PromotedApp > result ;
5254 if (apps .isEmpty ()) {
53- promotedApps .postValue (apps );
54- return ;
55+ result = apps ;
56+ } else {
57+ int startIndex = (int ) ((System .currentTimeMillis () / (24L * 60 * 60 * 1000 )) % apps .size ());
58+ result = new ArrayList <>();
59+ for (int i = 0 ; i < Math .min (4 , apps .size ()); i ++) {
60+ result .add (apps .get ((startIndex + i ) % apps .size ()));
61+ }
5562 }
56- int startIndex = (int ) ((System .currentTimeMillis () / (24L * 60 * 60 * 1000 )) % apps .size ());
57- List <PromotedApp > rotated = new ArrayList <>();
58- for (int i = 0 ; i < Math .min (4 , apps .size ()); i ++) {
59- rotated .add (apps .get ((startIndex + i ) % apps .size ()));
63+ HomeUiState current = uiState .getValue ();
64+ if (current == null ) {
65+ current = new HomeUiState ("" , "" , "" , result );
66+ } else {
67+ current = new HomeUiState (
68+ current .announcementTitle (),
69+ current .announcementSubtitle (),
70+ current .dailyTip (),
71+ result
72+ );
6073 }
61- promotedApps .postValue (rotated );
74+ uiState .postValue (current );
6275 });
6376 }
6477
6578 /**
66- * Provides a LiveData for the announcement title.
67- */
68- public LiveData <String > getAnnouncementTitle () {
69- return announcementTitle ;
70- }
71-
72- /**
73- * Provides a LiveData for the announcement subtitle.
74- */
75- public LiveData <String > getAnnouncementSubtitle () {
76- return announcementSubtitle ;
77- }
78-
79- /**
80- * Provides a LiveData for the tip of the day text.
79+ * Exposes the UI state for the Home screen.
8180 */
82- public LiveData <String > getDailyTip () {
83- return dailyTip ;
81+ public LiveData <HomeUiState > getUiState () {
82+ return uiState ;
8483 }
8584
8685 /**
@@ -91,13 +90,6 @@ public Intent getOpenPlayStoreIntent() {
9190 return buildPlayStoreIntent (homeRepository .getPlayStoreUrl ());
9291 }
9392
94- /**
95- * List of apps to promote on the Home screen.
96- */
97- public LiveData <List <PromotedApp >> getPromotedApps () {
98- return promotedApps ;
99- }
100-
10193 /**
10294 * Builds an intent to open the Google Play listing for the provided package.
10395 */
0 commit comments