feat(date-memories): add date memory marking logic to DateMatchesViewModel
This commit is contained in:
parent
e4f10551a0
commit
cf061f24f6
|
|
@ -3,9 +3,11 @@ package app.closer.ui.dates
|
|||
import android.util.Log
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import app.closer.data.remote.FirestoreDateMemoryDataSource
|
||||
import app.closer.data.repository.DateIdeaSeed
|
||||
import app.closer.domain.model.DateIdea
|
||||
import app.closer.domain.model.DateMatch
|
||||
import app.closer.domain.model.DateMemory
|
||||
import app.closer.domain.model.DateMatchSuggestion
|
||||
import app.closer.domain.model.DateSwipe
|
||||
import app.closer.domain.model.SwipeAction
|
||||
|
|
@ -16,8 +18,11 @@ import app.closer.domain.repository.UserRepository
|
|||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asSharedFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.launch
|
||||
|
|
@ -42,16 +47,44 @@ class DateMatchesViewModel @Inject constructor(
|
|||
private val repository: DateMatchRepository,
|
||||
private val authRepository: AuthRepository,
|
||||
private val coupleRepository: CoupleRepository,
|
||||
private val userRepository: UserRepository
|
||||
private val userRepository: UserRepository,
|
||||
private val memoryDataSource: FirestoreDateMemoryDataSource
|
||||
) : ViewModel() {
|
||||
|
||||
private val _uiState = MutableStateFlow(DateMatchesUiState())
|
||||
val uiState: StateFlow<DateMatchesUiState> = _uiState.asStateFlow()
|
||||
|
||||
/** Emits the dateId to open its reflection screen right after a date is marked done. */
|
||||
private val _markedDateId = MutableSharedFlow<String>(extraBufferCapacity = 1)
|
||||
val markedDateId: SharedFlow<String> = _markedDateId.asSharedFlow()
|
||||
|
||||
init {
|
||||
loadMatches()
|
||||
}
|
||||
|
||||
/** Log a matched date as completed (idempotent) → then open its reflection. */
|
||||
fun markCompleted(match: DateMatch) {
|
||||
val cid = _uiState.value.coupleId ?: return
|
||||
val uid = authRepository.currentUserId ?: return
|
||||
val idea = match.dateIdea ?: DateIdeaSeed.byId(match.dateIdeaId)
|
||||
viewModelScope.launch {
|
||||
runCatching {
|
||||
memoryDataSource.markCompleted(
|
||||
cid,
|
||||
DateMemory(
|
||||
id = match.id,
|
||||
dateIdeaId = match.dateIdeaId,
|
||||
title = idea?.title ?: "",
|
||||
category = idea?.category ?: "",
|
||||
completedAt = System.currentTimeMillis(),
|
||||
addedBy = uid
|
||||
)
|
||||
)
|
||||
}.onSuccess { _markedDateId.tryEmit(match.id) }
|
||||
.onFailure { Log.w(TAG, "markCompleted failed", it) }
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadMatches() {
|
||||
viewModelScope.launch {
|
||||
_uiState.value = DateMatchesUiState(isLoading = true)
|
||||
|
|
|
|||
Loading…
Reference in New Issue