Skip to content

Commit

Permalink
Merge branch 'feature/calendar_scroll' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
JaewonKim04 committed Apr 6, 2022
2 parents de7b911 + 15a0690 commit eceeacf
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 31 deletions.
94 changes: 66 additions & 28 deletions app/src/main/java/com/halill/halill2/features/calendar/Calendar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.compose.animation.*
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.*
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
Expand All @@ -21,7 +22,9 @@ import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import com.halill.halill2.features.list.TodoList
import com.halill.halill2.util.isToday
import com.halill.halill2.util.toMontDayList
import java.time.LocalDate
import kotlin.math.abs

@Composable
fun Calendar(navController: NavController, viewModel: CalendarViewModel = hiltViewModel()) {
Expand All @@ -37,7 +40,7 @@ fun Calendar(navController: NavController, viewModel: CalendarViewModel = hiltVi

CalendarContent(
state = state,
doOnNextClick = {
doOnNextMonthClick = {
viewModel.showNextMonth()
},
doOnBeforeMonthClick = {
Expand All @@ -63,7 +66,7 @@ fun Calendar(navController: NavController, viewModel: CalendarViewModel = hiltVi
fun CalendarContent(
state: CalendarState,
doOnBeforeMonthClick: () -> Unit,
doOnNextClick: () -> Unit,
doOnNextMonthClick: () -> Unit,
doOnDateSelect: (LocalDate) -> Unit,
doOnTodoClick: (Long) -> Unit,
doOnDoneClick: (Long) -> Unit,
Expand All @@ -73,7 +76,7 @@ fun CalendarContent(
CalendarMonthLayout(
state.showingMonthDate,
doOnBeforeMonthClick = doOnBeforeMonthClick,
doOnNextClick = doOnNextClick
doOnNextMonthClick = doOnNextMonthClick
)
WeekTextLinearLayout()
Divider(color = MaterialTheme.colors.onSurface)
Expand All @@ -91,8 +94,13 @@ fun CalendarContent(
SizeTransform(clip = false)
)
}
) {
CalendarView(state, doOnDateSelect)
) { showingDate ->
CalendarView(
showingDate = showingDate,
doOnDateSelect = doOnDateSelect,
showBeforeMonth = doOnBeforeMonthClick,
showNextMonth = doOnNextMonthClick
)
}

Divider(color = MaterialTheme.colors.onSurface)
Expand Down Expand Up @@ -133,7 +141,7 @@ fun WeekText(weekText: String, textColor: Color) {
fun CalendarMonthLayout(
showingDate: LocalDate,
doOnBeforeMonthClick: () -> Unit,
doOnNextClick: () -> Unit
doOnNextMonthClick: () -> Unit
) {
Row(
horizontalArrangement = Arrangement.SpaceBetween,
Expand All @@ -157,7 +165,7 @@ fun CalendarMonthLayout(
Text(text = monthText, fontSize = 22.sp)

IconButton(
onClick = doOnNextClick, modifier = Modifier
onClick = doOnNextMonthClick, modifier = Modifier
.padding(10.dp)
.size(60.dp)
) {
Expand All @@ -172,23 +180,51 @@ fun CalendarMonthLayout(
}

@Composable
fun CalendarView(state: CalendarState, doOnDateSelect: (LocalDate) -> Unit) {
Column(modifier = Modifier.padding(0.dp, 7.dp, 0.dp, 0.dp)) {
val monthDayList = state.showingMonthDayList
fun CalendarView(
doOnDateSelect: (LocalDate) -> Unit,
showNextMonth: () -> Unit,
showBeforeMonth: () -> Unit,
showingDate: LocalDate
) {
var changedDelta by remember {
mutableStateOf(0f)
}
Column(
modifier = Modifier
.padding(0.dp, 7.dp, 0.dp, 0.dp)
.draggable(
orientation = Orientation.Horizontal,
state = rememberDraggableState { delta ->
if (abs(delta) < 250) {
changedDelta += delta
}
when {
changedDelta > 300 -> {
showNextMonth()
changedDelta = 0f
}
changedDelta < -300 -> {
showBeforeMonth()
changedDelta = 0f
}
}
},
reverseDirection = true
)
) {
val monthDayList = showingDate.toMontDayList()
val firstDayOfWeek = monthDayList[0].dayOfWeek.value
val alreadyShowDateCount = if (firstDayOfWeek == 7) 7 else (7 - firstDayOfWeek)
CalendarFirstWeekLayout(
state = state,
firstDayOfWeek = firstDayOfWeek,
doOnDateSelect = doOnDateSelect
monthDayList = monthDayList,
doOnDateSelect = doOnDateSelect,
showingMonthDayList = monthDayList
)
CalendarWeekLayout(
state = state,
showingMonthDayList = monthDayList,
alreadyShowDateCount = alreadyShowDateCount,
doOnDateSelect = doOnDateSelect
)

}
}

Expand All @@ -197,17 +233,17 @@ val calendarItemHeight = 55.dp

@Composable
fun CalendarFirstWeekLayout(
state: CalendarState,
firstDayOfWeek: Int,
monthDayList: List<LocalDate>,
showingMonthDayList: List<LocalDate>,
doOnDateSelect: (LocalDate) -> Unit
) {
val firstDayOfWeek = monthDayList[0].dayOfWeek.value
WeekLineLayout {
CalendarFrontSpacer(dayOfWeek = firstDayOfWeek)

FirstWeekLayout(
state = state,
showingMonthDayList = state.showingMonthDayList,
doOnDateSelect = doOnDateSelect
doOnDateSelect = doOnDateSelect,
showingMonthDayList = showingMonthDayList
)
}
}
Expand All @@ -227,12 +263,11 @@ fun CalendarFrontSpacer(dayOfWeek: Int) {

@Composable
fun FirstWeekLayout(
state: CalendarState,
showingMonthDayList: List<LocalDate>,
doOnDateSelect: (LocalDate) -> Unit
doOnDateSelect: (LocalDate) -> Unit,
showingMonthDayList: List<LocalDate>
) {
for (day in showingMonthDayList) {
CalendarDayItem(day, doOnDateSelect = doOnDateSelect, state = state)
CalendarDayItem(day, doOnDateSelect = doOnDateSelect)
if (day.dayOfWeek.value == 6) {
break
}
Expand All @@ -241,7 +276,6 @@ fun FirstWeekLayout(

@Composable
fun CalendarWeekLayout(
state: CalendarState,
showingMonthDayList: List<LocalDate>,
alreadyShowDateCount: Int,
doOnDateSelect: (LocalDate) -> Unit
Expand All @@ -268,8 +302,7 @@ fun CalendarWeekLayout(
} else {
CalendarDayItem(
showingMonthDayList[showedDateCount],
doOnDateSelect = doOnDateSelect,
state = state
doOnDateSelect = doOnDateSelect
)
dayList.removeAt(0)
showedDateCount += 1
Expand All @@ -293,7 +326,12 @@ fun WeekLineLayout(content: @Composable () -> Unit) {
}

@Composable
fun CalendarDayItem(day: LocalDate, state: CalendarState, doOnDateSelect: (LocalDate) -> Unit) {
fun CalendarDayItem(
day: LocalDate,
doOnDateSelect: (LocalDate) -> Unit,
viewModel: CalendarViewModel = hiltViewModel()
) {
val state = viewModel.state.collectAsState().value
val borderColor =
if (day.isToday()) MaterialTheme.colors.primary else MaterialTheme.colors.background
val textColor = if (day.isToday()) Color.White else Color.Unspecified
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.halill.domain.features.todo.usecase.FetchTodoListWithDateUseCase
import com.halill.halill2.base.BaseViewModel
import com.halill.halill2.util.toMontDayList
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch
import java.time.LocalDate
import javax.inject.Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.halill.halill2.features.write
import com.halill.halill2.base.MviState
import java.time.LocalDateTime


data class WriteTodoState(
val title: String,
val content: String,
Expand All @@ -14,6 +13,7 @@ data class WriteTodoState(
val showDateSelectDialog: Boolean,
val showHourSelectDialog: Boolean
): MviState {

companion object {
fun initial() =
WriteTodoState(
Expand All @@ -26,6 +26,5 @@ data class WriteTodoState(
showDateSelectDialog = false,
showHourSelectDialog = false
)

}
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/halill/halill2/ui/theme/Theme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private val LightColorPalette = lightColors(
primaryVariant = Teal900,
secondary = Purple500,
background = Color.White,
surface = Color.White,
surface = Gray100,
onSurface = Gray900
)

Expand Down

0 comments on commit eceeacf

Please sign in to comment.