Skip to content

Commit b8f2f37

Browse files
committed
Implement the new notification feature and update the code.
1 parent 1e7b898 commit b8f2f37

File tree

2 files changed

+148
-3
lines changed

2 files changed

+148
-3
lines changed

lib/Constants/notification_keys.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ class NotificationConstants {
99
// Notification Action Button Keys
1010
static const String PAUSE_ACTION_KEY = 'PAUSE';
1111
static const String RESUME_ACTION_KEY = 'RESUME';
12+
13+
static const String CANCEL_ACTION_KEY = 'CANCEL';
14+
static const String STOP_ACTION_KEY = 'STOP';
1215
}

lib/Notifications/notification_controller.dart

Lines changed: 145 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
2+
13
import 'package:awesome_notifications/awesome_notifications.dart';
4+
import 'package:duration/duration.dart';
25
import 'package:flutter/material.dart';
36
import 'package:flutter_bloc/flutter_bloc.dart';
47
import 'package:flood_mobile/Api/torrent_api.dart';
58
import 'package:flood_mobile/Blocs/home_screen_bloc/home_screen_bloc.dart';
69
import 'package:flood_mobile/Constants/notification_keys.dart';
10+
import 'package:flood_mobile/l10n/l10n.dart';
711

812
class NotificationController {
913
/// Use this method to detect when the user taps on a notification or action button
@@ -13,10 +17,11 @@ class NotificationController {
1317
NavigationService.navigatorKey.currentContext!,
1418
listen: false);
1519
final actionKey = receivedAction.buttonKeyPressed;
16-
1720
// Not a desired action
1821
if (actionKey != NotificationConstants.PAUSE_ACTION_KEY &&
19-
actionKey != NotificationConstants.RESUME_ACTION_KEY) {
22+
actionKey != NotificationConstants.RESUME_ACTION_KEY &&
23+
actionKey != NotificationConstants.CANCEL_ACTION_KEY &&
24+
actionKey != NotificationConstants.STOP_ACTION_KEY) {
2025
return;
2126
}
2227

@@ -28,14 +33,151 @@ class NotificationController {
2833
}
2934

3035
// Resume downloads
31-
else {
36+
else if (actionKey == NotificationConstants.RESUME_ACTION_KEY) {
3237
await TorrentApi.startTorrent(
3338
hashes: [homeModel.state.torrentList[receivedAction.id!].hash],
3439
context: NavigationService.navigatorKey.currentContext!);
40+
} else if (actionKey == NotificationConstants.CANCEL_ACTION_KEY) {
41+
BlocProvider.of<HomeScreenBloc>(
42+
NavigationService.navigatorKey.currentContext!,
43+
listen: false)
44+
.add(
45+
UpdateNotificationCancelEvent(
46+
newNotificationCancel: {
47+
homeModel.state.torrentList[receivedAction.id!].hash: true
48+
},
49+
),
50+
);
51+
} else if (actionKey == NotificationConstants.STOP_ACTION_KEY) {
52+
await TorrentApi.stopTorrent(
53+
hashes: [homeModel.state.torrentList[receivedAction.id!].hash],
54+
context: NavigationService.navigatorKey.currentContext!);
55+
BlocProvider.of<HomeScreenBloc>(
56+
NavigationService.navigatorKey.currentContext!,
57+
listen: false)
58+
.add(
59+
UpdateNotificationCancelEvent(
60+
newNotificationCancel: {
61+
homeModel.state.torrentList[receivedAction.id!].hash: true
62+
},
63+
),
64+
);
3565
}
3666
}
3767
}
3868

3969
class NavigationService {
4070
static GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
4171
}
72+
73+
Future<void> createTorrentDownloadNotification(
74+
int id, BuildContext context) async {
75+
late bool displayNotification;
76+
late bool isPaused;
77+
isPaused = true;
78+
HomeScreenState homeModel =
79+
BlocProvider.of<HomeScreenBloc>(context, listen: false).state;
80+
81+
// Skip finished torrents
82+
if (homeModel.torrentList[id].status.contains('complete')) {
83+
displayNotification = false;
84+
} else {
85+
displayNotification = true;
86+
}
87+
88+
// Torrent not being downloaded
89+
if (homeModel.torrentList[id].status.contains('downloading')) {
90+
// Change to the 'RESUME' action
91+
isPaused = false;
92+
} else if (homeModel.torrentList[id].status.contains('error')) {
93+
displayNotification = false;
94+
}
95+
96+
// Torrent Paused
97+
else {
98+
isPaused = true;
99+
}
100+
101+
// Create notification for unfinished downloads
102+
if (displayNotification) {
103+
AwesomeNotifications().createNotification(
104+
content: NotificationContent(
105+
id: id,
106+
actionType: ActionType.Default,
107+
channelKey: NotificationConstants.DOWNLOADS_CHANNEL_KEY,
108+
category: NotificationCategory.Progress,
109+
notificationLayout: NotificationLayout.ProgressBar,
110+
title: homeModel.torrentList[id].name,
111+
body: isPaused
112+
? "${context.l10n.notification_stopped} ETA: ∞"
113+
: "${context.l10n.notification_downloading} ETA: " +
114+
prettyDuration(
115+
Duration(
116+
seconds: homeModel.torrentList[id].eta.toInt(),
117+
),
118+
abbreviated: true,
119+
),
120+
progress: homeModel.torrentList[id].percentComplete.round(),
121+
summary: isPaused ? 'Paused' : 'Downloading',
122+
locked: true,
123+
autoDismissible: false,
124+
showWhen: false,
125+
),
126+
actionButtons: [
127+
NotificationActionButton(
128+
key: isPaused
129+
? NotificationConstants.RESUME_ACTION_KEY
130+
: NotificationConstants.PAUSE_ACTION_KEY,
131+
label: isPaused ? context.l10n.notification_resume : context.l10n.notification_pause,
132+
actionType: ActionType.KeepOnTop,
133+
enabled: true,
134+
autoDismissible: false,
135+
),
136+
NotificationActionButton(
137+
key: isPaused
138+
? NotificationConstants.CANCEL_ACTION_KEY
139+
: NotificationConstants.STOP_ACTION_KEY,
140+
label: isPaused ? context.l10n.notification_cancel : context.l10n.notification_stop,
141+
actionType: ActionType.KeepOnTop,
142+
enabled: true,
143+
autoDismissible: true,
144+
),
145+
],
146+
);
147+
}
148+
}
149+
150+
Future<void> createDownloadFinishedNotification(
151+
int id, BuildContext context) async {
152+
AwesomeNotifications().createNotification(
153+
content: NotificationContent(
154+
id: id,
155+
autoDismissible: false,
156+
channelKey: NotificationConstants.PUSH_NOTIFICATION_CHANNEL_KEY,
157+
category: NotificationCategory.Event,
158+
notificationLayout: NotificationLayout.Default,
159+
title: BlocProvider.of<HomeScreenBloc>(context, listen: false)
160+
.state
161+
.torrentList[id]
162+
.name,
163+
body: context.l10n.notification_finished,
164+
));
165+
}
166+
167+
Future<void> createDownloadErrorNotification(
168+
int id, BuildContext context) async {
169+
AwesomeNotifications().createNotification(
170+
content: NotificationContent(
171+
id: id,
172+
autoDismissible: false,
173+
channelKey: NotificationConstants.PUSH_NOTIFICATION_CHANNEL_KEY,
174+
category: NotificationCategory.Event,
175+
notificationLayout: NotificationLayout.Default,
176+
title: BlocProvider.of<HomeScreenBloc>(context, listen: false)
177+
.state
178+
.torrentList[id]
179+
.name,
180+
body: context.l10n.notification_error,
181+
backgroundColor: Colors.red,
182+
));
183+
}

0 commit comments

Comments
 (0)