From 880e10309fe48d47bc93f702b5c8c08151de9bc6 Mon Sep 17 00:00:00 2001 From: StepanovPlaton Date: Sun, 15 Dec 2024 12:39:55 +0400 Subject: [PATCH] Complete main activity scroll --- .idea/misc.xml | 1 - .../com/example/ssau_schedule/MainActivity.kt | 60 ++++++++++++------- .../java/com/example/ssau_schedule/Utils.kt | 31 ++++++++-- .../ssau_schedule/components/EmptyDay.kt | 41 +++++++++++++ .../ssau_schedule/components/LessonCards.kt | 11 ++-- 5 files changed, 112 insertions(+), 32 deletions(-) create mode 100644 app/src/main/java/com/example/ssau_schedule/components/EmptyDay.kt diff --git a/.idea/misc.xml b/.idea/misc.xml index 0ad17cb..8978d23 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/app/src/main/java/com/example/ssau_schedule/MainActivity.kt b/app/src/main/java/com/example/ssau_schedule/MainActivity.kt index c41dfbd..dd3b09c 100644 --- a/app/src/main/java/com/example/ssau_schedule/MainActivity.kt +++ b/app/src/main/java/com/example/ssau_schedule/MainActivity.kt @@ -7,10 +7,10 @@ import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge import androidx.compose.foundation.background +import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.fillMaxSize @@ -18,7 +18,6 @@ import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.imePadding import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width import androidx.compose.foundation.pager.HorizontalPager import androidx.compose.foundation.pager.rememberPagerState @@ -27,13 +26,8 @@ import androidx.compose.material.icons.Icons import androidx.compose.material.icons.automirrored.filled.KeyboardArrowLeft import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight import androidx.compose.material.icons.filled.DateRange -import androidx.compose.material.icons.filled.KeyboardArrowRight -import androidx.compose.material3.Button -import androidx.compose.material3.ButtonDefaults -import androidx.compose.material3.Divider import androidx.compose.material3.HorizontalDivider import androidx.compose.material3.Icon -import androidx.compose.material3.IconButton import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Scaffold import androidx.compose.material3.Snackbar @@ -45,21 +39,25 @@ import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier -import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.shadow +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import androidx.core.view.WindowCompat import com.example.ssau_schedule.api.Http import com.example.ssau_schedule.api.LessonAPI import com.example.ssau_schedule.api.LessonAPIErrorMessage +import com.example.ssau_schedule.components.EmptyDay import com.example.ssau_schedule.components.LessonCards import com.example.ssau_schedule.data.base.Database import com.example.ssau_schedule.data.base.entity.lesson.Lesson import com.example.ssau_schedule.data.store.StoreUtils import com.example.ssau_schedule.ui.theme.SSAU_ScheduleTheme +import kotlinx.coroutines.launch import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json import java.util.Date @@ -73,8 +71,6 @@ class MainActivity : ComponentActivity() { super.onCreate(savedInstanceState) enableEdgeToEdge() - WindowCompat.setDecorFitsSystemWindows(window, false) - setContent { SSAU_ScheduleTheme { MainPage() @@ -87,14 +83,15 @@ class MainActivity : ComponentActivity() { database = remember { Database.getInstance(applicationContext) } val snackbarHostState = remember { SnackbarHostState() } val lessons = remember { mutableStateOf>(listOf()) } - + val animationScope = rememberCoroutineScope() val currentDate = remember { mutableStateOf(Date()) } val currentDayOfWeek = remember { - mutableIntStateOf(Utils.Date.getDateOfWeek(currentDate.value)) + mutableIntStateOf(Utils.Date.getDayOfWeek(currentDate.value)) } val pagerState = rememberPagerState( - initialPage = currentDayOfWeek.intValue-1, pageCount = {Int.MAX_VALUE}) - + initialPage = currentDayOfWeek.intValue, + pageCount = {Int.MAX_VALUE} + ) LaunchedEffect(false) { lessons.value = database.lessonDao().getAll() } @@ -145,13 +142,18 @@ class MainActivity : ComponentActivity() { .padding(padding) .imePadding(), ) { - Column { + Column(Modifier.fillMaxHeight()) { Box(Modifier.fillMaxWidth().height(60.dp)) { Row(Modifier.fillMaxSize().padding(10.dp), horizontalArrangement = Arrangement.SpaceBetween) { Box(Modifier.height(40.dp).width(40.dp) .shadow(elevation = 6.dp, shape = RoundedCornerShape(50)) - .background(MaterialTheme.colorScheme.surface), + .background(MaterialTheme.colorScheme.surface) + .clickable { + animationScope.launch { + pagerState.animateScrollToPage(pagerState.currentPage-1) + } + }, ) { Icon(Icons.AutoMirrored.Filled.KeyboardArrowLeft, contentDescription = "Forward icon", @@ -169,7 +171,10 @@ class MainActivity : ComponentActivity() { contentDescription = "Date icon", Modifier.height(40.dp).padding(0.dp, 0.dp, 10.dp, 0.dp), tint = MaterialTheme.colorScheme.primary) - Text(Utils.Date.format(currentDate.value), + Text(Utils.Date.format( + Utils.Date.addDays(currentDate.value, + pagerState.currentPage-currentDayOfWeek.intValue) + ), color = MaterialTheme.colorScheme.primary, fontWeight = FontWeight.SemiBold, style = MaterialTheme.typography.bodyLarge) @@ -177,7 +182,12 @@ class MainActivity : ComponentActivity() { } Box(Modifier.height(40.dp).width(40.dp) .shadow(elevation = 6.dp, shape = RoundedCornerShape(50)) - .background(MaterialTheme.colorScheme.surface), + .background(MaterialTheme.colorScheme.surface) + .clickable { + animationScope.launch { + pagerState.animateScrollToPage(pagerState.currentPage+1) + } + }, ) { Icon(Icons.AutoMirrored.Filled.KeyboardArrowRight, contentDescription = "Forward icon", @@ -187,12 +197,20 @@ class MainActivity : ComponentActivity() { } } HorizontalDivider(Modifier.padding(20.dp, 0.dp)) - HorizontalPager(state = pagerState) { _ -> - LessonCards(lessons.value) + HorizontalPager(state = pagerState) { page -> + val todayLessons = lessons.value.filter { lesson -> + lesson.dayOfWeek-1 == Utils.Date.getDayOfWeek( + Utils.Date.addDays(currentDate.value, page-currentDayOfWeek.intValue)) && + lesson.week == Utils.Date.getWeekOfStudyYear( + Utils.Date.addDays(currentDate.value, page-currentDayOfWeek.intValue)) + }.sortedBy { lesson -> lesson.beginTime } + if(todayLessons.isEmpty()) + EmptyDay(Modifier) + else + LessonCards(todayLessons) } } } } - } } \ No newline at end of file diff --git a/app/src/main/java/com/example/ssau_schedule/Utils.kt b/app/src/main/java/com/example/ssau_schedule/Utils.kt index 043a44f..3aa112b 100644 --- a/app/src/main/java/com/example/ssau_schedule/Utils.kt +++ b/app/src/main/java/com/example/ssau_schedule/Utils.kt @@ -2,6 +2,7 @@ package com.example.ssau_schedule import android.annotation.SuppressLint import android.graphics.Rect +import android.util.Log import android.view.ViewTreeObserver import androidx.compose.runtime.Composable import androidx.compose.runtime.DisposableEffect @@ -11,6 +12,7 @@ import androidx.compose.runtime.remember import androidx.compose.ui.platform.LocalView import kotlinx.serialization.json.Json import java.text.SimpleDateFormat +import java.time.LocalDate import java.util.Calendar class Utils { @@ -50,12 +52,31 @@ class Utils { fun storeFormat(date: java.util.Date): String = StoreDateFormat.format(date) fun format(date: java.util.Date): String = DateFormat.format(date) - fun getDateOfWeek(data: java.util.Date): Int { + fun getDayOfWeek(date: java.util.Date): Int { val calendar = Calendar.getInstance() - calendar.minimalDaysInFirstWeek = 6 - calendar.firstDayOfWeek = Calendar.MONDAY - calendar.time = data - return calendar.get(Calendar.DAY_OF_WEEK)-1 + calendar.time = date + return (calendar.get(Calendar.DAY_OF_WEEK)+5)%7 + } + private fun getWeekOfYear(date: java.util.Date): Int { + val calendar = Calendar.getInstance() + calendar.time = date + return calendar.get(Calendar.WEEK_OF_YEAR) - + (if(calendar.get(Calendar.DAY_OF_WEEK) == 1) 1 else 0) + } + fun getWeekOfStudyYear(date: java.util.Date): Int { + val calendar = Calendar.getInstance() + calendar.time = java.util.Date() + val year = calendar.get(Calendar.YEAR) + calendar.time = parse("${year}-09-01") + return getWeekOfYear(date) - (calendar.get(Calendar.WEEK_OF_YEAR) - + (if(calendar.get(Calendar.DAY_OF_WEEK) == 1) 1 else 0)) + } + + fun addDays(date: java.util.Date, days: Int): java.util.Date { + val calendar = Calendar.getInstance() + calendar.time = date + calendar.add(Calendar.DAY_OF_MONTH, days) + return java.util.Date(calendar.timeInMillis) } } } diff --git a/app/src/main/java/com/example/ssau_schedule/components/EmptyDay.kt b/app/src/main/java/com/example/ssau_schedule/components/EmptyDay.kt new file mode 100644 index 0000000..23d9a82 --- /dev/null +++ b/app/src/main/java/com/example/ssau_schedule/components/EmptyDay.kt @@ -0,0 +1,41 @@ +package com.example.ssau_schedule.components + +import androidx.compose.foundation.border +import androidx.compose.foundation.background +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxHeight +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.MaterialTheme +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.shadow +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import com.example.ssau_schedule.ui.theme.LessonColors + +@Composable +fun EmptyDay(modifier: Modifier) { + Box(Modifier.fillMaxHeight().fillMaxWidth()) { + Row(modifier.fillMaxWidth() + .padding(14.dp, 8.dp) + .shadow(elevation = 6.dp, shape = RoundedCornerShape(12.dp)) + .background( + if(isSystemInDarkTheme()) LessonColors.Background.Dark.Unknown + else LessonColors.Background.Light.Unknown + ), + ) { + AutoResizeText("Сегодня нет занятий", + modifier = modifier.fillMaxWidth().padding(14.dp), + fontSizeRange = FontSizeRange(10.sp, 24.sp), + color = MaterialTheme.colorScheme.secondary, + style = MaterialTheme.typography.titleLarge, + textAlign = TextAlign.Center) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/ssau_schedule/components/LessonCards.kt b/app/src/main/java/com/example/ssau_schedule/components/LessonCards.kt index 280bf28..f34abd8 100644 --- a/app/src/main/java/com/example/ssau_schedule/components/LessonCards.kt +++ b/app/src/main/java/com/example/ssau_schedule/components/LessonCards.kt @@ -1,6 +1,5 @@ package com.example.ssau_schedule.components -import androidx.compose.foundation.ScrollState import androidx.compose.foundation.background import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.foundation.layout.Arrangement @@ -12,8 +11,8 @@ import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.width +import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.foundation.verticalScroll import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.runtime.Composable @@ -63,9 +62,11 @@ fun LessonCard(modifier: Modifier, lesson: Lesson) { @Composable fun LessonCards(lessons: List) { - Column(Modifier.verticalScroll(ScrollState(0))) { - lessons.forEach { lesson -> - LessonCard(Modifier, lesson) + Box(Modifier.fillMaxHeight().fillMaxWidth()) { + LazyColumn { + items(lessons.count()) { + LessonCard(Modifier, lessons[it]) + } } } }