Skip to content

Commit

Permalink
fix(calendar): start to current date when past days are enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
JagandeepBrar committed Sep 27, 2022
1 parent d9623e9 commit 6eff6c9
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 16 deletions.
13 changes: 12 additions & 1 deletion lib/extensions/scroll_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,24 @@ import 'package:lunasea/core.dart';
extension ScrollControllerExtension on ScrollController {
static const _duration =
Duration(milliseconds: LunaUI.ANIMATION_SPEED_SCROLLING);
static const _curve = Curves.easeInOutQuart;

Future<void> animateToStart() async {
if (this.hasClients) {
this.animateTo(
0.00,
duration: _duration,
curve: Curves.easeInOutQuart,
curve: _curve,
);
}
}

Future<void> animateToOffset(double offset) async {
if (this.hasClients) {
return this.animateTo(
offset,
duration: _duration,
curve: _curve,
);
}
}
Expand Down
68 changes: 53 additions & 15 deletions lib/modules/dashboard/routes/dashboard/widgets/schedule_view.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:lunasea/database/tables/dashboard.dart';
import 'package:lunasea/extensions/datetime.dart';
import 'package:lunasea/extensions/scroll_controller.dart';
import 'package:lunasea/vendor.dart';

import 'package:lunasea/widgets/ui.dart';
Expand All @@ -22,6 +23,7 @@ class ScheduleView extends StatefulWidget {
}

class _State extends State<ScheduleView> {
final _formatter = DateFormat('EEEE / MMMM dd, y');
late DateTime _today;

@override
Expand All @@ -33,43 +35,79 @@ class _State extends State<ScheduleView> {

@override
Widget build(BuildContext context) {
final controller = HomeNavigationBar.scrollControllers[1];

if (widget.events.isEmpty) {
return LunaListView(
controller: HomeNavigationBar.scrollControllers[1],
controller: controller,
children: [
LunaMessage.inList(text: 'dashboard.NoNewContent'.tr()),
],
);
}
return LunaListView(
controller: HomeNavigationBar.scrollControllers[1],
children: _buildSchedule().expand((e) => e).toList(),

final schedule = _buildSchedule();
Future.microtask(() => controller.animateToOffset(schedule.item2));

return LunaCustomScrollView(
controller: controller,
slivers: [
const SliverPadding(
padding: EdgeInsets.symmetric(vertical: LunaUI.MARGIN_SIZE_HALF),
),
...schedule.item1,
const SliverPadding(
padding: EdgeInsets.only(bottom: LunaUI.MARGIN_SIZE_HALF),
),
],
);
}

List<List<Widget>> _buildSchedule() {
List<List<Widget>> days = [];
Tuple2<List<Widget>, double> _buildSchedule() {
double offset = 0.0;
double offsetOfToday = 0.0;
List<Widget> days = [];
List<DateTime> keys = widget.events.keys.toList();
keys.sort();

for (var key in keys) {
for (final key in keys) {
bool pastDays = DashboardDatabase.CALENDAR_SHOW_PAST_DAYS.read();
bool dayInFuture = key.isAfter(_today.subtract(const Duration(days: 1)));
bool hasEvents = widget.events[key]?.isNotEmpty ?? false;
if ((pastDays || dayInFuture) && hasEvents) days.add(_buildDay(key));
bool isToday = _today.isAtSameMomentAs(key);

if (isToday) {
offsetOfToday = offset;
}

if ((pastDays || dayInFuture) && hasEvents) {
final built = _buildDay(key);
offset += built.item2;
days.addAll(built.item1);
}
}

return days;
return Tuple2(days, offsetOfToday);
}

List<Widget> _buildDay(DateTime day) {
List<Widget> content = [];
Tuple2<List<Widget>, double> _buildDay(DateTime day) {
List<CalendarData> events = widget.events[day]!;
for (final event in events) content.add(ContentBlock(event));

return [
LunaHeader(text: DateFormat('EEEE / MMMM dd, y').format(day)),
...content,
final extent = LunaBlock.calculateItemExtent(3);
final offset = 39.0 + events.length * extent;
final slivers = [
SliverToBoxAdapter(
child: LunaHeader(text: _formatter.format(day)),
),
SliverFixedExtentList(
delegate: SliverChildBuilderDelegate(
(_, index) => ContentBlock(events[index]),
childCount: events.length,
),
itemExtent: extent,
),
];

return Tuple2(slivers, offset);
}
}
1 change: 1 addition & 0 deletions lib/widgets/ui/list_view.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export 'list_view/custom_scroll_view.dart';
export 'list_view/list_view.dart';
export 'list_view/list_view_builder.dart';
export 'list_view/list_view_modal.dart';
Expand Down
26 changes: 26 additions & 0 deletions lib/widgets/ui/list_view/custom_scroll_view.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:flutter/material.dart';

class LunaCustomScrollView extends StatelessWidget {
final ScrollController controller;
final List<Widget> slivers;

const LunaCustomScrollView({
super.key,
required this.controller,
required this.slivers,
});

@override
Widget build(BuildContext context) {
return Scrollbar(
controller: controller,
interactive: true,
child: CustomScrollView(
controller: controller,
keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
slivers: slivers,
physics: const AlwaysScrollableScrollPhysics(),
),
);
}
}

0 comments on commit 6eff6c9

Please sign in to comment.