|
1 | 1 | import 'dart:collection';
|
2 | 2 | import 'dart:convert';
|
| 3 | +import 'dart:io'; |
3 | 4 | import 'package:awesome_notifications/awesome_notifications.dart';
|
| 5 | +import 'package:battery_plus/battery_plus.dart'; |
4 | 6 | 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'; |
10 | 8 | import 'package:flutter/cupertino.dart';
|
| 9 | +import 'package:flutter/services.dart'; |
11 | 10 | import 'package:flutter_bloc/flutter_bloc.dart';
|
12 | 11 | import 'package:flutter_client_sse/flutter_client_sse.dart';
|
13 | 12 | 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'; |
14 | 16 | 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'; |
15 | 21 |
|
16 | 22 | String torrentLength = '0';
|
17 | 23 |
|
@@ -71,10 +77,10 @@ class EventHandlerApi {
|
71 | 77 | }
|
72 | 78 |
|
73 | 79 | //Updating the full list of torrent
|
74 |
| - static void updateFullTorrentList({ |
| 80 | + static Future<void> updateFullTorrentList({ |
75 | 81 | required SSEModel model,
|
76 | 82 | required BuildContext context,
|
77 |
| - }) { |
| 83 | + }) async { |
78 | 84 | Map<String, dynamic> oldTorrentList =
|
79 | 85 | BlocProvider.of<HomeScreenBloc>(context, listen: false)
|
80 | 86 | .state
|
@@ -122,6 +128,38 @@ class EventHandlerApi {
|
122 | 128 | //Setting the full list of torrent
|
123 | 129 | BlocProvider.of<HomeScreenBloc>(context, listen: false)
|
124 | 130 | .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 | + } |
125 | 163 | }
|
126 | 164 |
|
127 | 165 | static Future<void> showNotification(int id, BuildContext context) async {
|
@@ -321,3 +359,18 @@ class EventHandlerApi {
|
321 | 359 | }
|
322 | 360 | }
|
323 | 361 | }
|
| 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