From adb50ee985ffdab5f44680742f8a3b89952fe117 Mon Sep 17 00:00:00 2001 From: Mateusz Budnik Date: Mon, 3 Oct 2022 17:13:52 +0200 Subject: [PATCH] Support for landscape mode --- .../java/com/mabn/calendar/MainActivity.kt | 6 ++++- library/build.gradle | 1 + .../calendarlibrary/ExpandableCalendar.kt | 2 ++ .../mabn/calendarlibrary/component/DayView.kt | 5 +++- .../component/MonthViewCalendar.kt | 24 +++++++++++-------- 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/mabn/calendar/MainActivity.kt b/app/src/main/java/com/mabn/calendar/MainActivity.kt index d7a5641..f8911aa 100644 --- a/app/src/main/java/com/mabn/calendar/MainActivity.kt +++ b/app/src/main/java/com/mabn/calendar/MainActivity.kt @@ -6,7 +6,9 @@ import androidx.activity.compose.setContent import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.shape.CircleShape +import androidx.compose.foundation.verticalScroll import androidx.compose.material.MaterialTheme import androidx.compose.material.Surface import androidx.compose.material.Text @@ -42,7 +44,8 @@ class MainActivity : ComponentActivity() { @Composable fun Calendar() { val currentDate = remember { mutableStateOf(LocalDate.now()) } - Column() { + val scrollState = rememberScrollState() + Column(Modifier.verticalScroll(scrollState)) { ExpandableCalendar( theme = calendarDefaultTheme.copy( dayShape = CircleShape, @@ -61,6 +64,7 @@ fun Calendar() { } } + @Preview(showBackground = true) @Composable fun DefaultPreview() { diff --git a/library/build.gradle b/library/build.gradle index 3f260c1..3fbbb0d 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -44,6 +44,7 @@ dependencies { implementation "androidx.compose.material:material:$compose_version" implementation "androidx.compose.runtime:runtime-livedata:$compose_version" implementation "com.google.accompanist:accompanist-pager:0.23.1" + implementation "com.google.accompanist:accompanist-flowlayout:0.26.4-beta" testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' diff --git a/library/src/main/java/com/mabn/calendarlibrary/ExpandableCalendar.kt b/library/src/main/java/com/mabn/calendarlibrary/ExpandableCalendar.kt index e7440f8..1b5f160 100644 --- a/library/src/main/java/com/mabn/calendarlibrary/ExpandableCalendar.kt +++ b/library/src/main/java/com/mabn/calendarlibrary/ExpandableCalendar.kt @@ -3,6 +3,8 @@ package com.mabn.calendarlibrary import androidx.compose.animation.animateContentSize import androidx.compose.foundation.background import androidx.compose.foundation.layout.* +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.verticalScroll import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier diff --git a/library/src/main/java/com/mabn/calendarlibrary/component/DayView.kt b/library/src/main/java/com/mabn/calendarlibrary/component/DayView.kt index 7847ed1..b228f68 100644 --- a/library/src/main/java/com/mabn/calendarlibrary/component/DayView.kt +++ b/library/src/main/java/com/mabn/calendarlibrary/component/DayView.kt @@ -46,7 +46,10 @@ fun DayView( else modifier.background(theme.dayBackgroundColor, shape = theme.dayShape) Column( horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.Center, modifier = Modifier + .heightIn(max = if (weekDayLabel) 50.dp + 20.dp else 50.dp) + .widthIn(max = 50.dp) .testTag("day_view_column") ) { if (weekDayLabel) { @@ -61,7 +64,7 @@ fun DayView( } Box( dayValueModifier - .padding(10.dp) + .padding(5.dp) .aspectRatio(1f) .clickable { onDayClick(date) }, contentAlignment = Alignment.Center diff --git a/library/src/main/java/com/mabn/calendarlibrary/component/MonthViewCalendar.kt b/library/src/main/java/com/mabn/calendarlibrary/component/MonthViewCalendar.kt index 8de102b..048716e 100644 --- a/library/src/main/java/com/mabn/calendarlibrary/component/MonthViewCalendar.kt +++ b/library/src/main/java/com/mabn/calendarlibrary/component/MonthViewCalendar.kt @@ -1,15 +1,16 @@ package com.mabn.calendarlibrary.component -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.PaddingValues -import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.grid.GridCells import androidx.compose.foundation.lazy.grid.LazyVerticalGrid import androidx.compose.foundation.lazy.grid.itemsIndexed import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalConfiguration import androidx.compose.ui.unit.dp +import com.google.accompanist.flowlayout.FlowColumn +import com.google.accompanist.flowlayout.FlowRow import com.mabn.calendarlibrary.core.CalendarTheme import com.mabn.calendarlibrary.utils.dayViewModifier import java.time.LocalDate @@ -24,17 +25,20 @@ internal fun MonthViewCalendar( loadDatesForMonth: (YearMonth) -> Unit, onDayClick: (LocalDate) -> Unit ) { + val itemWidth = LocalConfiguration.current.screenWidthDp / 7 CalendarPager( loadedDates = loadedDates, loadNextDates = { loadDatesForMonth(currentMonth) }, loadPrevDates = { loadDatesForMonth(currentMonth.minusMonths(2)) } // why 2 ) { currentPage -> - LazyVerticalGrid( - columns = GridCells.Fixed(7), - verticalArrangement = Arrangement.Top, - ) { - itemsIndexed(loadedDates[currentPage]) { index, date -> - Box(Modifier.padding(5.dp)) { + FlowRow(Modifier.height(355.dp)) { + loadedDates[currentPage].forEachIndexed { index, date -> + Box( + Modifier + .width(itemWidth.dp) + .padding(5.dp), + contentAlignment = Alignment.Center + ) { DayView( date, theme = theme,