Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stats traffic tab add traffic tab #19942

Merged
merged 12 commits into from
Jan 15, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,17 @@ import org.wordpress.android.ui.stats.refresh.utils.StatsSiteProvider.SiteUpdate
import org.wordpress.android.ui.utils.UiHelpers
import org.wordpress.android.util.JetpackBrandingUtils
import org.wordpress.android.models.JetpackPoweredScreen
import org.wordpress.android.ui.stats.refresh.lists.StatsListViewModel.StatsSection.TRAFFIC
import org.wordpress.android.util.WPSwipeToRefreshHelper
import org.wordpress.android.util.config.StatsTrafficTabFeatureConfig
import org.wordpress.android.util.helpers.SwipeToRefreshHelper
import org.wordpress.android.viewmodel.observeEvent
import org.wordpress.android.widgets.WPSnackbar
import javax.inject.Inject

private val statsSections = listOf(INSIGHTS, DAYS, WEEKS, MONTHS, YEARS)
private val statsSectionsWithTrafficTab = listOf(TRAFFIC, INSIGHTS)
private var statsTrafficTabEnabled = false

@AndroidEntryPoint
class StatsFragment : Fragment(R.layout.stats_fragment), ScrollableViewInitializedListener {
Expand All @@ -58,6 +62,10 @@ class StatsFragment : Fragment(R.layout.stats_fragment), ScrollableViewInitializ

@Inject
lateinit var jetpackBrandingUtils: JetpackBrandingUtils

@Inject
lateinit var statsTrafficTabFeatureConfig: StatsTrafficTabFeatureConfig

private val viewModel: StatsViewModel by activityViewModels()
private lateinit var swipeToRefreshHelper: SwipeToRefreshHelper
private lateinit var selectedTabListener: SelectedTabListener
Expand Down Expand Up @@ -91,6 +99,8 @@ class StatsFragment : Fragment(R.layout.stats_fragment), ScrollableViewInitializ
}

private fun StatsFragmentBinding.initializeViews() {
statsTrafficTabEnabled = statsTrafficTabFeatureConfig.isEnabled()

val adapter = StatsPagerAdapter(this@StatsFragment)
statsPager.adapter = adapter
statsPager.setPageTransformer(
Expand Down Expand Up @@ -180,7 +190,11 @@ class StatsFragment : Fragment(R.layout.stats_fragment), ScrollableViewInitializ

viewModel.selectedSection.observe(viewLifecycleOwner) { selectedSection ->
selectedSection?.let {
handleSelectedSection(selectedSection)
if (statsTrafficTabEnabled) {
handleSelectedSectionWithTrafficTab(selectedSection)
} else {
handleSelectedSection(selectedSection)
}
}
}

Expand Down Expand Up @@ -209,6 +223,30 @@ class StatsFragment : Fragment(R.layout.stats_fragment), ScrollableViewInitializ
}
}

@Suppress("MagicNumber")
private fun StatsFragmentBinding.handleSelectedSectionWithTrafficTab(
selectedSection: StatsSection
) {
val position = when (selectedSection) {
TRAFFIC -> 0
INSIGHTS -> 1
DETAIL,
INSIGHT_DETAIL,
TOTAL_LIKES_DETAIL,
TOTAL_COMMENTS_DETAIL,
TOTAL_FOLLOWERS_DETAIL,
ANNUAL_STATS -> null
else -> null
}
position?.let {
if (statsPager.currentItem != position) {
tabLayout.removeOnTabSelectedListener(selectedTabListener)
statsPager.setCurrentItem(position, false)
tabLayout.addOnTabSelectedListener(selectedTabListener)
}
}
}

@Suppress("MagicNumber")
private fun StatsFragmentBinding.handleSelectedSection(
selectedSection: StatsSection
Expand All @@ -225,6 +263,7 @@ class StatsFragment : Fragment(R.layout.stats_fragment), ScrollableViewInitializ
TOTAL_COMMENTS_DETAIL,
TOTAL_FOLLOWERS_DETAIL,
ANNUAL_STATS -> null
else -> null
}
position?.let {
if (statsPager.currentItem != position) {
Expand Down Expand Up @@ -295,18 +334,20 @@ class StatsFragment : Fragment(R.layout.stats_fragment), ScrollableViewInitializ
}

class StatsPagerAdapter(private val parent: Fragment) : FragmentStateAdapter(parent) {
override fun getItemCount(): Int = statsSections.size
private val statsTabs = if (statsTrafficTabEnabled) statsSectionsWithTrafficTab else statsSections
override fun getItemCount(): Int = statsTabs.size

override fun createFragment(position: Int): Fragment {
return StatsListFragment.newInstance(statsSections[position])
return StatsListFragment.newInstance(statsTabs[position])
}

fun getTabTitle(position: Int): CharSequence {
return parent.context?.getString(statsSections[position].titleRes).orEmpty()
return parent.context?.getString(statsTabs[position].titleRes).orEmpty()
}
}

private class SelectedTabListener(val viewModel: StatsViewModel) : OnTabSelectedListener {
private val statsTabs = if (statsTrafficTabEnabled) statsSectionsWithTrafficTab else statsSections
override fun onTabReselected(tab: Tab?) {
// Do nothing
}
Expand All @@ -316,6 +357,6 @@ private class SelectedTabListener(val viewModel: StatsViewModel) : OnTabSelected
}

override fun onTabSelected(tab: Tab) {
viewModel.onSectionSelected(statsSections[tab.position])
viewModel.onSectionSelected(statsTabs[tab.position])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import org.wordpress.android.analytics.AnalyticsTracker.Stat.STATS_PERIOD_DAYS_A
import org.wordpress.android.analytics.AnalyticsTracker.Stat.STATS_PERIOD_MONTHS_ACCESSED
import org.wordpress.android.analytics.AnalyticsTracker.Stat.STATS_PERIOD_WEEKS_ACCESSED
import org.wordpress.android.analytics.AnalyticsTracker.Stat.STATS_PERIOD_YEARS_ACCESSED
import org.wordpress.android.analytics.AnalyticsTracker.Stat.STATS_TRAFFIC_ACCESSED
import org.wordpress.android.fluxc.network.utils.StatsGranularity
import org.wordpress.android.fluxc.store.DEFAULT_INSIGHTS
import org.wordpress.android.fluxc.store.JETPACK_DEFAULT_INSIGHTS
Expand Down Expand Up @@ -279,6 +280,7 @@ class StatsViewModel

private fun trackSectionSelected(statsSection: StatsSection) {
when (statsSection) {
StatsSection.TRAFFIC -> analyticsTracker.track(STATS_TRAFFIC_ACCESSED)
StatsSection.INSIGHTS -> analyticsTracker.track(STATS_INSIGHTS_ACCESSED)
StatsSection.DAYS -> analyticsTracker.trackGranular(STATS_PERIOD_DAYS_ACCESSED, StatsGranularity.DAYS)
StatsSection.WEEKS -> analyticsTracker.trackGranular(STATS_PERIOD_WEEKS_ACCESSED, StatsGranularity.WEEKS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ class StatsListFragment : ViewPagerFragment(R.layout.stats_list_fragment) {
StatsSection.TOTAL_COMMENTS_DETAIL -> TotalCommentsDetailListViewModel::class.java
StatsSection.TOTAL_FOLLOWERS_DETAIL -> TotalFollowersDetailListViewModel::class.java
StatsSection.ANNUAL_STATS,
StatsSection.TRAFFIC -> DaysListViewModel::class.java // Replace with TrafficListViewModel
StatsSection.INSIGHTS -> InsightsListViewModel::class.java
StatsSection.DAYS -> DaysListViewModel::class.java
StatsSection.WEEKS -> WeeksListViewModel::class.java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ abstract class StatsListViewModel(
private var isInitialized = false

enum class StatsSection(@StringRes val titleRes: Int) {
TRAFFIC(R.string.stats_traffic),
INSIGHTS(R.string.stats_insights),
DAYS(R.string.stats_timeframe_days),
WEEKS(R.string.stats_timeframe_weeks),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ fun StatsSection.toStatsGranularity(): StatsGranularity? {
return when (this) {
ANNUAL_STATS, DETAIL, TOTAL_LIKES_DETAIL, TOTAL_COMMENTS_DETAIL, TOTAL_FOLLOWERS_DETAIL, INSIGHTS -> null
StatsSection.INSIGHT_DETAIL,
StatsSection.TRAFFIC -> DAYS // Replace with TRAFFIC when it's implemented
StatsSection.DAYS -> DAYS
StatsSection.WEEKS -> WEEKS
StatsSection.MONTHS -> MONTHS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.wordpress.android.fluxc.network.utils.StatsGranularity
import org.wordpress.android.fluxc.store.StatsStore.InsightType
import org.wordpress.android.ui.stats.refresh.lists.StatsListViewModel.StatsSection
import org.wordpress.android.ui.stats.refresh.lists.StatsListViewModel.StatsSection.INSIGHT_DETAIL
import org.wordpress.android.ui.stats.refresh.lists.StatsListViewModel.StatsSection.TRAFFIC
import org.wordpress.android.ui.stats.refresh.lists.widget.configuration.StatsWidgetConfigureFragment.WidgetType
import org.wordpress.android.ui.stats.refresh.lists.widget.configuration.StatsWidgetConfigureFragment.WidgetType.ALL_TIME_VIEWS
import org.wordpress.android.ui.stats.refresh.lists.widget.configuration.StatsWidgetConfigureFragment.WidgetType.TODAY_VIEWS
Expand Down Expand Up @@ -55,7 +56,7 @@ fun AnalyticsTrackerWrapper.trackViewsVisitorsChips(position: Int) {

fun AnalyticsTrackerWrapper.trackWithSection(stat: Stat, section: StatsSection) {
val property = when (section) {
StatsSection.DAYS -> DAYS_PROPERTY
StatsSection.DAYS, TRAFFIC -> DAYS_PROPERTY // Replace with TRAFFIC when it's implemented
StatsSection.WEEKS -> WEEKS_PROPERTY
StatsSection.MONTHS -> MONTHS_PROPERTY
StatsSection.YEARS -> YEARS_PROPERTY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.wordpress.android.fluxc.network.utils.StatsGranularity.YEARS
import org.wordpress.android.ui.stats.refresh.StatsViewModel.DateSelectorUiModel
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.StatsListViewModel.StatsSection.TRAFFIC
import org.wordpress.android.ui.stats.refresh.lists.sections.granular.SelectedDateProvider
import org.wordpress.android.ui.stats.refresh.lists.sections.granular.SelectedDateProvider.SelectedDate
import org.wordpress.android.util.perform
Expand Down Expand Up @@ -78,7 +79,7 @@ constructor(
StatsSection.TOTAL_FOLLOWERS_DETAIL,
StatsSection.INSIGHTS,
StatsSection.INSIGHT_DETAIL,
StatsSection.DAYS -> DAYS
StatsSection.DAYS, TRAFFIC -> DAYS // Replace with TRAFFIC when it's implemented
StatsSection.WEEKS -> WEEKS
StatsSection.MONTHS -> MONTHS
StatsSection.ANNUAL_STATS,
Expand Down
1 change: 1 addition & 0 deletions WordPress/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,7 @@
<string name="stats_search_terms_unknown_search_terms">Unknown Search Terms</string>

<!-- Stats insights -->
<string name="stats_traffic">Traffic</string>
<string name="stats_insights">Insights</string>
<string name="stats_insights_all_time">All-time posts, views, and visitors</string>
<string name="stats_insights_today">Today\'s Stats</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public enum Stat {
READER_SITE_SHARED,
STATS_ACCESSED,
STATS_ACCESS_ERROR,
STATS_TRAFFIC_ACCESSED,
STATS_INSIGHTS_ACCESSED,
STATS_INSIGHTS_MANAGEMENT_HINT_DISMISSED,
STATS_INSIGHTS_MANAGEMENT_HINT_CLICKED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,8 @@ public static String getEventNameForStat(AnalyticsTracker.Stat stat) {
return "stats_accessed";
case STATS_ACCESS_ERROR:
return "stats_access_error";
case STATS_TRAFFIC_ACCESSED:
return "stats_traffic_accessed";
case STATS_INSIGHTS_ACCESSED:
return "stats_insights_accessed";
case STATS_INSIGHTS_MANAGEMENT_HINT_DISMISSED:
Expand Down
Loading