Skip to content
Open
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 @@ -59,12 +59,10 @@ class MainActivity : ComponentActivity() {

LaunchedEffect(key1 = hasSwiped) {
if (!hasSwiped) {
coroutineScope.launch {
delay(5000)
scaffoldState.snackbarHostState.showSnackbar(
message = getString(R.string.swipe_to_see_more)
)
}
delay(5000)
scaffoldState.snackbarHostState.showSnackbar(
message = getString(R.string.swipe_to_see_more)
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tt.co.jesses.moonlight.common.data.model

data class UserPreferences(
val analyticsAcceptance: Int = AnalyticsAcceptance.UNSET.ordinal,
val hasSwiped: Boolean = false,
)

enum class AnalyticsAcceptance {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ class UserPreferencesRepository @Inject constructor(
) {
private val _isAnalyticsPreferencePending = MutableStateFlow(true)

val hasSwiped: Flow<Boolean> = dataStore.data.map { preferences ->
preferences[PreferencesKeys.HAS_SWIPED] ?: false
}
val hasSwiped: Flow<Boolean> = dataStore.data
.map { preferences ->
preferences[PreferencesKeys.HAS_SWIPED] ?: false
}

suspend fun setHasSwiped(hasSwiped: Boolean) {
dataStore.edit { preferences ->
Expand All @@ -29,9 +30,10 @@ class UserPreferencesRepository @Inject constructor(
}

suspend fun fetchInitialPreferences(): UserPreferences {
val preferences = dataStore.data.first().toPreferences()
val preferences = dataStore.data.first()
return UserPreferences(
analyticsAcceptance = preferences[PreferencesKeys.ANALYTICS_ACCEPTANCE] ?: AnalyticsAcceptance.UNSET.ordinal
analyticsAcceptance = preferences[PreferencesKeys.ANALYTICS_ACCEPTANCE] ?: AnalyticsAcceptance.UNSET.ordinal,
hasSwiped = preferences[PreferencesKeys.HAS_SWIPED] ?: false
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package tt.co.jesses.moonlight.common.data.repository

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.MutablePreferences
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.emptyPreferences
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.mutablePreferencesOf
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.kotlin.any
Expand All @@ -26,6 +30,7 @@ class UserPreferencesRepositoryTest {
@BeforeEach
fun setUp() {
dataStore = mock()
whenever(dataStore.data).thenReturn(flowOf(emptyPreferences()))
repository = UserPreferencesRepository(dataStore)
}

Expand Down Expand Up @@ -65,8 +70,49 @@ class UserPreferencesRepositoryTest {
repository.updateAnalyticsAcceptance(acceptance)

// Then
argumentCaptor<suspend (Preferences) -> Unit>().apply {
argumentCaptor<suspend (MutablePreferences) -> Unit>().apply {
verify(dataStore).edit(capture())
}
}

@Test
fun hasSwipedShouldEmitValueFromDatastore() = runTest {
// Given
val key = booleanPreferencesKey("has_swiped")
val preferences = mutablePreferencesOf(key to true)
whenever(dataStore.data).thenReturn(flowOf(preferences))
val repository = UserPreferencesRepository(dataStore)

// When
val result = repository.hasSwiped.first()

// Then
assertEquals(true, result)
}

@Test
fun `setHasSwiped should update datastore`() = runTest {
// When
repository.setHasSwiped(true)

// Then
argumentCaptor<suspend (MutablePreferences) -> Unit>().apply {
verify(dataStore).edit(capture())
}
}

@Test
fun `fetchInitialPreferences should return correct hasSwiped`() = runTest {
// Given
val key = booleanPreferencesKey("has_swiped")
val preferences = mutablePreferencesOf(key to true)
whenever(dataStore.data).thenReturn(flowOf(preferences))
val repository = UserPreferencesRepository(dataStore)

// When
val result = repository.fetchInitialPreferences()

// Then
assertTrue(result.hasSwiped)
}
}
2 changes: 1 addition & 1 deletion app/gradle.properties
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these additions necessary?

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
### Gradle
org.gradle.jvmargs=-Xmx2048M -Dfile.encoding=UTF-8 -Dkotlin.daemon.jvm.options\="-Xmx2048M"
org.gradle.jvmargs=-Xmx2048M -Dfile.encoding=UTF-8 --add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED --add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED --add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED --add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED

# When set to true, Gradle will try to reuse outputs from previous builds
org.gradle.caching=true
Expand Down