dark mode-v19
This commit is contained in:
parent
e9125fc46f
commit
32c4c35f76
2 changed files with 63 additions and 10 deletions
|
|
@ -0,0 +1,31 @@
|
|||
package com.example.login_v3.data.di
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.core.DataStore
|
||||
// 修正 1: 確保匯入的是 androidx 的 Preferences
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import androidx.datastore.preferences.preferencesDataStore
|
||||
import com.example.login_v3.data.local.UserPreferences.UserPreferencesRepository
|
||||
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
|
||||
|
||||
// 定義擴充屬性,這裡保持為檔案私有 (private) 是沒問題的
|
||||
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "user_prefs")
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
// 修正 2: 移除 private,讓 Hilt 可以訪問這個模組
|
||||
object DataStoreModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideUserPreferencesRepository(
|
||||
@ApplicationContext context: Context
|
||||
): UserPreferencesRepository {
|
||||
return UserPreferencesRepository(context.dataStore)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,21 +1,43 @@
|
|||
package com.example.login_v3.home.setting.setting_detail_page.viewmodel
|
||||
|
||||
import android.util.Log
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.example.login_v3.data.local.UserPreferences.UserPreferencesRepository
|
||||
import com.example.login_v3.ui.theme.AppTheme
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
class Theme_ViewModel : ViewModel() {
|
||||
private val _currentTheme = MutableStateFlow(AppTheme.DARK)
|
||||
val currentTheme: StateFlow<AppTheme> = _currentTheme.asStateFlow()
|
||||
@HiltViewModel // 1. 加上 Hilt 註解
|
||||
class Theme_ViewModel @Inject constructor(
|
||||
private val repository: UserPreferencesRepository // 2. 注入 Repository
|
||||
) : ViewModel() {
|
||||
|
||||
// 3. 讀取:將 repository 的 Flow 映射為 AppTheme,並轉為 StateFlow
|
||||
val currentTheme: StateFlow<AppTheme> = repository.themeModeFlow
|
||||
.map { themeName ->
|
||||
try {
|
||||
AppTheme.valueOf(themeName.uppercase()) // 假設 AppTheme 是 Enum
|
||||
} catch (e: Exception) {
|
||||
AppTheme.DARK // 轉換失敗時的預設值
|
||||
}
|
||||
}
|
||||
.stateIn(
|
||||
scope = viewModelScope,
|
||||
started = SharingStarted.WhileSubscribed(5000),
|
||||
initialValue = AppTheme.DARK // 初始值
|
||||
)
|
||||
|
||||
// 4. 更新:除了更新狀態,還要存入 DataStore
|
||||
fun updateTheme(newTheme: AppTheme) {
|
||||
Log.d("ThemeDebug", "ViewModel 更新主題為: $newTheme")
|
||||
_currentTheme.value = newTheme
|
||||
Log.d("ThemeDebug", "ViewModel 更新主題並儲存至 DataStore: $newTheme")
|
||||
viewModelScope.launch {
|
||||
repository.saveThemeMode(newTheme.name) // 儲存 Enum 的名稱 (例如 "DARK")
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue