@@ -28,6 +28,7 @@ import org.wordpress.android.ui.mysite.MySiteCardAndItemBuilderParams.QuickActio
28
28
import org.wordpress.android.ui.mysite.MySiteCardAndItemBuilderParams.QuickStartCardBuilderParams
29
29
import org.wordpress.android.ui.mysite.MySiteCardAndItemBuilderParams.SiteInfoCardBuilderParams
30
30
import org.wordpress.android.ui.mysite.MySiteCardAndItemBuilderParams.SiteItemsBuilderParams
31
+ import org.wordpress.android.ui.mysite.MySiteSource.MySiteRefreshSource
31
32
import org.wordpress.android.ui.mysite.MySiteSource.SiteIndependentSource
32
33
import org.wordpress.android.ui.mysite.MySiteUiState.PartialState
33
34
import org.wordpress.android.ui.mysite.MySiteViewModel.State.NoSites
@@ -114,10 +115,10 @@ class MySiteViewModel @Inject constructor(
114
115
private val snackbarSequencer : SnackbarSequencer ,
115
116
private val cardsBuilder : CardsBuilder ,
116
117
private val dynamicCardsBuilder : DynamicCardsBuilder ,
117
- private val postCardsSource : PostCardsSource ,
118
- selectedSiteSource : SelectedSiteSource ,
118
+ postCardsSource : PostCardsSource ,
119
+ private val selectedSiteSource : SelectedSiteSource ,
119
120
siteIconProgressSource : SiteIconProgressSource ,
120
- mySiteDashboardPhase2FeatureConfig : MySiteDashboardPhase2FeatureConfig
121
+ private val mySiteDashboardPhase2FeatureConfig : MySiteDashboardPhase2FeatureConfig
121
122
) : ScopedViewModel(mainDispatcher) {
122
123
private val _onSnackbarMessage = MutableLiveData <Event <SnackbarMessageHolder >>()
123
124
private val _onTechInputDialogShown = MutableLiveData <Event <TextInputDialogModel >>()
@@ -126,7 +127,7 @@ class MySiteViewModel @Inject constructor(
126
127
private val _onNavigation = MutableLiveData <Event <SiteNavigationAction >>()
127
128
private val _onMediaUpload = MutableLiveData <Event <MediaModel >>()
128
129
private val _activeTaskPosition = MutableLiveData <Pair <QuickStartTask , Int >>()
129
- private val _onShowSwipeRefreshLayout = MutableLiveData (( Event (mySiteDashboardPhase2FeatureConfig.isEnabled())) )
130
+ private val _onShowSwipeRefreshLayout = MutableLiveData < Event < Boolean >>( )
130
131
131
132
val onScrollTo: LiveData <Event <Int >> = merge(
132
133
_activeTaskPosition .distinctUntilChanged(),
@@ -159,25 +160,26 @@ class MySiteViewModel @Inject constructor(
159
160
postCardsSource
160
161
)
161
162
162
- val state: LiveData <MySiteUiState > = selectedSiteRepository.siteSelected.switchMap { siteLocalId ->
163
- val result = MediatorLiveData <SiteIdToState >()
164
- val currentSources = if (siteLocalId != null ) {
165
- mySiteSources.map { source -> source.build(viewModelScope, siteLocalId).distinctUntilChanged() }
166
- } else {
167
- mySiteSources.filterIsInstance(SiteIndependentSource ::class .java)
168
- .map { source -> source.build(viewModelScope).distinctUntilChanged() }
169
- }
170
- for (newSource in currentSources) {
171
- result.addSource(newSource) { partialState ->
172
- if (partialState != null ) {
173
- result.value = (result.value ? : SiteIdToState (siteLocalId)).update(partialState)
163
+ val state: LiveData <MySiteUiState > =
164
+ selectedSiteRepository.siteSelected.switchMap { siteLocalId ->
165
+ val result = MediatorLiveData <SiteIdToState >()
166
+ val currentSources = if (siteLocalId != null ) {
167
+ mySiteSources.map { source -> source.build(viewModelScope, siteLocalId).distinctUntilChanged() }
168
+ } else {
169
+ mySiteSources.filterIsInstance(SiteIndependentSource ::class .java)
170
+ .map { source -> source.build(viewModelScope).distinctUntilChanged() }
171
+ }
172
+ for (newSource in currentSources) {
173
+ result.addSource(newSource) { partialState ->
174
+ if (partialState != null ) {
175
+ result.value = (result.value ? : SiteIdToState (siteLocalId)).update(partialState)
176
+ }
174
177
}
175
178
}
176
- }
177
- // We want to filter out the empty state where we have a site ID but site object is missing.
178
- // Without this check there is an emission of a NoSites state even if we have the site
179
- result.filter { it.siteId == null || it.state.site != null }.map { it.state }
180
- }.distinctUntilChanged()
179
+ // We want to filter out the empty state where we have a site ID but site object is missing.
180
+ // Without this check there is an emission of a NoSites state even if we have the site
181
+ result.filter { it.siteId == null || it.state.site != null }.map { it.state }
182
+ }.distinctUntilChanged()
181
183
182
184
val uiModel: LiveData <UiModel > = state.map { (
183
185
currentAvatarUrl,
@@ -483,11 +485,36 @@ class MySiteViewModel @Inject constructor(
483
485
}
484
486
485
487
fun refresh () {
488
+ if (mySiteDashboardPhase2FeatureConfig.isEnabled()) {
489
+ refreshNew()
490
+ } else {
491
+ refreshOld()
492
+ }
493
+ }
494
+
495
+ private fun refreshNew () {
496
+ mySiteSources.filterIsInstance(MySiteRefreshSource ::class .java).forEach { it.refresh() }
497
+ }
498
+
499
+ private fun refreshOld () {
486
500
selectedSiteRepository.updateSiteSettingsIfNecessary()
487
501
quickStartCardSource.refresh()
488
502
currentAvatarSource.refresh()
489
503
}
490
504
505
+ fun onResume (isFirstResume : Boolean ) {
506
+ when (isFirstResume) {
507
+ true -> refreshOld()
508
+ false -> if (mySiteDashboardPhase2FeatureConfig.isEnabled()) {
509
+ refreshNew()
510
+ } else {
511
+ refreshOld()
512
+ }
513
+ }
514
+ checkAndShowQuickStartNotice()
515
+ _onShowSwipeRefreshLayout .postValue(Event (mySiteDashboardPhase2FeatureConfig.isEnabled()))
516
+ }
517
+
491
518
fun clearActiveQuickStartTask () {
492
519
quickStartRepository.clearActiveTask()
493
520
}
@@ -671,6 +698,7 @@ class MySiteViewModel @Inject constructor(
671
698
domainRegistrationSource.clear()
672
699
quickStartRepository.clear()
673
700
scanAndBackupSource.clear()
701
+ selectedSiteSource.clear()
674
702
super .onCleared()
675
703
}
676
704
@@ -738,10 +766,6 @@ class MySiteViewModel @Inject constructor(
738
766
analyticsTrackerWrapper.track(Stat .QUICK_START_REQUEST_DIALOG_NEGATIVE_TAPPED )
739
767
}
740
768
741
- fun onPullToRefresh () {
742
- postCardsSource.refresh()
743
- }
744
-
745
769
private fun onPostItemClick (postId : Int ) {
746
770
selectedSiteRepository.getSelectedSite()?.let { site ->
747
771
_onNavigation .value = Event (SiteNavigationAction .EditPost (site, postId))
@@ -759,6 +783,19 @@ class MySiteViewModel @Inject constructor(
759
783
}
760
784
}
761
785
786
+ fun isRefreshing () = areSourcesRefreshing()
787
+
788
+ private fun areSourcesRefreshing (): Boolean {
789
+ if (mySiteDashboardPhase2FeatureConfig.isEnabled()) {
790
+ mySiteSources.filterIsInstance(MySiteRefreshSource ::class .java).forEach {
791
+ if (it.isRefreshing() == true ) {
792
+ return true
793
+ }
794
+ }
795
+ }
796
+ return false
797
+ }
798
+
762
799
fun setActionableEmptyViewGone (isVisible : Boolean , setGone : () -> Unit ) {
763
800
if (isVisible) analyticsTrackerWrapper.track(Stat .MY_SITE_NO_SITES_VIEW_HIDDEN )
764
801
setGone()
0 commit comments