Skip to content

Commit 2b8265c

Browse files
authored
Merge pull request #14677 from woocommerce/woomob-1410-woo-possurveys-implement-persistant-flags
[WOOMOB-1410][Woo POS][Surveys] Implement persistent flags
2 parents 4aa44eb + 03bff3e commit 2b8265c

File tree

5 files changed

+87
-2
lines changed

5 files changed

+87
-2
lines changed

WooCommerce/src/androidTest/kotlin/com/woocommerce/android/AppPrefsTest.kt

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import org.assertj.core.api.Assertions.assertThat
1010
import org.junit.Before
1111
import org.junit.Test
1212

13-
@Suppress("UnitTestNamingRule")
1413
class AppPrefsTest {
1514
@Before
1615
fun setup() {
@@ -593,4 +592,44 @@ class AppPrefsTest {
593592

594593
assertThat(AppPrefs.isPOSLaunchableForSite(siteId)).isFalse
595594
}
595+
596+
@Test
597+
fun givenWooPosSurveyNotificationCurrentUserShownNotSetThenReturnFalseByDefault() {
598+
assertThat(AppPrefs.isWooPosSurveyNotificationCurrentUserShown).isFalse
599+
}
600+
601+
@Test
602+
fun givenWooPosSurveyNotificationCurrentUserShownSetToTrueThenReturnTrue() {
603+
AppPrefs.isWooPosSurveyNotificationCurrentUserShown = true
604+
605+
assertThat(AppPrefs.isWooPosSurveyNotificationCurrentUserShown).isTrue
606+
}
607+
608+
@Test
609+
fun givenWooPosSurveyNotificationCurrentUserShownSetToFalseThenReturnFalse() {
610+
AppPrefs.isWooPosSurveyNotificationCurrentUserShown = true
611+
AppPrefs.isWooPosSurveyNotificationCurrentUserShown = false
612+
613+
assertThat(AppPrefs.isWooPosSurveyNotificationCurrentUserShown).isFalse
614+
}
615+
616+
@Test
617+
fun givenWooPosSurveyNotificationPotentialUserShownNotSetThenReturnFalseByDefault() {
618+
assertThat(AppPrefs.isWooPosSurveyNotificationPotentialUserShown).isFalse
619+
}
620+
621+
@Test
622+
fun givenWooPosSurveyNotificationPotentialUserShownSetToTrueThenReturnTrue() {
623+
AppPrefs.isWooPosSurveyNotificationPotentialUserShown = true
624+
625+
assertThat(AppPrefs.isWooPosSurveyNotificationPotentialUserShown).isTrue
626+
}
627+
628+
@Test
629+
fun givenWooPosSurveyNotificationPotentialUserShownSetToFalseThenReturnFalse() {
630+
AppPrefs.isWooPosSurveyNotificationPotentialUserShown = true
631+
AppPrefs.isWooPosSurveyNotificationPotentialUserShown = false
632+
633+
assertThat(AppPrefs.isWooPosSurveyNotificationPotentialUserShown).isFalse
634+
}
596635
}

WooCommerce/src/main/kotlin/com/woocommerce/android/AppPrefs.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,11 @@ object AppPrefs {
213213

214214
POS_TAB_VISIBILITY,
215215

216-
POS_LAUNCHABLE
216+
POS_LAUNCHABLE,
217+
218+
WOO_POS_SURVEY_NOTIFICATION_CURRENT_USER_SHOWN,
219+
220+
WOO_POS_SURVEY_NOTIFICATION_POTENTIAL_USER_SHOWN
217221
}
218222

219223
fun init(context: Context) {
@@ -1353,6 +1357,14 @@ object AppPrefs {
13531357
)
13541358
}
13551359

1360+
var isWooPosSurveyNotificationCurrentUserShown: Boolean
1361+
get() = getBoolean(UndeletablePrefKey.WOO_POS_SURVEY_NOTIFICATION_CURRENT_USER_SHOWN, false)
1362+
set(value) = setBoolean(UndeletablePrefKey.WOO_POS_SURVEY_NOTIFICATION_CURRENT_USER_SHOWN, value)
1363+
1364+
var isWooPosSurveyNotificationPotentialUserShown: Boolean
1365+
get() = getBoolean(UndeletablePrefKey.WOO_POS_SURVEY_NOTIFICATION_POTENTIAL_USER_SHOWN, false)
1366+
set(value) = setBoolean(UndeletablePrefKey.WOO_POS_SURVEY_NOTIFICATION_POTENTIAL_USER_SHOWN, value)
1367+
13561368
enum class CardReaderOnboardingStatus {
13571369
CARD_READER_ONBOARDING_COMPLETED,
13581370
CARD_READER_ONBOARDING_PENDING,

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/items/WooPosItemsViewModel.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import com.woocommerce.android.ui.woopos.util.analytics.WooPosAnalyticsEvent
1616
import com.woocommerce.android.ui.woopos.util.analytics.WooPosAnalyticsEvent.Event.SearchButtonTapped
1717
import com.woocommerce.android.ui.woopos.util.analytics.WooPosAnalyticsEventConstant
1818
import com.woocommerce.android.ui.woopos.util.analytics.WooPosAnalyticsTracker
19+
import com.woocommerce.android.ui.woopos.util.datastore.WooPosPreferencesRepository
1920
import dagger.hilt.android.lifecycle.HiltViewModel
2021
import kotlinx.coroutines.flow.MutableStateFlow
2122
import kotlinx.coroutines.flow.SharingStarted
@@ -32,6 +33,7 @@ class WooPosItemsViewModel @Inject constructor(
3233
private val couponCreationFacade: WooPosCouponCreationFacade,
3334
private val fromChildToParentEventSender: WooPosChildrenToParentEventSender,
3435
private val parentToChildrenEventReceiver: WooPosParentToChildrenEventReceiver,
36+
private val preferencesRepository: WooPosPreferencesRepository,
3537
private val analyticsTracker: WooPosAnalyticsTracker,
3638
) : ViewModel() {
3739
private var preservedStateBeforeOpeningVariations: WooPosItemsToolbarViewState? = null
@@ -49,6 +51,10 @@ class WooPosItemsViewModel @Inject constructor(
4951
coroutineScope = viewModelScope,
5052
viewStateFlow = _viewState
5153
)
54+
55+
viewModelScope.launch {
56+
preferencesRepository.setWasOpenedOnce(true)
57+
}
5258
}
5359

5460
fun onUIEvent(event: WooPosItemsUIEvent) {

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/util/datastore/WooPosPreferencesRepository.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.woocommerce.android.ui.woopos.util.datastore
22

33
import androidx.datastore.core.DataStore
44
import androidx.datastore.preferences.core.Preferences
5+
import androidx.datastore.preferences.core.booleanPreferencesKey
56
import androidx.datastore.preferences.core.edit
67
import androidx.datastore.preferences.core.stringPreferencesKey
78
import com.woocommerce.android.tools.SelectedSite
@@ -15,6 +16,7 @@ class WooPosPreferencesRepository @Inject constructor(
1516
) {
1617
private val recentProductSearchesSiteSpecificKey = buildSiteSpecificKey(RECENT_PRODUCT_SEARCHES_KEY)
1718
private val recentCouponSearchesSiteSpecificKey = buildSiteSpecificKey(RECENT_COUPON_SEARCHES_KEY)
19+
private val wasOpenedOnceKey = booleanPreferencesKey(POS_WAS_OPENED_ONCE_KEY)
1820

1921
val recentProductSearches: Flow<List<String>> = dataStore.data
2022
.map { preferences ->
@@ -28,6 +30,11 @@ class WooPosPreferencesRepository @Inject constructor(
2830
if (searchesString.isEmpty()) emptyList() else searchesString.split(",")
2931
}
3032

33+
val wasOpenedOnce: Flow<Boolean> = dataStore.data
34+
.map { preferences ->
35+
preferences[wasOpenedOnceKey] ?: false
36+
}
37+
3138
suspend fun addRecentProductSearch(search: String) {
3239
dataStore.edit { preferences ->
3340
val currentSearches = preferences[recentProductSearchesSiteSpecificKey]?.let {
@@ -56,12 +63,19 @@ class WooPosPreferencesRepository @Inject constructor(
5663
}
5764
}
5865

66+
suspend fun setWasOpenedOnce(wasOpened: Boolean) {
67+
dataStore.edit { preferences ->
68+
preferences[wasOpenedOnceKey] = wasOpened
69+
}
70+
}
71+
5972
private fun buildSiteSpecificKey(key: String): Preferences.Key<String> =
6073
stringPreferencesKey("${selectedSite.getOrNull()?.siteId}-$key")
6174

6275
private companion object {
6376
const val RECENT_PRODUCT_SEARCHES_KEY = "recent_product_searches_key"
6477
const val RECENT_COUPON_SEARCHES_KEY = "recent_coupon_searches_key"
78+
const val POS_WAS_OPENED_ONCE_KEY = "pos_was_opened_once_key"
6579

6680
const val MAX_RECENT_SEARCHES_COUNT = 10
6781
}

WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/items/WooPosItemsViewModelTest.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.woocommerce.android.ui.woopos.util.analytics.WooPosAnalyticsEvent
1414
import com.woocommerce.android.ui.woopos.util.analytics.WooPosAnalyticsEvent.Event.SearchButtonTapped
1515
import com.woocommerce.android.ui.woopos.util.analytics.WooPosAnalyticsEventConstant
1616
import com.woocommerce.android.ui.woopos.util.analytics.WooPosAnalyticsTracker
17+
import com.woocommerce.android.ui.woopos.util.datastore.WooPosPreferencesRepository
1718
import kotlinx.coroutines.ExperimentalCoroutinesApi
1819
import kotlinx.coroutines.flow.flowOf
1920
import kotlinx.coroutines.test.runTest
@@ -53,6 +54,7 @@ class WooPosItemsViewModelTest {
5354
private val couponCreationFacade: WooPosCouponCreationFacade = mock()
5455
private val fromChildToParentEventSender: WooPosChildrenToParentEventSender = mock()
5556
private val parentToChildrenEventReceiver: WooPosParentToChildrenEventReceiver = mock()
57+
private val preferencesRepository: WooPosPreferencesRepository = mock()
5658

5759
@Before
5860
fun setup() {
@@ -435,13 +437,25 @@ class WooPosItemsViewModelTest {
435437
verify(searchHelper).updateLoadingState(isLoading = false)
436438
}
437439

440+
@Test
441+
fun `when view model created, then wasOpenedOnce is set to true`() = runTest {
442+
// GIVEN
443+
444+
// WHEN
445+
createViewModel()
446+
447+
// THEN
448+
verify(preferencesRepository).setWasOpenedOnce(true)
449+
}
450+
438451
private fun createViewModel(): WooPosItemsViewModel {
439452
return WooPosItemsViewModel(
440453
searchHelper = searchHelper,
441454
tabsHelper = tabsHelper,
442455
couponCreationFacade = couponCreationFacade,
443456
fromChildToParentEventSender = fromChildToParentEventSender,
444457
parentToChildrenEventReceiver = parentToChildrenEventReceiver,
458+
preferencesRepository = preferencesRepository,
445459
analyticsTracker = analyticsTracker,
446460
)
447461
}

0 commit comments

Comments
 (0)