diff --git a/firka/lib/api/client/kreta_client.dart b/firka/lib/api/client/kreta_client.dart index 946d08cd..7acf7ebb 100644 --- a/firka/lib/api/client/kreta_client.dart +++ b/firka/lib/api/client/kreta_client.dart @@ -79,7 +79,11 @@ class KretaClient { return lessons; } - Future renewTimetable({bool wholeYear = false}) async { + Future renewTimetable({ + bool wholeYear = false, + DateTime? skipFrom, + DateTime? skipTo, + }) async { DateTime now = timeNow(); DateTime firstDayOfSchool = now.getFirstSchoolDay(); DateTime firstDayOfNextYear = firstDayOfSchool.copyWith( @@ -129,12 +133,20 @@ class KretaClient { int waitAfter = 5; while (date.isBefore(to)) { DateTime tmpTo = date.add(Duration(days: 14)); - requests.add(getLessons(date, tmpTo)); + final alreadyFetched = + skipFrom != null && + skipTo != null && + !date.isBefore(skipFrom) && + !tmpTo.isAfter(skipTo); + if (!alreadyFetched) { + requests.add(getLessons(date, tmpTo)); + i++; + } date = tmpTo; - i++; if (waitAfter == i) { await Future.wait(requests); + requests = []; i = 0; } } @@ -164,13 +176,16 @@ class KretaClient { if (reInit) { await init(); } + final currentFrom = timeNow().getMonday(); + final currentTo = currentFrom.add(const Duration(days: 14)); await Future.wait([ getTests(), getHomework(), renewMessages(), getGrades(), + getLessons(currentFrom, currentTo), ]); - await renewTimetable(); + await renewTimetable(skipFrom: currentFrom, skipTo: currentTo); // manual link await getOmissions(); @@ -556,6 +571,7 @@ class KretaClient { isarInit.writeTxnSync(() { isarInit.collection().putAllSync(caches); }); + initData.homeRefreshCubit.requestRefresh(); return caches; } diff --git a/firka/lib/ui/phone/pages/home/home_main.dart b/firka/lib/ui/phone/pages/home/home_main.dart index b15e71f9..7aa54f20 100644 --- a/firka/lib/ui/phone/pages/home/home_main.dart +++ b/firka/lib/ui/phone/pages/home/home_main.dart @@ -1,5 +1,3 @@ -import 'dart:async'; - import 'package:firka_common/data/models/lesson_cache_model.dart'; import 'package:firka/ui/phone/widgets/info_card.dart'; import 'package:firka/ui/phone/widgets/lesson_slider.dart'; @@ -13,6 +11,8 @@ import 'package:firka/core/debug_helper.dart'; import 'package:firka/core/state/firka_state.dart'; import 'package:firka/app/app_state.dart'; import 'package:firka/core/bloc/home_refresh_cubit.dart'; +import 'package:firka/core/bloc/toast_cubit.dart'; +import 'package:firka/ui/shared/delayed_spinner.dart'; import 'package:firka/ui/theme/style.dart'; import '../../widgets/home_main_welcome.dart'; @@ -28,6 +28,18 @@ class HomeMainScreen extends StatefulWidget { class _HomeMainScreen extends FirkaState { _HomeMainScreen(); + // After the first frame, newly inserted notice groups fade in. + // Groups present on the initial paint (warm cache) stay instant. + bool _fadeNewInserts = false; + + @override + void initState() { + super.initState(); + WidgetsBinding.instance.addPostFrameCallback((_) { + if (mounted) setState(() => _fadeNewInserts = true); + }); + } + void _onRefreshRequested(BuildContext context) async { final cubit = context.read(); if (mounted) { @@ -44,11 +56,14 @@ class _HomeMainScreen extends FirkaState { listener: (context, state) { _onRefreshRequested(context); }, - child: _buildContent(context), + child: BlocBuilder( + buildWhen: (previous, current) => previous.type != current.type, + builder: (context, toastState) => _buildContent(context, toastState), + ), ); } - Widget _buildContent(BuildContext context) { + Widget _buildContent(BuildContext context, ToastState toastState) { final now = timeNow(); final student = widget.data.client!.cache.findStudentOrNull(); final infoItems = widget.data.client!.cache.getMessages().findAllSync(); @@ -104,6 +119,10 @@ class _HomeMainScreen extends FirkaState { ) .length; + final bodyEmpty = todayLesson.isEmpty && noticeBoardWidgets.isEmpty; + final showInnerSpinner = + toastState.type == ActiveToastType.fetching && bodyEmpty; + return Padding( padding: const EdgeInsets.symmetric(horizontal: 20.0), child: RefreshIndicator( @@ -117,28 +136,49 @@ class _HomeMainScreen extends FirkaState { clipBehavior: Clip.none, children: [ SizedBox(height: 24), - if (student != null) - WelcomeWidget(widget.data.l10n, now, student, todayLesson), + _FadeInWhenLoaded( + loaded: student != null, + child: student != null + ? WelcomeWidget( + widget.data.l10n, + now, + student, + todayLesson, + ) + : const SizedBox.shrink(), + ), SizedBox(height: 48), - LessonSlider(todayLesson, testsTomorrow), + if (showInnerSpinner) + SizedBox( + height: MediaQuery.sizeOf(context).height * 0.35, + child: const Center(child: DelayedSpinnerWidget()), + ), + _FadeInWhenLoaded( + loaded: todayLesson.isNotEmpty, + child: LessonSlider(todayLesson, testsTomorrow), + ), SizedBox(height: 24), ...noticeBoardWidgets .groupList((e) => e.$2) .entries .map( - (e) => Column( - spacing: 10, - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - e.key.format(widget.data.l10n, FormatMode.main), - style: appStyle.fonts.B_16R.apply( - color: appStyle.colors.textSecondary, + (e) => _FadeInOnInsert( + key: ValueKey(e.key), + animate: _fadeNewInserts, + child: Column( + spacing: 10, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + e.key.format(widget.data.l10n, FormatMode.main), + style: appStyle.fonts.B_16R.apply( + color: appStyle.colors.textSecondary, + ), ), - ), - ...e.value.map((v) => v.$1), - SizedBox(height: 10), - ], + ...e.value.map((v) => v.$1), + SizedBox(height: 10), + ], + ), ), ), ], @@ -147,3 +187,92 @@ class _HomeMainScreen extends FirkaState { ); } } + +/// Fades in when [loaded] flips from false to true. First paint with data is instant. +class _FadeInWhenLoaded extends StatelessWidget { + const _FadeInWhenLoaded({ + required this.loaded, + required this.child, + }); + + final bool loaded; + final Widget child; + + static const _duration = Duration(milliseconds: 300); + + @override + Widget build(BuildContext context) { + return AnimatedSwitcher( + duration: _duration, + switchInCurve: Curves.easeOut, + transitionBuilder: (child, animation) => FadeTransition( + opacity: animation, + child: SlideTransition( + position: Tween( + begin: const Offset(0, 0.04), + end: Offset.zero, + ).animate(animation), + child: child, + ), + ), + child: loaded + ? KeyedSubtree(key: const ValueKey("content"), child: child) + : const SizedBox.shrink(key: ValueKey("empty")), + ); + } +} + +/// Fades newly inserted list sections. Skips animation when [animate] is false (warm cache). +class _FadeInOnInsert extends StatefulWidget { + const _FadeInOnInsert({ + super.key, + required this.animate, + required this.child, + }); + + final bool animate; + final Widget child; + + @override + State<_FadeInOnInsert> createState() => _FadeInOnInsertState(); +} + +class _FadeInOnInsertState extends State<_FadeInOnInsert> + with SingleTickerProviderStateMixin { + late final AnimationController _controller; + late final Animation _opacity; + late final Animation _slide; + + @override + void initState() { + super.initState(); + _controller = AnimationController( + vsync: this, + duration: const Duration(milliseconds: 300), + value: widget.animate ? 0 : 1, + ); + final curved = CurvedAnimation(parent: _controller, curve: Curves.easeOut); + _opacity = curved; + _slide = Tween( + begin: const Offset(0, 0.04), + end: Offset.zero, + ).animate(curved); + if (widget.animate) { + _controller.forward(); + } + } + + @override + void dispose() { + _controller.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + return FadeTransition( + opacity: _opacity, + child: SlideTransition(position: _slide, child: widget.child), + ); + } +} diff --git a/firka/lib/ui/phone/screens/home/home_screen.dart b/firka/lib/ui/phone/screens/home/home_screen.dart index ad68552b..52f40c3f 100644 --- a/firka/lib/ui/phone/screens/home/home_screen.dart +++ b/firka/lib/ui/phone/screens/home/home_screen.dart @@ -17,7 +17,6 @@ import 'package:firka/core/bloc/profile_picture_cubit.dart'; import 'package:firka/core/bloc/settings_cubit.dart'; import 'package:firka/core/state/firka_state.dart'; import 'package:firka/core/image_preloader.dart'; -import 'package:firka/ui/shared/delayed_spinner.dart'; bool _fetching = false; bool _prefetched = false; @@ -34,7 +33,6 @@ class HomeScreen extends StatefulWidget { class _HomeScreenState extends FirkaState with WidgetsBindingObserver { bool _disposed = false; - bool _preloadDone = false; bool _didRunLiveActivityLogin = false; bool _hasCompletedFirstPrefetch = false; @@ -178,32 +176,11 @@ class _HomeScreenState extends FirkaState await ImagePreloader.preloadMultipleAssets(FirkaBundle(), imagePaths); } catch (e) { logger.severe('Home: error preloading images: $e'); - } finally { - if (mounted) { - setState(() => _preloadDone = true); - } } } @override Widget build(BuildContext context) { - if (!_preloadDone) { - return Scaffold( - backgroundColor: appStyle.colors.background, - body: Column( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - SizedBox(), - Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [SizedBox(), DelayedSpinnerWidget(), SizedBox()], - ), - SizedBox(), - ], - ), - ); - } - SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge); return BlocListener( listener: (context, state) { @@ -228,7 +205,7 @@ class _HomeScreenState extends FirkaState child: Stack( children: [ widget.child, - ?(context.watch().state.buildWidget(context)), + ?(_toastOverlay(context)), ], ), ), @@ -239,6 +216,14 @@ class _HomeScreenState extends FirkaState ); } + Widget? _toastOverlay(BuildContext context) { + final toast = context.watch().state; + if (toast.type == .fetching && !_hasCompletedFirstPrefetch) { + return null; + } + return toast.buildWidget(context); + } + @override void didChangeAppLifecycleState(AppLifecycleState state) { super.didChangeAppLifecycleState(state);