mirror of
https://github.com/Iconica-Development/flutter_availability.git
synced 2025-05-19 13:13:44 +02:00
feat: load templates in templates overview through the service
This commit is contained in:
parent
3614191711
commit
8b138a8072
2 changed files with 46 additions and 47 deletions
|
@ -85,6 +85,34 @@ class AvailabilityService {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a stream of all templates
|
||||||
|
Stream<List<AvailabilityTemplateModel>> getTemplates() =>
|
||||||
|
dataInterface.getTemplatesForUser(
|
||||||
|
userId: userId,
|
||||||
|
);
|
||||||
|
|
||||||
|
/// Returns a stream of day templates
|
||||||
|
Stream<List<AvailabilityTemplateModel>> getDayTemplates() =>
|
||||||
|
getTemplates().map(
|
||||||
|
(templates) => templates
|
||||||
|
.where(
|
||||||
|
(template) =>
|
||||||
|
template.templateType == AvailabilityTemplateType.day,
|
||||||
|
)
|
||||||
|
.toList(),
|
||||||
|
);
|
||||||
|
|
||||||
|
/// Returns a stream of week templates
|
||||||
|
Stream<List<AvailabilityTemplateModel>> getWeekTemplates() =>
|
||||||
|
getTemplates().map(
|
||||||
|
(templates) => templates
|
||||||
|
.where(
|
||||||
|
(template) =>
|
||||||
|
template.templateType == AvailabilityTemplateType.week,
|
||||||
|
)
|
||||||
|
.toList(),
|
||||||
|
);
|
||||||
|
|
||||||
/// Creates a new template
|
/// Creates a new template
|
||||||
Future<void> createTemplate(AvailabilityTemplateModel template) async {
|
Future<void> createTemplate(AvailabilityTemplateModel template) async {
|
||||||
await dataInterface.createTemplateForUser(
|
await dataInterface.createTemplateForUser(
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
import "package:flutter/material.dart";
|
import "package:flutter/material.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_availability_data_interface/flutter_availability_data_interface.dart";
|
||||||
|
import "package:flutter_hooks/flutter_hooks.dart";
|
||||||
|
|
||||||
/// Overview screen for all the availability templates
|
/// Overview screen for all the availability templates
|
||||||
class AvailabilityTemplateOverview extends StatelessWidget {
|
class AvailabilityTemplateOverview extends HookWidget {
|
||||||
/// Constructor
|
/// Constructor
|
||||||
const AvailabilityTemplateOverview({
|
const AvailabilityTemplateOverview({
|
||||||
required this.onExit,
|
required this.onExit,
|
||||||
|
@ -25,10 +26,17 @@ class AvailabilityTemplateOverview extends StatelessWidget {
|
||||||
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 dayTemplateStream = useMemoized(() => service.getDayTemplates());
|
||||||
|
var weekTemplateStream = useMemoized(() => service.getWeekTemplates());
|
||||||
|
|
||||||
|
var dayTemplatesSnapshot = useStream(dayTemplateStream);
|
||||||
|
var weekTemplatesSnapshot = useStream(weekTemplateStream);
|
||||||
|
|
||||||
var title = Center(
|
var title = Center(
|
||||||
child: Text(
|
child: Text(
|
||||||
translations.templateScreenTitle,
|
translations.templateScreenTitle,
|
||||||
|
@ -39,56 +47,15 @@ class AvailabilityTemplateOverview extends StatelessWidget {
|
||||||
var dayTemplateSection = _TemplateListSection(
|
var dayTemplateSection = _TemplateListSection(
|
||||||
sectionTitle: translations.dayTemplates,
|
sectionTitle: translations.dayTemplates,
|
||||||
createButtonText: translations.createDayTemplate,
|
createButtonText: translations.createDayTemplate,
|
||||||
templates: [
|
|
||||||
for (var template in <(Color, String)>[
|
|
||||||
(Colors.red, "Template 1"),
|
|
||||||
(Colors.blue, "Template 2"),
|
|
||||||
(Colors.green, "Template 3"),
|
|
||||||
(Colors.yellow, "Template 4"),
|
|
||||||
]) ...[
|
|
||||||
AvailabilityTemplateModel(
|
|
||||||
userId: "1",
|
|
||||||
id: "1",
|
|
||||||
name: template.$2,
|
|
||||||
templateType: AvailabilityTemplateType.day,
|
|
||||||
templateData: DayTemplateData(
|
|
||||||
startTime: DateTime.now(),
|
|
||||||
endTime: DateTime.now(),
|
|
||||||
breaks: [],
|
|
||||||
),
|
|
||||||
color: template.$1.value,
|
|
||||||
),
|
|
||||||
],
|
|
||||||
],
|
|
||||||
onEditTemplate: onEditTemplate,
|
onEditTemplate: onEditTemplate,
|
||||||
onAddTemplate: () => onAddTemplate(AvailabilityTemplateType.day),
|
onAddTemplate: () => onAddTemplate(AvailabilityTemplateType.day),
|
||||||
|
templatesSnapshot: dayTemplatesSnapshot,
|
||||||
);
|
);
|
||||||
|
|
||||||
var weekTemplateSection = _TemplateListSection(
|
var weekTemplateSection = _TemplateListSection(
|
||||||
sectionTitle: translations.weekTemplates,
|
sectionTitle: translations.weekTemplates,
|
||||||
createButtonText: translations.createWeekTemplate,
|
createButtonText: translations.createWeekTemplate,
|
||||||
templates: [
|
templatesSnapshot: weekTemplatesSnapshot,
|
||||||
for (var template in <(Color, String)>[
|
|
||||||
(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.week,
|
|
||||||
templateData: DayTemplateData(
|
|
||||||
startTime: DateTime.now(),
|
|
||||||
endTime: DateTime.now(),
|
|
||||||
breaks: [],
|
|
||||||
),
|
|
||||||
color: template.$1.value,
|
|
||||||
),
|
|
||||||
],
|
|
||||||
],
|
|
||||||
onEditTemplate: onEditTemplate,
|
onEditTemplate: onEditTemplate,
|
||||||
onAddTemplate: () => onAddTemplate(AvailabilityTemplateType.week),
|
onAddTemplate: () => onAddTemplate(AvailabilityTemplateType.week),
|
||||||
);
|
);
|
||||||
|
@ -119,14 +86,14 @@ class _TemplateListSection extends StatelessWidget {
|
||||||
const _TemplateListSection({
|
const _TemplateListSection({
|
||||||
required this.sectionTitle,
|
required this.sectionTitle,
|
||||||
required this.createButtonText,
|
required this.createButtonText,
|
||||||
required this.templates,
|
required this.templatesSnapshot,
|
||||||
required this.onEditTemplate,
|
required this.onEditTemplate,
|
||||||
required this.onAddTemplate,
|
required this.onAddTemplate,
|
||||||
});
|
});
|
||||||
|
|
||||||
final String sectionTitle;
|
final String sectionTitle;
|
||||||
final String createButtonText;
|
final String createButtonText;
|
||||||
final List<AvailabilityTemplateModel> templates;
|
final AsyncSnapshot<List<AvailabilityTemplateModel>> templatesSnapshot;
|
||||||
final void Function(AvailabilityTemplateModel template) onEditTemplate;
|
final void Function(AvailabilityTemplateModel template) onEditTemplate;
|
||||||
final VoidCallback onAddTemplate;
|
final VoidCallback onAddTemplate;
|
||||||
|
|
||||||
|
@ -162,7 +129,8 @@ class _TemplateListSection extends StatelessWidget {
|
||||||
Text(sectionTitle, style: textTheme.titleMedium),
|
Text(sectionTitle, style: textTheme.titleMedium),
|
||||||
const SizedBox(height: 8),
|
const SizedBox(height: 8),
|
||||||
const Divider(height: 1),
|
const Divider(height: 1),
|
||||||
for (var template in templates) ...[
|
for (var template
|
||||||
|
in templatesSnapshot.data ?? <AvailabilityTemplateModel>[]) ...[
|
||||||
GestureDetector(
|
GestureDetector(
|
||||||
onTap: () => onEditTemplate(template),
|
onTap: () => onEditTemplate(template),
|
||||||
child: Container(
|
child: Container(
|
||||||
|
@ -191,6 +159,9 @@ class _TemplateListSection extends StatelessWidget {
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
if (templatesSnapshot.connectionState == ConnectionState.waiting) ...[
|
||||||
|
const Center(child: CircularProgressIndicator.adaptive()),
|
||||||
|
],
|
||||||
const SizedBox(height: 8),
|
const SizedBox(height: 8),
|
||||||
templateCreationButton,
|
templateCreationButton,
|
||||||
],
|
],
|
||||||
|
|
Loading…
Reference in a new issue