1
+
2
+
1
3
import 'package:awesome_notifications/awesome_notifications.dart' ;
4
+ import 'package:duration/duration.dart' ;
2
5
import 'package:flutter/material.dart' ;
3
6
import 'package:flutter_bloc/flutter_bloc.dart' ;
4
7
import 'package:flood_mobile/Api/torrent_api.dart' ;
5
8
import 'package:flood_mobile/Blocs/home_screen_bloc/home_screen_bloc.dart' ;
6
9
import 'package:flood_mobile/Constants/notification_keys.dart' ;
10
+ import 'package:flood_mobile/l10n/l10n.dart' ;
7
11
8
12
class NotificationController {
9
13
/// Use this method to detect when the user taps on a notification or action button
@@ -13,10 +17,11 @@ class NotificationController {
13
17
NavigationService .navigatorKey.currentContext! ,
14
18
listen: false );
15
19
final actionKey = receivedAction.buttonKeyPressed;
16
-
17
20
// Not a desired action
18
21
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 ) {
20
25
return ;
21
26
}
22
27
@@ -28,14 +33,151 @@ class NotificationController {
28
33
}
29
34
30
35
// Resume downloads
31
- else {
36
+ else if (actionKey == NotificationConstants . RESUME_ACTION_KEY ) {
32
37
await TorrentApi .startTorrent (
33
38
hashes: [homeModel.state.torrentList[receivedAction.id! ].hash],
34
39
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
+ );
35
65
}
36
66
}
37
67
}
38
68
39
69
class NavigationService {
40
70
static GlobalKey <NavigatorState > navigatorKey = GlobalKey <NavigatorState >();
41
71
}
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