|
1 | 1 | import 'dart:collection';
|
2 | 2 | import 'dart:convert';
|
| 3 | +import 'dart:io'; |
| 4 | +import 'package:battery_plus/battery_plus.dart'; |
3 | 5 | import 'package:flutter/cupertino.dart';
|
| 6 | +import 'package:flutter/services.dart'; |
4 | 7 | import 'package:flutter_bloc/flutter_bloc.dart';
|
5 | 8 | import 'package:flutter_client_sse/flutter_client_sse.dart';
|
6 | 9 | import 'package:json_patch/json_patch.dart';
|
7 | 10 | import 'package:wifi_iot/wifi_iot.dart';
|
| 11 | +import 'package:flood_mobile/Api/torrent_api.dart'; |
8 | 12 | import 'package:flood_mobile/Blocs/filter_torrent_bloc/filter_torrent_bloc.dart';
|
9 | 13 | import 'package:flood_mobile/Blocs/home_screen_bloc/home_screen_bloc.dart';
|
| 14 | +import 'package:flood_mobile/Blocs/power_management_bloc/power_management_bloc.dart'; |
10 | 15 | import 'package:flood_mobile/Model/download_rate_model.dart';
|
11 | 16 | import 'package:flood_mobile/Model/torrent_model.dart';
|
12 | 17 | import 'package:flood_mobile/Services/file_size_helper.dart';
|
@@ -120,6 +125,38 @@ class EventHandlerApi {
|
120 | 125 | //Setting the full list of torrent
|
121 | 126 | BlocProvider.of<HomeScreenBloc>(context, listen: false)
|
122 | 127 | .add(SetTorrentListEvent(newTorrentList: torrentList));
|
| 128 | + |
| 129 | + final PowerManagementBloc powerManagementBloc = |
| 130 | + BlocProvider.of<PowerManagementBloc>(context, listen: false); |
| 131 | + //Exit screen on all download finished |
| 132 | + if (powerManagementBloc.state.shutDownWhenFinishDownload && |
| 133 | + isAllDownloadFinished(context)) { |
| 134 | + SystemChannels.platform.invokeMethod('SystemNavigator.pop'); |
| 135 | + exit(0); |
| 136 | + } |
| 137 | + |
| 138 | + //Turn off wifi on all download finished |
| 139 | + if (powerManagementBloc.state.shutDownWifi && |
| 140 | + isAllDownloadFinished(context)) { |
| 141 | + turnOffWiFi(powerManagementBloc.state.shutDownWifi); |
| 142 | + } |
| 143 | + |
| 144 | + // Stop all download on low battery |
| 145 | + Battery _battery = Battery(); |
| 146 | + int currentBatteryLevel = await _battery.batteryLevel; |
| 147 | + bool isBatteryLimitSet = |
| 148 | + powerManagementBloc.state.batteryLimitLevel > 0 ? true : false; |
| 149 | + if (isBatteryLimitSet && |
| 150 | + 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 | + }); |
| 159 | + } |
123 | 160 | }
|
124 | 161 |
|
125 | 162 | static Future<void> filterDataRephrasor(
|
|
0 commit comments