fix: answer reveal and question category UI/state fixes
This commit is contained in:
parent
a0fd1d56f6
commit
c89e4f6bba
|
|
@ -55,7 +55,13 @@ fun AnswerRevealScreen(
|
||||||
questionId = questionId,
|
questionId = questionId,
|
||||||
onReveal = viewModel::revealAnswer,
|
onReveal = viewModel::revealAnswer,
|
||||||
onAnswerQuestion = {
|
onAnswerQuestion = {
|
||||||
onNavigate(AppRoute.questionThread("couple", questionId))
|
val coupleId = state.coupleId
|
||||||
|
if (coupleId != null) {
|
||||||
|
onNavigate(AppRoute.questionThread(coupleId, questionId))
|
||||||
|
} else {
|
||||||
|
// Discussing requires a paired partner; send unpaired users to invite one.
|
||||||
|
onNavigate(AppRoute.CREATE_INVITE)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
onHistory = { onNavigate(AppRoute.ANSWER_HISTORY) },
|
onHistory = { onNavigate(AppRoute.ANSWER_HISTORY) },
|
||||||
onHome = { onNavigate(AppRoute.HOME) }
|
onHome = { onNavigate(AppRoute.HOME) }
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@ import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import app.closer.domain.model.LocalAnswer
|
import app.closer.domain.model.LocalAnswer
|
||||||
import app.closer.domain.model.Question
|
import app.closer.domain.model.Question
|
||||||
|
import app.closer.domain.repository.AuthRepository
|
||||||
|
import app.closer.domain.repository.CoupleRepository
|
||||||
import app.closer.domain.repository.LocalAnswerRepository
|
import app.closer.domain.repository.LocalAnswerRepository
|
||||||
import app.closer.domain.repository.QuestionRepository
|
import app.closer.domain.repository.QuestionRepository
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
|
|
@ -19,13 +21,16 @@ data class AnswerRevealUiState(
|
||||||
val isLoading: Boolean = true,
|
val isLoading: Boolean = true,
|
||||||
val error: String? = null,
|
val error: String? = null,
|
||||||
val question: Question? = null,
|
val question: Question? = null,
|
||||||
val answer: LocalAnswer? = null
|
val answer: LocalAnswer? = null,
|
||||||
|
val coupleId: String? = null
|
||||||
)
|
)
|
||||||
|
|
||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class AnswerRevealViewModel @Inject constructor(
|
class AnswerRevealViewModel @Inject constructor(
|
||||||
private val questionRepository: QuestionRepository,
|
private val questionRepository: QuestionRepository,
|
||||||
private val localAnswerRepository: LocalAnswerRepository,
|
private val localAnswerRepository: LocalAnswerRepository,
|
||||||
|
private val authRepository: AuthRepository,
|
||||||
|
private val coupleRepository: CoupleRepository,
|
||||||
savedStateHandle: SavedStateHandle
|
savedStateHandle: SavedStateHandle
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
|
|
||||||
|
|
@ -43,10 +48,14 @@ class AnswerRevealViewModel @Inject constructor(
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
_uiState.value = AnswerRevealUiState(isLoading = true)
|
_uiState.value = AnswerRevealUiState(isLoading = true)
|
||||||
try {
|
try {
|
||||||
|
val coupleId = authRepository.currentUserId?.let { uid ->
|
||||||
|
runCatching { coupleRepository.getCoupleForUser(uid)?.id }.getOrNull()
|
||||||
|
}
|
||||||
_uiState.value = AnswerRevealUiState(
|
_uiState.value = AnswerRevealUiState(
|
||||||
isLoading = false,
|
isLoading = false,
|
||||||
question = questionRepository.getQuestionById(questionId),
|
question = questionRepository.getQuestionById(questionId),
|
||||||
answer = localAnswerRepository.getAnswer(questionId)
|
answer = localAnswerRepository.getAnswer(questionId),
|
||||||
|
coupleId = coupleId
|
||||||
)
|
)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
_uiState.value = AnswerRevealUiState(
|
_uiState.value = AnswerRevealUiState(
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,13 @@ fun DailyQuestionScreen(
|
||||||
subtitle = "Answer privately first, then choose whether to reveal it or keep the conversation going.",
|
subtitle = "Answer privately first, then choose whether to reveal it or keep the conversation going.",
|
||||||
primaryRouteLabel = "Discuss",
|
primaryRouteLabel = "Discuss",
|
||||||
onPrimaryRoute = { question ->
|
onPrimaryRoute = { question ->
|
||||||
onNavigate(AppRoute.questionThread(state.coupleId ?: "couple", question.id))
|
val coupleId = state.coupleId
|
||||||
|
if (coupleId != null) {
|
||||||
|
onNavigate(AppRoute.questionThread(coupleId, question.id))
|
||||||
|
} else {
|
||||||
|
// Discussing requires a paired partner; send unpaired users to invite one.
|
||||||
|
onNavigate(AppRoute.CREATE_INVITE)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
onSecondaryRoute = state.question?.let {
|
onSecondaryRoute = state.question?.let {
|
||||||
{ onNavigate(AppRoute.answerReveal(it.id)) }
|
{ onNavigate(AppRoute.answerReveal(it.id)) }
|
||||||
|
|
|
||||||
|
|
@ -55,7 +55,13 @@ fun QuestionCategoryScreen(
|
||||||
categoryId = categoryId,
|
categoryId = categoryId,
|
||||||
state = state,
|
state = state,
|
||||||
onQuestionSelected = { question ->
|
onQuestionSelected = { question ->
|
||||||
onNavigate(AppRoute.questionThread("couple", question.id))
|
val coupleId = state.coupleId
|
||||||
|
if (coupleId != null) {
|
||||||
|
onNavigate(AppRoute.questionThread(coupleId, question.id))
|
||||||
|
} else {
|
||||||
|
// Discussing requires a paired partner; send unpaired users to invite one.
|
||||||
|
onNavigate(AppRoute.CREATE_INVITE)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@ import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import app.closer.domain.model.Question
|
import app.closer.domain.model.Question
|
||||||
import app.closer.domain.model.QuestionCategory
|
import app.closer.domain.model.QuestionCategory
|
||||||
|
import app.closer.domain.repository.AuthRepository
|
||||||
|
import app.closer.domain.repository.CoupleRepository
|
||||||
import app.closer.domain.repository.QuestionRepository
|
import app.closer.domain.repository.QuestionRepository
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
@ -17,12 +19,15 @@ data class QuestionCategoryUiState(
|
||||||
val isLoading: Boolean = true,
|
val isLoading: Boolean = true,
|
||||||
val error: String? = null,
|
val error: String? = null,
|
||||||
val category: QuestionCategory? = null,
|
val category: QuestionCategory? = null,
|
||||||
val questions: List<Question> = emptyList()
|
val questions: List<Question> = emptyList(),
|
||||||
|
val coupleId: String? = null
|
||||||
)
|
)
|
||||||
|
|
||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class QuestionCategoryViewModel @Inject constructor(
|
class QuestionCategoryViewModel @Inject constructor(
|
||||||
private val repository: QuestionRepository,
|
private val repository: QuestionRepository,
|
||||||
|
private val authRepository: AuthRepository,
|
||||||
|
private val coupleRepository: CoupleRepository,
|
||||||
savedStateHandle: SavedStateHandle
|
savedStateHandle: SavedStateHandle
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
|
|
||||||
|
|
@ -41,10 +46,14 @@ class QuestionCategoryViewModel @Inject constructor(
|
||||||
try {
|
try {
|
||||||
val category = repository.getCategoryById(categoryId)
|
val category = repository.getCategoryById(categoryId)
|
||||||
val questions = repository.getQuestionsByCategory(categoryId)
|
val questions = repository.getQuestionsByCategory(categoryId)
|
||||||
|
val coupleId = authRepository.currentUserId?.let { uid ->
|
||||||
|
runCatching { coupleRepository.getCoupleForUser(uid)?.id }.getOrNull()
|
||||||
|
}
|
||||||
_uiState.value = QuestionCategoryUiState(
|
_uiState.value = QuestionCategoryUiState(
|
||||||
isLoading = false,
|
isLoading = false,
|
||||||
category = category,
|
category = category,
|
||||||
questions = questions
|
questions = questions,
|
||||||
|
coupleId = coupleId
|
||||||
)
|
)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
_uiState.value = QuestionCategoryUiState(
|
_uiState.value = QuestionCategoryUiState(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue