Refactor group creation and editing logic into separate methods
This commit is contained in:
@@ -160,42 +160,15 @@ class _CreateGroupViewState extends State<CreateGroupView> {
|
|||||||
/// depending on whether the widget is in edit mode.
|
/// depending on whether the widget is in edit mode.
|
||||||
Future<void> _saveGroup() async {
|
Future<void> _saveGroup() async {
|
||||||
final loc = AppLocalizations.of(context);
|
final loc = AppLocalizations.of(context);
|
||||||
Group? updatedGroup;
|
|
||||||
bool successfullNameChange = true;
|
|
||||||
bool successfullMemberChange = true;
|
|
||||||
late bool success;
|
late bool success;
|
||||||
|
Group? updatedGroup;
|
||||||
final groupName = _groupNameController.text.trim();
|
|
||||||
|
|
||||||
if (widget.groupToEdit == null) {
|
if (widget.groupToEdit == null) {
|
||||||
success = await db.groupDao.addGroup(
|
success = await _createGroup();
|
||||||
group: Group(
|
|
||||||
name: groupName,
|
|
||||||
description: '',
|
|
||||||
members: selectedPlayers,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
updatedGroup = Group(
|
final result = await _editGroup();
|
||||||
id: widget.groupToEdit!.id,
|
success = result.$1;
|
||||||
name: groupName,
|
updatedGroup = result.$2;
|
||||||
description: '',
|
|
||||||
members: selectedPlayers,
|
|
||||||
);
|
|
||||||
if (widget.groupToEdit!.name != groupName) {
|
|
||||||
successfullNameChange = await db.groupDao.updateGroupName(
|
|
||||||
groupId: widget.groupToEdit!.id,
|
|
||||||
newName: groupName,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (widget.groupToEdit!.members != selectedPlayers) {
|
|
||||||
successfullMemberChange = await db.groupDao.replaceGroupPlayers(
|
|
||||||
groupId: widget.groupToEdit!.id,
|
|
||||||
newPlayers: selectedPlayers,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
success = successfullNameChange && successfullMemberChange;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
@@ -211,6 +184,51 @@ class _CreateGroupViewState extends State<CreateGroupView> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Handles creating a new group and returns whether the operation was successful.
|
||||||
|
Future<bool> _createGroup() async {
|
||||||
|
final groupName = _groupNameController.text.trim();
|
||||||
|
|
||||||
|
final success = await db.groupDao.addGroup(
|
||||||
|
group: Group(name: groupName, description: '', members: selectedPlayers),
|
||||||
|
);
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Handles editing an existing group and returns a tuple of
|
||||||
|
/// (success, updatedGroup).
|
||||||
|
Future<(bool, Group?)> _editGroup() async {
|
||||||
|
final groupName = _groupNameController.text.trim();
|
||||||
|
|
||||||
|
Group? updatedGroup = Group(
|
||||||
|
id: widget.groupToEdit!.id,
|
||||||
|
name: groupName,
|
||||||
|
description: '',
|
||||||
|
members: selectedPlayers,
|
||||||
|
);
|
||||||
|
|
||||||
|
bool successfullNameChange = true;
|
||||||
|
bool successfullMemberChange = true;
|
||||||
|
|
||||||
|
if (widget.groupToEdit!.name != groupName) {
|
||||||
|
successfullNameChange = await db.groupDao.updateGroupName(
|
||||||
|
groupId: widget.groupToEdit!.id,
|
||||||
|
newName: groupName,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (widget.groupToEdit!.members != selectedPlayers) {
|
||||||
|
successfullMemberChange = await db.groupDao.replaceGroupPlayers(
|
||||||
|
groupId: widget.groupToEdit!.id,
|
||||||
|
newPlayers: selectedPlayers,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
final success = successfullNameChange && successfullMemberChange;
|
||||||
|
|
||||||
|
return (success, updatedGroup);
|
||||||
|
}
|
||||||
|
|
||||||
/// Displays a snackbar with the given message and optional action.
|
/// Displays a snackbar with the given message and optional action.
|
||||||
///
|
///
|
||||||
/// [message] The message to display in the snackbar.
|
/// [message] The message to display in the snackbar.
|
||||||
|
|||||||
Reference in New Issue
Block a user