Skip to content

Commit 5287c7a

Browse files
authored
Merge pull request #251 from amitamrutiya2210/notification-support
Improve Notification Support
2 parents 5da2a53 + e921b87 commit 5287c7a

36 files changed

+579
-274
lines changed

lib/Api/auth_api.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ class AuthApi {
119119
BlocProvider.of<UserDetailBloc>(context, listen: false)
120120
.add(SetUsersListEvent(usersList: usersList));
121121
print('---USERS LIST---');
122-
print(response);
123122
}
124123
} catch (error) {
125124
print('--ERROR IN GET USER LIST--');

lib/Api/client_api.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class ClientApi {
3434
BlocProvider.of<ClientSettingsBloc>(context, listen: false)
3535
.add(SetClientSettingsEvent(clientSettings: clientSetting));
3636
print('---CLIENT SETTINGS---');
37-
print(response);
3837
}
3938
} catch (error) {
4039
print('--ERROR IN GET CLIENT SETTINGS--');

lib/Api/event_handler_api.dart

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import 'dart:collection';
22
import 'dart:convert';
33
import 'dart:io';
4-
import 'package:awesome_notifications/awesome_notifications.dart';
54
import 'package:battery_plus/battery_plus.dart';
6-
import 'package:duration/duration.dart';
75
import 'package:flutter/cupertino.dart';
86
import 'package:flutter/services.dart';
97
import 'package:flutter_bloc/flutter_bloc.dart';
@@ -14,7 +12,6 @@ import 'package:flood_mobile/Api/torrent_api.dart';
1412
import 'package:flood_mobile/Blocs/filter_torrent_bloc/filter_torrent_bloc.dart';
1513
import 'package:flood_mobile/Blocs/home_screen_bloc/home_screen_bloc.dart';
1614
import 'package:flood_mobile/Blocs/power_management_bloc/power_management_bloc.dart';
17-
import 'package:flood_mobile/Constants/notification_keys.dart';
1815
import 'package:flood_mobile/Model/download_rate_model.dart';
1916
import 'package:flood_mobile/Model/torrent_model.dart';
2017
import 'package:flood_mobile/Services/file_size_helper.dart';
@@ -162,88 +159,6 @@ class EventHandlerApi {
162159
}
163160
}
164161

165-
static Future<void> showNotification(int id, BuildContext context) async {
166-
late bool displayNotification;
167-
late bool isPaused;
168-
isPaused = true;
169-
HomeScreenState homeModel =
170-
BlocProvider.of<HomeScreenBloc>(context, listen: false).state;
171-
172-
// Skip finished torrents
173-
if (homeModel.torrentList[id].status.contains('complete')) {
174-
displayNotification = false;
175-
} else {
176-
displayNotification = true;
177-
}
178-
179-
// Torrent not being downloaded
180-
if (homeModel.torrentList[id].status.contains('downloading')) {
181-
// Change to the 'RESUME' action
182-
isPaused = false;
183-
}
184-
185-
// Torrent Paused
186-
else {
187-
isPaused = true;
188-
}
189-
190-
// Create notification for unfinished downloads
191-
if (displayNotification) {
192-
AwesomeNotifications().createNotification(
193-
content: NotificationContent(
194-
id: id,
195-
actionType: ActionType.Default,
196-
channelKey: NotificationConstants.DOWNLOADS_CHANNEL_KEY,
197-
category: NotificationCategory.Progress,
198-
notificationLayout: NotificationLayout.ProgressBar,
199-
title: homeModel.torrentList[id].name,
200-
body: isPaused
201-
? "Stopped ETA: ∞"
202-
: "Downloading ETA: " +
203-
prettyDuration(
204-
Duration(
205-
seconds: homeModel.torrentList[id].eta.toInt(),
206-
),
207-
abbreviated: true,
208-
),
209-
progress: homeModel.torrentList[id].percentComplete.round(),
210-
summary: isPaused ? 'Paused' : 'Downloading',
211-
locked: true,
212-
autoDismissible: false,
213-
showWhen: false,
214-
),
215-
actionButtons: [
216-
NotificationActionButton(
217-
key: isPaused
218-
? NotificationConstants.RESUME_ACTION_KEY
219-
: NotificationConstants.PAUSE_ACTION_KEY,
220-
label: isPaused ? 'Resume' : 'Pause',
221-
actionType: ActionType.KeepOnTop,
222-
enabled: true,
223-
autoDismissible: false,
224-
),
225-
],
226-
);
227-
}
228-
}
229-
230-
static Future<void> showEventNotification(
231-
int id, BuildContext context) async {
232-
AwesomeNotifications().createNotification(
233-
content: NotificationContent(
234-
id: id,
235-
autoDismissible: false,
236-
channelKey: NotificationConstants.PUSH_NOTIFICATION_CHANNEL_KEY,
237-
category: NotificationCategory.Event,
238-
notificationLayout: NotificationLayout.Default,
239-
title: BlocProvider.of<HomeScreenBloc>(context, listen: false)
240-
.state
241-
.torrentList[id]
242-
.name,
243-
body: "Download Finished",
244-
));
245-
}
246-
247162
static Future<void> filterDataRephrasor(
248163
List<TorrentModel> torrentList, context) async {
249164
FilterTorrentBloc filterBloc =

lib/Blocs/home_screen_bloc/home_screen_bloc.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:async';
2+
13
import 'package:bloc/bloc.dart';
24
import 'package:equatable/equatable.dart';
35
import 'package:flood_mobile/Model/feeds_content_model.dart';
@@ -20,6 +22,7 @@ class HomeScreenBloc extends Bloc<HomeScreenEvent, HomeScreenState> {
2022
on<SetTorrentListJsonEvent>(_setTorrentListJson);
2123
on<SetFeedsAndRulesListJsonEvent>(_setFeedsAndRulesListJson);
2224
on<UpdateTorrentListEvent>(_updateTorrentList);
25+
on<UpdateNotificationCancelEvent>(_updateNotificationCancel);
2326
}
2427

2528
// Handle the SetUnreadNotifications event
@@ -81,4 +84,10 @@ class HomeScreenBloc extends Bloc<HomeScreenEvent, HomeScreenState> {
8184
UpdateTorrentListEvent event, Emitter<HomeScreenState> emit) {
8285
emit(state.copyWith(torrentListJson: event.newTorrentListJson));
8386
}
87+
88+
// Handle the UpdateNotificationCancel event
89+
FutureOr<void> _updateNotificationCancel(
90+
UpdateNotificationCancelEvent event, Emitter<HomeScreenState> emit) {
91+
emit(state.copyWith(notificationCancel: event.newNotificationCancel));
92+
}
8493
}

lib/Blocs/home_screen_bloc/home_screen_event.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,12 @@ class UpdateTorrentListEvent extends HomeScreenEvent {
9090
@override
9191
List<Object> get props => [newTorrentListJson];
9292
}
93+
94+
class UpdateNotificationCancelEvent extends HomeScreenEvent {
95+
final Map<String, bool> newNotificationCancel;
96+
97+
UpdateNotificationCancelEvent({required this.newNotificationCancel});
98+
99+
@override
100+
List<Object> get props => [newNotificationCancel];
101+
}

lib/Blocs/home_screen_bloc/home_screen_state.dart

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class HomeScreenState extends Equatable {
1313
final List<FeedsContentsModel> rssFeedsContentsList;
1414
String upSpeed;
1515
String downSpeed;
16+
final Map<String, bool> notificationCancel;
1617

1718
HomeScreenState({
1819
required this.torrentList,
@@ -25,6 +26,7 @@ class HomeScreenState extends Equatable {
2526
required this.rssFeedsContentsList,
2627
required this.upSpeed,
2728
required this.downSpeed,
29+
required this.notificationCancel,
2830
});
2931

3032
HomeScreenState copyWith({
@@ -38,19 +40,20 @@ class HomeScreenState extends Equatable {
3840
List<FeedsContentsModel>? rssFeedsContentsList,
3941
String? upSpeed,
4042
String? downSpeed,
43+
Map<String, bool>? notificationCancel,
4144
}) {
4245
return HomeScreenState(
43-
torrentList: torrentList ?? this.torrentList,
44-
torrentListJson: torrentListJson ?? this.torrentListJson,
45-
unreadNotifications: unreadNotifications ?? this.unreadNotifications,
46-
notificationModel: notificationModel ?? this.notificationModel,
47-
rssFeedsListJson: rssFeedsListJson ?? this.rssFeedsListJson,
48-
rssFeedsList: rssFeedsList ?? this.rssFeedsList,
49-
rssRulesList: rssRulesList ?? this.rssRulesList,
50-
rssFeedsContentsList: rssFeedsContentsList ?? this.rssFeedsContentsList,
51-
upSpeed: upSpeed ?? this.upSpeed,
52-
downSpeed: downSpeed ?? this.downSpeed,
53-
);
46+
torrentList: torrentList ?? this.torrentList,
47+
torrentListJson: torrentListJson ?? this.torrentListJson,
48+
unreadNotifications: unreadNotifications ?? this.unreadNotifications,
49+
notificationModel: notificationModel ?? this.notificationModel,
50+
rssFeedsListJson: rssFeedsListJson ?? this.rssFeedsListJson,
51+
rssFeedsList: rssFeedsList ?? this.rssFeedsList,
52+
rssRulesList: rssRulesList ?? this.rssRulesList,
53+
rssFeedsContentsList: rssFeedsContentsList ?? this.rssFeedsContentsList,
54+
upSpeed: upSpeed ?? this.upSpeed,
55+
downSpeed: downSpeed ?? this.downSpeed,
56+
notificationCancel: notificationCancel ?? this.notificationCancel);
5457
}
5558

5659
@override
@@ -65,6 +68,7 @@ class HomeScreenState extends Equatable {
6568
rssFeedsContentsList,
6669
upSpeed,
6770
downSpeed,
71+
notificationCancel,
6872
];
6973
}
7074

@@ -82,6 +86,7 @@ class HomeScreenInitial extends HomeScreenState {
8286
rssFeedsContentsList: [],
8387
upSpeed: '0 KB/s',
8488
downSpeed: '0 KB/s',
89+
notificationCancel: {},
8590
);
8691

8792
@override

lib/Blocs/sse_bloc/sse_bloc.dart

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:flood_mobile/Blocs/home_screen_bloc/home_screen_bloc.dart';
99
import 'package:flood_mobile/Blocs/user_detail_bloc/user_detail_bloc.dart';
1010
import 'package:flood_mobile/Constants/api_endpoints.dart';
1111
import 'package:flood_mobile/Constants/event_names.dart';
12+
import 'package:flood_mobile/Notifications/notification_controller.dart';
1213

1314
part 'sse_event.dart';
1415
part 'sse_state.dart';
@@ -56,22 +57,20 @@ class SSEBloc extends Bloc<SSEEvent, SSEState> {
5657
.torrentList
5758
.length;
5859
i++) {
59-
EventHandlerApi.showNotification(i, event.context);
60+
createTorrentDownloadNotification(i, event.context);
6061
}
6162
break;
6263
case Events.TORRENT_LIST_DIFF_CHANGE:
64+
var homeScreenBloc =
65+
BlocProvider.of<HomeScreenBloc>(event.context, listen: false)
66+
.state;
6367
EventHandlerApi.updateFullTorrentList(
6468
model: listenEvent, context: event.context);
6569
await Future.delayed(Duration.zero);
66-
for (int i = 0;
67-
i <
68-
BlocProvider.of<HomeScreenBloc>(event.context,
69-
listen: false)
70-
.state
71-
.torrentList
72-
.length;
73-
i++) {
74-
EventHandlerApi.showNotification(i, event.context);
70+
for (int i = 0; i < homeScreenBloc.torrentList.length; i++) {
71+
if (!homeScreenBloc.notificationCancel
72+
.containsKey(homeScreenBloc.torrentList[i].hash))
73+
await createTorrentDownloadNotification(i, event.context);
7574
}
7675
break;
7776
case Events.NOTIFICATION_COUNT_CHANGE:
@@ -91,7 +90,14 @@ class SSEBloc extends Bloc<SSEEvent, SSEState> {
9190
.torrentList[j]
9291
.status
9392
.contains('complete')) {
94-
EventHandlerApi.showEventNotification(j, event.context);
93+
createDownloadFinishedNotification(j, event.context);
94+
} else if (BlocProvider.of<HomeScreenBloc>(event.context,
95+
listen: false)
96+
.state
97+
.torrentList[j]
98+
.status
99+
.contains('error')) {
100+
createDownloadErrorNotification(j, event.context);
95101
}
96102
}
97103
break;

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
}

0 commit comments

Comments
 (0)