|
1 | 1 | import 'dart:convert';
|
2 | 2 | import 'package:awesome_notifications/awesome_notifications.dart';
|
| 3 | +import 'package:battery_plus/battery_plus.dart'; |
| 4 | +import 'package:connectivity_plus/connectivity_plus.dart'; |
3 | 5 | import 'package:dio/dio.dart';
|
| 6 | +import 'package:flutter/material.dart'; |
| 7 | +import 'package:flutter_bloc/flutter_bloc.dart'; |
| 8 | +import 'package:flood_mobile/Blocs/api_bloc/api_bloc.dart'; |
| 9 | +import 'package:flood_mobile/Blocs/power_management_bloc/power_management_bloc.dart'; |
| 10 | +import 'package:flood_mobile/Blocs/torrent_content_screen_bloc/torrent_content_screen_bloc.dart'; |
| 11 | +import 'package:flood_mobile/Blocs/user_detail_bloc/user_detail_bloc.dart'; |
4 | 12 | import 'package:flood_mobile/Constants/api_endpoints.dart';
|
5 | 13 | import 'package:flood_mobile/Model/torrent_content_model.dart';
|
6 | 14 | import 'package:flood_mobile/Model/torrent_model.dart';
|
| 15 | +import 'package:flood_mobile/Pages/widgets/flood_snackbar.dart'; |
7 | 16 | import 'package:flood_mobile/Services/file_folder_nester.dart';
|
8 |
| -import 'package:flood_mobile/Blocs/api_bloc/api_bloc.dart'; |
9 |
| -import 'package:flood_mobile/Blocs/torrent_content_screen_bloc/torrent_content_screen_bloc.dart'; |
10 |
| -import 'package:flutter/cupertino.dart'; |
11 |
| -import 'package:flutter_bloc/flutter_bloc.dart'; |
12 |
| - |
13 |
| -import '../Blocs/user_detail_bloc/user_detail_bloc.dart'; |
| 17 | +import 'package:flood_mobile/l10n/l10n.dart'; |
14 | 18 |
|
15 | 19 | class TorrentApi {
|
16 | 20 | // Gets list of torrents
|
@@ -50,35 +54,64 @@ class TorrentApi {
|
50 | 54 | }
|
51 | 55 | }
|
52 | 56 |
|
53 |
| - static Future<void> startTorrent( |
54 |
| - {required List<String> hashes, required BuildContext context}) async { |
55 |
| - try { |
56 |
| - String url = |
57 |
| - BlocProvider.of<ApiBloc>(context, listen: false).state.baseUrl + |
58 |
| - ApiEndpoints.startTorrentUrl; |
59 |
| - print('---START TORRENT---'); |
60 |
| - print(url); |
61 |
| - Response response; |
62 |
| - Dio dio = new Dio(); |
63 |
| - //Headers |
64 |
| - dio.options.headers['Accept'] = "application/json"; |
65 |
| - dio.options.headers['Content-Type'] = "application/json"; |
66 |
| - dio.options.headers['Connection'] = "keep-alive"; |
67 |
| - dio.options.headers['Cookie'] = |
68 |
| - BlocProvider.of<UserDetailBloc>(context, listen: false).token; |
69 |
| - Map<String, dynamic> mp = Map(); |
70 |
| - mp['hashes'] = hashes; |
71 |
| - String rawBody = json.encode(mp); |
72 |
| - response = await dio.post( |
73 |
| - url, |
74 |
| - data: rawBody, |
75 |
| - ); |
76 |
| - if (response.statusCode == 200) { |
77 |
| - print('--TORRENT STARTED--'); |
| 57 | + static Future<void> startTorrent({ |
| 58 | + required List<String> hashes, |
| 59 | + required BuildContext context, |
| 60 | + }) async { |
| 61 | + final powerState = BlocProvider.of<PowerManagementBloc>(context).state; |
| 62 | + final connectivityResult = await Connectivity().checkConnectivity(); |
| 63 | + |
| 64 | + if (!powerState.wifiOnlyDownload || |
| 65 | + connectivityResult == ConnectivityResult.wifi) { |
| 66 | + final chargingConnected = powerState.downloadChargingConnected; |
| 67 | + final batteryState = powerState.currentBatteryState; |
| 68 | + |
| 69 | + if (chargingConnected && batteryState == BatteryState.charging || |
| 70 | + !chargingConnected) { |
| 71 | + try { |
| 72 | + String url = |
| 73 | + BlocProvider.of<ApiBloc>(context, listen: false).state.baseUrl + |
| 74 | + ApiEndpoints.startTorrentUrl; |
| 75 | + print('---START TORRENT---'); |
| 76 | + print(url); |
| 77 | + Response response; |
| 78 | + Dio dio = new Dio(); |
| 79 | + //Headers |
| 80 | + dio.options.headers['Accept'] = "application/json"; |
| 81 | + dio.options.headers['Content-Type'] = "application/json"; |
| 82 | + dio.options.headers['Connection'] = "keep-alive"; |
| 83 | + dio.options.headers['Cookie'] = |
| 84 | + BlocProvider.of<UserDetailBloc>(context, listen: false).token; |
| 85 | + Map<String, dynamic> mp = {'hashes': hashes}; |
| 86 | + String rawBody = json.encode(mp); |
| 87 | + response = await dio.post( |
| 88 | + url, |
| 89 | + data: rawBody, |
| 90 | + ); |
| 91 | + if (response.statusCode == 200) { |
| 92 | + print('--TORRENT STARTED--'); |
| 93 | + } |
| 94 | + } catch (error) { |
| 95 | + print('--ERROR IN TORRENT START--'); |
| 96 | + print(error.toString()); |
| 97 | + } |
| 98 | + } else { |
| 99 | + final warningSnackbar = addFloodSnackBar( |
| 100 | + SnackbarType.caution, |
| 101 | + context.l10n.download_chanrging_only_snackbar, |
| 102 | + context.l10n.button_dismiss); |
| 103 | + ScaffoldMessenger.of(context).clearSnackBars(); |
| 104 | + ScaffoldMessenger.of(context).showSnackBar( |
| 105 | + warningSnackbar, |
| 106 | + ); |
78 | 107 | }
|
79 |
| - } catch (error) { |
80 |
| - print('--ERROR IN TORRENT START--'); |
81 |
| - print(error.toString()); |
| 108 | + } else { |
| 109 | + final warningSnackbar = addFloodSnackBar(SnackbarType.caution, |
| 110 | + context.l10n.wifi_only_snackbar, context.l10n.button_dismiss); |
| 111 | + ScaffoldMessenger.of(context).clearSnackBars(); |
| 112 | + ScaffoldMessenger.of(context).showSnackBar( |
| 113 | + warningSnackbar, |
| 114 | + ); |
82 | 115 | }
|
83 | 116 | }
|
84 | 117 |
|
|
0 commit comments