Skip to content

Commit

Permalink
Make swipes on Months
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablit0x committed Nov 8, 2023
1 parent b8e5e00 commit e53db2f
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 56 deletions.
46 changes: 29 additions & 17 deletions app/src/main/java/ru/beryukhov/coffeegram/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Create
import androidx.compose.material.icons.filled.Settings
Expand Down Expand Up @@ -42,6 +44,7 @@ import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.launch
import kotlinx.coroutines.tasks.await
import org.koin.androidx.compose.get
import org.threeten.bp.YearMonth
import ru.beryukhov.coffeegram.animations.TransitionSlot
import ru.beryukhov.coffeegram.app_ui.CoffeegramTheme
import ru.beryukhov.coffeegram.data.CoffeeType
Expand All @@ -59,6 +62,7 @@ import ru.beryukhov.coffeegram.pages.SettingsAppBar
import ru.beryukhov.coffeegram.pages.SettingsPage
import ru.beryukhov.coffeegram.pages.TableAppBar
import ru.beryukhov.coffeegram.pages.TablePage
import ru.beryukhov.coffeegram.utils.toTotalMonths

class MainActivity : ComponentActivity() {

Expand Down Expand Up @@ -95,6 +99,7 @@ internal fun DefaultPreview() {
PagesContent()
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun PagesContent(
modifier: Modifier = Modifier,
Expand All @@ -106,26 +111,33 @@ fun PagesContent(
val currentNavigationState = navigationState
val snackbarHostState = remember { SnackbarHostState() }

// You can scroll all the way up to the year 3000 with page count set to 36 000 --> (3000 * 12)
val pagerState = rememberPagerState(
pageCount = { 36_000 }, initialPage = if (currentNavigationState is NavigationState.TablePage) {
currentNavigationState.yearMonth.toTotalMonths()
} else {
YearMonth.now().toTotalMonths()
}
)

CoffeegramTheme(
themeState = themeState()
) {
Scaffold(
modifier,
topBar = {
when (currentNavigationState) {
is NavigationState.TablePage -> TableAppBar(
yearMonth = currentNavigationState.yearMonth
)
is NavigationState.CoffeeListPage -> CoffeeListAppBar(
localDate = currentNavigationState.date
)
is NavigationState.SettingsPage -> SettingsAppBar()
}
},
snackbarHost = {
SnackbarHost(hostState = snackbarHostState)
Scaffold(modifier, topBar = {
when (currentNavigationState) {
is NavigationState.TablePage -> TableAppBar(
pagerState = pagerState
)

is NavigationState.CoffeeListPage -> CoffeeListAppBar(
localDate = currentNavigationState.date
)

is NavigationState.SettingsPage -> SettingsAppBar()
}
) {
}, snackbarHost = {
SnackbarHost(hostState = snackbarHostState)
}) {
Column(modifier = Modifier.padding(it)) {
Spacer(
Modifier
Expand All @@ -134,7 +146,7 @@ fun PagesContent(
)
when (currentNavigationState) {
is NavigationState.TablePage -> TablePage(
yearMonth = currentNavigationState.yearMonth
pagerState = pagerState
)
is NavigationState.CoffeeListPage -> CoffeeListPage(
localDate = currentNavigationState.date
Expand Down
27 changes: 11 additions & 16 deletions app/src/main/java/ru/beryukhov/coffeegram/model/NavigationStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,34 @@ class NavigationStore(val yearMonth: YearMonth = YearMonth.now()) : InMemoryStor
initialState = NavigationState.TablePage(yearMonth = yearMonth)
) {

private val currentMonth = MutableStateFlow(YearMonth.now())
private val currentYearMonth = MutableStateFlow(YearMonth.now())

override suspend fun handleIntent(intent: NavigationIntent): NavigationState {
return when (intent) {
NavigationIntent.NextMonth -> NavigationState.TablePage(increaseMonth())
NavigationIntent.PreviousMonth -> NavigationState.TablePage(decreaseMonth())
is NavigationIntent.OpenCoffeeListPage -> NavigationState.CoffeeListPage(
LocalDate.of(
currentMonth.value.year,
currentMonth.value.month,
currentYearMonth.value.year,
currentYearMonth.value.month,
intent.dayOfMonth
)
)
NavigationIntent.ReturnToTablePage -> NavigationState.TablePage(currentMonth.value)
is NavigationIntent.SetYearMonth -> NavigationState.TablePage(
setYearMonth(yearMonth = intent.yearMonth)
)
NavigationIntent.ReturnToTablePage -> NavigationState.TablePage(currentYearMonth.value)
NavigationIntent.ToSettingsPage -> NavigationState.SettingsPage
}
}

private fun increaseMonth(): YearMonth {
currentMonth.value = currentMonth.value.plusMonths(1)
return currentMonth.value
}

private fun decreaseMonth(): YearMonth {
currentMonth.value = currentMonth.value.minusMonths(1)
return currentMonth.value
private fun setYearMonth(yearMonth: YearMonth) : YearMonth{
currentYearMonth.value = yearMonth
return currentYearMonth.value
}
}

sealed interface NavigationIntent {
object NextMonth : NavigationIntent
object PreviousMonth : NavigationIntent
data class OpenCoffeeListPage(val dayOfMonth: Int) : NavigationIntent
data class SetYearMonth(val yearMonth: YearMonth): NavigationIntent
object ReturnToTablePage : NavigationIntent
object ToSettingsPage : NavigationIntent
}
Expand Down
74 changes: 51 additions & 23 deletions app/src/main/java/ru/beryukhov/coffeegram/pages/TablePage.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package ru.beryukhov.coffeegram.pages

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.PagerState
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.KeyboardArrowLeft
import androidx.compose.material.icons.filled.KeyboardArrowRight
Expand All @@ -16,7 +20,9 @@ import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
Expand All @@ -33,20 +39,24 @@ import com.airbnb.lottie.compose.LottieCompositionSpec
import com.airbnb.lottie.compose.animateLottieCompositionAsState
import com.airbnb.lottie.compose.rememberLottieComposition
import kotlinx.collections.immutable.toPersistentMap
import kotlinx.coroutines.launch
import org.koin.androidx.compose.getViewModel
import org.threeten.bp.YearMonth
import org.threeten.bp.format.TextStyle
import ru.beryukhov.coffeegram.R
import ru.beryukhov.coffeegram.model.NavigationIntent
import ru.beryukhov.coffeegram.utils.toYearMonth
import ru.beryukhov.coffeegram.view.MonthTable

@OptIn(ExperimentalFoundationApi::class)
@ExperimentalMaterial3Api
@Composable
fun TableAppBar(
yearMonth: YearMonth,
modifier: Modifier = Modifier,
tablePageViewModel: TablePageViewModel = getViewModel<TablePageViewModelImpl>()
pagerState: PagerState,
modifier: Modifier = Modifier
) {
val scope = rememberCoroutineScope()
val yearMonth = pagerState.currentPage.toYearMonth()

TopAppBar(
modifier = modifier,
title = {
Expand All @@ -65,39 +75,57 @@ fun TableAppBar(
},
navigationIcon = {
IconButton(
onClick = { tablePageViewModel.newIntent(NavigationIntent.PreviousMonth) },
onClick = {
scope.launch {
pagerState.animateScrollToPage(pagerState.currentPage + 1)
}
},
modifier = Modifier.semantics {
contentDescription = "ArrowLeft"
}) { Icon(imageVector = Icons.Default.KeyboardArrowLeft, contentDescription = "") }
},
actions = {
IconButton(
onClick = { tablePageViewModel.newIntent(NavigationIntent.NextMonth) },
onClick = {
scope.launch {
pagerState.animateScrollToPage(pagerState.currentPage - 1)
}
},
modifier = Modifier.testTag("ArrowRight")
) { Icon(imageVector = Icons.Default.KeyboardArrowRight, contentDescription = "") }
}
)
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun ColumnScope.TablePage(
yearMonth: YearMonth,
pagerState: PagerState,
modifier: Modifier = Modifier,
tablePageViewModel: TablePageViewModel = getViewModel<TablePageViewModelImpl>()
tablePageViewModel: TablePageViewModel = getViewModel<TablePageViewModelImpl>(),
) {

val yearMonth = pagerState.currentPage.toYearMonth()

LaunchedEffect(key1 = pagerState.currentPage) {
tablePageViewModel.newIntent(NavigationIntent.SetYearMonth(pagerState.currentPage.toYearMonth()))
}

Column(horizontalAlignment = Alignment.End, modifier = modifier.weight(1f)) {
MonthTable(
yearMonth = yearMonth,
filledDayItemsMap = tablePageViewModel.getFilledDayItemsMap(yearMonth).toPersistentMap(),
onClick = { dayOfMonth: Int ->
tablePageViewModel.newIntent(
NavigationIntent.OpenCoffeeListPage(
dayOfMonth
HorizontalPager(state = pagerState) {
MonthTable(
yearMonth = yearMonth,
filledDayItemsMap = tablePageViewModel.getFilledDayItemsMap(yearMonth).toPersistentMap(),
onClick = { dayOfMonth: Int ->
tablePageViewModel.newIntent(
NavigationIntent.OpenCoffeeListPage(
dayOfMonth
)
)
)
},
modifier = Modifier.wrapContentHeight()
)
},
modifier = Modifier.wrapContentHeight()
)
}
Row(
modifier = Modifier.fillMaxSize(),
) {
Expand All @@ -109,20 +137,20 @@ fun ColumnScope.TablePage(
}
}

@OptIn(ExperimentalFoundationApi::class)
@Preview
@Composable
private fun Preview() {
Column {
val date = localDateStub
val pagerState = rememberPagerState(pageCount = { 36_000 })
TablePage(
yearMonth = YearMonth.of(date.year, date.month),
tablePageViewModel = TablePageViewModelStub
tablePageViewModel = TablePageViewModelStub, pagerState = pagerState
)
}
}

@Composable
fun LottieCoffee(modifier: Modifier = Modifier, alignment: Alignment = Alignment.Center,) {
fun LottieCoffee(modifier: Modifier = Modifier, alignment: Alignment = Alignment.Center) {
val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.lottie_coffee))
val progress by animateLottieCompositionAsState(composition)
LottieAnimation(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.beryukhov.coffeegram.utils

import org.threeten.bp.YearMonth

fun YearMonth.toTotalMonths() : Int {
val yearsInMonths = this.year * 12
val months = this.monthValue + 1
return yearsInMonths + months
}

fun Int.toYearMonth() : YearMonth {
val years = this / 12
val months = this % 12 + 1
return YearMonth.of(years, months)
}

0 comments on commit e53db2f

Please sign in to comment.