From 1555c4c32c7e1ec4c146e31800791def36b85673 Mon Sep 17 00:00:00 2001 From: Renan Ferrari Date: Tue, 14 Sep 2021 18:49:56 -0300 Subject: [PATCH 1/8] Add Weekly Roundup preference --- .../org/wordpress/android/ui/prefs/AppPrefs.java | 15 ++++++++++++++- .../wordpress/android/ui/prefs/AppPrefsWrapper.kt | 8 ++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/prefs/AppPrefs.java b/WordPress/src/main/java/org/wordpress/android/ui/prefs/AppPrefs.java index 8754a5ad709e..a0a92fee0956 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/prefs/AppPrefs.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/prefs/AppPrefs.java @@ -166,7 +166,8 @@ public enum DeletablePrefKey implements PrefKey { REMOVED_QUICK_START_CARD_TYPE, PINNED_DYNAMIC_CARD, BLOGGING_REMINDERS_SHOWN, - SHOULD_SCHEDULE_CREATE_SITE_NOTIFICATION + SHOULD_SCHEDULE_CREATE_SITE_NOTIFICATION, + SHOULD_SHOW_WEEKLY_ROUNDUP_NOTIFICATION } /** @@ -1300,6 +1301,18 @@ public static boolean shouldScheduleCreateSiteNotification() { return getBoolean(DeletablePrefKey.SHOULD_SCHEDULE_CREATE_SITE_NOTIFICATION, true); } + public static void setShouldShowWeeklyRoundupNotification(long remoteSiteId, boolean shouldShow) { + prefs().edit().putBoolean(getShouldShowWeeklyRoundupNotification(remoteSiteId), shouldShow).apply(); + } + + public static boolean shouldShowWeeklyRoundupNotification(long remoteSiteId) { + return prefs().getBoolean(getShouldShowWeeklyRoundupNotification(remoteSiteId), true); + } + + @NonNull private static String getShouldShowWeeklyRoundupNotification(long siteId) { + return DeletablePrefKey.SHOULD_SHOW_WEEKLY_ROUNDUP_NOTIFICATION.name() + siteId; + } + /* * adds a local site ID to the top of list of recently chosen sites */ diff --git a/WordPress/src/main/java/org/wordpress/android/ui/prefs/AppPrefsWrapper.kt b/WordPress/src/main/java/org/wordpress/android/ui/prefs/AppPrefsWrapper.kt index d7b6cd6e92fe..7a1a41ecf6d4 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/prefs/AppPrefsWrapper.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/prefs/AppPrefsWrapper.kt @@ -186,6 +186,14 @@ class AppPrefsWrapper @Inject constructor() { return AppPrefs.isBloggingRemindersShown(siteId) } + fun setShouldShowWeeklyRoundupNotification(siteId: Long, shouldShow: Boolean) { + AppPrefs.setShouldShowWeeklyRoundupNotification(siteId, shouldShow) + } + + fun shouldShowWeeklyRoundupNotification(siteId: Long): Boolean { + return AppPrefs.shouldShowWeeklyRoundupNotification(siteId) + } + fun setSiteJetpackCapabilities(remoteSiteId: Long, capabilities: List) = AppPrefs.setSiteJetpackCapabilities(remoteSiteId, capabilities) From fdc2c2e89cd7ad5c04744afbf47178f5ea676feb Mon Sep 17 00:00:00 2001 From: Renan Ferrari Date: Tue, 14 Sep 2021 18:50:32 -0300 Subject: [PATCH 2/8] Add preference check to WeeklyRoundupNotifier --- .../android/workers/weeklyroundup/WeeklyRoundupNotifier.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/WordPress/src/main/java/org/wordpress/android/workers/weeklyroundup/WeeklyRoundupNotifier.kt b/WordPress/src/main/java/org/wordpress/android/workers/weeklyroundup/WeeklyRoundupNotifier.kt index 97d8226219d1..4b543ea43e5c 100644 --- a/WordPress/src/main/java/org/wordpress/android/workers/weeklyroundup/WeeklyRoundupNotifier.kt +++ b/WordPress/src/main/java/org/wordpress/android/workers/weeklyroundup/WeeklyRoundupNotifier.kt @@ -12,6 +12,7 @@ import org.wordpress.android.push.NotificationPushIds.WEEKLY_ROUNDUP_NOTIFICATIO import org.wordpress.android.push.NotificationType.WEEKLY_ROUNDUP import org.wordpress.android.ui.ActivityLauncher import org.wordpress.android.ui.notifications.SystemNotificationsTracker +import org.wordpress.android.ui.prefs.AppPrefsWrapper import org.wordpress.android.ui.stats.StatsTimeframe.WEEK import org.wordpress.android.util.SiteUtilsWrapper import org.wordpress.android.viewmodel.ContextProvider @@ -26,7 +27,8 @@ class WeeklyRoundupNotifier @Inject constructor( private val weeklyRoundupScheduler: WeeklyRoundupScheduler, private val notificationsTracker: SystemNotificationsTracker, private val siteUtils: SiteUtilsWrapper, - private val weeklyRoundupRepository: WeeklyRoundupRepository + private val weeklyRoundupRepository: WeeklyRoundupRepository, + private val appPrefs: AppPrefsWrapper ) { fun shouldShowNotifications() = accountStore.hasAccessToken() && siteStore.hasSitesAccessedViaWPComRest() @@ -36,6 +38,7 @@ class WeeklyRoundupNotifier @Inject constructor( .awaitAll() .asSequence() .filterNotNull() + .filter { appPrefs.shouldShowWeeklyRoundupNotification(it.site.siteId) } .sortedByDescending { it.score } .take(TOP_FIVE_SITES) .filter { it.views >= MIN_SITE_VIEWS } From ef9fc372454d57c709470551c1456a864c6715fd Mon Sep 17 00:00:00 2001 From: Renan Ferrari Date: Tue, 14 Sep 2021 18:53:35 -0300 Subject: [PATCH 3/8] Extract method to setup setting view --- ...NotificationsSettingsDialogPreference.java | 75 +++++++++++-------- 1 file changed, 44 insertions(+), 31 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/prefs/notifications/NotificationsSettingsDialogPreference.java b/WordPress/src/main/java/org/wordpress/android/ui/prefs/notifications/NotificationsSettingsDialogPreference.java index 59d64e378438..2900af3e8a2d 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/prefs/notifications/NotificationsSettingsDialogPreference.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/prefs/notifications/NotificationsSettingsDialogPreference.java @@ -5,6 +5,7 @@ import android.app.AlertDialog.Builder; import android.content.Context; import android.preference.DialogPreference; +import android.text.TextUtils; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; @@ -16,6 +17,7 @@ import android.widget.TextView; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.appcompat.widget.SwitchCompat; import androidx.core.content.ContextCompat; @@ -182,48 +184,59 @@ private View configureLayoutForView(LinearLayout view) { continue; } - View commentsSetting = View.inflate(getContext(), R.layout.notifications_settings_switch, null); - TextView title = commentsSetting.findViewById(R.id.notifications_switch_title); - title.setText(settingName); - // Add special summary text for the WPCOM section + String settingSummary = null; if (mChannel == Channel.WPCOM && i < summaryArray.length) { - String summaryText = summaryArray[i]; - TextView summary = commentsSetting.findViewById(R.id.notifications_switch_summary); - summary.setVisibility(View.VISIBLE); - summary.setText(summaryText); + settingSummary = summaryArray[i]; } - final SwitchCompat toggleSwitch = commentsSetting.findViewById(R.id.notifications_switch); - toggleSwitch.setChecked(JSONUtils.queryJSON(settingsJson, settingValue, true)); - toggleSwitch.setTag(settingValue); - toggleSwitch.setOnCheckedChangeListener(mOnCheckedChangedListener); + boolean isSettingChecked = JSONUtils.queryJSON(settingsJson, settingValue, true); - View rowContainer = commentsSetting.findViewById(R.id.row_container); - rowContainer.setOnClickListener(new View.OnClickListener() { - @Override public void onClick(View v) { - toggleSwitch.setChecked(!toggleSwitch.isChecked()); - } - }); - - if (mShouldDisplayMainSwitch && i == mSettingsArray.length - 1) { - View divider = commentsSetting.findViewById(R.id.notifications_list_divider); - if (divider != null) { - MarginLayoutParams mlp = (MarginLayoutParams) divider.getLayoutParams(); - mlp.leftMargin = 0; - mlp.rightMargin = 0; - divider.setLayoutParams(mlp); - } - } + boolean isSettingLast = i == mSettingsArray.length - 1; - view.addView(commentsSetting); + view.addView(setupSettingView(settingName, settingValue, settingSummary, isSettingChecked, + isSettingLast, mOnCheckedChangedListener)); } } return view; } - private CompoundButton.OnCheckedChangeListener mOnCheckedChangedListener = + private View setupSettingView(String settingName, @Nullable String settingValue, @Nullable String settingSummary, + boolean isSettingChecked, boolean isSettingLast, + CompoundButton.OnCheckedChangeListener onCheckedChangeListener) { + View setting = View.inflate(getContext(), R.layout.notifications_settings_switch, null); + TextView title = setting.findViewById(R.id.notifications_switch_title); + title.setText(settingName); + + if (!TextUtils.isEmpty(settingSummary)) { + TextView summary = setting.findViewById(R.id.notifications_switch_summary); + summary.setVisibility(View.VISIBLE); + summary.setText(settingSummary); + } + + final SwitchCompat toggleSwitch = setting.findViewById(R.id.notifications_switch); + toggleSwitch.setChecked(isSettingChecked); + toggleSwitch.setTag(settingValue); + toggleSwitch.setOnCheckedChangeListener(onCheckedChangeListener); + + View rowContainer = setting.findViewById(R.id.row_container); + rowContainer.setOnClickListener(v -> toggleSwitch.setChecked(!toggleSwitch.isChecked())); + + if (mShouldDisplayMainSwitch && isSettingLast) { + View divider = setting.findViewById(R.id.notifications_list_divider); + if (divider != null) { + MarginLayoutParams mlp = (MarginLayoutParams) divider.getLayoutParams(); + mlp.leftMargin = 0; + mlp.rightMargin = 0; + divider.setLayoutParams(mlp); + } + } + + return setting; + } + + private final CompoundButton.OnCheckedChangeListener mOnCheckedChangedListener = new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { @@ -325,7 +338,7 @@ private void hideDisabledView(boolean isMainChecked) { * Updates Notifications current settings switches state based on the main switch state * * @param isMainChecked TRUE to switch on the settings switches. - * FALSE to switch off the settings switches. + * FALSE to switch off the settings switches. */ private void setSettingsSwitchesChecked(boolean isMainChecked) { for (String settingValue : mSettingsValues) { From db7573e069de196c5b0d6b8ba7d124a20c67e3b3 Mon Sep 17 00:00:00 2001 From: Renan Ferrari Date: Tue, 14 Sep 2021 19:00:26 -0300 Subject: [PATCH 4/8] Show Weekly Roundup toggle setting --- .../NotificationsSettingsDialogPreference.java | 10 ++++++++++ WordPress/src/main/res/values/strings.xml | 1 + 2 files changed, 11 insertions(+) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/prefs/notifications/NotificationsSettingsDialogPreference.java b/WordPress/src/main/java/org/wordpress/android/ui/prefs/notifications/NotificationsSettingsDialogPreference.java index 2900af3e8a2d..07e0a4efc248 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/prefs/notifications/NotificationsSettingsDialogPreference.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/prefs/notifications/NotificationsSettingsDialogPreference.java @@ -27,6 +27,7 @@ import org.wordpress.android.models.NotificationsSettings; import org.wordpress.android.models.NotificationsSettings.Channel; import org.wordpress.android.models.NotificationsSettings.Type; +import org.wordpress.android.ui.prefs.AppPrefs; import org.wordpress.android.ui.prefs.notifications.PrefMainSwitchToolbarView.MainSwitchToolbarListener; import org.wordpress.android.util.AppLog; import org.wordpress.android.util.AppLog.T; @@ -199,6 +200,15 @@ private View configureLayoutForView(LinearLayout view) { } } + // Add Weekly Roundup setting + if (mChannel == Channel.BLOGS && mType == Type.DEVICE) { + String settingName = getContext().getString(R.string.weekly_roundup); + boolean isSettingChecked = AppPrefs.shouldShowWeeklyRoundupNotification(mBlogId); + View settingView = setupSettingView(settingName, null, null, isSettingChecked, true, + (compoundButton, isChecked) -> AppPrefs.setShouldShowWeeklyRoundupNotification(mBlogId, isChecked)); + view.addView(settingView); + } + return view; } diff --git a/WordPress/src/main/res/values/strings.xml b/WordPress/src/main/res/values/strings.xml index 845f67056eae..bae2fd3131f7 100644 --- a/WordPress/src/main/res/values/strings.xml +++ b/WordPress/src/main/res/values/strings.xml @@ -3786,6 +3786,7 @@ translators: Block name. %s: The localized block name --> Create site + Weekly Roundup Weekly Roundup: %s Your site got %1$d views, %2$d likes, %3$d comments. From 2509bca3fdaf872f493d2e8ce0135dca7072eb96 Mon Sep 17 00:00:00 2001 From: Renan Ferrari Date: Tue, 14 Sep 2021 19:02:41 -0300 Subject: [PATCH 5/8] Add tests --- .../WeeklyRoundupNotifierTest.kt | 106 +++++++++++++++++- 1 file changed, 102 insertions(+), 4 deletions(-) diff --git a/WordPress/src/test/java/org/wordpress/android/workers/weeklyroundup/WeeklyRoundupNotifierTest.kt b/WordPress/src/test/java/org/wordpress/android/workers/weeklyroundup/WeeklyRoundupNotifierTest.kt index 588f8111680e..b1ba1765e423 100644 --- a/WordPress/src/test/java/org/wordpress/android/workers/weeklyroundup/WeeklyRoundupNotifierTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/workers/weeklyroundup/WeeklyRoundupNotifierTest.kt @@ -1,5 +1,7 @@ package org.wordpress.android.workers.weeklyroundup +import com.nhaarman.mockitokotlin2.any +import com.nhaarman.mockitokotlin2.anyOrNull import com.nhaarman.mockitokotlin2.mock import com.nhaarman.mockitokotlin2.times import com.nhaarman.mockitokotlin2.verify @@ -9,10 +11,15 @@ import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.mockito.junit.MockitoJUnitRunner +import org.mockito.stubbing.Answer +import org.wordpress.android.fluxc.model.SiteModel import org.wordpress.android.fluxc.store.AccountStore import org.wordpress.android.fluxc.store.SiteStore +import org.wordpress.android.push.NotificationPushIds.WEEKLY_ROUNDUP_NOTIFICATION_ID import org.wordpress.android.push.NotificationType.WEEKLY_ROUNDUP +import org.wordpress.android.test import org.wordpress.android.ui.notifications.SystemNotificationsTracker +import org.wordpress.android.ui.prefs.AppPrefsWrapper import org.wordpress.android.util.SiteUtilsWrapper import org.wordpress.android.viewmodel.ContextProvider import org.wordpress.android.viewmodel.ResourceProvider @@ -22,13 +29,22 @@ class WeeklyRoundupNotifierTest { private lateinit var weeklyRoundupNotifier: WeeklyRoundupNotifier private val accountStore: AccountStore = mock() - private val siteStore: SiteStore = mock() + private val siteStore: SiteStore = mock { + on { sitesAccessedViaWPComRest }.thenReturn(buildMockSites()) + } private val contextProvider: ContextProvider = mock() - private val resourceProvider: ResourceProvider = mock() + private val resourceProvider: ResourceProvider = mock { + on { getString(any(), anyOrNull()) }.thenReturn("mock_string") + } private val weeklyRoundupScheduler: WeeklyRoundupScheduler = mock() private val notificationsTracker: SystemNotificationsTracker = mock() private val siteUtils: SiteUtilsWrapper = mock() - private val weeklyRoundupRepository: WeeklyRoundupRepository = mock() + private val weeklyRoundupRepository: WeeklyRoundupRepository = mock { + onBlocking { fetchWeeklyRoundupData(any()) }.then(buildMockData()) + } + private val appPrefs: AppPrefsWrapper = mock { + on { shouldShowWeeklyRoundupNotification(any()) }.thenReturn(true) + } @Before fun setUp() { @@ -40,7 +56,8 @@ class WeeklyRoundupNotifierTest { weeklyRoundupScheduler, notificationsTracker, siteUtils, - weeklyRoundupRepository + weeklyRoundupRepository, + appPrefs ) } @@ -78,4 +95,85 @@ class WeeklyRoundupNotifierTest { verify(notificationsTracker, times(numberOfNotifications)).trackShownNotification(WEEKLY_ROUNDUP) } + + @Test + fun `buildNotifications should not have more than 5 sites`() = test { + whenever(siteStore.sitesAccessedViaWPComRest).thenReturn(buildMockSites(10)) + + val list = weeklyRoundupNotifier.buildNotifications() + + assertThat(list).hasSize(5) + } + + @Test + fun `buildNotifications should filter out null data`() = test { + whenever(weeklyRoundupRepository.fetchWeeklyRoundupData(any())).then(buildMockData(isNull = true)) + + val list = weeklyRoundupNotifier.buildNotifications() + + assertThat(list).isEmpty() + } + + @Test + fun `buildNotifications should filter out disabled preference`() = test { + whenever(appPrefs.shouldShowWeeklyRoundupNotification(any())).thenReturn(false) + + val list = weeklyRoundupNotifier.buildNotifications() + + assertThat(list).isEmpty() + } + + @Test + fun `buildNotifications should filter out sites with less than 5 views`() = test { + whenever(weeklyRoundupRepository.fetchWeeklyRoundupData(any())).then(buildMockData(views = 1)) + + val list = weeklyRoundupNotifier.buildNotifications() + + assertThat(list).isEmpty() + } + + @Test + fun `buildNotifications should sort sites by ascending score`() = test { + val mockSites = buildMockSites() + val data1 = buildMockData(mockSites[0], views = 10, comments = 0, likes = 0) + val data2 = buildMockData(mockSites[1], views = 9, comments = 8, likes = 8) + val data3 = buildMockData(mockSites[2], views = 10, comments = 1, likes = 1) + val unsortedData = listOf(data1, data2, data3) + val sortedData = listOf(data1, data3, data2) + + whenever(siteStore.sitesAccessedViaWPComRest).thenReturn(mockSites) + whenever(weeklyRoundupRepository.fetchWeeklyRoundupData(any())).then { + unsortedData[(it.arguments.first() as SiteModel).id - 1] + } + + val list = weeklyRoundupNotifier.buildNotifications().map { it.id } + + assertThat(list).isEqualTo(sortedData.map { WEEKLY_ROUNDUP_NOTIFICATION_ID + (it?.site?.id ?: 0) }) + } + + private companion object { + fun buildMockSites(quantity: Int = 3) = (1..quantity).map { + SiteModel().apply { + id = it + siteId = 1000L + it + } + } + + fun buildMockData( + site: SiteModel, + period: String = "2021W09W01", + views: Long = 10, + likes: Long = 10, + comments: Long = 10, + isNull: Boolean = false + ) = if (isNull) null else WeeklyRoundupData(site, period, views, likes, comments) + + fun buildMockData( + period: String = "2021W09W01", + views: Long = 10, + likes: Long = 10, + comments: Long = 10, + isNull: Boolean = false + ) = Answer { buildMockData(it.arguments.first() as SiteModel, period, views, likes, comments, isNull) } + } } From d05527831e6a63943dc4674e0e216387a2fa1bbc Mon Sep 17 00:00:00 2001 From: Renan Ferrari Date: Tue, 14 Sep 2021 19:54:33 -0300 Subject: [PATCH 6/8] Add notify method to NotificationManagerWrapper --- .../android/ui/notifications/NotificationManagerWrapper.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/notifications/NotificationManagerWrapper.kt b/WordPress/src/main/java/org/wordpress/android/ui/notifications/NotificationManagerWrapper.kt index 9ec2b611cfac..b860ac74c73f 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/notifications/NotificationManagerWrapper.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/notifications/NotificationManagerWrapper.kt @@ -1,5 +1,6 @@ package org.wordpress.android.ui.notifications +import android.app.Notification import android.content.Context import androidx.core.app.NotificationManagerCompat import javax.inject.Inject @@ -7,4 +8,5 @@ import javax.inject.Inject class NotificationManagerWrapper @Inject constructor(private val context: Context) { fun areNotificationsEnabled() = NotificationManagerCompat.from(context).areNotificationsEnabled() + fun notify(id: Int, notification: Notification) = NotificationManagerCompat.from(context).notify(id, notification) } From 655ec08ddfaada27701abb73d50e0553c678b82c Mon Sep 17 00:00:00 2001 From: Renan Ferrari Date: Tue, 14 Sep 2021 19:55:34 -0300 Subject: [PATCH 7/8] Add option to force show Weekly Roundup notification on debug settings --- .../ui/debug/DebugSettingsViewModel.kt | 20 +++++++++++++++++-- WordPress/src/main/res/values/strings.xml | 1 + 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/debug/DebugSettingsViewModel.kt b/WordPress/src/main/java/org/wordpress/android/ui/debug/DebugSettingsViewModel.kt index e91d0a823fc6..c1294fd8e521 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/debug/DebugSettingsViewModel.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/debug/DebugSettingsViewModel.kt @@ -4,6 +4,7 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import kotlinx.coroutines.CoroutineDispatcher import org.wordpress.android.R +import org.wordpress.android.modules.BG_THREAD import org.wordpress.android.modules.UI_THREAD import org.wordpress.android.ui.debug.DebugSettingsViewModel.NavigationAction.DebugCookies import org.wordpress.android.ui.debug.DebugSettingsViewModel.UiItem.Button @@ -18,23 +19,31 @@ import org.wordpress.android.ui.debug.DebugSettingsViewModel.UiItem.Type.BUTTON import org.wordpress.android.ui.debug.DebugSettingsViewModel.UiItem.Type.FEATURE import org.wordpress.android.ui.debug.DebugSettingsViewModel.UiItem.Type.HEADER import org.wordpress.android.ui.debug.DebugSettingsViewModel.UiItem.Type.ROW +import org.wordpress.android.ui.notifications.NotificationManagerWrapper import org.wordpress.android.ui.utils.ListItemInteraction +import org.wordpress.android.ui.utils.ListItemInteraction.Companion.create import org.wordpress.android.util.DebugUtils import org.wordpress.android.util.config.FeaturesInDevelopment import org.wordpress.android.util.config.ManualFeatureConfig import org.wordpress.android.util.config.RemoteConfig import org.wordpress.android.util.config.RemoteConfigDefaults +import org.wordpress.android.viewmodel.ContextProvider import org.wordpress.android.viewmodel.Event import org.wordpress.android.viewmodel.ScopedViewModel +import org.wordpress.android.workers.weeklyroundup.WeeklyRoundupNotifier import javax.inject.Inject import javax.inject.Named class DebugSettingsViewModel @Inject constructor( @Named(UI_THREAD) private val mainDispatcher: CoroutineDispatcher, + @Named(BG_THREAD) private val bgDispatcher: CoroutineDispatcher, private val manualFeatureConfig: ManualFeatureConfig, private val remoteConfig: RemoteConfig, - private val debugUtils: DebugUtils + private val debugUtils: DebugUtils, + private val weeklyRoundupNotifier: WeeklyRoundupNotifier, + private val notificationManager: NotificationManagerWrapper, + private val contextProvider: ContextProvider ) : ScopedViewModel(mainDispatcher) { private val _uiState = MutableLiveData() val uiState: LiveData = _uiState @@ -65,7 +74,8 @@ class DebugSettingsViewModel uiItems.add(Button(R.string.debug_settings_restart_app, debugUtils::restartApp)) } uiItems.add(Header(R.string.debug_settings_tools)) - uiItems.add(Row(R.string.debug_cookies_title, ListItemInteraction.create(this::onDebugCookiesClick))) + uiItems.add(Row(R.string.debug_cookies_title, create(this::onDebugCookiesClick))) + uiItems.add(Row(R.string.debug_settings_force_show_weekly_roundup, create(this::onForceShowWeeklyRoundupClick))) _uiState.value = UiState(uiItems) } @@ -73,6 +83,12 @@ class DebugSettingsViewModel _onNavigation.value = Event(DebugCookies) } + private fun onForceShowWeeklyRoundupClick() = launch(bgDispatcher) { + weeklyRoundupNotifier.buildNotifications().forEach { + notificationManager.notify(it.id, it.asNotificationCompatBuilder(contextProvider.getContext()).build()) + } + } + private fun buildDevelopedFeatures(): List { return FeaturesInDevelopment.featuresInDevelopment.map { name -> val value = if (manualFeatureConfig.hasManualSetup(name)) { diff --git a/WordPress/src/main/res/values/strings.xml b/WordPress/src/main/res/values/strings.xml index bae2fd3131f7..5aec08cb305b 100644 --- a/WordPress/src/main/res/values/strings.xml +++ b/WordPress/src/main/res/values/strings.xml @@ -913,6 +913,7 @@ Restart the app Don\'t see a feature you\'re working on? Check that your feature config file is annotated with the FeatureInDevelopment annotation. Tools + Force show Weekly Roundup notification Debug cookies From 78e8d9644f6bc93694f736eeafc5095f7962b4db Mon Sep 17 00:00:00 2001 From: Renan Ferrari Date: Tue, 14 Sep 2021 19:56:53 -0300 Subject: [PATCH 8/8] Update tests --- .../ui/debug/DebugSettingsViewModelTest.kt | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/WordPress/src/test/java/org/wordpress/android/ui/debug/DebugSettingsViewModelTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/debug/DebugSettingsViewModelTest.kt index 6ef2e22520d9..00bda11f1497 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/debug/DebugSettingsViewModelTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/debug/DebugSettingsViewModelTest.kt @@ -19,21 +19,36 @@ import org.wordpress.android.ui.debug.DebugSettingsViewModel.UiItem.Feature.Stat import org.wordpress.android.ui.debug.DebugSettingsViewModel.UiItem.Header import org.wordpress.android.ui.debug.DebugSettingsViewModel.UiItem.Row import org.wordpress.android.ui.debug.DebugSettingsViewModel.UiState +import org.wordpress.android.ui.notifications.NotificationManagerWrapper import org.wordpress.android.util.DebugUtils import org.wordpress.android.util.config.ManualFeatureConfig import org.wordpress.android.util.config.RemoteConfig +import org.wordpress.android.viewmodel.ContextProvider +import org.wordpress.android.workers.weeklyroundup.WeeklyRoundupNotifier class DebugSettingsViewModelTest : BaseUnitTest() { @Mock lateinit var manualFeatureConfig: ManualFeatureConfig @Mock lateinit var remoteConfig: RemoteConfig @Mock lateinit var debugUtils: DebugUtils + @Mock lateinit var weeklyRoundupNotifier: WeeklyRoundupNotifier + @Mock lateinit var notificationManager: NotificationManagerWrapper + @Mock lateinit var contextProvider: ContextProvider private lateinit var viewModel: DebugSettingsViewModel private val uiStates = mutableListOf() @InternalCoroutinesApi @Before fun setUp() { - viewModel = DebugSettingsViewModel(TEST_DISPATCHER, manualFeatureConfig, remoteConfig, debugUtils) + viewModel = DebugSettingsViewModel( + TEST_DISPATCHER, + TEST_DISPATCHER, + manualFeatureConfig, + remoteConfig, + debugUtils, + weeklyRoundupNotifier, + notificationManager, + contextProvider + ) } @Test