ADD: last mood on app opening
This commit is contained in:
parent
7ad753df68
commit
4b30d4f446
4 changed files with 38 additions and 2 deletions
|
@ -22,6 +22,13 @@ val LocalMoodState = compositionLocalOf<MoodWithInfo> { error("No MoodState prov
|
|||
@Composable
|
||||
fun SoulEchoApp(moodViewModel: MoodViewModel) {
|
||||
var currentMood by remember { mutableStateOf(MoodWithInfo(mood = PossibleMood.HAPPY, info = "")) }
|
||||
LaunchedEffect(Unit) {
|
||||
val lastMood = moodViewModel.getLastMood()
|
||||
if (lastMood != null) {
|
||||
currentMood = lastMood
|
||||
}
|
||||
}
|
||||
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
|
||||
CompositionLocalProvider(LocalMoodState provides currentMood) {
|
||||
|
|
|
@ -23,6 +23,9 @@ interface MoodDao {
|
|||
@Query("SELECT * FROM moods WHERE DATE(timestamp) = DATE('now', 'localtime') ORDER BY timestamp DESC")
|
||||
fun getTodaysMoods(): Flow<List<MoodEntity>>
|
||||
|
||||
@Query("SELECT * FROM moods ORDER BY timestamp DESC LIMIT 1")
|
||||
suspend fun getLastMood(): MoodEntity?
|
||||
|
||||
@Update
|
||||
suspend fun updateMood(mood: MoodEntity)
|
||||
|
||||
|
|
|
@ -16,6 +16,10 @@ class MoodRepository(private val moodDao: MoodDao) {
|
|||
return moodDao.insertMood(moodEntity)
|
||||
}
|
||||
|
||||
suspend fun getLastMood(): MoodWithInfo? {
|
||||
return moodDao.getLastMood()?.toMoodWithInfo()
|
||||
}
|
||||
|
||||
fun getAllMoods(): Flow<List<MoodEntity>> {
|
||||
return moodDao.getAllMoods()
|
||||
}
|
||||
|
@ -44,7 +48,6 @@ class MoodRepository(private val moodDao: MoodDao) {
|
|||
moodDao.deleteAllMoods()
|
||||
}
|
||||
|
||||
// Hilfsmethode um MoodEntity zu MoodWithInfo zu konvertieren
|
||||
fun MoodEntity.toMoodWithInfo(): MoodWithInfo {
|
||||
return MoodWithInfo(mood = this.mood, info = this.info)
|
||||
}
|
||||
|
|
|
@ -8,6 +8,9 @@ import de.webfussel.soulecho.data.repository.MoodRepository
|
|||
import de.webfussel.soulecho.mood.MoodWithInfo
|
||||
import de.webfussel.soulecho.mood.PossibleMood
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class MoodViewModel(private val repository: MoodRepository) : ViewModel() {
|
||||
|
@ -15,8 +18,28 @@ class MoodViewModel(private val repository: MoodRepository) : ViewModel() {
|
|||
val allMoods: Flow<List<MoodEntity>> = repository.getAllMoods()
|
||||
val todaysMoods: Flow<List<MoodEntity>> = repository.getTodaysMoods()
|
||||
|
||||
private val _currentMood = MutableStateFlow<MoodWithInfo?>(null)
|
||||
val currentMood: StateFlow<MoodWithInfo?> = _currentMood.asStateFlow()
|
||||
|
||||
init {
|
||||
loadLastMood()
|
||||
}
|
||||
|
||||
private fun loadLastMood() {
|
||||
viewModelScope.launch {
|
||||
val lastMood = repository.getLastMood()
|
||||
_currentMood.value = lastMood
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun saveMood(moodWithInfo: MoodWithInfo): Long {
|
||||
return repository.saveMood(moodWithInfo)
|
||||
val result = repository.saveMood(moodWithInfo)
|
||||
_currentMood.value = moodWithInfo
|
||||
return result
|
||||
}
|
||||
|
||||
suspend fun getLastMood(): MoodWithInfo? {
|
||||
return repository.getLastMood()
|
||||
}
|
||||
|
||||
fun getMoodsByType(mood: PossibleMood): Flow<List<MoodEntity>> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue