Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import org.assertj.core.api.Assertions.assertThat
import org.junit.Before
import org.junit.Test

@Suppress("UnitTestNamingRule")
class AppPrefsTest {
@Before
fun setup() {
Expand Down Expand Up @@ -593,4 +592,44 @@ class AppPrefsTest {

assertThat(AppPrefs.isPOSLaunchableForSite(siteId)).isFalse
}

@Test
fun givenWooPosSurveyNotificationCurrentUserShownNotSetThenReturnFalseByDefault() {
assertThat(AppPrefs.isWooPosSurveyNotificationCurrentUserShown).isFalse
}

@Test
fun givenWooPosSurveyNotificationCurrentUserShownSetToTrueThenReturnTrue() {
AppPrefs.isWooPosSurveyNotificationCurrentUserShown = true

assertThat(AppPrefs.isWooPosSurveyNotificationCurrentUserShown).isTrue
}

@Test
fun givenWooPosSurveyNotificationCurrentUserShownSetToFalseThenReturnFalse() {
AppPrefs.isWooPosSurveyNotificationCurrentUserShown = true
AppPrefs.isWooPosSurveyNotificationCurrentUserShown = false

assertThat(AppPrefs.isWooPosSurveyNotificationCurrentUserShown).isFalse
}

@Test
fun givenWooPosSurveyNotificationPotentialUserShownNotSetThenReturnFalseByDefault() {
assertThat(AppPrefs.isWooPosSurveyNotificationPotentialUserShown).isFalse
}

@Test
fun givenWooPosSurveyNotificationPotentialUserShownSetToTrueThenReturnTrue() {
AppPrefs.isWooPosSurveyNotificationPotentialUserShown = true

assertThat(AppPrefs.isWooPosSurveyNotificationPotentialUserShown).isTrue
}

@Test
fun givenWooPosSurveyNotificationPotentialUserShownSetToFalseThenReturnFalse() {
AppPrefs.isWooPosSurveyNotificationPotentialUserShown = true
AppPrefs.isWooPosSurveyNotificationPotentialUserShown = false

assertThat(AppPrefs.isWooPosSurveyNotificationPotentialUserShown).isFalse
}
}
14 changes: 13 additions & 1 deletion WooCommerce/src/main/kotlin/com/woocommerce/android/AppPrefs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,11 @@ object AppPrefs {

POS_TAB_VISIBILITY,

POS_LAUNCHABLE
POS_LAUNCHABLE,

WOO_POS_SURVEY_NOTIFICATION_CURRENT_USER_SHOWN,

WOO_POS_SURVEY_NOTIFICATION_POTENTIAL_USER_SHOWN
}

fun init(context: Context) {
Expand Down Expand Up @@ -1353,6 +1357,14 @@ object AppPrefs {
)
}

var isWooPosSurveyNotificationCurrentUserShown: Boolean
get() = getBoolean(UndeletablePrefKey.WOO_POS_SURVEY_NOTIFICATION_CURRENT_USER_SHOWN, false)
set(value) = setBoolean(UndeletablePrefKey.WOO_POS_SURVEY_NOTIFICATION_CURRENT_USER_SHOWN, value)

var isWooPosSurveyNotificationPotentialUserShown: Boolean
get() = getBoolean(UndeletablePrefKey.WOO_POS_SURVEY_NOTIFICATION_POTENTIAL_USER_SHOWN, false)
set(value) = setBoolean(UndeletablePrefKey.WOO_POS_SURVEY_NOTIFICATION_POTENTIAL_USER_SHOWN, value)

enum class CardReaderOnboardingStatus {
CARD_READER_ONBOARDING_COMPLETED,
CARD_READER_ONBOARDING_PENDING,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.woocommerce.android.ui.woopos.util.analytics.WooPosAnalyticsEvent
import com.woocommerce.android.ui.woopos.util.analytics.WooPosAnalyticsEvent.Event.SearchButtonTapped
import com.woocommerce.android.ui.woopos.util.analytics.WooPosAnalyticsEventConstant
import com.woocommerce.android.ui.woopos.util.analytics.WooPosAnalyticsTracker
import com.woocommerce.android.ui.woopos.util.datastore.WooPosPreferencesRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
Expand All @@ -32,6 +33,7 @@ class WooPosItemsViewModel @Inject constructor(
private val couponCreationFacade: WooPosCouponCreationFacade,
private val fromChildToParentEventSender: WooPosChildrenToParentEventSender,
private val parentToChildrenEventReceiver: WooPosParentToChildrenEventReceiver,
private val preferencesRepository: WooPosPreferencesRepository,
private val analyticsTracker: WooPosAnalyticsTracker,
) : ViewModel() {
private var preservedStateBeforeOpeningVariations: WooPosItemsToolbarViewState? = null
Expand All @@ -49,6 +51,10 @@ class WooPosItemsViewModel @Inject constructor(
coroutineScope = viewModelScope,
viewStateFlow = _viewState
)

viewModelScope.launch {
preferencesRepository.setWasOpenedOnce(true)
}
}

fun onUIEvent(event: WooPosItemsUIEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.woocommerce.android.ui.woopos.util.datastore

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import com.woocommerce.android.tools.SelectedSite
Expand All @@ -15,6 +16,7 @@ class WooPosPreferencesRepository @Inject constructor(
) {
private val recentProductSearchesSiteSpecificKey = buildSiteSpecificKey(RECENT_PRODUCT_SEARCHES_KEY)
private val recentCouponSearchesSiteSpecificKey = buildSiteSpecificKey(RECENT_COUPON_SEARCHES_KEY)
private val wasOpenedOnceKey = booleanPreferencesKey(POS_WAS_OPENED_ONCE_KEY)

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

val wasOpenedOnce: Flow<Boolean> = dataStore.data
.map { preferences ->
preferences[wasOpenedOnceKey] ?: false
}

suspend fun addRecentProductSearch(search: String) {
dataStore.edit { preferences ->
val currentSearches = preferences[recentProductSearchesSiteSpecificKey]?.let {
Expand Down Expand Up @@ -56,12 +63,19 @@ class WooPosPreferencesRepository @Inject constructor(
}
}

suspend fun setWasOpenedOnce(wasOpened: Boolean) {
dataStore.edit { preferences ->
preferences[wasOpenedOnceKey] = wasOpened
}
}

private fun buildSiteSpecificKey(key: String): Preferences.Key<String> =
stringPreferencesKey("${selectedSite.getOrNull()?.siteId}-$key")

private companion object {
const val RECENT_PRODUCT_SEARCHES_KEY = "recent_product_searches_key"
const val RECENT_COUPON_SEARCHES_KEY = "recent_coupon_searches_key"
const val POS_WAS_OPENED_ONCE_KEY = "pos_was_opened_once_key"

const val MAX_RECENT_SEARCHES_COUNT = 10
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.woocommerce.android.ui.woopos.util.analytics.WooPosAnalyticsEvent
import com.woocommerce.android.ui.woopos.util.analytics.WooPosAnalyticsEvent.Event.SearchButtonTapped
import com.woocommerce.android.ui.woopos.util.analytics.WooPosAnalyticsEventConstant
import com.woocommerce.android.ui.woopos.util.analytics.WooPosAnalyticsTracker
import com.woocommerce.android.ui.woopos.util.datastore.WooPosPreferencesRepository
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
Expand Down Expand Up @@ -53,6 +54,7 @@ class WooPosItemsViewModelTest {
private val couponCreationFacade: WooPosCouponCreationFacade = mock()
private val fromChildToParentEventSender: WooPosChildrenToParentEventSender = mock()
private val parentToChildrenEventReceiver: WooPosParentToChildrenEventReceiver = mock()
private val preferencesRepository: WooPosPreferencesRepository = mock()

@Before
fun setup() {
Expand Down Expand Up @@ -435,13 +437,25 @@ class WooPosItemsViewModelTest {
verify(searchHelper).updateLoadingState(isLoading = false)
}

@Test
fun `when view model created, then wasOpenedOnce is set to true`() = runTest {
// GIVEN

// WHEN
createViewModel()

// THEN
verify(preferencesRepository).setWasOpenedOnce(true)
}

private fun createViewModel(): WooPosItemsViewModel {
return WooPosItemsViewModel(
searchHelper = searchHelper,
tabsHelper = tabsHelper,
couponCreationFacade = couponCreationFacade,
fromChildToParentEventSender = fromChildToParentEventSender,
parentToChildrenEventReceiver = parentToChildrenEventReceiver,
preferencesRepository = preferencesRepository,
analyticsTracker = analyticsTracker,
)
}
Expand Down