Skip to content

Commit b82491c

Browse files
authored
Add file name checks on create profile (#800)
* Add file name checks on create profile * remove redundant type declaration
1 parent 4ac9e89 commit b82491c

File tree

2 files changed

+65
-15
lines changed

2 files changed

+65
-15
lines changed

lib/app/modules/addOrUpdateAlarm/controllers/add_or_update_alarm_controller.dart

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,7 +1286,35 @@ class AddOrUpdateAlarmController extends GetxController {
12861286
}
12871287

12881288
Future<void> createProfile() async {
1289-
profileModel = ProfileModel(
1289+
try {
1290+
if (profileTextEditingController.text.trim().isEmpty) {
1291+
Get.snackbar(
1292+
'Error',
1293+
'Profile name cannot be empty',
1294+
snackPosition: SnackPosition.BOTTOM,
1295+
backgroundColor: Colors.red,
1296+
colorText: Colors.white,
1297+
duration: const Duration(seconds: 3),
1298+
margin: const EdgeInsets.all(10),
1299+
);
1300+
return;
1301+
}
1302+
1303+
bool exists = await IsarDb.profileExists(profileTextEditingController.text.trim());
1304+
if (exists) {
1305+
Get.snackbar(
1306+
'Error',
1307+
'A profile with this name already exists',
1308+
snackPosition: SnackPosition.BOTTOM,
1309+
backgroundColor: Colors.red,
1310+
colorText: Colors.white,
1311+
duration: const Duration(seconds: 3),
1312+
margin: const EdgeInsets.all(10),
1313+
);
1314+
return;
1315+
}
1316+
1317+
profileModel = ProfileModel(
12901318
profileName: profileTextEditingController.text,
12911319
deleteAfterGoesOff: deleteAfterGoesOff.value,
12921320
snoozeDuration: snoozeDuration.value,
@@ -1342,20 +1370,43 @@ class AddOrUpdateAlarmController extends GetxController {
13421370
guardian: guardian.value,
13431371
isCall: isCall.value,
13441372
ringOn: isFutureDate.value,
1345-
);
1373+
);
1374+
1375+
if (homeController.isProfileUpdate.value) {
1376+
var profileId =
1377+
await IsarDb.profileId(homeController.selectedProfile.value);
1378+
print(profileId);
1379+
if (profileId != 'null') profileModel.isarId = profileId;
1380+
print(profileModel.isarId);
1381+
await IsarDb.updateAlarmProfiles(profileTextEditingController.text);
1382+
}
13461383

1347-
if (homeController.isProfileUpdate.value) {
1348-
var profileId =
1349-
await IsarDb.profileId(homeController.selectedProfile.value);
1350-
print(profileId);
1351-
if (profileId != 'null') profileModel.isarId = profileId;
1352-
print(profileModel.isarId);
1353-
await IsarDb.updateAlarmProfiles(profileTextEditingController.text);
1384+
await IsarDb.addProfile(profileModel);
1385+
homeController.selectedProfile.value = profileModel.profileName;
1386+
storage.writeProfile(profileModel.profileName);
1387+
homeController.writeProfileName(profileModel.profileName);
1388+
1389+
Get.snackbar(
1390+
'Success',
1391+
'Profile created successfully',
1392+
snackPosition: SnackPosition.BOTTOM,
1393+
backgroundColor: Colors.green,
1394+
colorText: Colors.white,
1395+
duration: const Duration(seconds: 2),
1396+
margin: const EdgeInsets.all(10),
1397+
);
1398+
} catch (e) {
1399+
debugPrint('Error creating profile: $e');
1400+
Get.snackbar(
1401+
'Error',
1402+
'Failed to create profile. Please try again.',
1403+
snackPosition: SnackPosition.BOTTOM,
1404+
backgroundColor: Colors.red,
1405+
colorText: Colors.white,
1406+
duration: const Duration(seconds: 3),
1407+
margin: const EdgeInsets.all(10),
1408+
);
13541409
}
1355-
await IsarDb.addProfile(profileModel);
1356-
homeController.selectedProfile.value = profileModel.profileName;
1357-
storage.writeProfile(profileModel.profileName);
1358-
homeController.writeProfileName(profileModel.profileName);
13591410
}
13601411
}
13611412

lib/app/modules/addOrUpdateAlarm/views/add_or_update_alarm_view.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,8 +1189,7 @@ class AddOrUpdateAlarmView extends GetView<AddOrUpdateAlarmController> {
11891189
debugPrint(e.toString());
11901190
}
11911191
} else {
1192-
if (controller.profileTextEditingController.text
1193-
.isNotEmpty) controller.createProfile();
1192+
controller.createProfile();
11941193
}
11951194
}
11961195
},

0 commit comments

Comments
 (0)