saveActivity method
Save the activity to Firestore. If the user is editing an activity, the activity is updated. Otherwise, a new activity is created.
Implementation
Future<CreateActivityResult> saveActivity() async {
final isEdit = ref.watch(createActivityProvider.select((state) => state.isEdited));
final now = DateTime.now();
ActivityModel data = state;
if (isEdit) {
data = data.copyWith(lastEdited: now);
} else {
data = data.copyWith(postDate: now, lastEdited: now);
// Schedule an event reminder notification as creators are automatically
// added to the going list
final hasNotificationPermission = await ref
.read(localNotificationServiceProvider)
.checkNotificationPermission();
if (hasNotificationPermission) {
final minutesInAdvance = ref.read(eventReminderPreferenceProvider);
ref
.read(localNotificationServiceProvider)
.scheduleEventNotification(data, minutesInAdvance);
}
}
// Update the activity or create a new one in Firestore
return Future.wait([
if (isEdit)
ref.read(firestoreServiceProvider).updateActivity(data)
else
ref.read(firestoreServiceProvider).setActivity(data),
]).then((value) {
return value.first;
}).onError(
(error, stackTrace) {
debugPrint('Error while saving activity: $error\n$stackTrace');
return CreateActivityResult.error;
},
);
}