-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
20 changed files
with
225 additions
and
34 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
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
18 changes: 18 additions & 0 deletions
18
.../src/main/java/com/droidknights/app2023/core/data/repository/DefaultSettingsRepository.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,18 @@ | ||
package com.droidknights.app2023.core.data.repository | ||
|
||
import com.droidknights.app2023.core.datastore.SettingsPreferencesDataSource | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
import javax.inject.Inject | ||
|
||
internal class DefaultSettingsRepository @Inject constructor( | ||
private val preferencesDataSource: SettingsPreferencesDataSource | ||
) : SettingsRepository { | ||
|
||
override fun getIsDarkTheme(): Flow<Boolean> = | ||
preferencesDataSource.settingsData.map { settingsData -> settingsData.isDarkTheme } | ||
|
||
override suspend fun updateIsDarkTheme(isDarkTheme: Boolean) { | ||
preferencesDataSource.updateIsDarkTheme(isDarkTheme) | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
core/data/src/main/java/com/droidknights/app2023/core/data/repository/SettingsRepository.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,10 @@ | ||
package com.droidknights.app2023.core.data.repository | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface SettingsRepository { | ||
|
||
fun getIsDarkTheme(): Flow<Boolean> | ||
|
||
suspend fun updateIsDarkTheme(isDarkTheme: Boolean) | ||
} |
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 @@ | ||
/build |
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,13 @@ | ||
plugins { | ||
id("droidknights.android.library") | ||
} | ||
|
||
android { | ||
namespace = "com.droidknights.app2023.core.datastore" | ||
} | ||
|
||
dependencies { | ||
testImplementation(libs.junit4) | ||
testImplementation(libs.kotlin.test) | ||
implementation(libs.androidx.datastore) | ||
} |
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,2 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest /> |
29 changes: 29 additions & 0 deletions
29
...re/src/main/java/com/droidknights/app2023/core/datastore/SettingsPreferencesDataSource.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,29 @@ | ||
package com.droidknights.app2023.core.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 com.droidknights.app2023.core.datastore.model.SettingsData | ||
import kotlinx.coroutines.flow.map | ||
import javax.inject.Inject | ||
|
||
class SettingsPreferencesDataSource @Inject constructor( | ||
private val dataStore: DataStore<Preferences> | ||
) { | ||
object PreferencesKey { | ||
val IS_DARK_THEME = booleanPreferencesKey("IS_DARK_THEME") | ||
} | ||
|
||
val settingsData = dataStore.data.map { preferences -> | ||
SettingsData( | ||
isDarkTheme = preferences[PreferencesKey.IS_DARK_THEME] ?: false | ||
) | ||
} | ||
|
||
suspend fun updateIsDarkTheme(isDarkTheme: Boolean) { | ||
dataStore.edit { preferences -> | ||
preferences[PreferencesKey.IS_DARK_THEME] = isDarkTheme | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
core/datastore/src/main/java/com/droidknights/app2023/core/datastore/di/DataStoreModule.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,25 @@ | ||
package com.droidknights.app2023.core.datastore.di | ||
|
||
import android.content.Context | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.preferencesDataStore | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object DataStoreModule { | ||
private const val DATASTORE_NAME = "SETTINGS_PREFERENCES" | ||
private val Context.dataStore by preferencesDataStore(DATASTORE_NAME) | ||
|
||
@Provides | ||
@Singleton | ||
fun provideSettingsDataStore( | ||
@ApplicationContext context: Context | ||
): DataStore<Preferences> = context.dataStore | ||
} |
5 changes: 5 additions & 0 deletions
5
core/datastore/src/main/java/com/droidknights/app2023/core/datastore/model/SettingsData.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,5 @@ | ||
package com.droidknights.app2023.core.datastore.model | ||
|
||
data class SettingsData( | ||
val isDarkTheme: Boolean | ||
) |
59 changes: 59 additions & 0 deletions
59
...rc/test/java/com/droidknights/app2023/core/datastore/SettingsPreferencesDataSourceTest.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,59 @@ | ||
package com.droidknights.app2023.core.datastore | ||
|
||
import androidx.datastore.preferences.core.PreferenceDataStoreFactory | ||
import io.kotest.core.spec.style.StringSpec | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.test.StandardTestDispatcher | ||
import kotlinx.coroutines.test.TestDispatcher | ||
import org.junit.rules.TemporaryFolder | ||
|
||
internal class SettingsPreferencesDataSourceTest : StringSpec() { | ||
|
||
private lateinit var testDispatcher: TestDispatcher | ||
private lateinit var tempFolder: TemporaryFolder | ||
private lateinit var dataSource: SettingsPreferencesDataSource | ||
|
||
init { | ||
beforeSpec { | ||
testDispatcher = StandardTestDispatcher() | ||
tempFolder = TemporaryFolder.builder().assureDeletion().build() | ||
dataSource = SettingsPreferencesDataSource( | ||
PreferenceDataStoreFactory.create( | ||
scope = CoroutineScope(testDispatcher), | ||
produceFile = { tempFolder.newFile("SETTINGS_PREFERENCES_TEST") } | ||
) | ||
) | ||
} | ||
|
||
afterSpec { | ||
tempFolder.delete() | ||
} | ||
|
||
"isDarkTheme 초기상태 테스트".config(true) { | ||
CoroutineScope(testDispatcher).launch { | ||
// Given - 초기상태 | ||
|
||
// When - dataSource 의 초기 SettingsData 값 조회 | ||
val settingsData = dataSource.settingsData.first() | ||
|
||
// Then - SettingsData.isDarkTheme 값이 false 이어야 한다 | ||
assert(settingsData.isDarkTheme == false) | ||
} | ||
} | ||
|
||
"isDarkTheme 저장 및 조회 테스트" { | ||
CoroutineScope(testDispatcher).launch { | ||
// Given - isDarkTheme is true | ||
dataSource.updateIsDarkTheme(true) | ||
|
||
// When - isDarkTheme 을 true 로 업데이트 후 SettingsData 값 조회 | ||
val settingsData = dataSource.settingsData.first() | ||
|
||
// Then - SettingsData.isDarkTheme 값이 true 이어야 한다 | ||
assert(settingsData.isDarkTheme == true) | ||
} | ||
} | ||
} | ||
} |
23 changes: 7 additions & 16 deletions
23
feature/main/src/main/java/com/droidknights/app2023/feature/main/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,30 @@ | ||
package com.droidknights.app2023.feature.main | ||
|
||
import android.content.res.Configuration | ||
import android.os.Bundle | ||
import androidx.activity.compose.setContent | ||
import androidx.activity.viewModels | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
import androidx.core.view.WindowCompat | ||
import androidx.lifecycle.compose.collectAsStateWithLifecycle | ||
import com.droidknights.app2023.core.designsystem.theme.KnightsTheme | ||
import dagger.hilt.android.AndroidEntryPoint | ||
|
||
@AndroidEntryPoint | ||
class MainActivity : AppCompatActivity() { | ||
private var isDarkTheme by mutableStateOf(false) | ||
private val viewModel: MainViewModel by viewModels() | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
WindowCompat.setDecorFitsSystemWindows(window, false) | ||
|
||
setContent { | ||
val isDarkTheme by viewModel.isDarkTheme.collectAsStateWithLifecycle(false, this) | ||
KnightsTheme(darkTheme = isDarkTheme) { | ||
MainScreen() | ||
MainScreen( | ||
onChangeDarkTheme = { isDarkTheme -> viewModel.updateIsDarkTheme(isDarkTheme) } | ||
) | ||
} | ||
} | ||
} | ||
|
||
private fun isNightModeEnabled(uiMode: Int): Boolean { | ||
val currentNightMode = uiMode and Configuration.UI_MODE_NIGHT_MASK | ||
return currentNightMode == Configuration.UI_MODE_NIGHT_YES | ||
} | ||
|
||
// FIXME : configurationChanges를 사용하지 않고 깜빡이지 않게 테마를 바꾸는 방법 찾기 | ||
override fun onConfigurationChanged(newConfig: Configuration) { | ||
super.onConfigurationChanged(newConfig) | ||
isDarkTheme = isNightModeEnabled(newConfig.uiMode) | ||
} | ||
} |
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
19 changes: 19 additions & 0 deletions
19
feature/main/src/main/java/com/droidknights/app2023/feature/main/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,19 @@ | ||
package com.droidknights.app2023.feature.main | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.droidknights.app2023.core.data.repository.SettingsRepository | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class MainViewModel @Inject constructor( | ||
private val settingsRepository: SettingsRepository, | ||
) : ViewModel() { | ||
val isDarkTheme = settingsRepository.getIsDarkTheme() | ||
|
||
fun updateIsDarkTheme(isDarkTheme: Boolean) = viewModelScope.launch { | ||
settingsRepository.updateIsDarkTheme(isDarkTheme) | ||
} | ||
} |
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
Oops, something went wrong.