Skip to content

Commit

Permalink
[feat/#76] date-time parsing 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnseo committed Feb 6, 2024
1 parent a932a44 commit 63680ce
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,22 @@ class AttendViewModel @Inject constructor(
getAttendScoreUseCase: GetAttendScoreUseCase
):ViewModel() {

val attendListInit = getAttendCurrentListUseCase().catch {
}.stateIn(
viewModelScope,
started = SharingStarted.Eagerly,
initialValue = emptyList()
)
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(
Expand All @@ -61,12 +71,6 @@ class AttendViewModel @Inject constructor(
private val _attendCurrentList = MutableStateFlow<List<AttendCurrentModel>>(emptyList())
val attendCurrentList : StateFlow<List<AttendCurrentModel>> = _attendCurrentList.asStateFlow()

private val _eventDateTime = MutableStateFlow<LocalDateTime?>(null)
val eventDateTime: StateFlow<LocalDateTime?> = _eventDateTime

private val _attendScore = MutableStateFlow(
AttendModel(penalty = 0, present = 0, absent = 0, late = 0, passYn = "수료 가능한 점수에요")
)

fun formatDate(dateString: String): String {
val originalFormat = SimpleDateFormat("MM월 dd일", Locale.KOREA)
Expand All @@ -75,11 +79,24 @@ class AttendViewModel @Inject constructor(
val parsedDate = originalFormat.parse(dateString)
parsedDate?.let { targetFormat.format(it) } ?: dateString
} catch (e: Exception) {
// 파싱에 실패시, 원본 날짜를 그대로 사용
Log.d("변환 실패", "date")
dateString
}
}

fun formatTime(timeString: String): String {
val inputFormat = SimpleDateFormat("a hh:mm", Locale.KOREAN)
val outputFormat = SimpleDateFormat("a h:mm", Locale.KOREAN)

return try {
val date = inputFormat.parse(timeString)
outputFormat.format(date)
} catch (e: Exception) {
Log.d("변환 실패", "time")
timeString
}
}


@RequiresApi(Build.VERSION_CODES.O)
fun combineDateAndTime(date: String, time: String): LocalDateTime? {
Expand All @@ -97,18 +114,6 @@ class AttendViewModel @Inject constructor(
}
}

init {
viewModelScope.launch {
getAttendCurrentListUseCase().catch {

}.collect() {
_attendCurrentList.value = it.map { attendModel ->
attendModel.copy(date = formatDate(attendModel.date))
}
}
}
}

enum class Status(val displayName: String) {
PRESENT("출석"),
ABSENT("결석"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kusitms.presentation.ui.home.attend

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
Expand All @@ -25,6 +26,8 @@ import com.kusitms.presentation.model.home.attend.AttendViewModel
fun CurriItem(
model: AttendCurrentModel
) {
Log.d("model data", model.time.toString())
Log.d("model data", model.date.toString())
Row(modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 20.dp)
Expand Down

0 comments on commit 63680ce

Please sign in to comment.