4
4
import android .content .Intent ;
5
5
import android .net .Uri ;
6
6
7
- import androidx .lifecycle .ViewModel ;
8
7
import androidx .lifecycle .LiveData ;
9
8
import androidx .lifecycle .MutableLiveData ;
9
+ import androidx .lifecycle .ViewModel ;
10
10
11
11
import com .d4rk .androidtutorials .java .R ;
12
12
import com .d4rk .androidtutorials .java .data .model .PromotedApp ;
@@ -29,10 +29,7 @@ public class HomeViewModel extends ViewModel {
29
29
private final GetDailyTipUseCase getDailyTipUseCase ;
30
30
private final GetPromotedAppsUseCase getPromotedAppsUseCase ;
31
31
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 <>();
36
33
37
34
@ Inject
38
35
public HomeViewModel (Application application ,
@@ -44,43 +41,45 @@ public HomeViewModel(Application application,
44
41
this .getDailyTipUseCase = getDailyTipUseCase ;
45
42
this .getPromotedAppsUseCase = getPromotedAppsUseCase ;
46
43
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 );
50
51
51
52
getPromotedAppsUseCase .invoke (apps -> {
53
+ List <PromotedApp > result ;
52
54
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
+ }
55
62
}
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
+ );
60
73
}
61
- promotedApps .postValue (rotated );
74
+ uiState .postValue (current );
62
75
});
63
76
}
64
77
65
78
/**
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.
81
80
*/
82
- public LiveData <String > getDailyTip () {
83
- return dailyTip ;
81
+ public LiveData <HomeUiState > getUiState () {
82
+ return uiState ;
84
83
}
85
84
86
85
/**
@@ -91,13 +90,6 @@ public Intent getOpenPlayStoreIntent() {
91
90
return buildPlayStoreIntent (homeRepository .getPlayStoreUrl ());
92
91
}
93
92
94
- /**
95
- * List of apps to promote on the Home screen.
96
- */
97
- public LiveData <List <PromotedApp >> getPromotedApps () {
98
- return promotedApps ;
99
- }
100
-
101
93
/**
102
94
* Builds an intent to open the Google Play listing for the provided package.
103
95
*/
0 commit comments