@@ -2,6 +2,7 @@ import 'dart:collection';
2
2
import 'dart:convert' ;
3
3
import 'dart:io' ;
4
4
import 'package:battery_plus/battery_plus.dart' ;
5
+ import 'package:connectivity_plus/connectivity_plus.dart' ;
5
6
import 'package:flutter/cupertino.dart' ;
6
7
import 'package:flutter/services.dart' ;
7
8
import 'package:flutter_bloc/flutter_bloc.dart' ;
@@ -96,6 +97,7 @@ class EventHandlerApi {
96
97
newTorrentMap,
97
98
strict: true ,
98
99
);
100
+
99
101
//Updating data in provider
100
102
BlocProvider .of <HomeScreenBloc >(context, listen: false )
101
103
.add (SetTorrentListJsonEvent (newTorrentListJson: newTorrentListJson));
@@ -119,7 +121,7 @@ class EventHandlerApi {
119
121
120
122
if (torrentList.length > int .parse (torrentLength) ||
121
123
torrentList.length < int .parse (torrentLength)) {
122
- filterDataRephrasor (torrentList, context);
124
+ await filterDataRephrasor (torrentList, context);
123
125
}
124
126
125
127
//Setting the full list of torrent
@@ -128,6 +130,7 @@ class EventHandlerApi {
128
130
129
131
final PowerManagementBloc powerManagementBloc =
130
132
BlocProvider .of <PowerManagementBloc >(context, listen: false );
133
+
131
134
//Exit screen on all download finished
132
135
if (powerManagementBloc.state.shutDownWhenFinishDownload &&
133
136
isAllDownloadFinished (context)) {
@@ -138,7 +141,15 @@ class EventHandlerApi {
138
141
//Turn off wifi on all download finished
139
142
if (powerManagementBloc.state.shutDownWifi &&
140
143
isAllDownloadFinished (context)) {
141
- turnOffWiFi (powerManagementBloc.state.shutDownWifi);
144
+ await turnOffWiFi (powerManagementBloc.state.shutDownWifi);
145
+ }
146
+
147
+ //Stop all download on wifi disconnect
148
+ if (powerManagementBloc.state.wifiOnlyDownload) {
149
+ final connectivityResult = await Connectivity ().checkConnectivity ();
150
+ if (connectivityResult != ConnectivityResult .wifi) {
151
+ await stopAllDownload (context);
152
+ }
142
153
}
143
154
144
155
// Stop all download on low battery
@@ -148,14 +159,7 @@ class EventHandlerApi {
148
159
powerManagementBloc.state.batteryLimitLevel > 0 ? true : false ;
149
160
if (isBatteryLimitSet &&
150
161
currentBatteryLevel <= powerManagementBloc.state.batteryLimitLevel) {
151
- BlocProvider .of <HomeScreenBloc >(context, listen: false )
152
- .state
153
- .torrentList
154
- .forEach ((element) {
155
- if (element.status.contains ('downloading' )) {
156
- TorrentApi .stopTorrent (hashes: [element.hash], context: context);
157
- }
158
- });
162
+ await stopAllDownload (context);
159
163
}
160
164
}
161
165
@@ -286,6 +290,17 @@ bool isAllDownloadFinished(BuildContext context) {
286
290
);
287
291
}
288
292
289
- void turnOffWiFi (bool wifiStatus) async {
290
- WiFiForIoTPlugin .setEnabled (! wifiStatus);
293
+ Future <void > turnOffWiFi (bool wifiStatus) async {
294
+ await WiFiForIoTPlugin .setEnabled (! wifiStatus);
295
+ }
296
+
297
+ Future <void > stopAllDownload (BuildContext context) async {
298
+ BlocProvider .of <HomeScreenBloc >(context, listen: false )
299
+ .state
300
+ .torrentList
301
+ .forEach ((element) async {
302
+ if (element.status.contains ('downloading' )) {
303
+ await TorrentApi .stopTorrent (hashes: [element.hash], context: context);
304
+ }
305
+ });
291
306
}
0 commit comments