1
1
import 'dart:convert' ;
2
-
2
+ import 'package:awesome_notifications/awesome_notifications.dart' ;
3
+ import 'package:duration/duration.dart' ;
3
4
import 'package:flood_mobile/Model/download_rate_model.dart' ;
4
5
import 'package:flood_mobile/Model/torrent_model.dart' ;
5
6
import 'package:flood_mobile/Provider/home_provider.dart' ;
@@ -8,6 +9,10 @@ import 'package:flutter/cupertino.dart';
8
9
import 'package:flutter_client_sse/flutter_client_sse.dart' ;
9
10
import 'package:json_patch/json_patch.dart' ;
10
11
import 'package:provider/provider.dart' ;
12
+ import '../Constants/notification_keys.dart' ;
13
+ import '../Provider/filter_provider.dart' ;
14
+
15
+ String torrentLength = '' ;
11
16
12
17
class EventHandlerApi {
13
18
//Sets the transfer rate if the event returned is TRANSFER_SUMMARY_FULL_UPDATE
@@ -46,6 +51,7 @@ class EventHandlerApi {
46
51
//Setting the full list of torrent
47
52
Provider .of <HomeProvider >(context, listen: false )
48
53
.setTorrentList (torrentList);
54
+ filterDataRephrasor (torrentList, context);
49
55
}
50
56
}
51
57
@@ -98,8 +104,155 @@ class EventHandlerApi {
98
104
print (e.toString ());
99
105
}
100
106
}
107
+
108
+ if (torrentList.length > int .parse (torrentLength) ||
109
+ torrentList.length < int .parse (torrentLength)) {
110
+ filterDataRephrasor (torrentList, context);
111
+ }
112
+
101
113
//Setting the full list of torrent
102
114
Provider .of <HomeProvider >(context, listen: false )
103
115
.setTorrentList (torrentList);
104
116
}
117
+
118
+ static Future <void > showNotification (int id, BuildContext context) async {
119
+ late bool displayNotification;
120
+ late bool isPaused;
121
+ isPaused = true ;
122
+ HomeProvider homeModel = Provider .of <HomeProvider >(context, listen: false );
123
+
124
+ // Skip finished torrents
125
+ if (homeModel.torrentList[id].status.contains ('complete' )) {
126
+ displayNotification = false ;
127
+ } else {
128
+ displayNotification = true ;
129
+ }
130
+
131
+ // Torrent not being downloaded
132
+ if (homeModel.torrentList[id].status.contains ('downloading' )) {
133
+ // Change to the 'RESUME' action
134
+ isPaused = false ;
135
+ }
136
+
137
+ // Torrent Paused
138
+ else {
139
+ isPaused = true ;
140
+ }
141
+
142
+ // Create notification for unfinished downloads
143
+ if (displayNotification) {
144
+ AwesomeNotifications ().createNotification (
145
+ content: NotificationContent (
146
+ id: id,
147
+ actionType: ActionType .Default ,
148
+ channelKey: NotificationConstants .DOWNLOADS_CHANNEL_KEY ,
149
+ category: NotificationCategory .Progress ,
150
+ notificationLayout: NotificationLayout .ProgressBar ,
151
+ title: homeModel.torrentList[id].name,
152
+ body: isPaused
153
+ ? "Stopped ETA: ∞"
154
+ : "Downloading ETA: " +
155
+ prettyDuration (
156
+ Duration (
157
+ seconds: homeModel.torrentList[id].eta.toInt (),
158
+ ),
159
+ abbreviated: true ,
160
+ ),
161
+ progress: homeModel.torrentList[id].percentComplete.round (),
162
+ summary: isPaused ? 'Paused' : 'Downloading' ,
163
+ locked: true ,
164
+ autoDismissible: false ,
165
+ showWhen: false ,
166
+ ),
167
+ actionButtons: [
168
+ NotificationActionButton (
169
+ key: isPaused
170
+ ? NotificationConstants .RESUME_ACTION_KEY
171
+ : NotificationConstants .PAUSE_ACTION_KEY ,
172
+ label: isPaused ? 'Resume' : 'Pause' ,
173
+ actionType: ActionType .KeepOnTop ,
174
+ enabled: true ,
175
+ autoDismissible: false ,
176
+ ),
177
+ ],
178
+ );
179
+ }
180
+ }
181
+
182
+ static Future <void > showEventNotification (
183
+ int id, BuildContext context) async {
184
+ HomeProvider homeModel = Provider .of <HomeProvider >(context, listen: false );
185
+ AwesomeNotifications ().createNotification (
186
+ content: NotificationContent (
187
+ id: id,
188
+ autoDismissible: false ,
189
+ channelKey: NotificationConstants .PUSH_NOTIFICATION_CHANNEL_KEY ,
190
+ category: NotificationCategory .Event ,
191
+ notificationLayout: NotificationLayout .Default ,
192
+ title: homeModel.torrentList[id].name,
193
+ body: "Download Finished" ,
194
+ ));
195
+ }
196
+ static Future <void > filterDataRephrasor (
197
+ List <TorrentModel > torrentList, context) async {
198
+ var maptrackerURIs = {};
199
+ var mapStatus = {};
200
+ List <String > statusList = [];
201
+ torrentLength = torrentList.length.toString ();
202
+ try {
203
+ for (int i = 0 ; i < torrentList.length; i++ ) {
204
+ for (int j = 0 ; j < torrentList[i].trackerURIs.length; j++ ) {
205
+ Provider .of <FilterProvider >(context, listen: false )
206
+ .trackerURIsListMain
207
+ .add (torrentList[i].trackerURIs[j].toString ());
208
+ Provider .of <FilterProvider >(context, listen: false )
209
+ .settrackerURIsListMain (
210
+ Provider .of <FilterProvider >(context, listen: false )
211
+ .trackerURIsListMain);
212
+ }
213
+ }
214
+ } catch (e) {
215
+ print (e);
216
+ }
217
+ try {
218
+ Provider .of <FilterProvider >(context, listen: false )
219
+ .trackerURIsListMain
220
+ .forEach ((element) {
221
+ if (! maptrackerURIs.containsKey (element)) {
222
+ maptrackerURIs[element] = 1 ;
223
+ } else {
224
+ maptrackerURIs[element] += 1 ;
225
+ }
226
+ });
227
+ Provider .of <FilterProvider >(context, listen: false )
228
+ .setmaptrackerURIs (maptrackerURIs);
229
+ } catch (e) {
230
+ print (e);
231
+ }
232
+ try {
233
+ for (int i = 0 ; i < torrentList.length; i++ ) {
234
+ for (int j = 0 ; j < torrentList[i].status.length; j++ ) {
235
+ statusList.add (torrentList[i].status[j].toString ());
236
+ }
237
+ }
238
+ Provider .of <FilterProvider >(context, listen: false )
239
+ .setstatusList (statusList);
240
+ } catch (e) {
241
+ print (e);
242
+ }
243
+
244
+ try {
245
+ statusList.forEach ((element) {
246
+ if (! mapStatus.containsKey (element)) {
247
+ mapStatus[element] = 1 ;
248
+ } else {
249
+ mapStatus[element] += 1 ;
250
+ }
251
+ });
252
+ Provider .of <FilterProvider >(context, listen: false )
253
+ .setmapStatus (mapStatus);
254
+ } catch (error) {
255
+ print (error);
256
+ }
257
+ }
105
258
}
0 commit comments