Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Presentation] Added some dialogs for selecting the character role
- Loading branch information
Showing
7 changed files
with
263 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
lib/presentation/shared/dialogs/select_character_role_sub_type_dialog.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:shiori/domain/enums/enums.dart'; | ||
import 'package:shiori/generated/l10n.dart'; | ||
import 'package:shiori/presentation/shared/dialogs/select_enum_dialog.dart'; | ||
import 'package:shiori/presentation/shared/extensions/i18n_extensions.dart'; | ||
import 'package:shiori/presentation/shared/utils/enum_utils.dart'; | ||
|
||
class SelectCharacterRoleSubTypeDialog extends StatelessWidget { | ||
final List<CharacterRoleSubType> excluded; | ||
final List<CharacterRoleSubType> selectedValues; | ||
final Function(CharacterRoleSubType?)? onSave; | ||
|
||
const SelectCharacterRoleSubTypeDialog({ | ||
Key? key, | ||
this.excluded = const <CharacterRoleSubType>[], | ||
this.selectedValues = const <CharacterRoleSubType>[], | ||
this.onSave, | ||
}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final s = S.of(context); | ||
final translatedValues = EnumUtils.getTranslatedAndSortedEnum<CharacterRoleSubType>( | ||
CharacterRoleSubType.values, | ||
(type, _) => s.translateCharacterRoleSubType(type), | ||
); | ||
return SelectEnumDialog<CharacterRoleSubType>( | ||
title: s.subType, | ||
values: translatedValues.map((e) => e.enumValue).toList(), | ||
selectedValues: selectedValues, | ||
excluded: excluded, | ||
lineThroughOnSelectedValues: true, | ||
textResolver: (type) => translatedValues.firstWhere((el) => el.enumValue == type).translation, | ||
onSave: (type) => _onSave(type, context), | ||
); | ||
} | ||
|
||
void _onSave(CharacterRoleSubType? type, BuildContext context) { | ||
onSave?.call(type); | ||
Navigator.pop<CharacterRoleSubType>(context, type); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
lib/presentation/shared/dialogs/select_character_role_type_dialog.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:shiori/domain/enums/enums.dart'; | ||
import 'package:shiori/generated/l10n.dart'; | ||
import 'package:shiori/presentation/shared/dialogs/select_enum_dialog.dart'; | ||
import 'package:shiori/presentation/shared/extensions/i18n_extensions.dart'; | ||
import 'package:shiori/presentation/shared/utils/enum_utils.dart'; | ||
|
||
class SelectCharacterRoleTypeDialog extends StatelessWidget { | ||
final List<CharacterRoleType> excluded; | ||
final List<CharacterRoleType> selectedValues; | ||
final Function(CharacterRoleType?)? onSave; | ||
|
||
const SelectCharacterRoleTypeDialog({ | ||
Key? key, | ||
this.excluded = const <CharacterRoleType>[], | ||
this.selectedValues = const <CharacterRoleType>[], | ||
this.onSave, | ||
}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final s = S.of(context); | ||
final translatedValues = EnumUtils.getTranslatedAndSortedEnum<CharacterRoleType>( | ||
CharacterRoleType.values, | ||
(type, _) => s.translateCharacterRoleType(type), | ||
); | ||
return SelectEnumDialog<CharacterRoleType>( | ||
title: s.role, | ||
values: translatedValues.map((e) => e.enumValue).toList(), | ||
selectedValues: selectedValues, | ||
excluded: excluded, | ||
lineThroughOnSelectedValues: true, | ||
textResolver: (type) => translatedValues.firstWhere((el) => el.enumValue == type).translation, | ||
onSave: (type) => _onSave(type, context), | ||
); | ||
} | ||
|
||
void _onSave(CharacterRoleType? type, BuildContext context) { | ||
onSave?.call(type); | ||
Navigator.pop<CharacterRoleType>(context, type); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:shiori/domain/models/models.dart'; | ||
import 'package:shiori/generated/l10n.dart'; | ||
import 'package:shiori/presentation/shared/extensions/media_query_extensions.dart'; | ||
import 'package:shiori/presentation/shared/utils/toast_utils.dart'; | ||
|
||
class SortItemsDialog extends StatefulWidget { | ||
final List<SortableItem> items; | ||
final void Function(SortResult result) onSave; | ||
|
||
const SortItemsDialog({ | ||
Key? key, | ||
required this.items, | ||
required this.onSave, | ||
}) : super(key: key); | ||
|
||
@override | ||
State<SortItemsDialog> createState() => _SortItemsDialogState(); | ||
} | ||
|
||
class _SortItemsDialogState extends State<SortItemsDialog> { | ||
List<SortableItem> _items = []; | ||
|
||
@override | ||
void initState() { | ||
_items = [...widget.items]; | ||
super.initState(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final theme = Theme.of(context); | ||
final s = S.of(context); | ||
final fToast = ToastUtils.of(context); | ||
final mq = MediaQuery.of(context); | ||
return AlertDialog( | ||
title: Text(s.sort), | ||
content: SizedBox( | ||
height: mq.getHeightForDialogs(_items.length), | ||
width: mq.getWidthForDialogs(), | ||
child: ReorderableListView.builder( | ||
shrinkWrap: true, | ||
itemCount: _items.length, | ||
itemBuilder: (ctx, index) { | ||
final item = _items[index]; | ||
final position = index + 1; | ||
return ListTile( | ||
key: Key('$index'), | ||
title: Text('#$position - ${item.text}', overflow: TextOverflow.ellipsis), | ||
onTap: () => ToastUtils.showInfoToast(fToast, s.holdToReorder), | ||
); | ||
}, | ||
onReorder: (oldIndex, newIndex) => _onReorder(oldIndex, newIndex, context), | ||
), | ||
), | ||
actions: [ | ||
OutlinedButton( | ||
onPressed: () => _discardChanges(context), | ||
child: Text(s.cancel, style: TextStyle(color: theme.primaryColor)), | ||
), | ||
ElevatedButton( | ||
onPressed: () => _applyChanges(context), | ||
child: Text(s.save), | ||
) | ||
], | ||
); | ||
} | ||
|
||
void _onReorder(int oldIndex, int newIndex, BuildContext context) { | ||
final item = _items.elementAt(oldIndex); | ||
|
||
int updatedNewIndex = newIndex; | ||
if (updatedNewIndex >= _items.length) { | ||
updatedNewIndex--; | ||
} | ||
if (updatedNewIndex < 0) { | ||
updatedNewIndex = 0; | ||
} | ||
|
||
setState(() { | ||
_items.removeAt(oldIndex); | ||
_items.insert(updatedNewIndex, item); | ||
}); | ||
} | ||
|
||
void _discardChanges(BuildContext context) { | ||
Navigator.of(context).pop(); | ||
} | ||
|
||
void _applyChanges(BuildContext context) { | ||
bool somethingChanged = false; | ||
for (var i = 0; i < widget.items.length; i++) { | ||
final current = widget.items[i]; | ||
if (current.key != _items[i].key) { | ||
somethingChanged = true; | ||
break; | ||
} | ||
} | ||
|
||
widget.onSave(SortResult(somethingChanged, _items)); | ||
Navigator.of(context).pop(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters