feat: load template legend data through the service

This commit is contained in:
Freek van de Ven 2024-07-08 16:39:18 +02:00 committed by Bart Ribbers
parent fe2786d34f
commit baad9204c2
3 changed files with 30 additions and 40 deletions

View file

@ -2,10 +2,10 @@ import "package:flutter/material.dart";
import "package:flutter_availability/src/ui/widgets/calendar.dart"; import "package:flutter_availability/src/ui/widgets/calendar.dart";
import "package:flutter_availability/src/ui/widgets/template_legend.dart"; import "package:flutter_availability/src/ui/widgets/template_legend.dart";
import "package:flutter_availability/src/util/scope.dart"; import "package:flutter_availability/src/util/scope.dart";
import "package:flutter_availability_data_interface/flutter_availability_data_interface.dart"; import "package:flutter_hooks/flutter_hooks.dart";
/// ///
class AvailabilityOverview extends StatefulWidget { class AvailabilityOverview extends StatefulHookWidget {
/// ///
const AvailabilityOverview({ const AvailabilityOverview({
required this.onEditDateRange, required this.onEditDateRange,
@ -35,10 +35,18 @@ class _AvailabilityOverviewState extends State<AvailabilityOverview> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
var theme = Theme.of(context); var theme = Theme.of(context);
var availabilityScope = AvailabilityScope.of(context); var availabilityScope = AvailabilityScope.of(context);
var service = availabilityScope.service;
var options = availabilityScope.options; var options = availabilityScope.options;
var translations = options.translations; var translations = options.translations;
var spacing = options.spacing; var spacing = options.spacing;
var availabilityStream = useMemoized(
() => service.getOverviewDataForMonth(_selectedDate),
[_selectedDate],
);
var availabilitySnapshot = useStream(availabilityStream);
var title = Center( var title = Center(
child: Text( child: Text(
translations.overviewScreenTitle, translations.overviewScreenTitle,
@ -62,33 +70,7 @@ class _AvailabilityOverviewState extends State<AvailabilityOverview> {
var templateLegend = TemplateLegend( var templateLegend = TemplateLegend(
onViewTemplates: widget.onViewTemplates, onViewTemplates: widget.onViewTemplates,
templates: [ availabilities: availabilitySnapshot,
for (var template in <(Color, String)>[
(Colors.red, "Template 1"),
(Colors.blue, "Template 2"),
// do 10 more
(Colors.green, "Template 3"),
(Colors.yellow, "Template 4"),
(Colors.purple, "Template 5"),
(Colors.orange, "Template 6"),
(Colors.teal, "Template 7"),
(Colors.pink, "Template 8"),
(Colors.indigo, "Template 9"),
]) ...[
AvailabilityTemplateModel(
userId: "1",
id: "1",
name: template.$2,
templateType: AvailabilityTemplateType.day,
templateData: DayTemplateData(
startTime: DateTime.now(),
endTime: DateTime.now(),
breaks: [],
),
color: template.$1.value,
),
],
],
); );
// if there is no range selected we want to disable the button // if there is no range selected we want to disable the button

View file

@ -1,19 +1,19 @@
import "package:flutter/material.dart"; import "package:flutter/material.dart";
import "package:flutter_availability/src/service/availability_service.dart";
import "package:flutter_availability/src/util/scope.dart"; import "package:flutter_availability/src/util/scope.dart";
import "package:flutter_availability_data_interface/flutter_availability_data_interface.dart";
/// This shows all the templates of a given month and an option to navigate to /// This shows all the templates of a given month and an option to navigate to
/// the template overview /// the template overview
class TemplateLegend extends StatefulWidget { class TemplateLegend extends StatefulWidget {
/// ///
const TemplateLegend({ const TemplateLegend({
required this.templates, required this.availabilities,
required this.onViewTemplates, required this.onViewTemplates,
super.key, super.key,
}); });
/// /// The stream of availabilities with templates for the current month
final List<AvailabilityTemplateModel> templates; final AsyncSnapshot<List<AvailabilityWithTemplate>> availabilities;
/// Callback for when the user wants to navigate to the overview of templates /// Callback for when the user wants to navigate to the overview of templates
final VoidCallback onViewTemplates; final VoidCallback onViewTemplates;
@ -36,7 +36,10 @@ class _TemplateLegendState extends State<TemplateLegend> {
var colors = options.colors; var colors = options.colors;
var translations = options.translations; var translations = options.translations;
var templatesAvailable = widget.templates.isNotEmpty; var templatesLoading =
widget.availabilities.connectionState == ConnectionState.waiting;
var templatesAvailable = widget.availabilities.data?.isNotEmpty ?? false;
var templates = widget.availabilities.data?.getUniqueTemplates() ?? [];
void onDrawerHeaderClick() { void onDrawerHeaderClick() {
if (!templatesAvailable) { if (!templatesAvailable) {
@ -79,7 +82,7 @@ class _TemplateLegendState extends State<TemplateLegend> {
style: textTheme.titleMedium, style: textTheme.titleMedium,
), ),
// a button to open a drawer with all the templates // a button to open a drawer with all the templates
if (templatesAvailable) ...[ if (templatesAvailable && !templatesLoading) ...[
Icon( Icon(
_templateDrawerOpen _templateDrawerOpen
? Icons.arrow_drop_up ? Icons.arrow_drop_up
@ -116,7 +119,7 @@ class _TemplateLegendState extends State<TemplateLegend> {
child: ListView.builder( child: ListView.builder(
controller: _scrollController, controller: _scrollController,
shrinkWrap: true, shrinkWrap: true,
itemCount: widget.templates.length + 2, itemCount: templates.length + 2,
itemBuilder: (context, index) { itemBuilder: (context, index) {
if (index == 0) { if (index == 0) {
return _TemplateLegendItem( return _TemplateLegendItem(
@ -126,7 +129,7 @@ class _TemplateLegendState extends State<TemplateLegend> {
borderColor: colorScheme.primary, borderColor: colorScheme.primary,
); );
} }
if (index == widget.templates.length + 1) { if (index == templates.length + 1) {
return Padding( return Padding(
padding: const EdgeInsets.only( padding: const EdgeInsets.only(
top: 10, top: 10,
@ -135,7 +138,7 @@ class _TemplateLegendState extends State<TemplateLegend> {
child: createNewTemplateButton, child: createNewTemplateButton,
); );
} }
var template = widget.templates[index - 1]; var template = templates[index - 1];
return _TemplateLegendItem( return _TemplateLegendItem(
name: template.name, name: template.name,
backgroundColor: Color(template.color), backgroundColor: Color(template.color),
@ -149,11 +152,15 @@ class _TemplateLegendState extends State<TemplateLegend> {
) )
: const Divider(height: 1), : const Divider(height: 1),
), ),
if (!templatesAvailable) ...[ if (!templatesAvailable && !_templateDrawerOpen) ...[
const SizedBox(height: 12), const SizedBox(height: 12),
if (templatesLoading) ...[
const CircularProgressIndicator.adaptive(),
] else ...[
createNewTemplateButton, createNewTemplateButton,
], ],
], ],
],
); );
} }
} }

View file

@ -12,6 +12,7 @@ dependencies:
sdk: flutter sdk: flutter
intl: any intl: any
rxdart: ^0.27.0 rxdart: ^0.27.0
flutter_hooks: ^0.20.5
flutter_availability_data_interface: flutter_availability_data_interface:
git: git:
url: https://github.com/Iconica-Development/flutter_availability url: https://github.com/Iconica-Development/flutter_availability