Skip to content

Commit

Permalink
Merge pull request #20316 from wordpress-mobile/fix/issue-with-traffi…
Browse files Browse the repository at this point in the history
…c-widgets

Fix granularity issue with traffic widgets
  • Loading branch information
ravishanker authored Feb 29, 2024
2 parents 97b1c1b + de5904e commit 255d180
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class StatsActivity : LocaleAwareActivity() {
const val INITIAL_SELECTED_PERIOD_KEY = "INITIAL_SELECTED_PERIOD_KEY"
const val ARG_LAUNCHED_FROM = "ARG_LAUNCHED_FROM"
const val ARG_DESIRED_TIMEFRAME = "ARG_DESIRED_TIMEFRAME"
const val ARG_GRANULARITY = "ARG_GRANULARITY"

@JvmStatic
@JvmOverloads
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,18 @@ class StatsViewModel

val launchedFrom = intent.getSerializableExtraCompat<StatsLaunchedFrom>(StatsActivity.ARG_LAUNCHED_FROM)
val initialTimeFrame = getInitialTimeFrame(intent)
val initialGranularity = intent.getSerializableExtraCompat<StatsGranularity>(StatsActivity.ARG_GRANULARITY)
val initialSelectedPeriod = intent.getStringExtra(StatsActivity.INITIAL_SELECTED_PERIOD_KEY)
val notificationType = intent.getSerializableExtraCompat<NotificationType>(ARG_NOTIFICATION_TYPE)
start(localSiteId, launchedFrom, initialTimeFrame, initialSelectedPeriod, restart, notificationType)
start(
localSiteId,
launchedFrom,
initialTimeFrame,
initialSelectedPeriod,
restart,
notificationType,
initialGranularity
)
}

fun onSaveInstanceState(outState: Bundle) {
Expand Down Expand Up @@ -158,7 +167,8 @@ class StatsViewModel
initialSection: StatsSection?,
initialSelectedPeriod: String?,
restart: Boolean,
notificationType: NotificationType?
notificationType: NotificationType?,
granularity: StatsGranularity? = null
) {
if (restart) {
selectedDateProvider.clear()
Expand All @@ -173,10 +183,11 @@ class StatsViewModel
)

initialSection?.let { statsSectionManager.setSelectedSection(it) }
granularity?.let { selectedTrafficGranularityManager.setSelectedTrafficGranularity(it) }
updateSelectedSectionByTrafficTabFeatureConfig()
trackSectionSelected(statsSectionManager.getSelectedSection())

val initialGranularity = initialSection?.toStatsGranularity()
val initialGranularity = granularity ?: initialSection?.toStatsGranularity()
if (initialGranularity != null && initialSelectedPeriod != null) {
selectedDateProvider.setInitialSelectedPeriod(initialGranularity, initialSelectedPeriod)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ class StatsListFragment : ViewPagerFragment(R.layout.stats_list_fragment) {
super.onResume()
@Suppress("DEPRECATION")
setHasOptionsMenu(statsSection == StatsSection.INSIGHTS)

val viewModelGranularity = viewModel.dateSelector?.statsGranularity
val storedGranularity = selectedTrafficGranularityManager.getSelectedTrafficGranularity()
if (viewModelGranularity != storedGranularity) {
// Coming from widget
(viewModel as? TrafficListViewModel)?.onGranularitySelected(storedGranularity)
}
}

override fun onDestroyView() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ abstract class StatsListViewModel(
defaultDispatcher: CoroutineDispatcher,
protected var statsUseCase: BaseListUseCase,
private val analyticsTracker: AnalyticsTrackerWrapper,
protected var dateSelector: StatsDateSelector?,
var dateSelector: StatsDateSelector?,
popupMenuHandler: ItemPopupMenuHandler? = null,
private val newsCardHandler: NewsCardHandler? = null,
actionCardHandler: ActionCardHandler? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kotlinx.coroutines.launch
import org.wordpress.android.R
import org.wordpress.android.analytics.AnalyticsTracker
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.fluxc.network.utils.StatsGranularity
import org.wordpress.android.fluxc.store.AccountStore
import org.wordpress.android.fluxc.store.SiteStore
import org.wordpress.android.fluxc.store.stats.insights.TodayInsightsStore
Expand Down Expand Up @@ -79,10 +80,11 @@ class MinifiedWidgetUpdater
views.setViewVisibility(R.id.widget_retry_button, View.GONE)

val timeframe = if (statsTrafficTabFeatureConfig.isEnabled()) TRAFFIC else INSIGHTS
val granularity = if (statsTrafficTabFeatureConfig.isEnabled()) StatsGranularity.DAYS else null

views.setOnClickPendingIntent(
R.id.widget_container,
widgetUtils.getPendingSelfIntent(context, siteModel.id, timeframe)
widgetUtils.getPendingSelfIntent(context, siteModel.id, timeframe, granularity)
)
showValue(widgetManager, appWidgetId, views, siteModel, dataType, isWideView)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.View
import android.widget.RemoteViews
import org.wordpress.android.R
import org.wordpress.android.analytics.AnalyticsTracker
import org.wordpress.android.fluxc.network.utils.StatsGranularity
import org.wordpress.android.fluxc.store.AccountStore
import org.wordpress.android.fluxc.store.SiteStore
import org.wordpress.android.ui.prefs.AppPrefsWrapper
Expand Down Expand Up @@ -59,10 +60,11 @@ class TodayWidgetUpdater
} else {
StatsTimeframe.INSIGHTS
}
val granularity = if (statsTrafficTabFeatureConfig.isEnabled()) StatsGranularity.DAYS else null
siteModel.let {
views.setOnClickPendingIntent(
R.id.widget_title_container,
widgetUtils.getPendingSelfIntent(context, siteModel.id, timeframe)
widgetUtils.getPendingSelfIntent(context, siteModel.id, timeframe, granularity)
)
}
widgetUtils.showList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import org.wordpress.android.WordPress
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.fluxc.model.stats.time.VisitsAndViewsModel
import org.wordpress.android.fluxc.model.stats.time.VisitsAndViewsModel.PeriodData
import org.wordpress.android.fluxc.network.utils.StatsGranularity
import org.wordpress.android.modules.UI_THREAD
import org.wordpress.android.ui.stats.StatsTimeframe
import org.wordpress.android.ui.stats.refresh.StatsActivity
Expand Down Expand Up @@ -174,13 +175,15 @@ class WidgetUtils
fun getPendingSelfIntent(
context: Context,
localSiteId: Int,
statsTimeframe: StatsTimeframe
statsTimeframe: StatsTimeframe,
granularity: StatsGranularity? = null
): PendingIntent {
val intent = Intent(context, StatsActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.putExtra(WordPress.LOCAL_SITE_ID, localSiteId)
intent.putExtra(StatsActivity.ARG_DESIRED_TIMEFRAME, statsTimeframe)
intent.putExtra(StatsActivity.ARG_LAUNCHED_FROM, StatsLaunchedFrom.WIDGET)
intent.putExtra(StatsActivity.ARG_GRANULARITY, granularity)
return PendingIntent.getActivity(
context,
getRandomId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.View
import android.widget.RemoteViews
import org.wordpress.android.R
import org.wordpress.android.analytics.AnalyticsTracker
import org.wordpress.android.fluxc.network.utils.StatsGranularity
import org.wordpress.android.fluxc.store.AccountStore
import org.wordpress.android.fluxc.store.SiteStore
import org.wordpress.android.ui.prefs.AppPrefsWrapper
Expand Down Expand Up @@ -53,7 +54,7 @@ class ViewsWidgetUpdater
siteModel.let {
views.setOnClickPendingIntent(
R.id.widget_title_container,
widgetUtils.getPendingSelfIntent(context, siteModel.id, DAY)
widgetUtils.getPendingSelfIntent(context, siteModel.id, DAY, StatsGranularity.DAYS)
)
}
widgetUtils.showList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.View
import android.widget.RemoteViews
import org.wordpress.android.R
import org.wordpress.android.analytics.AnalyticsTracker
import org.wordpress.android.fluxc.network.utils.StatsGranularity
import org.wordpress.android.fluxc.store.AccountStore
import org.wordpress.android.fluxc.store.SiteStore
import org.wordpress.android.ui.prefs.AppPrefsWrapper
Expand Down Expand Up @@ -53,12 +54,13 @@ class WeekViewsWidgetUpdater @Inject constructor(
val hasAccessToken = accountStore.hasAccessToken()
val widgetHasData = appPrefsWrapper.hasAppWidgetData(appWidgetId)
val timeframe = if (statsTrafficTabFeatureConfig.isEnabled()) TRAFFIC else INSIGHTS
val granularity = if (statsTrafficTabFeatureConfig.isEnabled()) StatsGranularity.WEEKS else null
if (networkAvailable && hasAccessToken && siteModel != null) {
widgetUtils.setSiteIcon(siteModel, context, views, appWidgetId)
siteModel.let {
views.setOnClickPendingIntent(
R.id.widget_title_container,
widgetUtils.getPendingSelfIntent(context, siteModel.id, timeframe)
widgetUtils.getPendingSelfIntent(context, siteModel.id, timeframe, granularity)
)
}
widgetUtils.showList(
Expand Down

0 comments on commit 255d180

Please sign in to comment.