-
Notifications
You must be signed in to change notification settings - Fork 4
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
Calendar 컴포즈 구현 #173
Merged
TTC1018
merged 7 commits into
boostcampwm-2022:develop-compose
from
junhyeongleeee:develop
Sep 18, 2023
Merged
Calendar 컴포즈 구현 #173
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3a4dd10
issue #171 chore: gray 추가
junhyeongleeee 4483499
issue #171 feat: rememberlifecyclerEvent , noRippleClickable util 추가
junhyeongleeee 2dc079a
issue #171 chore: error 주석처리
junhyeongleeee fee1db1
issue #171 feat: content text remember 설정
junhyeongleeee 6b5b20f
issue #171 feat: content text nullable
junhyeongleeee c270295
issue #171 feat: calendar compose migration
junhyeongleeee e7774c9
issue #171 refactor: measure 하지 않아 불필요한 코드 삭제
junhyeongleeee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
271 changes: 271 additions & 0 deletions
271
app/src/main/java/com/boostcamp/dailyfilm/presentation/calendar/compose/CalendarView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,271 @@ | ||
package com.boostcamp.dailyfilm.presentation.calendar.compose | ||
|
||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.alpha | ||
import androidx.compose.ui.draw.clip | ||
import androidx.compose.ui.draw.drawWithCache | ||
import androidx.compose.ui.geometry.Offset | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.layout.ContentScale | ||
import androidx.compose.ui.layout.Layout | ||
import androidx.compose.ui.platform.LocalDensity | ||
import androidx.compose.ui.text.style.TextAlign | ||
import androidx.compose.ui.unit.Constraints | ||
import androidx.compose.ui.unit.TextUnit | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.compose.collectAsStateWithLifecycle | ||
import com.boostcamp.dailyfilm.data.model.DailyFilmItem | ||
import com.boostcamp.dailyfilm.presentation.calendar.DateViewModel | ||
import com.boostcamp.dailyfilm.presentation.calendar.model.DateModel | ||
import com.boostcamp.dailyfilm.presentation.util.compose.noRippleClickable | ||
import com.boostcamp.dailyfilm.presentation.util.compose.rememberLifecycleEvent | ||
import com.boostcamp.dailyfilm.presentation.util.createCalendar | ||
import com.boostcamp.dailyfilm.presentation.util.month | ||
import com.bumptech.glide.integration.compose.ExperimentalGlideComposeApi | ||
import com.bumptech.glide.integration.compose.GlideImage | ||
import java.util.Calendar | ||
|
||
@Composable | ||
fun CalendarView( | ||
viewModel: DateViewModel, | ||
resetFilm: (List<DateModel>) -> Unit, | ||
imgClick: (Int, DateModel) -> Unit, | ||
) { | ||
val lifecycleEvent = rememberLifecycleEvent() | ||
val itemList by viewModel.itemFlow.collectAsStateWithLifecycle(initialValue = null) | ||
val reloadList by viewModel.dateFlow.collectAsStateWithLifecycle(minActiveState = Lifecycle.State.RESUMED) | ||
val dateState by viewModel.dateState.collectAsStateWithLifecycle() | ||
|
||
CalendarView( | ||
lifecycleEvent = lifecycleEvent, | ||
itemList = itemList, | ||
reloadList = reloadList, | ||
dateState = dateState, | ||
currentCalendar = viewModel.calendar, | ||
todayCalendar = viewModel.todayCalendar, | ||
reloadCalendar = { | ||
viewModel.reloadCalendar(it) | ||
}, | ||
resetFilm = resetFilm, | ||
imgClick = imgClick | ||
) | ||
} | ||
|
||
@Composable | ||
fun CalendarView( | ||
lifecycleEvent: Lifecycle.Event, | ||
itemList: List<DailyFilmItem?>?, | ||
reloadList: List<DateModel>, | ||
dateState: DateState, | ||
currentCalendar: Calendar, | ||
todayCalendar: Calendar, | ||
reloadCalendar: (List<DailyFilmItem?>) -> Unit, | ||
resetFilm: (List<DateModel>) -> Unit, | ||
imgClick: (Int, DateModel) -> Unit, | ||
) { | ||
|
||
val textSize = 12.sp | ||
val textHeight = with(LocalDensity.current) { | ||
textSize.toPx() + 10 | ||
}.toInt() | ||
|
||
LaunchedEffect(lifecycleEvent) { | ||
when (lifecycleEvent) { | ||
Lifecycle.Event.ON_PAUSE -> { | ||
dateState.selectedDay = null | ||
} | ||
|
||
Lifecycle.Event.ON_RESUME -> { | ||
// onResume 에서 가 아닌 repeatOnLifecycle 의 RESUMED 상태로 받아도 됐었음. | ||
// LaunchedEffect(key1 = reloadList) 가 안됨 | ||
resetFilm( | ||
reloadList.filter { dateModel -> dateModel.videoUrl != null } | ||
) | ||
} | ||
|
||
else -> {} | ||
} | ||
} | ||
LaunchedEffect(key1 = reloadList) { | ||
resetFilm( | ||
reloadList.filter { dateModel -> dateModel.videoUrl != null } | ||
) | ||
} | ||
|
||
LaunchedEffect(key1 = itemList) { | ||
reloadCalendar(itemList ?: return@LaunchedEffect) | ||
} | ||
|
||
CustomCalendarView( | ||
textHeight = textHeight, | ||
textSize = textSize, | ||
reloadList = reloadList, | ||
currentCalendar = currentCalendar, | ||
todayCalendar = todayCalendar, | ||
dateState = dateState, | ||
imgClick = imgClick | ||
) | ||
} | ||
|
||
@OptIn(ExperimentalGlideComposeApi::class) | ||
@Composable | ||
private fun DateImage(background: Color, alpha: Float, url: String?, onClick: () -> Unit) { | ||
|
||
GlideImage( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.background(background) | ||
.alpha(alpha) | ||
.padding(2.dp) | ||
.clip(RoundedCornerShape(5.dp)) | ||
.noRippleClickable(onClick = onClick), | ||
contentScale = ContentScale.Crop, | ||
model = url, | ||
contentDescription = "" | ||
) | ||
} | ||
|
||
@Composable | ||
private fun CustomCalendarView( | ||
textHeight: Int, | ||
textSize: TextUnit, | ||
reloadList: List<DateModel>, | ||
currentCalendar: Calendar, | ||
todayCalendar: Calendar, | ||
dateState: DateState, | ||
imgClick: (Int, DateModel) -> Unit, | ||
) { | ||
|
||
CustomCalendarView( | ||
textHeight = textHeight | ||
) { | ||
reloadList.forEachIndexed { index, dateModel -> | ||
|
||
val isNotCurrentMonth = isNotCurrentMonth( | ||
dateModel, | ||
currentCalendar.month(), | ||
todayCalendar | ||
) | ||
dateState.isCurrentMonth = isNotCurrentMonth | ||
|
||
Text( | ||
modifier = Modifier | ||
.alpha(dateState.alpha) | ||
.background(dateState.isSelected(index)), | ||
text = dateModel.day, | ||
textAlign = TextAlign.Center, | ||
color = MaterialTheme.colors.primary, | ||
fontSize = textSize | ||
) | ||
DateImage( | ||
background = dateState.isSelected(index), | ||
alpha = dateState.alpha, | ||
url = dateModel.videoUrl | ||
) { | ||
if (!isNotCurrentMonth) { | ||
dateState.apply { | ||
if (dateModel.videoUrl != null) { | ||
selectedDay = null | ||
imgClick(index, dateModel) | ||
} else { | ||
selectedDay = index | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
@Composable | ||
private fun CustomCalendarView(textHeight: Int, content: @Composable () -> Unit) { | ||
|
||
val lineColor = MaterialTheme.colors.primary | ||
|
||
Layout( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.drawWithCache { | ||
onDrawWithContent { | ||
drawContent() | ||
repeat(5) { idx -> | ||
val y = (idx + 1) * size.height / 6 | ||
drawLine( | ||
color = lineColor, | ||
start = Offset(0f, y), | ||
end = Offset(size.width, y), | ||
strokeWidth = 2f | ||
) | ||
} | ||
} | ||
}, | ||
content = content, | ||
) { measureables, constraints -> | ||
|
||
val dayWidth = constraints.maxWidth / 7 | ||
val dayHeight = constraints.maxHeight / 6 | ||
|
||
val placeables = measureables.mapIndexed { idx, measurable -> | ||
measurable.measure( | ||
when (idx % 2) { | ||
0 -> constraints.fixConstraints(width = dayWidth, height = textHeight) | ||
1 -> constraints.fixConstraints( | ||
width = dayWidth, | ||
height = dayHeight - textHeight | ||
) | ||
|
||
else -> constraints | ||
} | ||
) | ||
} | ||
|
||
layout(constraints.maxWidth, constraints.maxHeight) { | ||
|
||
placeables.forEachIndexed { index, placeable -> | ||
val idx = index / 2 | ||
|
||
val verticalIdx = idx / 7 | ||
val horizontalIdx = idx % 7 | ||
|
||
val left = horizontalIdx * dayWidth | ||
val top = verticalIdx * dayHeight | ||
|
||
when (index % 2) { | ||
0 -> placeable.placeRelative(x = left, y = top) | ||
1 -> placeable.placeRelative(x = left, y = top + textHeight) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun isNotCurrentMonth( | ||
dateModel: DateModel, | ||
currentMonth: Int, | ||
todayCalendar: Calendar | ||
): Boolean { | ||
val itemCalendar = with(dateModel) { | ||
createCalendar(year.toInt(), month.toInt() - 1, day.toInt()) | ||
} | ||
return dateModel.month.toInt() != currentMonth || | ||
itemCalendar.timeInMillis > todayCalendar.timeInMillis | ||
} | ||
|
||
private fun Constraints.fixConstraints(width: Int = maxWidth, height: Int = maxHeight) = copy( | ||
minWidth = width, | ||
maxWidth = width, | ||
minHeight = height, | ||
maxHeight = height | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아마 이거 작동하지 않는 이유가
MutableStateFlow
가List
가 변하는 거 잘 인지 못해서 그런 거 같아SharedFlow
로 바꾸거나MutableStateList
써보면 인식 잘 될 듯