-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Add App Theme Selection and Preferences DataStore (#15)
- Loading branch information
1 parent
75b988f
commit 751d96d
Showing
74 changed files
with
696 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...der/ui/component/MovieFinderAppBarTest.kt → ...ore/ui/component/MovieFinderAppBarTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletions
10
...inder/data/local/database/MovieDaoTest.kt → ...movie/data/local/database/MovieDaoTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...finder/ui/screen/MovieDetailScreenTest.kt → .../movie/ui/screen/MovieDetailScreenTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...oviefinder/userpreferences/data/source/local/DefaultUserPreferencesLocalDataSourceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.jogasoft.moviefinder.userpreferences.data.source.local | ||
|
||
import android.content.Context | ||
import androidx.datastore.preferences.core.PreferenceDataStoreFactory | ||
import androidx.datastore.preferences.core.edit | ||
import androidx.datastore.preferences.preferencesDataStoreFile | ||
import androidx.test.core.app.ApplicationProvider | ||
import com.jogasoft.moviefinder.userpreferences.data.model.ThemePreference | ||
import junit.framework.TestCase.assertEquals | ||
import junit.framework.TestCase.assertNotSame | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.test.StandardTestDispatcher | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
class DefaultUserPreferencesLocalDataSourceTest { | ||
private val context = ApplicationProvider.getApplicationContext<Context>() | ||
private val testDispatcher = StandardTestDispatcher() | ||
private val testCoroutineScope = CoroutineScope(testDispatcher) | ||
private val dataStore = PreferenceDataStoreFactory.create( | ||
scope = testCoroutineScope, | ||
produceFile = { | ||
context.preferencesDataStoreFile("test_user_preferences_dataStore") | ||
} | ||
) | ||
private lateinit var defaultUserPreferencesLocalDataSource: DefaultUserPreferencesLocalDataSource | ||
|
||
@Before | ||
fun setUp() { | ||
defaultUserPreferencesLocalDataSource = DefaultUserPreferencesLocalDataSource(dataStore) | ||
} | ||
|
||
@After | ||
fun tearDown() { | ||
testCoroutineScope.launch { dataStore.edit { it.clear() } } | ||
testCoroutineScope.cancel() | ||
} | ||
|
||
@Test | ||
fun confirm_default_theme_is_system() { | ||
testCoroutineScope.launch { | ||
val theme = defaultUserPreferencesLocalDataSource.observeThemePreference.first() | ||
assertEquals(ThemePreference.SYSTEM, theme) | ||
} | ||
} | ||
|
||
@Test | ||
fun updateThemePreference_properly_updates_dataStore() { | ||
testCoroutineScope.launch { | ||
val initialTheme = defaultUserPreferencesLocalDataSource.observeThemePreference.first() | ||
assertNotSame(ThemePreference.DARK, initialTheme) | ||
|
||
defaultUserPreferencesLocalDataSource.updateThemePreference(ThemePreference.DARK) | ||
|
||
val updatedTheme = defaultUserPreferencesLocalDataSource.observeThemePreference.first() | ||
|
||
assertEquals(ThemePreference.DARK, updatedTheme) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...va/com/jogasoft/MovieFinderApplication.kt → ...oviefinder/core/MovieFinderApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 18 additions & 5 deletions
23
...m/jogasoft/moviefinder/ui/HomeActivity.kt → ...asoft/moviefinder/core/ui/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
app/src/main/java/com/jogasoft/moviefinder/core/ui/MainViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.jogasoft.moviefinder.core.ui | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.jogasoft.moviefinder.userpreferences.data.UserPreferenceRepository | ||
import com.jogasoft.moviefinder.userpreferences.data.model.ThemePreference | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.flow.SharingStarted | ||
import kotlinx.coroutines.flow.mapLatest | ||
import kotlinx.coroutines.flow.stateIn | ||
import javax.inject.Inject | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
@HiltViewModel | ||
class MainViewModel @Inject constructor( | ||
userPreferenceRepository: UserPreferenceRepository | ||
): ViewModel() { | ||
val uiState = userPreferenceRepository.observeThemePreference.mapLatest { theme -> | ||
MainUiState(theme) | ||
}.stateIn( | ||
started = SharingStarted.WhileSubscribed(5000L), | ||
scope = viewModelScope, | ||
initialValue = MainUiState() | ||
) | ||
} | ||
|
||
data class MainUiState( | ||
val theme: ThemePreference = ThemePreference.SYSTEM | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.