Skip to content
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

Handle the null value on time in getPrayers method #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
65 changes: 40 additions & 25 deletions lib/prayer_services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class PrayerService {
List<NotificationInfoModel> prayersList = [];
var scheduledCount = 0;
for (var key in prayerKeys) {
var obj = await PrayerNotificationService().getPrayerNotificationFromDB(key);
var obj =
await PrayerNotificationService().getPrayerNotificationFromDB(key);
if (check(obj)) {
scheduledCount++;
}
Expand All @@ -47,43 +48,57 @@ class PrayerService {
while (prayersList.length < (Platform.isIOS ? 70 : 10)) {
i++;
for (var key in prayerKeys) {
var obj = await PrayerNotificationService().getPrayerNotificationFromDB(key);
var obj =
await PrayerNotificationService().getPrayerNotificationFromDB(key);
var index = prayerKeys.indexOf(key);
if (check(obj)) {
final mosque = await getMosque(obj.mosqueUuid ?? '');

var time = getPrayerTime(mosque, key, time: DateTime.now().add(Duration(days: i)));
var notificationData = await getPrayerDataByIndex(mosque, index);
var prayerName = _getPrayerName(index);

String indexStr = index.toString(), dayStr = time!.day.toString(), monthStr = time.month.toString();
String str = indexStr + dayStr + monthStr;
int alarmId = int.parse(str);
print('Alarm ID: $alarmId');

NotificationInfoModel prayer = NotificationInfoModel(
mosqueName: mosque.name,
sound: obj.notificationSound,
prayerName: prayerName,
time: time,
notificationBeforeAthan: notificationData?.notificationBeforeAthan ?? 0,
alarmId: alarmId,
);

prayersList.add(prayer);
var time = getPrayerTime(mosque, key,
time: DateTime.now().add(Duration(days: i)));
if (time != null) {
var notificationData = await getPrayerDataByIndex(mosque, index);
var prayerName = _getPrayerName(index);

String indexStr = index.toString(),
dayStr = time.day.toString(),
monthStr = time.month.toString();
String str = indexStr + dayStr + monthStr;
int alarmId = int.parse(str);
print('Alarm ID: $alarmId');

NotificationInfoModel prayer = NotificationInfoModel(
mosqueName: mosque.name,
sound: obj.notificationSound,
prayerName: prayerName,
time: time,
notificationBeforeAthan:
notificationData?.notificationBeforeAthan ?? 0,
alarmId: alarmId,
);
prayersList.add(prayer);
}
}
}
}

prayersList.removeWhere((element) {
return element.time!.isBefore(DateTime.now());
return element.time == null || element.time!.isBefore(DateTime.now());
});

prayersList.sort((a, b) {
if (a.time == null && b.time == null) return 0; // Both null, consider equal
if (a.time == null) return 1; // `a` is null, so `b` should come first
if (b.time == null) return -1; // `b` is null, so `a` should come first
return a.time!.millisecondsSinceEpoch
.compareTo(b.time!.millisecondsSinceEpoch);
});
prayersList.sort(
(a, b) => a.time!.millisecondsSinceEpoch.toString().compareTo(b.time!.millisecondsSinceEpoch.toString()),
);

prayersList = prayersList.sublist(0, Platform.isIOS ? 63 : 5);
return prayersList;
}


Future<PrayerNotification?> getPrayerDataByIndex(DetailedMosque? mosque, int index) async {
final nextPrayerKey = prayerKeys[index];
final notificationData = await PrayerNotificationService().getPrayerNotificationFromDB(nextPrayerKey);
Expand Down