From 1b254adc28ed06e4266ff21e74f95c73dd573d02 Mon Sep 17 00:00:00 2001 From: Irfan Omur Date: Mon, 29 Jan 2024 20:55:04 +0300 Subject: [PATCH 1/3] Make dateSelector parameter nullable in `StatsViewAllViewModel` --- .../ui/stats/refresh/StatsViewAllFragment.kt | 2 +- .../ui/stats/refresh/StatsViewAllViewModel.kt | 24 +++++------ .../refresh/StatsViewAllViewModelFactory.kt | 40 ++++++++----------- 3 files changed, 30 insertions(+), 36 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/StatsViewAllFragment.kt b/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/StatsViewAllFragment.kt index 952113eb5ee0..65af0a803741 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/StatsViewAllFragment.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/StatsViewAllFragment.kt @@ -240,7 +240,7 @@ class StatsViewAllFragment : Fragment(R.layout.stats_view_all_fragment) { navigator.navigate(activity, target) }) - viewModel.selectedDate.observe(viewLifecycleOwner, { event -> + viewModel.selectedDate?.observe(viewLifecycleOwner, { event -> if (event != null) { viewModel.onDateChanged() } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/StatsViewAllViewModel.kt b/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/StatsViewAllViewModel.kt index 57785b26a6be..73de971c26d3 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/StatsViewAllViewModel.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/StatsViewAllViewModel.kt @@ -20,8 +20,8 @@ import org.wordpress.android.ui.stats.refresh.lists.sections.granular.SelectedDa import org.wordpress.android.ui.stats.refresh.utils.StatsDateSelector import org.wordpress.android.ui.stats.refresh.utils.StatsSiteProvider import org.wordpress.android.ui.utils.UiString.UiStringRes -import org.wordpress.android.util.mapSafe import org.wordpress.android.util.mapNullable +import org.wordpress.android.util.mapSafe import org.wordpress.android.util.throttle import org.wordpress.android.viewmodel.Event import org.wordpress.android.viewmodel.ScopedViewModel @@ -31,14 +31,14 @@ class StatsViewAllViewModel( val bgDispatcher: CoroutineDispatcher, val useCase: BaseStatsUseCase<*, *>, private val statsSiteProvider: StatsSiteProvider, - private val dateSelector: StatsDateSelector, + private val dateSelector: StatsDateSelector?, @StringRes val title: Int ) : ScopedViewModel(mainDispatcher) { - val selectedDate = dateSelector.selectedDate + val selectedDate = dateSelector?.selectedDate - val dateSelectorData: LiveData = dateSelector.dateSelectorData.mapNullable { + val dateSelectorData: LiveData = dateSelector?.dateSelectorData?.mapNullable { it ?: DateSelectorUiModel(false) - } + } ?: MutableLiveData(DateSelectorUiModel(false)) val navigationTarget: LiveData> = useCase.navigationTarget @@ -62,12 +62,12 @@ class StatsViewAllViewModel( fun start(startDate: SelectedDate?) { launch { startDate?.let { - dateSelector.start(startDate) + dateSelector?.start(startDate) } loadData(refresh = false, forced = false) - dateSelector.updateDateSelector() + dateSelector?.updateDateSelector() } - dateSelector.updateDateSelector() + dateSelector?.updateDateSelector() } @SuppressLint("NullSafeMutableLiveData") @@ -109,13 +109,13 @@ class StatsViewAllViewModel( fun onNextDateSelected() { launch(mainDispatcher) { - dateSelector.onNextDateSelected() + dateSelector?.onNextDateSelected() } } fun onPreviousDateSelected() { launch(mainDispatcher) { - dateSelector.onPreviousDateSelected() + dateSelector?.onPreviousDateSelected() } } @@ -131,7 +131,7 @@ class StatsViewAllViewModel( } } - fun getSelectedDate(): SelectedDate { - return dateSelector.getSelectedDate() + fun getSelectedDate(): SelectedDate? { + return dateSelector?.getSelectedDate() } } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/StatsViewAllViewModelFactory.kt b/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/StatsViewAllViewModelFactory.kt index 167735228b8c..463afd2eee92 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/StatsViewAllViewModelFactory.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/StatsViewAllViewModelFactory.kt @@ -30,7 +30,6 @@ import org.wordpress.android.ui.stats.StatsViewType.TAGS_AND_CATEGORIES import org.wordpress.android.ui.stats.StatsViewType.TOP_POSTS_AND_PAGES import org.wordpress.android.ui.stats.StatsViewType.VIDEO_PLAYS import org.wordpress.android.ui.stats.refresh.lists.StatsListViewModel.StatsSection -import org.wordpress.android.ui.stats.refresh.lists.StatsListViewModel.StatsSection.INSIGHTS import org.wordpress.android.ui.stats.refresh.lists.detail.PostAverageViewsPerDayUseCase import org.wordpress.android.ui.stats.refresh.lists.detail.PostMonthsAndYearsUseCase import org.wordpress.android.ui.stats.refresh.lists.detail.PostRecentWeeksUseCase @@ -66,7 +65,7 @@ class StatsViewAllViewModelFactory( private val bgDispatcher: CoroutineDispatcher, private val useCase: BaseStatsUseCase<*, *>, private val statsSiteProvider: StatsSiteProvider, - private val dateSelector: StatsDateSelector, + private val dateSelector: StatsDateSelector?, @StringRes private val titleResource: Int ) : ViewModelProvider.Factory { override fun create(modelClass: Class): T { @@ -92,34 +91,29 @@ class StatsViewAllViewModelFactory( private val statsSiteProvider: StatsSiteProvider, private val dateSelectorFactory: StatsDateSelector.Factory ) { - fun build(type: StatsViewType, granularity: StatsGranularity?): StatsViewAllViewModelFactory { - return when { - type == ANNUAL_STATS -> buildAnnualStatsFactory() - granularity == null -> buildFactory(type) - else -> buildFactory(type, granularity) - } - } - - private fun buildFactory(type: StatsViewType, granularity: StatsGranularity): StatsViewAllViewModelFactory { - val (useCase, title) = getGranularUseCase(type, granularity, granularFactories) - return StatsViewAllViewModelFactory( - mainDispatcher, - bgDispatcher, - useCase, - statsSiteProvider, - dateSelectorFactory.build(granularity.toStatsSection()), - title - ) + fun build(type: StatsViewType, granularity: StatsGranularity?) = if (type == ANNUAL_STATS) { + buildAnnualStatsFactory() + } else { + buildFactory(type, granularity) } - private fun buildFactory(type: StatsViewType): StatsViewAllViewModelFactory { - val (useCase, title) = getInsightsUseCase(type, insightsUseCases) + private fun buildFactory(type: StatsViewType, granularity: StatsGranularity?): StatsViewAllViewModelFactory { + val (useCase, title) = if (granularity == null) { + getInsightsUseCase(type, insightsUseCases) + } else { + getGranularUseCase(type, granularity, granularFactories) + } + val dateSelector = if (granularity == null) { + null + } else { + dateSelectorFactory.build(granularity.toStatsSection()) + } return StatsViewAllViewModelFactory( mainDispatcher, bgDispatcher, useCase, statsSiteProvider, - dateSelectorFactory.build(INSIGHTS), + dateSelector, title ) } From 71322d6c4008a89debc793912a961d9b244d7231 Mon Sep 17 00:00:00 2001 From: Irfan Omur Date: Mon, 29 Jan 2024 20:59:37 +0300 Subject: [PATCH 2/3] Move lambda arguments out of parentheses in `StatsViewAllFragment` --- .../ui/stats/refresh/StatsViewAllFragment.kt | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/StatsViewAllFragment.kt b/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/StatsViewAllFragment.kt index 65af0a803741..80d8688c73a7 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/StatsViewAllFragment.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/StatsViewAllFragment.kt @@ -191,17 +191,17 @@ class StatsViewAllFragment : Fragment(R.layout.stats_view_all_fragment) { } private fun StatsViewAllFragmentBinding.setupObservers(activity: FragmentActivity) { - viewModel.isRefreshing.observe(viewLifecycleOwner, { + viewModel.isRefreshing.observe(viewLifecycleOwner) { it?.let { isRefreshing -> swipeToRefreshHelper.isRefreshing = isRefreshing } - }) + } - viewModel.showSnackbarMessage.observeEvent(viewLifecycleOwner, { holder -> + viewModel.showSnackbarMessage.observeEvent(viewLifecycleOwner) { holder -> showSnackbar(activity, holder) - }) + } - viewModel.data.observe(viewLifecycleOwner, { + viewModel.data.observe(viewLifecycleOwner) { if (it != null) { with(statsListFragment) { recyclerView.visibility = if (it is Success) View.VISIBLE else View.GONE @@ -214,41 +214,44 @@ class StatsViewAllFragment : Fragment(R.layout.stats_view_all_fragment) { is Success -> { loadData(recyclerView, prepareLayout(it.data, it.type)) } + is Loading -> { loadData(loadingRecyclerView, prepareLayout(it.data, it.type)) } + is Error -> { errorView.statsErrorView.button.setOnClickListener { viewModel.onRetryClick() } } + is EmptyBlock -> { } } } } - }) - viewModel.navigationTarget.observeEvent(viewLifecycleOwner, { target -> + } + viewModel.navigationTarget.observeEvent(viewLifecycleOwner) { target -> navigator.navigate(activity, target) - }) + } - viewModel.dateSelectorData.observe(viewLifecycleOwner, { dateSelectorUiModel -> + viewModel.dateSelectorData.observe(viewLifecycleOwner) { dateSelectorUiModel -> statsListFragment.drawDateSelector(dateSelectorUiModel) - }) + } - viewModel.navigationTarget.observeEvent(viewLifecycleOwner, { target -> + viewModel.navigationTarget.observeEvent(viewLifecycleOwner) { target -> navigator.navigate(activity, target) - }) + } - viewModel.selectedDate?.observe(viewLifecycleOwner, { event -> + viewModel.selectedDate?.observe(viewLifecycleOwner) { event -> if (event != null) { viewModel.onDateChanged() } - }) + } - viewModel.toolbarHasShadow.observe(viewLifecycleOwner, { hasShadow -> + viewModel.toolbarHasShadow.observe(viewLifecycleOwner) { hasShadow -> appBarLayout.showShadow(hasShadow == true) - }) + } } private fun showSnackbar( From 0a04666daf19c672b33caf416682eae748a945ed Mon Sep 17 00:00:00 2001 From: Irfan Omur Date: Tue, 30 Jan 2024 16:39:16 +0300 Subject: [PATCH 3/3] Make dateSelector parameter nullable in `StatsListViewModel` --- .../stats/refresh/lists/StatsListFragment.kt | 2 +- .../stats/refresh/lists/StatsListViewModel.kt | 18 +++++++++--------- .../lists/detail/DetailListViewModel.kt | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/lists/StatsListFragment.kt b/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/lists/StatsListFragment.kt index 8169b738f924..30ca269635ce 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/lists/StatsListFragment.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/lists/StatsListFragment.kt @@ -243,7 +243,7 @@ class StatsListFragment : ViewPagerFragment(R.layout.stats_list_fragment) { navigator.navigate(activity, target) } - viewModel.selectedDate.observe(viewLifecycleOwner) { event -> + viewModel.selectedDate?.observe(viewLifecycleOwner) { event -> if (event != null) { viewModel.onDateChanged(event.selectedSection) } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/lists/StatsListViewModel.kt b/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/lists/StatsListViewModel.kt index 386a6575a3a7..a3b14286a6f8 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/lists/StatsListViewModel.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/lists/StatsListViewModel.kt @@ -49,7 +49,7 @@ abstract class StatsListViewModel( defaultDispatcher: CoroutineDispatcher, private val statsUseCase: BaseListUseCase, private val analyticsTracker: AnalyticsTrackerWrapper, - protected val dateSelector: StatsDateSelector, + protected val dateSelector: StatsDateSelector?, popupMenuHandler: ItemPopupMenuHandler? = null, private val newsCardHandler: NewsCardHandler? = null, actionCardHandler: ActionCardHandler? = null @@ -72,7 +72,7 @@ abstract class StatsListViewModel( ANNUAL_STATS(R.string.stats_insights_annual_site_stats); } - val selectedDate = dateSelector.selectedDate + val selectedDate = dateSelector?.selectedDate private val mutableNavigationTarget = MutableLiveData>() val navigationTarget: LiveData> = mergeNotNull( @@ -85,9 +85,9 @@ abstract class StatsListViewModel( statsUseCase.data.throttle(viewModelScope, distinct = true) } - val dateSelectorData: LiveData = dateSelector.dateSelectorData.mapNullable { + val dateSelectorData: LiveData = dateSelector?.dateSelectorData?.mapNullable { it ?: DateSelectorUiModel(false) - } + } ?: MutableLiveData(DateSelectorUiModel(false)) val typesChanged = merge( popupMenuHandler?.typeMoved, @@ -115,13 +115,13 @@ abstract class StatsListViewModel( fun onNextDateSelected() { launch(Dispatchers.Default) { - dateSelector.onNextDateSelected() + dateSelector?.onNextDateSelected() } } fun onPreviousDateSelected() { launch(Dispatchers.Default) { - dateSelector.onPreviousDateSelected() + dateSelector?.onPreviousDateSelected() } } @@ -138,7 +138,7 @@ abstract class StatsListViewModel( } fun onListSelected() { - dateSelector.updateDateSelector() + dateSelector?.updateDateSelector() } fun onEmptyInsightsButtonClicked() { @@ -156,10 +156,10 @@ abstract class StatsListViewModel( isInitialized = true launch { statsUseCase.loadData() - dateSelector.updateDateSelector() + dateSelector?.updateDateSelector() } } - dateSelector.updateDateSelector() + dateSelector?.updateDateSelector() } sealed class UiModel { diff --git a/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/lists/detail/DetailListViewModel.kt b/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/lists/detail/DetailListViewModel.kt index 553ff06d2997..23199d7b74f4 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/lists/detail/DetailListViewModel.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/lists/detail/DetailListViewModel.kt @@ -20,6 +20,6 @@ class DetailListViewModel ) : StatsListViewModel(mainDispatcher, detailUseCase, analyticsTracker, dateSelectorFactory.build(DETAIL)) { override fun onCleared() { super.onCleared() - dateSelector.clear() + dateSelector?.clear() } }