Skip to content

Commit

Permalink
Merge pull request #16621 from wordpress-mobile/issue/sync-bp-reminde…
Browse files Browse the repository at this point in the history
…rs-swith-with-store

Issue/sync bp reminders swith with store
  • Loading branch information
khaykov authored May 25, 2022
2 parents c16b020 + 5d68100 commit e824bb2
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class BloggingRemindersModelMapper
}
}.toSet(),
uiModel.hour,
uiModel.minute
uiModel.minute,
uiModel.isPromptIncluded
)
}

Expand All @@ -38,7 +39,8 @@ class BloggingRemindersModelMapper
domainModel.siteId,
domainModel.enabledDays.map { DayOfWeek.valueOf(it.name) }.toSet(),
domainModel.hour,
domainModel.minute
domainModel.minute,
domainModel.isPromptIncluded
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ data class BloggingRemindersUiModel(
val enabledDays: Set<DayOfWeek> = setOf(),
val hour: Int,
val minute: Int,
val isPromptIncluded: Boolean = true // TODO: get a real value here when implemented
val isPromptIncluded: Boolean
) {
fun getNotificationTime(): CharSequence =
LocalTime.of(hour, minute).format(DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ class BloggingRemindersViewModel @Inject constructor(
outState.putStringArrayList(SELECTED_DAYS, ArrayList(model.enabledDays.map { it.name }))
outState.putInt(SELECTED_HOUR, model.hour)
outState.putInt(SELECTED_MINUTE, model.minute)
outState.putBoolean(IS_BLOGGING_PROMPT_INCLUDED, model.isPromptIncluded)
}
_isFirstTimeFlow.value?.let {
outState.putBoolean(IS_FIRST_TIME_FLOW, it)
Expand All @@ -230,7 +231,14 @@ class BloggingRemindersViewModel @Inject constructor(
val enabledDays = state.getStringArrayList(SELECTED_DAYS)?.map { DayOfWeek.valueOf(it) }?.toSet() ?: setOf()
val selectedHour = state.getInt(SELECTED_HOUR)
val selectedMinute = state.getInt(SELECTED_MINUTE)
_bloggingRemindersModel.value = BloggingRemindersUiModel(siteId, enabledDays, selectedHour, selectedMinute)
val isPromptIncluded = state.getBoolean(IS_BLOGGING_PROMPT_INCLUDED)
_bloggingRemindersModel.value = BloggingRemindersUiModel(
siteId,
enabledDays,
selectedHour,
selectedMinute,
isPromptIncluded
)
}
_isFirstTimeFlow.value = state.getBoolean(IS_FIRST_TIME_FLOW)
}
Expand Down Expand Up @@ -295,6 +303,7 @@ class BloggingRemindersViewModel @Inject constructor(
private const val SELECTED_DAYS = "key_selected_days"
private const val SELECTED_HOUR = "key_selected_hour"
private const val SELECTED_MINUTE = "key_selected_minute"
private const val IS_BLOGGING_PROMPT_INCLUDED = "key_is_blogging_prompt_included"
private const val IS_FIRST_TIME_FLOW = "is_first_time_flow"
private const val SITE_ID = "key_site_id"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.core.app.NotificationCompat.PRIORITY_DEFAULT
import kotlinx.coroutines.flow.firstOrNull
import org.wordpress.android.R
import org.wordpress.android.fluxc.store.AccountStore
import org.wordpress.android.fluxc.store.BloggingRemindersStore
import org.wordpress.android.fluxc.store.SiteStore
import org.wordpress.android.fluxc.store.bloggingprompts.BloggingPromptsStore
import org.wordpress.android.push.NotificationPushIds.REMINDER_NOTIFICATION_ID
Expand All @@ -26,11 +27,9 @@ class PromptReminderNotifier @Inject constructor(
val accountStore: AccountStore,
val reminderNotificationManager: ReminderNotificationManager,
val bloggingPromptsFeatureConfig: BloggingPromptsFeatureConfig,
val bloggingPromptsStore: BloggingPromptsStore
val bloggingPromptsStore: BloggingPromptsStore,
private val bloggingRemindersStore: BloggingRemindersStore
) {
// TODO @RenanLukas replace with remote field in SiteModel after endpoint integration
var hasOptedInBloggingPromptsReminders = true

suspend fun notify(siteId: Int) {
val notificationId = REMINDER_NOTIFICATION_ID + siteId
val context = contextProvider.getContext()
Expand Down Expand Up @@ -89,11 +88,17 @@ class PromptReminderNotifier @Inject constructor(
// analyticsTracker.trackNotificationReceived()
}

fun shouldNotify(siteId: Int): Boolean {
suspend fun shouldNotify(siteId: Int): Boolean {
val hasAccessToken = accountStore.hasAccessToken()
val isBloggingPromptsEnabled = bloggingPromptsFeatureConfig.isEnabled()
val siteModel = siteStore.getSiteByLocalId(siteId)
val hasOptedInBloggingPromptsReminders = siteModel != null && hasOptedInBloggingPromptsReminders

val isPromptOptedIn = bloggingRemindersStore.bloggingRemindersModel(siteId).firstOrNull()?.isPromptIncluded
?: false
// TODO @klymyam check the value from the site model when we will start syncing the settings with remote
// val isPromptOptedIn = siteModel.isBloggingPromptsOptedIn

val hasOptedInBloggingPromptsReminders = siteModel != null && isPromptOptedIn
return hasAccessToken && isBloggingPromptsEnabled && hasOptedInBloggingPromptsReminders
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class BloggingRemindersAnalyticsTrackerTest {
@Test
fun `trackRemindersScheduled tracks correct event and properties`() {
bloggingRemindersUiModel = BloggingRemindersUiModel(
1, setOf(MONDAY, THURSDAY, FRIDAY), 14, 30)
1, setOf(MONDAY, THURSDAY, FRIDAY), 14, 30, true)
bloggingRemindersAnalyticsTracker.trackRemindersScheduled(
bloggingRemindersUiModel.enabledDays.size, bloggingRemindersUiModel.getNotificationTime24hour())
verify(analyticsTracker).track(eq(BLOGGING_REMINDERS_SCHEDULED), checkMap {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ class BloggingRemindersViewModelTest : BaseUnitTest() {
siteId,
setOf(DayOfWeek.MONDAY, DayOfWeek.SUNDAY),
hour,
minute
minute,
false
)
)
).thenReturn(dayLabel)
Expand Down Expand Up @@ -423,7 +424,7 @@ class BloggingRemindersViewModelTest : BaseUnitTest() {
private fun initEmptyStore(): BloggingRemindersUiModel {
val emptyModel = BloggingRemindersModel(siteId)
whenever(bloggingRemindersStore.bloggingRemindersModel(siteId)).thenReturn(flowOf(emptyModel))
return BloggingRemindersUiModel(siteId, hour = hour, minute = minute)
return BloggingRemindersUiModel(siteId, hour = hour, minute = minute, isPromptIncluded = false)
}

private fun assertPrologue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class DaySelectionBuilderTest {

@Test
fun `builds UI model with no selected days`() {
val bloggingRemindersModel = BloggingRemindersUiModel(1, hour = hour, minute = minute)
val bloggingRemindersModel = BloggingRemindersUiModel(1, hour = hour, minute = minute, isPromptIncluded = false)
val dayLabel = UiStringText("Not set")
whenever(dayLabelUtils.buildNTimesLabel(bloggingRemindersModel))
.thenReturn(dayLabel)
Expand All @@ -92,7 +92,13 @@ class DaySelectionBuilderTest {

@Test
fun `builds UI model with selected days`() {
val bloggingRemindersModel = BloggingRemindersUiModel(1, setOf(WEDNESDAY, SUNDAY), hour, minute)
val bloggingRemindersModel = BloggingRemindersUiModel(
1,
setOf(WEDNESDAY, SUNDAY),
hour,
minute,
isPromptIncluded = false
)
val dayLabel = UiStringText("Twice a week")
whenever(dayLabelUtils.buildNTimesLabel(bloggingRemindersModel))
.thenReturn(dayLabel)
Expand All @@ -110,7 +116,7 @@ class DaySelectionBuilderTest {

@Test
fun `click on a day select day`() {
val bloggingRemindersModel = BloggingRemindersUiModel(1, hour = hour, minute = minute)
val bloggingRemindersModel = BloggingRemindersUiModel(1, hour = hour, minute = minute, isPromptIncluded = false)
whenever(dayLabelUtils.buildNTimesLabel(bloggingRemindersModel)).thenReturn(UiStringText("Once a week"))

val uiModel = daySelectionBuilder.buildSelection(
Expand All @@ -129,7 +135,7 @@ class DaySelectionBuilderTest {

@Test
fun `primary button disabled when model is empty and is first time flow`() {
val bloggingRemindersModel = BloggingRemindersUiModel(1, hour = hour, minute = minute)
val bloggingRemindersModel = BloggingRemindersUiModel(1, hour = hour, minute = minute, isPromptIncluded = false)

val primaryButton = daySelectionBuilder.buildPrimaryButton(bloggingRemindersModel, true, onConfirm)

Expand All @@ -144,7 +150,13 @@ class DaySelectionBuilderTest {

@Test
fun `primary button enabled when model is not empty and is first time flow`() {
val bloggingRemindersModel = BloggingRemindersUiModel(1, setOf(WEDNESDAY, SUNDAY), hour, minute)
val bloggingRemindersModel = BloggingRemindersUiModel(
1,
setOf(WEDNESDAY, SUNDAY),
hour,
minute,
isPromptIncluded = false
)

val primaryButton = daySelectionBuilder.buildPrimaryButton(bloggingRemindersModel, true, onConfirm)

Expand All @@ -159,7 +171,7 @@ class DaySelectionBuilderTest {

@Test
fun `primary button enabled when model is empty and is not first time flow`() {
val bloggingRemindersModel = BloggingRemindersUiModel(1, hour = hour, minute = minute)
val bloggingRemindersModel = BloggingRemindersUiModel(1, hour = hour, minute = minute, isPromptIncluded = false)

val primaryButton = daySelectionBuilder.buildPrimaryButton(bloggingRemindersModel, false, onConfirm)

Expand All @@ -176,7 +188,13 @@ class DaySelectionBuilderTest {
fun `primary button shows a different label when blogging prompt FF is on`() {
whenever(bloggingPromptsFeatureConfig.isEnabled()).thenReturn(true)

val bloggingRemindersModel = BloggingRemindersUiModel(1, setOf(WEDNESDAY, SUNDAY), hour, minute)
val bloggingRemindersModel = BloggingRemindersUiModel(
1,
setOf(WEDNESDAY, SUNDAY),
hour,
minute,
isPromptIncluded = false
)

val primaryButton = daySelectionBuilder.buildPrimaryButton(bloggingRemindersModel, true, onConfirm)

Expand All @@ -191,7 +209,7 @@ class DaySelectionBuilderTest {

@Test
fun `click on primary button confirm selection`() {
val bloggingRemindersModel = BloggingRemindersUiModel(1, setOf(WEDNESDAY, SUNDAY), hour, minute)
val bloggingRemindersModel = BloggingRemindersUiModel(1, setOf(WEDNESDAY, SUNDAY), hour, minute, false)

val primaryButton = daySelectionBuilder.buildPrimaryButton(bloggingRemindersModel, false, onConfirm)

Expand All @@ -204,7 +222,7 @@ class DaySelectionBuilderTest {
fun `include prompt switch is visible when days are selected`() {
whenever(bloggingPromptsFeatureConfig.isEnabled()).thenReturn(true)

val bloggingRemindersModel = BloggingRemindersUiModel(1, setOf(WEDNESDAY, SUNDAY), hour, minute)
val bloggingRemindersModel = BloggingRemindersUiModel(1, setOf(WEDNESDAY, SUNDAY), hour, minute, false)
val dayLabel = UiStringText("Twice a week")
whenever(dayLabelUtils.buildNTimesLabel(bloggingRemindersModel))
.thenReturn(dayLabel)
Expand All @@ -223,7 +241,7 @@ class DaySelectionBuilderTest {

@Test
fun `include prompt switch is not visible when days are not selected`() {
val bloggingRemindersModel = BloggingRemindersUiModel(1, hour = hour, minute = minute)
val bloggingRemindersModel = BloggingRemindersUiModel(1, hour = hour, minute = minute, isPromptIncluded = false)
val dayLabel = UiStringText("Not set")
whenever(dayLabelUtils.buildNTimesLabel(bloggingRemindersModel))
.thenReturn(dayLabel)
Expand All @@ -244,7 +262,7 @@ class DaySelectionBuilderTest {
fun `single include prompt switch is visible when FF is on`() {
whenever(bloggingPromptsFeatureConfig.isEnabled()).thenReturn(true)

val bloggingRemindersModel = BloggingRemindersUiModel(1, setOf(WEDNESDAY, SUNDAY), hour, minute)
val bloggingRemindersModel = BloggingRemindersUiModel(1, setOf(WEDNESDAY, SUNDAY), hour, minute, false)
val dayLabel = UiStringText("Twice a week")
whenever(dayLabelUtils.buildNTimesLabel(bloggingRemindersModel))
.thenReturn(dayLabel)
Expand All @@ -265,7 +283,7 @@ class DaySelectionBuilderTest {
fun `include prompt switch is not visible when FF is off`() {
whenever(bloggingPromptsFeatureConfig.isEnabled()).thenReturn(false)

val bloggingRemindersModel = BloggingRemindersUiModel(1, setOf(WEDNESDAY, SUNDAY), hour, minute)
val bloggingRemindersModel = BloggingRemindersUiModel(1, setOf(WEDNESDAY, SUNDAY), hour, minute, false)
val dayLabel = UiStringText("Twice a week")
whenever(dayLabelUtils.buildNTimesLabel(bloggingRemindersModel))
.thenReturn(dayLabel)
Expand All @@ -286,7 +304,7 @@ class DaySelectionBuilderTest {
fun `click on a prompt switch toggles the prompt state`() {
whenever(bloggingPromptsFeatureConfig.isEnabled()).thenReturn(true)

val bloggingRemindersModel = BloggingRemindersUiModel(1, setOf(WEDNESDAY, SUNDAY), hour, minute)
val bloggingRemindersModel = BloggingRemindersUiModel(1, setOf(WEDNESDAY, SUNDAY), hour, minute, false)
val dayLabel = UiStringText("Twice a week")
whenever(dayLabelUtils.buildNTimesLabel(bloggingRemindersModel))
.thenReturn(dayLabel)
Expand All @@ -309,7 +327,7 @@ class DaySelectionBuilderTest {
fun `click on a blogging prompt help button shows blogging prompt dialog`() {
whenever(bloggingPromptsFeatureConfig.isEnabled()).thenReturn(true)

val bloggingRemindersModel = BloggingRemindersUiModel(1, setOf(WEDNESDAY, SUNDAY), hour, minute)
val bloggingRemindersModel = BloggingRemindersUiModel(1, setOf(WEDNESDAY, SUNDAY), hour, minute, false)
val dayLabel = UiStringText("Twice a week")
whenever(dayLabelUtils.buildNTimesLabel(bloggingRemindersModel))
.thenReturn(dayLabel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class EpilogueBuilderTest {

@Test
fun `builds UI model with no selected days`() {
val bloggingRemindersModel = BloggingRemindersUiModel(1, setOf(), hour, minute)
val bloggingRemindersModel = BloggingRemindersUiModel(1, setOf(), hour, minute, false)
val uiModel = epilogueBuilder.buildUiItems(bloggingRemindersModel)

assertModelWithNoSelection(uiModel)
Expand All @@ -58,7 +58,8 @@ class EpilogueBuilderTest {
1,
setOf(DayOfWeek.WEDNESDAY, DayOfWeek.SUNDAY),
hour,
minute
minute,
false
)
val dayLabel = "twice"
whenever(dayLabelUtils.buildLowercaseNTimesLabel(bloggingRemindersModel))
Expand Down Expand Up @@ -88,7 +89,8 @@ class EpilogueBuilderTest {
1,
DayOfWeek.values().toSet(),
hour,
minute
minute,
false
)
val message = "You'll get reminders to blog <b>everyday</b> at <b>10:00 am</b>."
whenever(
Expand Down
Loading

0 comments on commit e824bb2

Please sign in to comment.