mirror of
https://github.com/JGeek00/adguard-home-manager.git
synced 2025-05-14 14:02:48 +00:00
Fixed countdown
This commit is contained in:
parent
44e0f274ec
commit
7f0c5fcefd
3 changed files with 72 additions and 54 deletions
|
@ -14,3 +14,16 @@ String formatTimeOfDay(TimeOfDay timestamp, String format) {
|
|||
DateFormat f = DateFormat(format);
|
||||
return f.format(DateTime(0, 0, 0, timestamp.hour, timestamp.minute));
|
||||
}
|
||||
|
||||
String formatRemainingSeconds(int seconds) {
|
||||
int h, m, s;
|
||||
h = seconds ~/ 3600;
|
||||
m = ((seconds - h * 3600)) ~/ 60;
|
||||
s = seconds - (h * 3600) - (m * 60);
|
||||
|
||||
String hourLeft = h.toString().length < 2 ? "0$h" : h.toString();
|
||||
String minuteLeft = m.toString().length < 2 ? "0$m" : m.toString();
|
||||
String secondsLeft = s.toString().length < 2 ? "0$s" : s.toString();
|
||||
|
||||
return "$hourLeft:$minuteLeft:$secondsLeft";
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:adguard_home_manager/models/server_status.dart';
|
||||
|
@ -19,6 +21,11 @@ class StatusProvider with ChangeNotifier {
|
|||
List<String> _protectionsManagementProcess = []; // protections that are currenty being enabled or disabled
|
||||
FilteringStatus? _filteringStatus;
|
||||
|
||||
// Countdown
|
||||
DateTime? _currentDeadline;
|
||||
Timer? _countdown;
|
||||
int _remaining = 0;
|
||||
|
||||
LoadStatus get loadStatus {
|
||||
return _loadStatus;
|
||||
}
|
||||
|
@ -35,10 +42,24 @@ class StatusProvider with ChangeNotifier {
|
|||
return _filteringStatus;
|
||||
}
|
||||
|
||||
int get remainingTime {
|
||||
return _remaining;
|
||||
}
|
||||
|
||||
DateTime? get currentDeadline {
|
||||
return _currentDeadline;
|
||||
}
|
||||
|
||||
void setServerStatusData({
|
||||
required ServerStatus data,
|
||||
}) {
|
||||
_serverStatus = data;
|
||||
if (
|
||||
(_countdown == null ||( _countdown != null && _countdown!.isActive == false)) &&
|
||||
data.disabledUntil != null
|
||||
) {
|
||||
startCountdown(data.disabledUntil!);
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
|
@ -52,6 +73,37 @@ class StatusProvider with ChangeNotifier {
|
|||
notifyListeners();
|
||||
}
|
||||
|
||||
void startCountdown(DateTime deadline) {
|
||||
stopCountdown();
|
||||
|
||||
_currentDeadline = deadline;
|
||||
_remaining = deadline.difference(DateTime.now()).inSeconds+1;
|
||||
|
||||
_countdown = Timer.periodic(
|
||||
const Duration(seconds: 1),
|
||||
(Timer timer) async {
|
||||
if (_remaining == 0) {
|
||||
timer.cancel();
|
||||
notifyListeners();
|
||||
getServerStatus();
|
||||
}
|
||||
else {
|
||||
_remaining = _remaining - 1;
|
||||
notifyListeners();
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void stopCountdown() {
|
||||
if (_countdown != null && _countdown!.isActive) {
|
||||
_countdown!.cancel();
|
||||
_countdown = null;
|
||||
_remaining = 0;
|
||||
_currentDeadline = null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<dynamic> updateBlocking({
|
||||
required String block,
|
||||
required bool newStatus,
|
||||
|
@ -72,12 +124,15 @@ class StatusProvider with ChangeNotifier {
|
|||
if (result['result'] == 'success') {
|
||||
_serverStatus!.generalEnabled = newStatus;
|
||||
if (time != null) {
|
||||
final deadline = generateTimeDeadline(time);
|
||||
_serverStatus!.timeGeneralDisabled = time;
|
||||
_serverStatus!.disabledUntil = generateTimeDeadline(time);
|
||||
_serverStatus!.disabledUntil = deadline;
|
||||
startCountdown(deadline);
|
||||
}
|
||||
else {
|
||||
_serverStatus!.timeGeneralDisabled = 0;
|
||||
_serverStatus!.disabledUntil = null;
|
||||
stopCountdown();
|
||||
}
|
||||
notifyListeners();
|
||||
return null;
|
||||
|
|
|
@ -11,7 +11,7 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|||
import 'package:adguard_home_manager/functions/snackbar.dart';
|
||||
import 'package:adguard_home_manager/functions/compare_versions.dart';
|
||||
import 'package:adguard_home_manager/providers/status_provider.dart';
|
||||
import 'package:adguard_home_manager/functions/time_server_disabled.dart';
|
||||
import 'package:adguard_home_manager/functions/format_time.dart';
|
||||
import 'package:adguard_home_manager/providers/app_config_provider.dart';
|
||||
|
||||
class ManagementModal extends StatefulWidget {
|
||||
|
@ -31,10 +31,6 @@ class _ManagementModalState extends State<ManagementModal> with SingleTickerProv
|
|||
late Animation<double> animation;
|
||||
final ExpandableController expandableController = ExpandableController();
|
||||
|
||||
DateTime? currentDeadline;
|
||||
Timer? countdown;
|
||||
int start = 0;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
expandableController.addListener(() async {
|
||||
|
@ -65,7 +61,6 @@ class _ManagementModalState extends State<ManagementModal> with SingleTickerProv
|
|||
|
||||
@override
|
||||
void dispose() {
|
||||
if (countdown != null) countdown!.cancel();
|
||||
animationController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
@ -75,51 +70,6 @@ class _ManagementModalState extends State<ManagementModal> with SingleTickerProv
|
|||
final statusProvider = Provider.of<StatusProvider>(context);
|
||||
final appConfigProvider = Provider.of<AppConfigProvider>(context);
|
||||
|
||||
void startTimer(DateTime deadline) {
|
||||
setState(() {
|
||||
currentDeadline = deadline;
|
||||
start = deadline.difference(DateTime.now()).inSeconds+1;
|
||||
});
|
||||
|
||||
const oneSec = Duration(seconds: 1);
|
||||
countdown = Timer.periodic(
|
||||
oneSec,
|
||||
(Timer timer) async {
|
||||
if (start == 0) {
|
||||
setState(() {
|
||||
timer.cancel();
|
||||
});
|
||||
final result = await statusProvider.getServerStatus();
|
||||
if (result == false) {
|
||||
setState(() {
|
||||
start = start - 1;
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
statusProvider.serverStatus != null &&
|
||||
statusProvider.serverStatus!.disabledUntil != null &&
|
||||
statusProvider.serverStatus!.disabledUntil != currentDeadline
|
||||
) {
|
||||
startTimer(statusProvider.serverStatus!.disabledUntil!);
|
||||
}
|
||||
|
||||
if (
|
||||
statusProvider.serverStatus != null &&
|
||||
statusProvider.serverStatus!.generalEnabled == true
|
||||
) {
|
||||
setState(() {
|
||||
start = 0;
|
||||
currentDeadline = null;
|
||||
if (countdown != null) countdown!.cancel();
|
||||
countdown = null;
|
||||
});
|
||||
}
|
||||
|
||||
void updateBlocking({
|
||||
required bool value,
|
||||
required String filter,
|
||||
|
@ -178,8 +128,8 @@ class _ManagementModalState extends State<ManagementModal> with SingleTickerProv
|
|||
),
|
||||
if (statusProvider.serverStatus!.timeGeneralDisabled > 0) ...[
|
||||
const SizedBox(height: 2),
|
||||
if (currentDeadline != null) Text(
|
||||
"${AppLocalizations.of(context)!.remainingTime}: ${generateRemainingTimeString(currentDeadline!.difference(DateTime.now()))}"
|
||||
if (statusProvider.currentDeadline != null) Text(
|
||||
"${AppLocalizations.of(context)!.remainingTime}: ${formatRemainingSeconds(statusProvider.remainingTime)}"
|
||||
)
|
||||
]
|
||||
],
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue