Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT/#129] 캘린더뷰 / 스크랩 서버 통신 구현 #136

Merged
merged 13 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fun CalendarTopAppBar(
Icon(
painter = painterResource(id = R.drawable.ic_calendar_previous),
contentDescription = stringResource(id = R.string.calendar_button_description_previous),
tint = Grey300,
tint = TerningMain,
modifier = Modifier.noRippleClickable { onMonthNavigationButtonClicked(-1) }
)
}
Expand All @@ -68,7 +68,7 @@ fun CalendarTopAppBar(
Icon(
painter = painterResource(id = R.drawable.ic_calendar_next),
contentDescription = stringResource(id = R.string.calendar_button_description_next),
tint = Grey300,
tint = TerningMain,
modifier = Modifier.noRippleClickable { onMonthNavigationButtonClicked(1) }
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ class ScrapRepositoryImpl @Inject constructor(
: Result<Unit> = runCatching {
scrapDataSource.deleteScrap(scrapRequestModel)
}

override suspend fun patchScrap(scrapRequestModel: ScrapRequestModel)
: Result<Unit> = runCatching {
scrapDataSource.patchScrap(scrapRequestModel)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import com.terning.domain.entity.request.ScrapRequestModel
interface ScrapRepository {
suspend fun postScrap(scrapRequestModel: ScrapRequestModel): Result<Unit>
suspend fun deleteScrap(scrapRequestModel: ScrapRequestModel): Result<Unit>
suspend fun patchScrap(scrapRequestModel: ScrapRequestModel): Result<Unit>
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private fun CalendarScreen(
CalendarTopAppBar(
date = currentDate,
isListExpanded = calendarUiState.isListEnabled,
isWeekExpanded = calendarUiState.isListEnabled,
isWeekExpanded = calendarUiState.isWeekEnabled,
onListButtonClicked = {
viewModel.changeListVisibility()
if (calendarUiState.isWeekEnabled) {
Expand Down Expand Up @@ -163,8 +163,9 @@ private fun CalendarScreen(
},
contentTwo = {
CalendarWeekScreen(
uiState = calendarUiState,
calendarUiState = calendarUiState,
viewModel = viewModel,
navController = navController,
modifier = Modifier
.fillMaxSize(),
)
Expand All @@ -177,7 +178,7 @@ private fun CalendarScreen(
listState = listState,
pages = state.getPageCount(),
viewModel = viewModel,
uiState = calendarUiState,
navController = navController,
modifier = Modifier
.fillMaxSize()
.padding(top = paddingValues.calculateTopPadding())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
package com.terning.feature.calendar.calendar

import android.util.Log
import androidx.compose.ui.graphics.Color
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.terning.core.designsystem.theme.CalBlue1
import com.terning.core.designsystem.theme.CalBlue2
import com.terning.core.designsystem.theme.CalGreen1
import com.terning.core.designsystem.theme.CalGreen2
import com.terning.core.designsystem.theme.CalOrange1
import com.terning.core.designsystem.theme.CalOrange2
import com.terning.core.designsystem.theme.CalPink
import com.terning.core.designsystem.theme.CalPurple
import com.terning.core.designsystem.theme.CalRed
import com.terning.core.designsystem.theme.CalYellow
import com.terning.core.state.UiState
import com.terning.domain.entity.request.ScrapRequestModel
import com.terning.domain.entity.response.CalendarScrapDetailModel
import com.terning.domain.repository.CalendarRepository
import com.terning.domain.repository.ScrapRepository
import com.terning.feature.R
import com.terning.feature.calendar.month.CalendarMonthState
import com.terning.feature.calendar.scrap.CalendarListState
import com.terning.feature.calendar.week.CalendarWeekState
import com.terning.feature.intern.model.InternScrapState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
Expand All @@ -18,13 +33,13 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import timber.log.Timber
import java.time.LocalDate
import javax.inject.Inject

@HiltViewModel
class CalendarViewModel @Inject constructor(
private val calendarRepository: CalendarRepository
private val calendarRepository: CalendarRepository,
private val scrapRepository: ScrapRepository
) : ViewModel() {

private var _uiState: MutableStateFlow<CalendarUiState> = MutableStateFlow(
Expand Down Expand Up @@ -91,10 +106,17 @@ class CalendarViewModel @Inject constructor(
}
}

fun updateInternDialogVisible(scrapDetailModel: CalendarScrapDetailModel?) {
fun updateInternDialogVisible(visibility: Boolean = false) {
_uiState.update { currentState ->
currentState.copy(
isInternshipClicked = visibility
)
}
}

fun updateInternshipModel(scrapDetailModel: CalendarScrapDetailModel?) {
_uiState.update { currentState ->
currentState.copy(
isInternshipClicked = !currentState.isInternshipClicked,
internshipModel = scrapDetailModel
)
}
Expand Down Expand Up @@ -153,7 +175,6 @@ class CalendarViewModel @Inject constructor(
_calendarWeekState.update { currentState ->
currentState.copy(
loadState = if (it.isNotEmpty()) UiState.Success(it) else UiState.Empty
//loadState = UiState.Success(it)
)
}
},
Expand All @@ -168,4 +189,66 @@ class CalendarViewModel @Inject constructor(
}
)
}

fun deleteScrap(isFromWeekScreen: Boolean = false) = viewModelScope.launch {
_calendarWeekState.value.loadState
.takeIf { it is UiState.Success }
?.let { ScrapRequestModel(_uiState.value.scrapId, null) }?.let { scrapRequestModel ->
scrapRepository.deleteScrap(
scrapRequestModel
).onSuccess {
runCatching {
if (isFromWeekScreen) {
getScrapWeekList()
} else {
getScrapMonthList(
_uiState.value.selectedDate.year,
_uiState.value.selectedDate.monthValue
)
}
}.onSuccess {
updateScrapCancelDialogVisible()
}
}.onFailure {
_calendarSideEffect.emit(
CalendarSideEffect.ShowToast(R.string.server_failure)
)
}
}
}

fun patchScrap(color: Color, isFromWeekScreen: Boolean = false) = viewModelScope.launch {
val scrapId = _uiState.value.internshipModel?.scrapId ?: 0
val colorIndex = getColorIndex(color)

scrapRepository.patchScrap(ScrapRequestModel(scrapId, colorIndex))
.onSuccess {
runCatching {
if (isFromWeekScreen) {
getScrapWeekList()
} else {
getScrapMonthList(
_uiState.value.selectedDate.year,
_uiState.value.selectedDate.monthValue
)
}
}
}.onFailure {
_calendarSideEffect.emit(CalendarSideEffect.ShowToast(R.string.server_failure))
}
}

private fun getColorIndex(color: Color): Int = listOf(
CalRed,
CalOrange1,
CalOrange2,
CalYellow,
CalGreen1,
CalGreen2,
CalBlue1,
CalBlue2,
CalPurple,
CalPink
).indexOf(color)

}
Loading
Loading