Skip to content

Add delete profile functionality #803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/app/data/providers/isar_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,31 @@ class IsarDb {
return profileModel;
}

static void deleteProfile(String profileName) async {
final isarProvider = IsarDb();
final db = await isarProvider.db;

final profile = await db.profileModels.filter().
profileNameEqualTo(profileName).findFirst();

if (profile == null) return;

final deletedAlarms = await db.alarmModels.filter().
profileEqualTo(profileName).findAll();

try {
await db.writeTxn(() async {
for (final alarm in deletedAlarms) {
await db.alarmModels.delete(alarm.isarId);
}
await db.profileModels.delete(profile.isarId);
});
} catch (e) {
debugPrint('Error deleting profile: $e');
rethrow;
}
}

static Stream<List<ProfileModel>> getProfiles() async* {
try {
final isarProvider = IsarDb();
Expand Down
118 changes: 118 additions & 0 deletions lib/app/modules/home/controllers/home_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,124 @@ class HomeController extends GetxController {
profileModel.value = p!;
}

void deleteProfile(ProfileModel profile, BuildContext context) async {
if (profile.profileName == 'Default') {
Get.snackbar(
'Error',
'Cannot delete the default profile',
snackPosition: SnackPosition.BOTTOM,
backgroundColor: Colors.red,
colorText: Colors.white,
);
return;
}

if (profile.isSharedAlarmEnabled) {
Get.snackbar(
'Error',
'This profile contains shared alarms.',
snackPosition: SnackPosition.BOTTOM,
backgroundColor: Colors.red,
colorText: Colors.white,
);
return;
}

Get.defaultDialog(
titlePadding: const EdgeInsets.symmetric(
vertical: 20,
),
backgroundColor: themeController.secondaryBackgroundColor.value,
title: 'Delete Profile',
titleStyle: Theme.of(context).textTheme.displaySmall,
content: Column(
children: [
Text(
'This action will permanently delete '
'this profile and all its alarms.',
style: Theme.of(context).textTheme.bodyMedium,
textAlign: TextAlign.center,
),
Padding(
padding: const EdgeInsets.only(
top: 20,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
TextButton(
onPressed: () => Get.back(),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
kprimaryTextColor.withOpacity(0.5),
),
),
child: Text(
'Cancel',
style: Theme.of(context).textTheme.displaySmall!,
),
),
TextButton(
onPressed: () async {
ProfileModel deletedProfile = profile;
try{
IsarDb.deleteProfile(profile.profileName);
} catch (e) {
Get.snackbar(
'Error',
'Failed to delete profile',
snackPosition: SnackPosition.BOTTOM,
backgroundColor: Colors.red,
colorText: Colors.white,
);
return;
}

if (profile.profileName == selectedProfile.value) {
writeProfileName('Default');
}
Get.back();
Get.snackbar(
'Success',
'Profile deleted successfully',
snackPosition: SnackPosition.BOTTOM,
colorText: Colors.white,
margin: const EdgeInsets.symmetric(
horizontal: 10,
vertical: 15,
),
mainButton: TextButton(
onPressed: () async {
await IsarDb.addProfile(deletedProfile);
writeProfileName(deletedProfile.profileName);
// might want to add the alarms back to the profile
// however, patch alarm addition is not implemented yet
},
child: Text(
'Undo',
style: TextStyle(color: Colors.white),
),
),
);
},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(kprimaryColor),
),
child: Text(
'Delete',
style: Theme.of(context).textTheme.displaySmall!.copyWith(
color: kprimaryBackgroundColor,
),
),
),
],
),
),
],
),
);
}

@override
void onInit() async {
super.onInit();
Expand Down
7 changes: 5 additions & 2 deletions lib/app/modules/home/views/profile_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class _ProfileSelectState extends State<ProfileSelect> {
scrollDirection: Axis.horizontal,
child: Row(
children: profiles!
.map((e) => profileCapsule(e))
.map((e) => profileCapsule(e, context))
.toList(),
),
);
Expand All @@ -82,7 +82,7 @@ class _ProfileSelectState extends State<ProfileSelect> {
));
}

Widget profileCapsule(ProfileModel profile) {
Widget profileCapsule(ProfileModel profile, BuildContext context) {
return Padding(
key: profile.profileName == controller.selectedProfile.value
? scrollKey
Expand All @@ -94,6 +94,9 @@ class _ProfileSelectState extends State<ProfileSelect> {
controller.writeProfileName(profile.profileName);
controller.expandProfile.value = !controller.expandProfile.value;
},
onLongPress: () {
controller.deleteProfile(profile, context);
},
child: Obx(() => Container(
padding: EdgeInsets.symmetric(
horizontal: 18 * controller.scalingFactor.value,
Expand Down