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

Add delete button when schedule in today #1112

Merged
merged 1 commit into from
Mar 23, 2024
Merged
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
269 changes: 136 additions & 133 deletions lib/features/schedule_post/schedule_post_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,152 +89,155 @@ class _SchedulePostPage extends HookConsumerWidget {
backgroundColor: PilllColors.white,
),
body: SafeArea(
child: Column(
children: [
Expanded(
child: SingleChildScrollView(
controller: scrollController,
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
ConstrainedBox(
constraints: BoxConstraints(
minWidth: MediaQuery.of(context).size.width,
maxWidth: MediaQuery.of(context).size.width,
minHeight: 40,
maxHeight: 200,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
Expanded(
child: SingleChildScrollView(
controller: scrollController,
child: Column(
children: [
ConstrainedBox(
constraints: BoxConstraints(
minWidth: MediaQuery.of(context).size.width,
maxWidth: MediaQuery.of(context).size.width,
minHeight: 40,
maxHeight: 200,
),
child: TextFormField(
onChanged: (text) {
title.value = text;
},
decoration: const InputDecoration(
hintText: "通院する",
border: OutlineInputBorder(),
),
controller: textEditingController,
maxLines: null,
maxLength: 60,
keyboardType: TextInputType.multiline,
focusNode: focusNode,
),
),
child: TextFormField(
onChanged: (text) {
title.value = text;
SwitchListTile(
title: const Text("当日9:00に通知を受け取る",
style: TextStyle(
fontFamily: FontFamily.roboto,
fontWeight: FontWeight.w300,
fontSize: 16,
)),
activeColor: PilllColors.secondary,
onChanged: (bool value) {
analytics.logEvent(
name: "schedule_post_remind_toggle",
);
isOnRemind.value = value;
},
decoration: const InputDecoration(
hintText: "通院する",
border: OutlineInputBorder(),
),
controller: textEditingController,
maxLines: null,
maxLength: 60,
keyboardType: TextInputType.multiline,
focusNode: focusNode,
value: isOnRemind.value,
// NOTE: when configured subtitle, the space between elements becomes very narrow
contentPadding: const EdgeInsets.all(0),
),
),
SwitchListTile(
title: const Text("当日9:00に通知を受け取る",
style: TextStyle(
fontFamily: FontFamily.roboto,
fontWeight: FontWeight.w300,
fontSize: 16,
)),
activeColor: PilllColors.secondary,
onChanged: (bool value) {
analytics.logEvent(
name: "schedule_post_remind_toggle",
);
isOnRemind.value = value;
},
value: isOnRemind.value,
// NOTE: when configured subtitle, the space between elements becomes very narrow
contentPadding: const EdgeInsets.all(0),
),
if (date.date().isAfter(today())) ...[
PrimaryButton(
text: "保存",
onPressed: isInvalid()
? null
: () async {
analytics.logEvent(name: "schedule_post_pressed");
final navigator = Navigator.of(context);
],
),
),
),
if (focusNode.hasPrimaryFocus) ...[
KeyboardToolbar(
doneButton: AlertButton(
text: '完了',
onPressed: () async {
analytics.logEvent(name: "schedule_post_toolbar_done");
focusNode.unfocus();
},
),
),
],
const Spacer(),
if (date.date().isAfter(today())) ...[
PrimaryButton(
text: "保存",
onPressed: isInvalid()
? null
: () async {
analytics.logEvent(name: "schedule_post_pressed");
final navigator = Navigator.of(context);

try {
final localNotificationID = schedule.localNotification?.localNotificationID;
if (localNotificationID != null) {
await localNotificationService.cancelNotification(localNotificationID: localNotificationID);
}
try {
final localNotificationID = schedule.localNotification?.localNotificationID;
if (localNotificationID != null) {
await localNotificationService.cancelNotification(localNotificationID: localNotificationID);
}

final Schedule newSchedule;
if (isOnRemind.value) {
newSchedule = schedule.copyWith(
title: title.value,
localNotification: LocalNotification(
localNotificationID: Random().nextInt(scheduleNotificationIdentifierOffset),
remindDateTime: DateTime(date.year, date.month, date.day, 9),
),
);
await localNotificationService.scheduleCalendarScheduleNotification(schedule: newSchedule);
} else {
newSchedule = schedule.copyWith(
title: title.value,
localNotification: null,
);
}
final Schedule newSchedule;
if (isOnRemind.value) {
newSchedule = schedule.copyWith(
title: title.value,
localNotification: LocalNotification(
localNotificationID: Random().nextInt(scheduleNotificationIdentifierOffset),
remindDateTime: DateTime(date.year, date.month, date.day, 9),
),
);
await localNotificationService.scheduleCalendarScheduleNotification(schedule: newSchedule);
} else {
newSchedule = schedule.copyWith(
title: title.value,
localNotification: null,
);
}

await ref.read(databaseProvider).schedulesReference().doc(newSchedule.id).set(
newSchedule,
SetOptions(merge: true),
);
navigator.pop();
} catch (error) {
if (context.mounted) showErrorAlert(context, error);
}
},
),
const SizedBox(height: 10),
if (scheduleID != null)
AppOutlinedButton(
text: "削除",
await ref.read(databaseProvider).schedulesReference().doc(newSchedule.id).set(
newSchedule,
SetOptions(merge: true),
);
navigator.pop();
} catch (error) {
if (context.mounted) showErrorAlert(context, error);
}
},
),
const SizedBox(height: 10),
],
if (scheduleID != null)
AppOutlinedButton(
text: "削除",
onPressed: () async {
analytics.logEvent(name: "schedule_delete_pressed");
showDiscardDialog(
context,
title: "予定を削除します",
message: "削除された予定は復元ができません",
actions: [
AlertButton(
text: "キャンセル",
onPressed: () async {
analytics.logEvent(name: "schedule_delete_pressed");
showDiscardDialog(
context,
title: "予定を削除します",
message: "削除された予定は復元ができません",
actions: [
AlertButton(
text: "キャンセル",
onPressed: () async {
Navigator.of(context).pop();
},
),
AlertButton(
text: "削除する",
onPressed: () async {
final navigator = Navigator.of(context);
try {
final localNotificationID = schedule.localNotification?.localNotificationID;
if (localNotificationID != null) {
await localNotificationService.cancelNotification(localNotificationID: localNotificationID);
}
Navigator.of(context).pop();
},
),
AlertButton(
text: "削除する",
onPressed: () async {
final navigator = Navigator.of(context);
try {
final localNotificationID = schedule.localNotification?.localNotificationID;
if (localNotificationID != null) {
await localNotificationService.cancelNotification(localNotificationID: localNotificationID);
}

await ref.read(databaseProvider).schedulesReference().doc(scheduleID).delete();
navigator.popUntil((route) => route.isFirst);
} catch (error) {
if (context.mounted) showErrorAlert(context, error);
}
},
),
],
);
await ref.read(databaseProvider).schedulesReference().doc(scheduleID).delete();
navigator.popUntil((route) => route.isFirst);
} catch (error) {
if (context.mounted) showErrorAlert(context, error);
}
},
),
],
const SizedBox(height: 20),
],
),
),
),
if (focusNode.hasPrimaryFocus) ...[
KeyboardToolbar(
doneButton: AlertButton(
text: '完了',
onPressed: () async {
analytics.logEvent(name: "schedule_post_toolbar_done");
focusNode.unfocus();
],
);
},
),
),
const SizedBox(height: 20),
],
],
),
),
),
);
Expand Down
Loading