Refactor group saving logic into a separate method
This commit is contained in:
@@ -146,15 +146,31 @@ class _CreateGroupViewState extends State<CreateGroupView> {
|
|||||||
(_groupNameController.text.isEmpty ||
|
(_groupNameController.text.isEmpty ||
|
||||||
(selectedPlayers.length < 2))
|
(selectedPlayers.length < 2))
|
||||||
? null
|
? null
|
||||||
: () async {
|
: _saveGroup,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 20),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Saves the group by creating a new one or updating the existing one,
|
||||||
|
/// depending on whether the widget is in edit mode.
|
||||||
|
Future<void> _saveGroup() async {
|
||||||
|
final loc = AppLocalizations.of(context);
|
||||||
Group? updatedGroup;
|
Group? updatedGroup;
|
||||||
bool successfullNameChange = true;
|
bool successfullNameChange = true;
|
||||||
bool successfullMemberChange = true;
|
bool successfullMemberChange = true;
|
||||||
late bool success;
|
late bool success;
|
||||||
|
|
||||||
|
final groupName = _groupNameController.text.trim();
|
||||||
|
|
||||||
if (widget.groupToEdit == null) {
|
if (widget.groupToEdit == null) {
|
||||||
success = await db.groupDao.addGroup(
|
success = await db.groupDao.addGroup(
|
||||||
group: Group(
|
group: Group(
|
||||||
name: _groupNameController.text.trim(),
|
name: groupName,
|
||||||
description: '',
|
description: '',
|
||||||
members: selectedPlayers,
|
members: selectedPlayers,
|
||||||
),
|
),
|
||||||
@@ -162,30 +178,28 @@ class _CreateGroupViewState extends State<CreateGroupView> {
|
|||||||
} else {
|
} else {
|
||||||
updatedGroup = Group(
|
updatedGroup = Group(
|
||||||
id: widget.groupToEdit!.id,
|
id: widget.groupToEdit!.id,
|
||||||
name: _groupNameController.text.trim(),
|
name: groupName,
|
||||||
description: '',
|
description: '',
|
||||||
members: selectedPlayers,
|
members: selectedPlayers,
|
||||||
);
|
);
|
||||||
if (widget.groupToEdit!.name !=
|
if (widget.groupToEdit!.name != groupName) {
|
||||||
_groupNameController.text.trim()) {
|
successfullNameChange = await db.groupDao.updateGroupName(
|
||||||
successfullNameChange = await db.groupDao
|
|
||||||
.updateGroupName(
|
|
||||||
groupId: widget.groupToEdit!.id,
|
groupId: widget.groupToEdit!.id,
|
||||||
newName: _groupNameController.text.trim(),
|
newName: groupName,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (widget.groupToEdit!.members != selectedPlayers) {
|
if (widget.groupToEdit!.members != selectedPlayers) {
|
||||||
successfullMemberChange = await db.groupDao
|
successfullMemberChange = await db.groupDao.replaceGroupPlayers(
|
||||||
.replaceGroupPlayers(
|
|
||||||
groupId: widget.groupToEdit!.id,
|
groupId: widget.groupToEdit!.id,
|
||||||
newPlayers: selectedPlayers,
|
newPlayers: selectedPlayers,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
success =
|
success = successfullNameChange && successfullMemberChange;
|
||||||
successfullNameChange && successfullMemberChange;
|
|
||||||
}
|
}
|
||||||
if (!context.mounted) return;
|
|
||||||
|
if (!mounted) return;
|
||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
Navigator.pop(context, updatedGroup);
|
Navigator.pop(context, updatedGroup);
|
||||||
} else {
|
} else {
|
||||||
@@ -195,14 +209,6 @@ class _CreateGroupViewState extends State<CreateGroupView> {
|
|||||||
: loc.error_editing_group,
|
: loc.error_editing_group,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
},
|
|
||||||
),
|
|
||||||
const SizedBox(height: 20),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Displays a snackbar with the given message and optional action.
|
/// Displays a snackbar with the given message and optional action.
|
||||||
|
|||||||
Reference in New Issue
Block a user