Skip to content

Commit b4bc3ff

Browse files
committed
Implemented the Shut Down WiFi and Set Battery Optimization features.
1 parent 6128fa0 commit b4bc3ff

File tree

1 file changed

+60
-7
lines changed

1 file changed

+60
-7
lines changed

lib/Api/event_handler_api.dart

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
import 'dart:collection';
22
import 'dart:convert';
3+
import 'dart:io';
34
import 'package:awesome_notifications/awesome_notifications.dart';
5+
import 'package:battery_plus/battery_plus.dart';
46
import 'package:duration/duration.dart';
5-
import 'package:flood_mobile/Model/download_rate_model.dart';
6-
import 'package:flood_mobile/Model/torrent_model.dart';
7-
import 'package:flood_mobile/Services/file_size_helper.dart';
8-
import 'package:flood_mobile/Blocs/filter_torrent_bloc/filter_torrent_bloc.dart';
9-
import 'package:flood_mobile/Blocs/home_screen_bloc/home_screen_bloc.dart';
7+
import 'package:flood_mobile/Api/torrent_api.dart';
108
import 'package:flutter/cupertino.dart';
9+
import 'package:flutter/services.dart';
1110
import 'package:flutter_bloc/flutter_bloc.dart';
1211
import 'package:flutter_client_sse/flutter_client_sse.dart';
1312
import 'package:json_patch/json_patch.dart';
13+
import 'package:flood_mobile/Blocs/filter_torrent_bloc/filter_torrent_bloc.dart';
14+
import 'package:flood_mobile/Blocs/home_screen_bloc/home_screen_bloc.dart';
15+
import 'package:flood_mobile/Blocs/power_management_bloc/power_management_bloc.dart';
1416
import 'package:flood_mobile/Constants/notification_keys.dart';
17+
import 'package:flood_mobile/Model/download_rate_model.dart';
18+
import 'package:flood_mobile/Model/torrent_model.dart';
19+
import 'package:flood_mobile/Services/file_size_helper.dart';
20+
import 'package:wifi_iot/wifi_iot.dart';
1521

1622
String torrentLength = '0';
1723

@@ -71,10 +77,10 @@ class EventHandlerApi {
7177
}
7278

7379
//Updating the full list of torrent
74-
static void updateFullTorrentList({
80+
static Future<void> updateFullTorrentList({
7581
required SSEModel model,
7682
required BuildContext context,
77-
}) {
83+
}) async {
7884
Map<String, dynamic> oldTorrentList =
7985
BlocProvider.of<HomeScreenBloc>(context, listen: false)
8086
.state
@@ -122,6 +128,38 @@ class EventHandlerApi {
122128
//Setting the full list of torrent
123129
BlocProvider.of<HomeScreenBloc>(context, listen: false)
124130
.add(SetTorrentListEvent(newTorrentList: torrentList));
131+
132+
final PowerManagementBloc powerManagementBloc =
133+
BlocProvider.of<PowerManagementBloc>(context, listen: false);
134+
//Exit screen on all download finished
135+
if (powerManagementBloc.state.shutDownWhenFinishDownload &&
136+
isAllDownloadFinished(context)) {
137+
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
138+
exit(0);
139+
}
140+
141+
//Turn off wifi on all download finished
142+
if (powerManagementBloc.state.shutDownWifi &&
143+
isAllDownloadFinished(context)) {
144+
turnOffWiFi(powerManagementBloc.state.shutDownWifi);
145+
}
146+
147+
// Stop all download on low battery
148+
Battery _battery = Battery();
149+
int currentBatteryLevel = await _battery.batteryLevel;
150+
bool isBatteryLimitSet =
151+
powerManagementBloc.state.batteryLimitLevel > 0 ? true : false;
152+
if (isBatteryLimitSet &&
153+
currentBatteryLevel <= powerManagementBloc.state.batteryLimitLevel) {
154+
BlocProvider.of<HomeScreenBloc>(context, listen: false)
155+
.state
156+
.torrentList
157+
.forEach((element) {
158+
if (element.status.contains('downloading')) {
159+
TorrentApi.stopTorrent(hashes: [element.hash], context: context);
160+
}
161+
});
162+
}
125163
}
126164

127165
static Future<void> showNotification(int id, BuildContext context) async {
@@ -321,3 +359,18 @@ class EventHandlerApi {
321359
}
322360
}
323361
}
362+
363+
bool isAllDownloadFinished(BuildContext context) {
364+
return BlocProvider.of<HomeScreenBloc>(context, listen: false)
365+
.state
366+
.torrentList
367+
.every(
368+
(element) {
369+
return element.status.contains('complete');
370+
},
371+
);
372+
}
373+
374+
void turnOffWiFi(bool wifiStatus) async {
375+
WiFiForIoTPlugin.setEnabled(!wifiStatus);
376+
}

0 commit comments

Comments
 (0)