From 9f0110340981e3602b69dc949531005bdf315789 Mon Sep 17 00:00:00 2001 From: amit patel Date: Sat, 16 Sep 2023 11:55:52 +0530 Subject: [PATCH] fix some small issues --- lib/Api/event_handler_api.dart | 39 +++++++++++++------ lib/Blocs/language_bloc/language_bloc.dart | 3 +- lib/Blocs/theme_bloc/theme_bloc.dart | 7 ++-- .../widgets/rss_feed_home_page.dart | 3 +- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/lib/Api/event_handler_api.dart b/lib/Api/event_handler_api.dart index 94abb77..b643413 100644 --- a/lib/Api/event_handler_api.dart +++ b/lib/Api/event_handler_api.dart @@ -2,6 +2,7 @@ import 'dart:collection'; import 'dart:convert'; import 'dart:io'; import 'package:battery_plus/battery_plus.dart'; +import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/services.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; @@ -96,6 +97,7 @@ class EventHandlerApi { newTorrentMap, strict: true, ); + //Updating data in provider BlocProvider.of(context, listen: false) .add(SetTorrentListJsonEvent(newTorrentListJson: newTorrentListJson)); @@ -119,7 +121,7 @@ class EventHandlerApi { if (torrentList.length > int.parse(torrentLength) || torrentList.length < int.parse(torrentLength)) { - filterDataRephrasor(torrentList, context); + await filterDataRephrasor(torrentList, context); } //Setting the full list of torrent @@ -128,6 +130,7 @@ class EventHandlerApi { final PowerManagementBloc powerManagementBloc = BlocProvider.of(context, listen: false); + //Exit screen on all download finished if (powerManagementBloc.state.shutDownWhenFinishDownload && isAllDownloadFinished(context)) { @@ -138,7 +141,15 @@ class EventHandlerApi { //Turn off wifi on all download finished if (powerManagementBloc.state.shutDownWifi && isAllDownloadFinished(context)) { - turnOffWiFi(powerManagementBloc.state.shutDownWifi); + await turnOffWiFi(powerManagementBloc.state.shutDownWifi); + } + + //Stop all download on wifi disconnect + if (powerManagementBloc.state.wifiOnlyDownload) { + final connectivityResult = await Connectivity().checkConnectivity(); + if (connectivityResult != ConnectivityResult.wifi) { + await stopAllDownload(context); + } } // Stop all download on low battery @@ -148,14 +159,7 @@ class EventHandlerApi { powerManagementBloc.state.batteryLimitLevel > 0 ? true : false; if (isBatteryLimitSet && currentBatteryLevel <= powerManagementBloc.state.batteryLimitLevel) { - BlocProvider.of(context, listen: false) - .state - .torrentList - .forEach((element) { - if (element.status.contains('downloading')) { - TorrentApi.stopTorrent(hashes: [element.hash], context: context); - } - }); + await stopAllDownload(context); } } @@ -286,6 +290,17 @@ bool isAllDownloadFinished(BuildContext context) { ); } -void turnOffWiFi(bool wifiStatus) async { - WiFiForIoTPlugin.setEnabled(!wifiStatus); +Future turnOffWiFi(bool wifiStatus) async { + await WiFiForIoTPlugin.setEnabled(!wifiStatus); +} + +Future stopAllDownload(BuildContext context) async { + BlocProvider.of(context, listen: false) + .state + .torrentList + .forEach((element) async { + if (element.status.contains('downloading')) { + await TorrentApi.stopTorrent(hashes: [element.hash], context: context); + } + }); } diff --git a/lib/Blocs/language_bloc/language_bloc.dart b/lib/Blocs/language_bloc/language_bloc.dart index 7183591..4f1b273 100644 --- a/lib/Blocs/language_bloc/language_bloc.dart +++ b/lib/Blocs/language_bloc/language_bloc.dart @@ -27,7 +27,8 @@ class LanguageBloc extends Bloc { Future getPreviousLang() async { try { final prefs = await SharedPreferences.getInstance(); - if (prefs.getString('languageCode') != 'null') + if (prefs.containsKey('languageCode') && + prefs.getString('languageCode') != 'null') return Locale(prefs.getString('languageCode')!); else { return null; diff --git a/lib/Blocs/theme_bloc/theme_bloc.dart b/lib/Blocs/theme_bloc/theme_bloc.dart index 311309b..5c06141 100644 --- a/lib/Blocs/theme_bloc/theme_bloc.dart +++ b/lib/Blocs/theme_bloc/theme_bloc.dart @@ -19,9 +19,10 @@ class ThemeBloc extends Bloc { Future getPreviousTheme() async { try { final prefs = await SharedPreferences.getInstance(); - final storedThemeMode = prefs.getString('themeMode'); - - themeMode = _convertStringToThemeMode(storedThemeMode); + if (prefs.containsKey('themeMode')) { + final storedThemeMode = prefs.getString('themeMode'); + themeMode = _convertStringToThemeMode(storedThemeMode); + } } catch (error) { print('Error retrieving theme mode from SharedPreferences: $error'); } diff --git a/lib/Pages/home_screen/widgets/rss_feed_home_page.dart b/lib/Pages/home_screen/widgets/rss_feed_home_page.dart index 1da973b..487091c 100644 --- a/lib/Pages/home_screen/widgets/rss_feed_home_page.dart +++ b/lib/Pages/home_screen/widgets/rss_feed_home_page.dart @@ -737,7 +737,7 @@ class _RSSFeedHomePageState extends State FeedsApi .listAllFeedsAndRules( context: context); - clearFeedsFields(); + if (isUpdateFeedSelected) { UpdateFeedApi.updateFeed( type: "feed", @@ -758,6 +758,7 @@ class _RSSFeedHomePageState extends State context: context); } + clearFeedsFields(); }); } } else {