Add migration scripts, remove dependencies to codegen, and cleanups

This commit is contained in:
Anton Stubenbord
2023-10-06 14:27:42 +02:00
parent 29b7c8bdc0
commit 5dcae3f0b3
32 changed files with 738 additions and 583 deletions

View File

@@ -116,16 +116,16 @@ class _FullscreenTagsFormState extends State<FullscreenTagsForm> {
icon: const Icon(Icons.done),
onPressed: () {
if (widget.allowOnlySelection) {
widget.onSubmit(returnValue: TagsQuery.ids(include: _include));
widget.onSubmit(returnValue: IdsTagsQuery(include: _include));
return;
}
late final TagsQuery query;
if (_notAssigned) {
query = const TagsQuery.notAssigned();
query = const NotAssignedTagsQuery();
} else if (_anyAssigned) {
query = TagsQuery.anyAssigned(tagIds: _include);
query = AnyAssignedTagsQuery(tagIds: _include);
} else {
query = TagsQuery.ids(include: _include, exclude: _exclude);
query = IdsTagsQuery(include: _include, exclude: _exclude);
}
widget.onSubmit(returnValue: query);
},

View File

@@ -96,19 +96,17 @@ class TagsFormField extends StatelessWidget {
if (query == null) {
yield Container();
} else {
final widgets = query.map(
ids: (value) => [
for (var inc in value.include)
_buildTagIdQueryWidget(context, inc, field, false),
for (var exc in value.exclude)
_buildTagIdQueryWidget(context, exc, field, true),
],
anyAssigned: (value) => [
for (var id in value.tagIds)
_buildAnyAssignedTagWidget(context, id, field, value),
],
notAssigned: (value) => [_buildNotAssignedTagWidget(context, field)],
);
final widgets = switch (query) {
IdsTagsQuery(include: var inc, exclude: var exc) => [
for (var i in inc) _buildTagIdQueryWidget(context, i, field, false),
for (var e in exc) _buildTagIdQueryWidget(context, e, field, true),
],
AnyAssignedTagsQuery query => [
for (var id in query.tagIds)
_buildAnyAssignedTagWidget(context, id, field, query),
],
NotAssignedTagsQuery() => [_buildNotAssignedTagWidget(context, field)],
};
for (var child in widgets) {
yield child;
}
@@ -185,7 +183,7 @@ class TagsFormField extends StatelessWidget {
tagIds: query.tagIds.whereNot((element) => element == e).toList(),
);
if (updatedQuery.tagIds.isEmpty) {
field.didChange(const TagsQuery.ids());
field.didChange(const IdsTagsQuery());
} else {
field.didChange(updatedQuery);
}

View File

@@ -259,7 +259,7 @@ class _LabelsPageState extends State<LabelsPage>
LabelTabView<Correspondent>(
labels: state.correspondents,
filterBuilder: (label) => DocumentFilter(
correspondent: IdQueryParameter.fromId(label.id!),
correspondent: SetIdQueryParameter(id: label.id!),
),
canEdit: user.canEditCorrespondents,
canAddNew: user.canCreateCorrespondents,
@@ -287,7 +287,7 @@ class _LabelsPageState extends State<LabelsPage>
LabelTabView<DocumentType>(
labels: state.documentTypes,
filterBuilder: (label) => DocumentFilter(
documentType: IdQueryParameter.fromId(label.id!),
documentType: SetIdQueryParameter(id: label.id!),
),
canEdit: user.canEditDocumentTypes,
canAddNew: user.canCreateDocumentTypes,
@@ -315,7 +315,7 @@ class _LabelsPageState extends State<LabelsPage>
LabelTabView<Tag>(
labels: state.tags,
filterBuilder: (label) => DocumentFilter(
tags: TagsQuery.ids(include: [label.id!]),
tags: IdsTagsQuery(include: [label.id!]),
),
canEdit: user.canEditTags,
canAddNew: user.canCreateTags,
@@ -354,7 +354,7 @@ class _LabelsPageState extends State<LabelsPage>
EditLabelRoute(label).push(context);
},
filterBuilder: (label) => DocumentFilter(
storagePath: IdQueryParameter.fromId(label.id!),
storagePath: SetIdQueryParameter(id: label.id!),
),
canEdit: user.canEditStoragePaths,
canAddNew: user.canCreateStoragePaths,

View File

@@ -32,11 +32,10 @@ class FullscreenLabelForm<T extends Label> extends StatefulWidget {
this.allowSelectUnassigned = true,
required this.canCreateNewLabel,
}) : assert(
!(initialValue?.isOnlyAssigned() ?? false) || showAnyAssignedOption,
!(initialValue?.isOnlyAssigned ?? false) || showAnyAssignedOption,
),
assert(
!(initialValue?.isOnlyNotAssigned() ?? false) ||
showNotAssignedOption,
!(initialValue?.isOnlyNotAssigned ?? false) || showNotAssignedOption,
),
assert((addNewLabelText != null) == (onCreateNewLabel != null));
@@ -87,11 +86,10 @@ class _FullscreenLabelFormState<T extends Label>
final index = AutocompleteHighlightedOption.of(context);
final value = index.isNegative ? null : options.elementAt(index);
widget.onSubmit(
returnValue: value?.maybeWhen(
fromId: (id) => IdQueryParameter.fromId(id),
orElse: () => const IdQueryParameter.unset(),
) ??
const IdQueryParameter.unset(),
returnValue: switch (value) {
SetIdQueryParameter query => query,
_ => const UnsetIdQueryParameter(),
},
);
},
autofocus: true,
@@ -169,7 +167,7 @@ class _FullscreenLabelFormState<T extends Label>
final label = await widget.onCreateNewLabel!(_textEditingController.text);
if (label?.id != null) {
widget.onSubmit(
returnValue: IdQueryParameter.fromId(label!.id!),
returnValue: SetIdQueryParameter(id: label!.id!),
);
}
}
@@ -184,21 +182,21 @@ class _FullscreenLabelFormState<T extends Label>
if (widget.initialValue == null) {
// If nothing is selected yet, show all options first.
for (final option in widget.options.values) {
yield IdQueryParameter.fromId(option.id!);
yield SetIdQueryParameter(id: option.id!);
}
if (widget.showNotAssignedOption) {
yield const IdQueryParameter.notAssigned();
yield const NotAssignedIdQueryParameter();
}
if (widget.showAnyAssignedOption) {
yield const IdQueryParameter.anyAssigned();
yield const AnyAssignedIdQueryParameter();
}
} else {
// If an initial value is given, show not assigned first, which will be selected by default when pressing "done" on keyboard.
if (widget.showNotAssignedOption) {
yield const IdQueryParameter.notAssigned();
yield const NotAssignedIdQueryParameter();
}
if (widget.showAnyAssignedOption) {
yield const IdQueryParameter.anyAssigned();
yield const AnyAssignedIdQueryParameter();
}
for (final option in widget.options.values) {
// Don't include the initial value in the selection
@@ -207,7 +205,7 @@ class _FullscreenLabelFormState<T extends Label>
option.id == initialValue.id) {
continue;
}
yield IdQueryParameter.fromId(option.id!);
yield SetIdQueryParameter(id: option.id!);
}
}
} else {
@@ -216,77 +214,76 @@ class _FullscreenLabelFormState<T extends Label>
.where((e) => e.name.trim().toLowerCase().contains(normalizedQuery));
if (matches.isNotEmpty) {
for (final match in matches) {
yield IdQueryParameter.fromId(match.id!);
yield SetIdQueryParameter(id: match.id!);
}
if (widget.showNotAssignedOption) {
yield const IdQueryParameter.notAssigned();
yield const NotAssignedIdQueryParameter();
}
if (widget.showAnyAssignedOption) {
yield const IdQueryParameter.anyAssigned();
yield const AnyAssignedIdQueryParameter();
}
} else {
if (widget.showNotAssignedOption) {
yield const IdQueryParameter.notAssigned();
yield const NotAssignedIdQueryParameter();
}
if (widget.showAnyAssignedOption) {
yield const IdQueryParameter.anyAssigned();
yield const AnyAssignedIdQueryParameter();
}
if (!(widget.showAnyAssignedOption || widget.showNotAssignedOption)) {
yield const IdQueryParameter.unset();
yield const UnsetIdQueryParameter();
}
}
}
}
String? _buildHintText() {
return widget.initialValue?.when(
unset: () => S.of(context)!.startTyping,
notAssigned: () => S.of(context)!.notAssigned,
anyAssigned: () => S.of(context)!.anyAssigned,
fromId: (id) => widget.options[id]?.name ?? S.of(context)!.startTyping,
);
return switch (widget.initialValue) {
UnsetIdQueryParameter() => S.of(context)!.startTyping,
NotAssignedIdQueryParameter() => S.of(context)!.notAssigned,
AnyAssignedIdQueryParameter() => S.of(context)!.anyAssigned,
SetIdQueryParameter(id: var id) =>
widget.options[id]?.name ?? S.of(context)!.startTyping,
_ => null,
};
}
Widget _buildOptionWidget(IdQueryParameter option, bool highlight) {
void onTap() => widget.onSubmit(returnValue: option);
if (option.isUnset()) {
return Center(
child: Column(
children: [
Text(S.of(context)!.noItemsFound).padded(),
if (widget.onCreateNewLabel != null)
TextButton(
child: Text(widget.addNewLabelText!),
onPressed: _onCreateNewLabel,
),
],
return switch (option) {
NotAssignedIdQueryParameter() => ListTile(
selected: highlight,
selectedTileColor: Theme.of(context).focusColor,
title: Text(S.of(context)!.notAssigned),
onTap: onTap,
),
);
}
return option.whenOrNull(
notAssigned: () => ListTile(
selected: highlight,
selectedTileColor: Theme.of(context).focusColor,
title: Text(S.of(context)!.notAssigned),
onTap: onTap,
),
anyAssigned: () => ListTile(
selected: highlight,
selectedTileColor: Theme.of(context).focusColor,
title: Text(S.of(context)!.anyAssigned),
onTap: onTap,
),
fromId: (id) => ListTile(
selected: highlight,
selectedTileColor: Theme.of(context).focusColor,
title: Text(widget.options[id]!.name),
onTap: onTap,
enabled: widget.allowSelectUnassigned
? true
: widget.options[id]!.documentCount != 0,
),
)!; // Never null, since we already return on unset before
AnyAssignedIdQueryParameter() => ListTile(
selected: highlight,
selectedTileColor: Theme.of(context).focusColor,
title: Text(S.of(context)!.anyAssigned),
onTap: onTap,
),
SetIdQueryParameter(id: var id) => ListTile(
selected: highlight,
selectedTileColor: Theme.of(context).focusColor,
title: Text(widget.options[id]!.name),
onTap: onTap,
enabled: widget.allowSelectUnassigned
? true
: widget.options[id]!.documentCount != 0,
),
UnsetIdQueryParameter() => Center(
child: Column(
children: [
Text(S.of(context)!.noItemsFound).padded(),
if (widget.onCreateNewLabel != null)
TextButton(
child: Text(widget.addNewLabelText!),
onPressed: _onCreateNewLabel,
),
],
),
),
};
}
}

View File

@@ -47,13 +47,13 @@ class LabelFormField<T extends Label> extends StatelessWidget {
}) : super(key: key);
String _buildText(BuildContext context, IdQueryParameter? value) {
return value?.when(
unset: () => '',
notAssigned: () => S.of(context)!.notAssigned,
anyAssigned: () => S.of(context)!.anyAssigned,
fromId: (id) => options[id]?.name,
) ??
'';
return switch (value) {
UnsetIdQueryParameter() => '',
NotAssignedIdQueryParameter() => S.of(context)!.notAssigned,
AnyAssignedIdQueryParameter() => S.of(context)!.anyAssigned,
SetIdQueryParameter(id: var id) => options[id]?.name ?? '',
_ => '',
};
}
@override
@@ -70,9 +70,14 @@ class LabelFormField<T extends Label> extends StatelessWidget {
text: _buildText(context, field.value),
);
final displayedSuggestions = suggestions
.whereNot((e) =>
e.id ==
field.value?.maybeWhen(fromId: (id) => id, orElse: () => -1))
.whereNot(
(e) =>
e.id ==
switch (field.value) {
SetIdQueryParameter(id: var id) => id,
_ => -1,
},
)
.toList();
return Column(
@@ -98,7 +103,7 @@ class LabelFormField<T extends Label> extends StatelessWidget {
? IconButton(
icon: const Icon(Icons.clear),
onPressed: () =>
field.didChange(const IdQueryParameter.unset()),
field.didChange(const UnsetIdQueryParameter()),
)
: null,
),
@@ -151,7 +156,7 @@ class LabelFormField<T extends Label> extends StatelessWidget {
child: ActionChip(
label: Text(suggestion.name),
onPressed: () => field.didChange(
IdQueryParameter.fromId(suggestion.id!),
SetIdQueryParameter(id: suggestion.id!),
),
),
);