Skip to content

Commit

Permalink
[feat/#76] 호출 시점 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
eshc123 committed Feb 9, 2024
1 parent 1bd2174 commit b255a8b
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,55 +34,76 @@ import javax.inject.Inject
@HiltViewModel
class AttendViewModel @Inject constructor(
private val getAttendCurrentListUseCase: GetAttendCurrentListUseCase,
getAttendInfoUseCase: GetAttendInfoUseCase,
getAttendScoreUseCase: GetAttendScoreUseCase,
private val getAttendInfoUseCase: GetAttendInfoUseCase,
private val getAttendScoreUseCase: GetAttendScoreUseCase,
getAttendQrUseCase: GetAttendQrUseCase,
private val getIsLoginUseCase: GetIsLoginUseCase,
private val PostAttendCheckUseCase: PostAttendCheckUseCase
):ViewModel() {

val attendListInit: StateFlow<List<AttendCurrentModel>> = getAttendCurrentListUseCase()
.catch { e ->
}
.map { list ->
list.map { model ->
model.copy(
date = formatDate(model.date),
time = formatTime(model.time)
)
}
}
.stateIn(
scope = viewModelScope,
started = SharingStarted.Eagerly,
initialValue = emptyList()
)

val upcomingAttend = getAttendInfoUseCase().catch {
}.stateIn(
viewModelScope,
started = SharingStarted.Eagerly,
initialValue = AttendInfoModel(0, "", false, "", "")
)
private val _attendListInit = MutableStateFlow<List<AttendCurrentModel>>(emptyList())
val attendListInit : StateFlow<List<AttendCurrentModel>> = _attendListInit.asStateFlow()

private val _upcomingAttend = MutableStateFlow(AttendInfoModel(0, "", false, "", ""))
val upcomingAttend : StateFlow<AttendInfoModel> = _upcomingAttend.asStateFlow()

private val _attendScore = MutableStateFlow(AttendModel(0, 0, 0, 0, "수료 가능한 점수에요"))
val attendScore : StateFlow<AttendModel> = _attendScore.asStateFlow()

private val _attendCheckModel = MutableStateFlow(
AttendCheckModel(curriculumId = upcomingAttend.value.curriculumId, text = "")
)
val attendCheckModel = _attendCheckModel.asStateFlow()

fun updateScannedQrCode(qrText: String) {
_attendCheckModel.value = _attendCheckModel.value.copy(text = qrText)
}

private val _snackbarEvent = MutableSharedFlow<AttendSnackBarEvent>()
val snackbarEvent : SharedFlow<AttendSnackBarEvent> = _snackbarEvent.asSharedFlow()

val attendScore = getAttendScoreUseCase().catch {
}.stateIn(
viewModelScope,
started = SharingStarted.Eagerly,
initialValue = AttendModel(0, 0, 0, 0, "수료 가능한 점수에요")
)
fun getAttendList(){
viewModelScope.launch {
getAttendCurrentListUseCase()
.catch { e ->
}
.map { list ->
list.map { model ->
model.copy(
date = formatDate(model.date),
time = formatTime(model.time)
)
}
}.collect {
_attendListInit
}
}
}

fun getUpcomingAttend() {
viewModelScope.launch {
getAttendInfoUseCase()
.catch {
//TODO 에러처리
}.collect {
_upcomingAttend.emit(it)
_attendCheckModel.emit(
AttendCheckModel(curriculumId = it.curriculumId, text = "")
)
}
}
}

fun getAttendScore(){
viewModelScope.launch {
getAttendScoreUseCase()
.catch {
//TODO 에러처리
}.collect {
_attendScore.emit(it)
}
}
}

fun updateScannedQrCode(qrText: String) {
_attendCheckModel.value = _attendCheckModel.value.copy(text = qrText)
}

fun formatDate(dateString: String): String {
val originalFormat = SimpleDateFormat("MM월 dd일", Locale.KOREA)
Expand All @@ -100,10 +121,10 @@ class AttendViewModel @Inject constructor(
viewModelScope.launch {
val model = attendCheckModel
PostAttendCheckUseCase(curriculumId = model.value.curriculumId, qrText = model.value.text).
catch {
Log.d("출석 확인", "출석 실패")
_snackbarEvent.emit(AttendSnackBarEvent.Attend_fail)
}
catch {
Log.d("출석 확인", "출석 실패")
_snackbarEvent.emit(AttendSnackBarEvent.Attend_fail)
}
.collectLatest {
Log.d("출석 확인", "출석 성공")
_snackbarEvent.emit(AttendSnackBarEvent.Attend_success)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,44 +46,51 @@ fun AttendScreen(
viewModel: AttendViewModel,
navController: NavHostController
) {

LaunchedEffect(Unit){
viewModel.getAttendList()
viewModel.getUpcomingAttend()
viewModel.getAttendScore()
}

val attendCurrentList by viewModel.attendListInit.collectAsState()
val scrollState = rememberScrollState()

Column(
modifier = Modifier
.wrapContentHeight()
.fillMaxWidth()
.background(color = KusitmsColorPalette.current.Grey800),
horizontalAlignment = Alignment.Start,
verticalArrangement = Arrangement.Top
) {
Column(
modifier = Modifier
.wrapContentHeight()
.fillMaxWidth()
.background(color = KusitmsColorPalette.current.Grey800),
.background(color = KusitmsColorPalette.current.Grey900),
horizontalAlignment = Alignment.Start,
verticalArrangement = Arrangement.Top
) {
Column(
modifier = Modifier
.wrapContentHeight()
.fillMaxWidth()
.background(color = KusitmsColorPalette.current.Grey900),
horizontalAlignment = Alignment.Start,
verticalArrangement = Arrangement.Top
) {
AttendTopBar()
KusitmsMarginVerticalSpacer(size = 8)
AttendPreColumn(viewModel,navController)
KusitmsMarginVerticalSpacer(size = 24)
AttendRecordColumn(viewModel)
KusitmsMarginVerticalSpacer(size = 32)
}
if (attendCurrentList.isNotEmpty()) {
KusitmsMarginVerticalSpacer(size = 30)
attendCurrentList.forEach { model ->
CurriItem(model = model)
}
} else {
AttendNotAttend()
AttendTopBar()
KusitmsMarginVerticalSpacer(size = 8)
AttendPreColumn(viewModel,navController)
KusitmsMarginVerticalSpacer(size = 24)
AttendRecordColumn(viewModel)
KusitmsMarginVerticalSpacer(size = 32)
}
if (attendCurrentList.isNotEmpty()) {
KusitmsMarginVerticalSpacer(size = 30)
attendCurrentList.forEach { model ->
CurriItem(model = model)
}
Spacer(modifier = Modifier
.weight(1f)
.background(color = KusitmsColorPalette.current.Grey800))
} else {
AttendNotAttend()
}
Spacer(modifier = Modifier
.weight(1f)
.background(color = KusitmsColorPalette.current.Grey800))
}
ScrollBtn(scrollState = scrollState)
}

Expand Down

0 comments on commit b255a8b

Please sign in to comment.