mirror of
https://github.com/venera-app/venera.git
synced 2025-09-27 15:57:25 +00:00
Improve checking follow updates.
This commit is contained in:
@@ -6,6 +6,7 @@ import 'package:venera/foundation/app.dart';
|
|||||||
import 'package:venera/foundation/appdata.dart';
|
import 'package:venera/foundation/appdata.dart';
|
||||||
import 'package:venera/foundation/favorites.dart';
|
import 'package:venera/foundation/favorites.dart';
|
||||||
import 'package:venera/foundation/log.dart';
|
import 'package:venera/foundation/log.dart';
|
||||||
|
import 'package:venera/utils/data_sync.dart';
|
||||||
import 'package:venera/utils/translations.dart';
|
import 'package:venera/utils/translations.dart';
|
||||||
import '../foundation/global_state.dart';
|
import '../foundation/global_state.dart';
|
||||||
|
|
||||||
@@ -440,7 +441,7 @@ class _FollowUpdatesPageState extends AutomaticGlobalState<FollowUpdatesPage> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void setFolder(String folder) async {
|
void setFolder(String folder) async {
|
||||||
FollowUpdatesService.cancelChecking?.call();
|
FollowUpdatesService._cancelChecking?.call();
|
||||||
LocalFavoritesManager().prepareTableForFollowUpdates(folder);
|
LocalFavoritesManager().prepareTableForFollowUpdates(folder);
|
||||||
|
|
||||||
var count = LocalFavoritesManager().count(folder);
|
var count = LocalFavoritesManager().count(folder);
|
||||||
@@ -479,7 +480,7 @@ class _FollowUpdatesPageState extends AutomaticGlobalState<FollowUpdatesPage> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void checkNow() async {
|
void checkNow() async {
|
||||||
FollowUpdatesService.cancelChecking?.call();
|
FollowUpdatesService._cancelChecking?.call();
|
||||||
|
|
||||||
bool isCanceled = false;
|
bool isCanceled = false;
|
||||||
void onCancel() {
|
void onCancel() {
|
||||||
@@ -649,12 +650,14 @@ Stream<_UpdateProgress> _updateFolder(String folder, bool ignoreCheckTime) {
|
|||||||
|
|
||||||
/// Background service for checking updates
|
/// Background service for checking updates
|
||||||
abstract class FollowUpdatesService {
|
abstract class FollowUpdatesService {
|
||||||
static bool isChecking = false;
|
static bool _isChecking = false;
|
||||||
|
|
||||||
static void Function()? cancelChecking;
|
static void Function()? _cancelChecking;
|
||||||
|
|
||||||
static void check() async {
|
static bool _isInitialized = false;
|
||||||
if (isChecking) {
|
|
||||||
|
static void _check() async {
|
||||||
|
if (_isChecking) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var folder = appdata.settings["followUpdatesFolder"];
|
var folder = appdata.settings["followUpdatesFolder"];
|
||||||
@@ -662,11 +665,16 @@ abstract class FollowUpdatesService {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
bool isCanceled = false;
|
bool isCanceled = false;
|
||||||
cancelChecking = () {
|
_cancelChecking = () {
|
||||||
isCanceled = true;
|
isCanceled = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
isChecking = true;
|
_isChecking = true;
|
||||||
|
|
||||||
|
while (DataSync().isDownloading) {
|
||||||
|
await Future.delayed(const Duration(milliseconds: 100));
|
||||||
|
}
|
||||||
|
|
||||||
int updated = 0;
|
int updated = 0;
|
||||||
try {
|
try {
|
||||||
await for (var progress in _updateFolder(folder, false)) {
|
await for (var progress in _updateFolder(folder, false)) {
|
||||||
@@ -676,21 +684,27 @@ abstract class FollowUpdatesService {
|
|||||||
updated = progress.updated;
|
updated = progress.updated;
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
cancelChecking = null;
|
_cancelChecking = null;
|
||||||
isChecking = false;
|
_isChecking = false;
|
||||||
if (updated > 0) {
|
if (updated > 0) {
|
||||||
updateFollowUpdatesUI();
|
updateFollowUpdatesUI();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Initialize the checker.
|
||||||
static void initChecker() {
|
static void initChecker() {
|
||||||
Timer.periodic(const Duration(hours: 1), (timer) {
|
if (_isInitialized) return;
|
||||||
check();
|
_isInitialized = true;
|
||||||
|
_check();
|
||||||
|
// A short interval will not affect the performance since every comic has a check time.
|
||||||
|
Timer.periodic(const Duration(minutes: 5), (timer) {
|
||||||
|
_check();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Update the UI of follow updates.
|
||||||
void updateFollowUpdatesUI() {
|
void updateFollowUpdatesUI() {
|
||||||
GlobalState.findOrNull<_FollowUpdatesWidgetState>()?.updateCount();
|
GlobalState.findOrNull<_FollowUpdatesWidgetState>()?.updateCount();
|
||||||
GlobalState.findOrNull<_FollowUpdatesPageState>()?.updateComics();
|
GlobalState.findOrNull<_FollowUpdatesPageState>()?.updateComics();
|
||||||
|
@@ -32,9 +32,13 @@ class DataSync with ChangeNotifier {
|
|||||||
|
|
||||||
factory DataSync() => instance ?? (instance = DataSync._());
|
factory DataSync() => instance ?? (instance = DataSync._());
|
||||||
|
|
||||||
bool isDownloading = false;
|
bool _isDownloading = false;
|
||||||
|
|
||||||
bool isUploading = false;
|
bool get isDownloading => _isDownloading;
|
||||||
|
|
||||||
|
bool _isUploading = false;
|
||||||
|
|
||||||
|
bool get isUploading => _isUploading;
|
||||||
|
|
||||||
bool haveWaitingTask = false;
|
bool haveWaitingTask = false;
|
||||||
|
|
||||||
@@ -66,7 +70,7 @@ class DataSync with ChangeNotifier {
|
|||||||
await Future.delayed(const Duration(milliseconds: 100));
|
await Future.delayed(const Duration(milliseconds: 100));
|
||||||
}
|
}
|
||||||
haveWaitingTask = false;
|
haveWaitingTask = false;
|
||||||
isUploading = true;
|
_isUploading = true;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
try {
|
try {
|
||||||
var config = _validateConfig();
|
var config = _validateConfig();
|
||||||
@@ -130,7 +134,7 @@ class DataSync with ChangeNotifier {
|
|||||||
return Res.error(e.toString());
|
return Res.error(e.toString());
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
isUploading = false;
|
_isUploading = false;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -142,7 +146,7 @@ class DataSync with ChangeNotifier {
|
|||||||
await Future.delayed(const Duration(milliseconds: 100));
|
await Future.delayed(const Duration(milliseconds: 100));
|
||||||
}
|
}
|
||||||
haveWaitingTask = false;
|
haveWaitingTask = false;
|
||||||
isDownloading = true;
|
_isDownloading = true;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
try {
|
try {
|
||||||
var config = _validateConfig();
|
var config = _validateConfig();
|
||||||
@@ -205,7 +209,7 @@ class DataSync with ChangeNotifier {
|
|||||||
return Res.error(e.toString());
|
return Res.error(e.toString());
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
isDownloading = false;
|
_isDownloading = false;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user