Skip to content

Commit

Permalink
Merge branch 'main' into feat/language
Browse files Browse the repository at this point in the history
  • Loading branch information
nqmgaming authored Dec 7, 2024
2 parents aaba480 + 3eaf1fe commit 87c6e5a
Show file tree
Hide file tree
Showing 20 changed files with 542 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.pwhs.quickmem.R
import com.pwhs.quickmem.core.data.enums.LearnFrom
import com.pwhs.quickmem.core.data.enums.LearnMode
import com.pwhs.quickmem.core.utils.AppConstant
import com.pwhs.quickmem.domain.model.study_set.GetStudySetResponseModel
import com.pwhs.quickmem.presentation.app.folder.detail.component.FolderDetailStudySetList
Expand All @@ -50,6 +51,11 @@ import com.ramcosta.composedestinations.annotation.RootGraph
import com.ramcosta.composedestinations.generated.destinations.AddStudySetToFolderScreenDestination
import com.ramcosta.composedestinations.generated.destinations.EditFolderScreenDestination
import com.ramcosta.composedestinations.generated.destinations.FlipFlashCardScreenDestination
import com.ramcosta.composedestinations.generated.destinations.LearnByQuizScreenDestination
import com.ramcosta.composedestinations.generated.destinations.LearnByTrueFalseScreenDestination
import com.ramcosta.composedestinations.generated.destinations.LearnByTrueFalseScreenDestination.invoke
import com.ramcosta.composedestinations.generated.destinations.LearnByWriteScreenDestination
import com.ramcosta.composedestinations.generated.destinations.LearnByWriteScreenDestination.invoke
import com.ramcosta.composedestinations.generated.destinations.ReportScreenDestination
import com.ramcosta.composedestinations.generated.destinations.StudySetDetailScreenDestination
import com.ramcosta.composedestinations.generated.destinations.UserDetailScreenDestination
Expand Down Expand Up @@ -131,6 +137,63 @@ fun FolderDetailScreen(
FolderDetailUiEvent.FolderDeleted -> {
resultNavigator.navigateBack(true)
}

is FolderDetailUiEvent.OnNavigateToFlipFlashcard -> {
navigator.navigate(
FlipFlashCardScreenDestination(
studySetId = "",
studySetTitle = "",
studySetDescription = "",
studySetColorId = 0,
studySetSubjectId = 0,
folderId = uiState.id,
learnFrom = LearnFrom.FOLDER,
isGetAll = event.isGetAll
)
)
}
is FolderDetailUiEvent.OnNavigateToQuiz -> {
navigator.navigate(
LearnByQuizScreenDestination(
studySetId = "",
studySetTitle = "",
studySetDescription = "",
studySetColorId = 0,
studySetSubjectId = 0,
folderId = uiState.id,
learnFrom = LearnFrom.FOLDER,
isGetAll = event.isGetAll
)
)
}
is FolderDetailUiEvent.OnNavigateToTrueFalse -> {
navigator.navigate(
LearnByTrueFalseScreenDestination(
studySetId = "",
studySetTitle = "",
studySetDescription = "",
studySetColorId = 0,
studySetSubjectId = 0,
folderId = uiState.id,
learnFrom = LearnFrom.FOLDER,
isGetAll = event.isGetAll
)
)
}
is FolderDetailUiEvent.OnNavigateToWrite -> {
navigator.navigate(
LearnByWriteScreenDestination(
studySetId = "",
studySetTitle = "",
studySetDescription = "",
studySetColorId = 0,
studySetSubjectId = 0,
folderId = uiState.id,
learnFrom = LearnFrom.FOLDER,
isGetAll = event.isGetAll
)
)
}
}
}
}
Expand All @@ -154,20 +217,6 @@ fun FolderDetailScreen(
)
},
onEditFolder = { viewModel.onEvent(FolderDetailUiAction.EditFolder) },
onLearnFlipFlashcardClick = {
navigator.navigate(
FlipFlashCardScreenDestination(
studySetId = "",
studySetTitle = "",
studySetDescription = "",
studySetColorId = 0,
studySetSubjectId = 0,
folderId = uiState.id,
learnFrom = LearnFrom.FOLDER,
isGetAll = true
)
)
},
onNavigateBack = {
resultNavigator.navigateBack(true)
},
Expand Down Expand Up @@ -195,6 +244,10 @@ fun FolderDetailScreen(
)
)
},
totalFlashCards = uiState.totalFlashCards,
onNavigateToLearn = { learnMode, isGetAll ->
viewModel.onEvent(FolderDetailUiAction.NavigateToLearn(learnMode, isGetAll))
},
)
}

Expand All @@ -214,11 +267,12 @@ fun FolderDetail(
onStudySetClick: (String) -> Unit = {},
onEditFolder: () -> Unit = {},
onDeleteFolder: () -> Unit = {},
onLearnFlipFlashcardClick: () -> Unit = {},
onNavigateBack: () -> Unit = {},
onAddStudySet: () -> Unit = {},
onNavigateToUserDetail: () -> Unit = {},
onReportClick: () -> Unit = {}
totalFlashCards: Int = 0,
onReportClick: () -> Unit = {},
onNavigateToLearn: (LearnMode, Boolean) -> Unit = { _, _ -> },
) {
val context = LocalContext.current
val formattedCreatedAt = formatDate(createdAt)
Expand All @@ -234,7 +288,9 @@ fun FolderDetail(
val sheetShowMoreState = rememberModalBottomSheetState()
var showDeleteConfirmationDialog by remember { mutableStateOf(false) }
var showStudyFolderBottomSheet by remember { mutableStateOf(false) }
var showGetAllDialog by remember { mutableStateOf(false) }
val sheetStudyFolderState = rememberModalBottomSheetState()
var learningMode by remember { mutableStateOf(LearnMode.NONE) }

Scaffold(
modifier = modifier,
Expand Down Expand Up @@ -346,7 +402,8 @@ fun FolderDetail(
containerColor = colorScheme.surface,
elevation = 0.dp,
onClick = {
onLearnFlipFlashcardClick()
showGetAllDialog = true
learningMode = LearnMode.FLIP
showStudyFolderBottomSheet = false
},
)
Expand All @@ -355,24 +412,58 @@ fun FolderDetail(
icon = R.drawable.ic_quiz,
containerColor = colorScheme.surface,
elevation = 0.dp,
onClick = {
showGetAllDialog = true
learningMode = LearnMode.QUIZ
showStudyFolderBottomSheet = false
}
)
LearnModeCard(
title = stringResource(R.string.txt_true_false),
icon = R.drawable.ic_tf,
containerColor = colorScheme.surface,
elevation = 0.dp,
leadingText = stringResource(R.string.txt_coming_soon)
onClick = {
showGetAllDialog = true
learningMode = LearnMode.TRUE_FALSE
showStudyFolderBottomSheet = false
}
)
LearnModeCard(
title = stringResource(R.string.txt_write),
icon = R.drawable.ic_write,
containerColor = Color.Transparent,
elevation = 0.dp,
leadingText = stringResource(R.string.txt_coming_soon)
onClick = {
showGetAllDialog = true
learningMode = LearnMode.WRITE
showStudyFolderBottomSheet = false
}
)
}
}
}
if (showGetAllDialog && totalFlashCards > 10) {
QuickMemAlertDialog(
onDismissRequest = {
showGetAllDialog = false
onNavigateToLearn(learningMode, true)
learningMode = LearnMode.NONE
},
onConfirm = {
showGetAllDialog = false
onNavigateToLearn(learningMode, false)
learningMode = LearnMode.NONE
},
title = stringResource(R.string.txt_get_all),
text = stringResource(R.string.txt_are_you_sure_you_want_to_get_all_flashcards),
confirmButtonTitle = stringResource(R.string.txt_ok),
dismissButtonTitle = stringResource(R.string.txt_no_thanks),
)
} else {
onNavigateToLearn(learningMode, true)
learningMode = LearnMode.NONE
}
}

@Preview(showBackground = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.pwhs.quickmem.presentation.app.folder.detail

import com.pwhs.quickmem.core.data.enums.LearnMode

sealed class FolderDetailUiAction {
data object Refresh : FolderDetailUiAction()
data object DeleteFolder : FolderDetailUiAction()
data object EditFolder : FolderDetailUiAction()
data class NavigateToLearn(val learnMode: LearnMode, val isGetAll: Boolean) : FolderDetailUiAction()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ sealed class FolderDetailUiEvent {
data object FolderDeleted : FolderDetailUiEvent()
data object NavigateToEditFolder : FolderDetailUiEvent()
data class ShowError(val message: String) : FolderDetailUiEvent()
data class OnNavigateToFlipFlashcard(val isGetAll: Boolean) : FolderDetailUiEvent()
data class OnNavigateToQuiz(val isGetAll: Boolean) : FolderDetailUiEvent()
data class OnNavigateToTrueFalse(val isGetAll: Boolean) : FolderDetailUiEvent()
data class OnNavigateToWrite(val isGetAll: Boolean) : FolderDetailUiEvent()
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ data class FolderDetailUiState(
val updatedAt: String = "",
val isLoading: Boolean = false,
val isOwner: Boolean = false,
val totalFlashCards: Int = 0
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package com.pwhs.quickmem.presentation.app.folder.detail
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.pwhs.quickmem.core.data.enums.LearnMode
import com.pwhs.quickmem.core.datastore.AppManager
import com.pwhs.quickmem.core.datastore.TokenManager
import com.pwhs.quickmem.core.utils.Resources
import com.pwhs.quickmem.domain.model.folder.SaveRecentAccessFolderRequestModel
import com.pwhs.quickmem.domain.model.study_set.GetStudySetResponseModel
import com.pwhs.quickmem.domain.repository.FolderRepository
import com.pwhs.quickmem.presentation.app.folder.detail.FolderDetailUiEvent.*
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableStateFlow
Expand Down Expand Up @@ -51,15 +54,39 @@ class FolderDetailViewModel @Inject constructor(
_uiState.update { it.copy(isLoading = true) }
deleteFolder()
} else {
_uiEvent.trySend(FolderDetailUiEvent.ShowError("You can't delete this folder"))
_uiEvent.trySend(ShowError("You can't delete this folder"))
}
}

FolderDetailUiAction.EditFolder -> {
if (_uiState.value.isOwner) {
_uiEvent.trySend(FolderDetailUiEvent.NavigateToEditFolder)
_uiEvent.trySend(NavigateToEditFolder)
} else {
_uiEvent.trySend(FolderDetailUiEvent.ShowError("You can't edit this folder"))
_uiEvent.trySend(ShowError("You can't edit this folder"))
}
}

is FolderDetailUiAction.NavigateToLearn -> {
when (event.learnMode) {
LearnMode.FLIP -> {
_uiEvent.trySend(OnNavigateToFlipFlashcard(event.isGetAll))
}

LearnMode.QUIZ -> {
_uiEvent.trySend(OnNavigateToQuiz(event.isGetAll))
}

LearnMode.TRUE_FALSE -> {
_uiEvent.trySend(OnNavigateToTrueFalse(event.isGetAll))
}

LearnMode.WRITE -> {
_uiEvent.trySend(OnNavigateToWrite(event.isGetAll))
}

else -> {
// Do nothing
}
}
}
}
Expand All @@ -77,6 +104,7 @@ class FolderDetailViewModel @Inject constructor(
is Resources.Success -> {
resource.data?.let { data ->
val isOwner = appManager.userId.firstOrNull() == data.owner.id
val totalFlashCards = calculateTotalFlashCards(data.studySets ?: emptyList())
_uiState.update {
it.copy(
title = data.title,
Expand All @@ -89,12 +117,14 @@ class FolderDetailViewModel @Inject constructor(
createdAt = data.createdAt,
updatedAt = data.updatedAt,
isLoading = false,
isOwner = isOwner
isOwner = isOwner,
totalFlashCards = totalFlashCards
)
}
} ?: run {
_uiEvent.send(FolderDetailUiEvent.ShowError("Folder not found"))
_uiEvent.send(ShowError("Folder not found"))
}
Timber.d("")
}

is Resources.Error -> {
Expand All @@ -109,7 +139,7 @@ class FolderDetailViewModel @Inject constructor(
private fun deleteFolder() {
viewModelScope.launch {
val token = tokenManager.accessToken.firstOrNull() ?: run {
_uiEvent.send(FolderDetailUiEvent.ShowError("Please login again!"))
_uiEvent.send(ShowError("Please login again!"))
return@launch
}
folderRepository.deleteFolder(token, _uiState.value.id).collectLatest { resource ->
Expand All @@ -123,7 +153,7 @@ class FolderDetailViewModel @Inject constructor(
resource.data?.let {
Timber.d("Folder deleted")
_uiState.update { it.copy(isLoading = false) }
_uiEvent.send(FolderDetailUiEvent.FolderDeleted)
_uiEvent.send(FolderDeleted)
}
}

Expand Down Expand Up @@ -164,4 +194,12 @@ class FolderDetailViewModel @Inject constructor(
}
}
}

private fun calculateTotalFlashCards(studySets: List<GetStudySetResponseModel>): Int {
var totalFlashCards = 0
studySets.forEach { studySet ->
totalFlashCards += studySet.flashcardCount
}
return totalFlashCards
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ fun StudySetDetailScreen(
studySetDescription = uiState.description,
studySetColorId = uiState.colorModel.id,
studySetSubjectId = uiState.subject.id,
folderId = "",
learnFrom = LearnFrom.STUDY_SET,
isGetAll = event.isGetAll
)
)
Expand All @@ -263,6 +265,8 @@ fun StudySetDetailScreen(
studySetDescription = uiState.description,
studySetColorId = uiState.colorModel.id,
studySetSubjectId = uiState.subject.id,
folderId = "",
learnFrom = LearnFrom.STUDY_SET,
isGetAll = event.isGetAll
)
)
Expand All @@ -276,6 +280,8 @@ fun StudySetDetailScreen(
studySetDescription = uiState.description,
studySetColorId = uiState.colorModel.id,
studySetSubjectId = uiState.subject.id,
folderId = "",
learnFrom = LearnFrom.STUDY_SET,
isGetAll = event.isGetAll
)
)
Expand Down
Loading

0 comments on commit 87c6e5a

Please sign in to comment.