Skip to content

Commit

Permalink
Update Compose to 1.6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechOsak committed Mar 26, 2024
1 parent cabf01e commit ac6f67f
Show file tree
Hide file tree
Showing 21 changed files with 162 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class CalendarAnimator(private val startDate: LocalDate) {
pageOffsetFraction: Float = 0f,
animationSpec: AnimationSpec<Float> = spring(stiffness = Spring.StiffnessMediumLow),
) {
val initialPage = pagerState?.initialPage ?: INITIAL_PAGE_INDEX
val initialPage = INITIAL_PAGE_INDEX
val currentDate =
when (mode) {
AnimationMode.MONTH ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.wojciechosak.calendar.config

object CalendarConstants {
internal const val INITIAL_PAGE_INDEX = Int.MAX_VALUE / 2
// Compose 1.6.1 bug: https://issuetracker.google.com/issues/311414925, let's use fixed numbers for now.
// internal const val MAX_PAGES = Int.MAX_VALUE
internal const val MAX_PAGES = 100000
internal const val INITIAL_PAGE_INDEX = MAX_PAGES / 2
}
Original file line number Diff line number Diff line change
@@ -1,58 +1,54 @@
package io.wojciechosak.calendar.view

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.PageSize
import androidx.compose.foundation.pager.PagerDefaults
import androidx.compose.foundation.pager.PagerState
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.unit.dp
import io.wojciechosak.calendar.animation.CalendarAnimator
import io.wojciechosak.calendar.config.CalendarConstants.INITIAL_PAGE_INDEX
import io.wojciechosak.calendar.config.CalendarConstants.MAX_PAGES
import kotlinx.datetime.LocalDate

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun HorizontalCalendarView(
startDate: LocalDate,
pagerState: PagerState =
rememberPagerState(
initialPage = INITIAL_PAGE_INDEX,
pageCount = { MAX_PAGES },
initialPageOffsetFraction = 0f,
),
modifier: Modifier = Modifier,
pageSize: PageSize = PageSize.Fill,
beyondBoundsPageCount: Int = 0,
contentPadding: PaddingValues = PaddingValues(0.dp),
pageNestedScrollConnection: NestedScrollConnection =
PagerDefaults.pageNestedScrollConnection(
Orientation.Horizontal,
),
calendarAnimator: CalendarAnimator = CalendarAnimator(startDate),
calendarView: @Composable (monthOffset: Int) -> Unit,
) {
val pagerState =
rememberPagerState(
initialPage = INITIAL_PAGE_INDEX,
pageCount = { Int.MAX_VALUE },
)

HorizontalPager(
state = pagerState,
modifier = modifier,
pageSize = pageSize,
beyondBoundsPageCount = beyondBoundsPageCount,
pageNestedScrollConnection = pageNestedScrollConnection,
contentPadding = contentPadding,
) {
val index = it - INITIAL_PAGE_INDEX
calendarAnimator.updatePagerState(pagerState)
LaunchedEffect(Unit) {
calendarAnimator.setAnimationMode(CalendarAnimator.AnimationMode.MONTH)
}
Column {
calendarView(index)
if (pagerState.currentPage == it) {
val index = it - INITIAL_PAGE_INDEX
calendarAnimator.updatePagerState(pagerState)
LaunchedEffect(Unit) {
calendarAnimator.setAnimationMode(CalendarAnimator.AnimationMode.MONTH)
}
Column {
calendarView(index)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import io.wojciechosak.calendar.animation.CalendarAnimator
import io.wojciechosak.calendar.config.CalendarConstants.INITIAL_PAGE_INDEX
import io.wojciechosak.calendar.config.CalendarConstants.MAX_PAGES
import kotlinx.datetime.LocalDate

@OptIn(ExperimentalFoundationApi::class)
Expand All @@ -27,7 +28,7 @@ fun VerticalCalendarView(
val pagerState =
rememberPagerState(
initialPage = INITIAL_PAGE_INDEX,
pageCount = { Int.MAX_VALUE },
pageCount = { MAX_PAGES },
)
LaunchedEffect(pagerState) {
calendarAnimator.setAnimationMode(CalendarAnimator.AnimationMode.MONTH)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import io.wojciechosak.calendar.animation.CalendarAnimator
import io.wojciechosak.calendar.config.CalendarConstants.INITIAL_PAGE_INDEX
import io.wojciechosak.calendar.config.CalendarConstants.MAX_PAGES
import io.wojciechosak.calendar.config.DayState
import io.wojciechosak.calendar.utils.copy
import io.wojciechosak.calendar.utils.daySimpleName
Expand Down Expand Up @@ -61,7 +62,7 @@ fun WeekView(
},
) {
val minIndex = if (showDaysBesideRange) 0 else minDate.daysUntil(startDate)
val maxIndex = if (showDaysBesideRange) Int.MAX_VALUE else startDate.daysUntil(maxDate)
val maxIndex = if (showDaysBesideRange) MAX_PAGES else startDate.daysUntil(maxDate)
val initialPageIndex = if (showDaysBesideRange) INITIAL_PAGE_INDEX else minIndex + daysOffset
LaunchedEffect(Unit) {
calendarAnimator.setAnimationMode(CalendarAnimator.AnimationMode.WEEK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import io.wojciechosak.calendar.config.CalendarConstants
import io.wojciechosak.calendar.config.CalendarConstants.MAX_PAGES
import io.wojciechosak.calendar.modifiers.passTouchGesture
import io.wojciechosak.calendar.utils.today
import kotlinx.datetime.LocalDate
Expand All @@ -38,7 +39,7 @@ fun YearPicker(
val pagerState =
rememberPagerState(
initialPage = CalendarConstants.INITIAL_PAGE_INDEX,
pageCount = { Int.MAX_VALUE },
pageCount = { MAX_PAGES },
)
when (mode) {
YearPickerMode.VERTICAL -> {
Expand Down
10 changes: 5 additions & 5 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[versions]

kotlin = "1.9.21"
compose = "1.5.11"
agp = "8.2.0"
compose = "1.6.1"
agp = "8.2.2"
androidx-appcompat = "1.6.1"
androidx-activityCompose = "1.8.1"
compose-uitooling = "1.5.4"
kotlinx-coroutines = "1.7.3"
androidx-activityCompose = "1.8.2"
compose-uitooling = "1.6.4"
kotlinx-coroutines = "1.8.0"
composeIcons = "1.1.0"
kotlinx-datetime = "0.5.0"
voyager = "1.0.0"
Expand Down
Binary file modified readme/info.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,14 +1,68 @@
package io.wojciechosak.calendar.calendar

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.safeContentPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBackIos
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SmallTopAppBar
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.sp
import cafe.adriel.voyager.navigator.CurrentScreen
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.Navigator
import io.wojciechosak.calendar.calendar.screens.NamedScreen

@OptIn(ExperimentalMaterial3Api::class)
@Composable
internal fun App() {
Box(modifier = Modifier.safeContentPadding()) {
Navigator(MenuScreen())
Box {
Navigator(MenuScreen()) {
Scaffold(
topBar = {
val navigator = LocalNavigator.current
SmallTopAppBar(
colors =
TopAppBarDefaults.topAppBarColors(
containerColor = MaterialTheme.colorScheme.primary,
titleContentColor = Color.White,
),
title = {
if (navigator?.canPop == true) {
Text((navigator.lastItem as? NamedScreen)?.name ?: "", fontSize = 14.sp)
} else {
Text("KMP Calendar Demo", fontSize = 14.sp)
}
},
navigationIcon = {
if (navigator?.canPop == true) {
IconButton(
onClick = { navigator.pop() },
) {
Icon(
imageVector = Icons.AutoMirrored.Filled.ArrowBackIos,
contentDescription = "Back",
tint = Color.White,
)
}
}
},
)
},
content = { padding ->
Box(Modifier.padding(padding)) {
CurrentScreen()
}
},
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class MenuScreen : Screen {
item { ScreenButton(AnimationScreen(), "Animations") }
item { ScreenButton(RangeSelectionScreen(), "Range selection") }
item { ScreenButton(FullDateScreen(), "Full date selector (day/month/year)") }
item { Text("Lib version: 0.0.5") }
item { Text("Lib version: 0.0.8") }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import cafe.adriel.voyager.core.screen.Screen
import io.wojciechosak.calendar.animation.CalendarAnimator
import io.wojciechosak.calendar.config.rememberCalendarState
import io.wojciechosak.calendar.utils.daySimpleName
Expand All @@ -27,7 +26,10 @@ import kotlinx.coroutines.launch
import kotlinx.datetime.LocalDate
import kotlinx.datetime.Month

class AnimationScreen : Screen {
class AnimationScreen : NamedScreen {
override val name: String
get() = "Animations"

@OptIn(ExperimentalFoundationApi::class)
@Composable
override fun Content() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeGesturesPadding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.OutlinedButton
Expand All @@ -17,7 +18,6 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import cafe.adriel.voyager.core.screen.Screen
import io.wojciechosak.calendar.config.MonthYear
import io.wojciechosak.calendar.config.rememberCalendarState
import io.wojciechosak.calendar.config.toLocalDate
Expand All @@ -26,27 +26,32 @@ import kotlinx.datetime.LocalDate
import kotlinx.datetime.Month
import kotlin.random.Random

class CalendarViewScreen : Screen {
class CalendarViewScreen : NamedScreen {
override val name: String
get() = "CalendarView"

@Composable
override fun Content() {
CalendarView(
day = { state ->
DayView(
date = state.date,
isDotVisible = state.isActiveDay || Random.nextBoolean(),
onClick = { },
)
},
config =
rememberCalendarState(
startDate = MonthYear(year = 1994, month = Month.APRIL).toLocalDate(),
monthOffset = 0,
showNextMonthDays = false,
showPreviousMonthDays = false,
showHeader = false,
showWeekdays = false,
),
)
Box(Modifier.safeGesturesPadding()) {
CalendarView(
day = { state ->
DayView(
date = state.date,
isDotVisible = state.isActiveDay || Random.nextBoolean(),
onClick = { },
)
},
config =
rememberCalendarState(
startDate = MonthYear(year = 1994, month = Month.APRIL).toLocalDate(),
monthOffset = 0,
showNextMonthDays = false,
showPreviousMonthDays = false,
showHeader = false,
showWeekdays = false,
),
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.wojciechosak.calendar.calendar.screens

import androidx.compose.runtime.Composable
import cafe.adriel.voyager.core.screen.Screen

class DateRangeScreen : Screen {
class DateRangeScreen : NamedScreen {
override val name: String
get() = "Date range"

@Composable
override fun Content() {
TODO("Not yet implemented")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import cafe.adriel.voyager.core.screen.Screen
import io.wojciechosak.calendar.config.SelectionMode
import io.wojciechosak.calendar.config.rememberCalendarState
import io.wojciechosak.calendar.utils.copy
Expand All @@ -25,7 +24,10 @@ import io.wojciechosak.calendar.view.MonthPicker
import io.wojciechosak.calendar.view.YearPicker
import kotlinx.datetime.LocalDate

class FullDateScreen : Screen {
class FullDateScreen : NamedScreen {
override val name: String
get() = "Full date selector"

@OptIn(ExperimentalFoundationApi::class)
@Composable
override fun Content() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import cafe.adriel.voyager.core.screen.Screen
import io.wojciechosak.calendar.config.rememberCalendarState
import io.wojciechosak.calendar.utils.today
import io.wojciechosak.calendar.view.CalendarDay
import io.wojciechosak.calendar.view.CalendarView
import io.wojciechosak.calendar.view.HorizontalCalendarView
import kotlinx.datetime.LocalDate

class HorizontalCalendarScreen : Screen {
class HorizontalCalendarScreen : NamedScreen {
override val name: String
get() = "Horizontal calendar"

@OptIn(ExperimentalFoundationApi::class)
@Composable
override fun Content() {
Expand Down
Loading

0 comments on commit ac6f67f

Please sign in to comment.