Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class StatsModule {
mainDispatcher,
statsSiteProvider,
useCases,
{ statsStore.getInsights() },
{ statsStore.getInsights(statsSiteProvider.siteModel) },
uiModelMapper::mapInsights
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class BaseListUseCase(
useCases.associateBy { it.type }.mapValues { entry -> entry.value.liveData }
)
private val statsTypes = DistinctMutableLiveData<List<StatsTypes>>(listOf())
val data: MediatorLiveData<UiModel> = mergeNotNull(statsTypes, blockListData) { insights, map ->
insights.mapNotNull {
val data: MediatorLiveData<UiModel> = mergeNotNull(statsTypes, blockListData) { types, map ->
types.mapNotNull {
if (map.containsKey(it)) {
map[it]
} else {
Expand Down Expand Up @@ -69,16 +69,26 @@ class BaseListUseCase(
loadData(true, forced)
}

suspend fun refreshTypes(): List<StatsTypes> {
val items = getStatsTypes()
withContext(mainDispatcher) {
statsTypes.value = items
}
return items
}

private suspend fun loadData(refresh: Boolean, forced: Boolean) {
if (statsSiteProvider.hasLoadedSite()) {
withContext(bgDispatcher) {
if (PackageUtils.isDebugBuild() && useCases.distinctBy { it.type }.size < useCases.size) {
throw RuntimeException("Duplicate stats type in a use case")
}
useCases.forEach { block -> launch { block.fetch(refresh, forced) } }
val items = getStatsTypes()
withContext(mainDispatcher) {
statsTypes.value = items
val visibleTypes = refreshTypes()
visibleTypes.forEach { type ->
useCases.find { it.type == type }
?.let { block -> launch(bgDispatcher) {
block.fetch(refresh, forced) }
}
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.wordpress.android.ui.stats.refresh.lists

import android.support.v7.util.DiffUtil.Callback
import org.wordpress.android.ui.stats.refresh.lists.StatsBlock.Success

class StatsBlockDiffCallback(
private val oldList: List<StatsBlock>,
Expand Down Expand Up @@ -30,7 +29,7 @@ class StatsBlockDiffCallback(
override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
val newItem = newList[newItemPosition]
val oldItem = oldList[oldItemPosition]
if (oldItem is Success && newItem is Success) {
if (oldItem.type == newItem.type) {
return Payload
}
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ class StatsListFragment : DaggerFragment() {
viewModel.listSelected.observe(this, Observer {
viewModel.onListSelected()
})

viewModel.typeMoved?.observeEvent(this) {
viewModel.onTypeMoved()
true
}
}

private fun updateInsights(statsState: List<StatsBlock>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.wordpress.android.ui.stats.refresh.lists.StatsListViewModel.StatsSect
import org.wordpress.android.ui.stats.refresh.lists.StatsListViewModel.StatsSection.MONTHS
import org.wordpress.android.ui.stats.refresh.lists.StatsListViewModel.StatsSection.WEEKS
import org.wordpress.android.ui.stats.refresh.lists.StatsListViewModel.StatsSection.YEARS
import org.wordpress.android.ui.stats.refresh.utils.ItemPopupMenuHandler
import org.wordpress.android.ui.stats.refresh.utils.StatsDateSelector
import org.wordpress.android.util.analytics.AnalyticsTrackerWrapper
import org.wordpress.android.util.mapNullable
Expand All @@ -36,7 +37,8 @@ abstract class StatsListViewModel(
defaultDispatcher: CoroutineDispatcher,
private val statsUseCase: BaseListUseCase,
private val analyticsTracker: AnalyticsTrackerWrapper,
private val dateSelector: StatsDateSelector
private val dateSelector: StatsDateSelector,
private val popupMenuHandler: ItemPopupMenuHandler? = null
) : ScopedViewModel(defaultDispatcher) {
private var trackJob: Job? = null
private var isInitialized = false
Expand All @@ -62,6 +64,8 @@ abstract class StatsListViewModel(
it ?: DateSelectorUiModel(false)
}

val typeMoved = popupMenuHandler?.typeMoved

override fun onCleared() {
statsUseCase.onCleared()
super.onCleared()
Expand Down Expand Up @@ -119,15 +123,28 @@ abstract class StatsListViewModel(
data class Success(val data: List<StatsBlock>) : UiModel()
class Error(val message: Int = R.string.stats_loading_error) : UiModel()
}

fun onTypeMoved() {
launch {
statsUseCase.refreshTypes()
}
}
}

class InsightsListViewModel
@Inject constructor(
@Named(UI_THREAD) mainDispatcher: CoroutineDispatcher,
@Named(INSIGHTS_USE_CASE) private val insightsUseCase: BaseListUseCase,
analyticsTracker: AnalyticsTrackerWrapper,
dateSelectorFactory: StatsDateSelector.Factory
) : StatsListViewModel(mainDispatcher, insightsUseCase, analyticsTracker, dateSelectorFactory.build(INSIGHTS))
dateSelectorFactory: StatsDateSelector.Factory,
popupMenuHandler: ItemPopupMenuHandler
) : StatsListViewModel(
mainDispatcher,
insightsUseCase,
analyticsTracker,
dateSelectorFactory.build(INSIGHTS),
popupMenuHandler
)

class YearsListViewModel @Inject constructor(
@Named(UI_THREAD) mainDispatcher: CoroutineDispatcher,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.ListI
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.ListItemWithIcon
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.LoadingItem
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.MapItem
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.ReferredItem
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.QuickScanItem
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.ReferredItem
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.TabsItem
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Text
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Title
Expand All @@ -39,8 +39,8 @@ import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.LIST_ITEM_WITH_ICON
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.LOADING_ITEM
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.MAP
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.REFERRED_ITEM
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.QUICK_SCAN_ITEM
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.REFERRED_ITEM
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.TABS
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.TEXT
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.TITLE
Expand All @@ -62,8 +62,8 @@ import org.wordpress.android.ui.stats.refresh.lists.sections.viewholders.ListIte
import org.wordpress.android.ui.stats.refresh.lists.sections.viewholders.ListItemWithIconViewHolder
import org.wordpress.android.ui.stats.refresh.lists.sections.viewholders.LoadingItemViewHolder
import org.wordpress.android.ui.stats.refresh.lists.sections.viewholders.MapViewHolder
import org.wordpress.android.ui.stats.refresh.lists.sections.viewholders.ReferredItemViewHolder
import org.wordpress.android.ui.stats.refresh.lists.sections.viewholders.QuickScanItemViewHolder
import org.wordpress.android.ui.stats.refresh.lists.sections.viewholders.ReferredItemViewHolder
import org.wordpress.android.ui.stats.refresh.lists.sections.viewholders.TabsViewHolder
import org.wordpress.android.ui.stats.refresh.lists.sections.viewholders.TextViewHolder
import org.wordpress.android.ui.stats.refresh.lists.sections.viewholders.TitleViewHolder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.wordpress.android.ui.stats.refresh.lists.sections

import android.support.annotation.DrawableRes
import android.support.annotation.StringRes
import android.view.View
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.ListItemWithIcon.IconStyle.NORMAL
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.ACTIVITY_ITEM
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Type.BAR_CHART
Expand Down Expand Up @@ -54,7 +55,11 @@ sealed class BlockListItem(val type: Type) {
QUICK_SCAN_ITEM
}

data class Title(@StringRes val textResource: Int? = null, val text: String? = null) : BlockListItem(TITLE)
data class Title(
@StringRes val textResource: Int? = null,
val text: String? = null,
val menuAction: ((View) -> Unit)? = null
) : BlockListItem(TITLE)

data class ReferredItem(@StringRes val label: Int, val itemTitle: String) : BlockListItem(REFERRED_ITEM)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.wordpress.android.ui.stats.refresh.lists.sections.insights.usecases

import android.view.View
import kotlinx.coroutines.CoroutineDispatcher
import org.wordpress.android.R
import org.wordpress.android.R.string
Expand All @@ -13,6 +14,7 @@ import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Empty
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.QuickScanItem
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.QuickScanItem.Column
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Title
import org.wordpress.android.ui.stats.refresh.utils.ItemPopupMenuHandler
import org.wordpress.android.ui.stats.refresh.utils.StatsDateFormatter
import org.wordpress.android.ui.stats.refresh.utils.StatsSiteProvider
import org.wordpress.android.ui.stats.refresh.utils.toFormattedString
Expand All @@ -24,10 +26,15 @@ class AllTimeStatsUseCase
@Named(UI_THREAD) private val mainDispatcher: CoroutineDispatcher,
private val allTimeStore: AllTimeInsightsStore,
private val statsSiteProvider: StatsSiteProvider,
private val statsDateFormatter: StatsDateFormatter
private val statsDateFormatter: StatsDateFormatter,
private val popupMenuHandler: ItemPopupMenuHandler
) : StatelessUseCase<InsightsAllTimeModel>(ALL_TIME_STATS, mainDispatcher) {
override fun buildLoadingItem(): List<BlockListItem> = listOf(Title(R.string.stats_insights_all_time_stats))

override fun buildEmptyItem(): List<BlockListItem> {
return listOf(buildTitle(), Empty())
}

override suspend fun loadCachedData(): InsightsAllTimeModel? {
return allTimeStore.getAllTimeInsights(statsSiteProvider.siteModel)
}
Expand All @@ -51,7 +58,7 @@ class AllTimeStatsUseCase

override fun buildUiModel(domainModel: InsightsAllTimeModel): List<BlockListItem> {
val items = mutableListOf<BlockListItem>()
items.add(Title(R.string.stats_insights_all_time_stats))
items.add(buildTitle())

val hasPosts = domainModel.posts > 0
val hasViews = domainModel.views > 0
Expand Down Expand Up @@ -79,4 +86,10 @@ class AllTimeStatsUseCase
}
return items
}

private fun buildTitle() = Title(string.stats_insights_all_time_stats, menuAction = this::onMenuClick)

private fun onMenuClick(view: View) {
popupMenuHandler.onMenuClick(view, type)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.wordpress.android.ui.stats.refresh.lists.sections.insights.usecases

import android.view.View
import kotlinx.coroutines.CoroutineDispatcher
import org.wordpress.android.R
import org.wordpress.android.R.string
Expand All @@ -24,6 +25,7 @@ import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Navig
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.TabsItem
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.Title
import org.wordpress.android.ui.stats.refresh.lists.sections.insights.InsightUseCaseFactory
import org.wordpress.android.ui.stats.refresh.utils.ItemPopupMenuHandler
import org.wordpress.android.ui.stats.refresh.utils.StatsSiteProvider
import org.wordpress.android.ui.stats.refresh.utils.toFormattedString
import org.wordpress.android.util.analytics.AnalyticsTrackerWrapper
Expand All @@ -40,6 +42,7 @@ class CommentsUseCase
private val commentsStore: CommentsStore,
private val statsSiteProvider: StatsSiteProvider,
private val analyticsTracker: AnalyticsTrackerWrapper,
private val popupMenuHandler: ItemPopupMenuHandler,
private val useCaseMode: UseCaseMode
) : StatefulUseCase<CommentsModel, SelectedTabUiState>(COMMENTS, mainDispatcher, 0) {
override suspend fun fetchRemoteData(forced: Boolean): State<CommentsModel> {
Expand All @@ -52,7 +55,8 @@ class CommentsUseCase
error != null -> State.Error(error.message ?: error.type.name)
model != null && (model.authors.isNotEmpty() || model.posts.isNotEmpty()) -> State.Data(
model,
cached = response.cached)
cached = response.cached
)
else -> State.Empty()
}
}
Expand All @@ -64,11 +68,15 @@ class CommentsUseCase

override fun buildLoadingItem(): List<BlockListItem> = listOf(Title(R.string.stats_view_comments))

override fun buildEmptyItem(): List<BlockListItem> {
return listOf(buildTitle(), Empty())
}

override fun buildStatefulUiModel(model: CommentsModel, uiState: Int): List<BlockListItem> {
val items = mutableListOf<BlockListItem>()

if (useCaseMode == BLOCK) {
items.add(Title(string.stats_view_comments))
items.add(buildTitle())
}

if (model.authors.isNotEmpty() || model.posts.isNotEmpty()) {
Expand Down Expand Up @@ -99,6 +107,8 @@ class CommentsUseCase
return items
}

private fun buildTitle() = Title(string.stats_view_comments, menuAction = this::onMenuClick)

private fun buildAuthorsTab(authors: List<CommentsModel.Author>): List<BlockListItem> {
val mutableItems = mutableListOf<BlockListItem>()
if (authors.isNotEmpty()) {
Expand Down Expand Up @@ -140,19 +150,25 @@ class CommentsUseCase
navigateTo(ViewCommentsStats(selectedTab))
}

private fun onMenuClick(view: View) {
popupMenuHandler.onMenuClick(view, type)
}

class CommentsUseCaseFactory
@Inject constructor(
@Named(UI_THREAD) private val mainDispatcher: CoroutineDispatcher,
private val commentsStore: CommentsStore,
private val statsSiteProvider: StatsSiteProvider,
private val analyticsTracker: AnalyticsTrackerWrapper
private val analyticsTracker: AnalyticsTrackerWrapper,
private val popupMenuHandler: ItemPopupMenuHandler
) : InsightUseCaseFactory {
override fun build(useCaseMode: UseCaseMode) =
CommentsUseCase(
mainDispatcher,
commentsStore,
statsSiteProvider,
analyticsTracker,
popupMenuHandler,
useCaseMode
)
}
Expand Down
Loading