forked from Nexus/nexus
pause sync when app is backgrounded
This commit is contained in:
parent
b6ee5aec64
commit
9f0b80072d
4 changed files with 40 additions and 1 deletions
2
gomuks
2
gomuks
|
|
@ -1 +1 @@
|
|||
Subproject commit 7e43a63e578b0d6f52269d65297198077bf5b45d
|
||||
Subproject commit ba82b6e31c87c99766a8366a72a9f07d643b323f
|
||||
18
lib/controllers/app_paused.dart
Normal file
18
lib/controllers/app_paused.dart
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import "package:flutter/widgets.dart";
|
||||
import "package:flutter_riverpod/flutter_riverpod.dart";
|
||||
|
||||
class AppPausedController extends Notifier<bool> {
|
||||
@override
|
||||
bool build() {
|
||||
final listener = AppLifecycleListener(
|
||||
onPause: () => state = true,
|
||||
onResume: () => state = false,
|
||||
);
|
||||
ref.onDispose(listener.dispose);
|
||||
return WidgetsBinding.instance.lifecycleState == .paused;
|
||||
}
|
||||
|
||||
static final provider = NotifierProvider<AppPausedController, bool>(
|
||||
AppPausedController.new,
|
||||
);
|
||||
}
|
||||
|
|
@ -95,6 +95,11 @@ class ClientController extends AsyncNotifier<int> {
|
|||
}
|
||||
});
|
||||
|
||||
if (!ref.mounted) {
|
||||
GomuksDestroy(handle);
|
||||
return Completer<int>().future;
|
||||
}
|
||||
|
||||
ref.onDispose(() => GomuksDestroy(handle));
|
||||
|
||||
return handle;
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
import "dart:async";
|
||||
import "dart:ffi";
|
||||
import "dart:io";
|
||||
import "dart:isolate";
|
||||
|
||||
import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
||||
import "package:flutter/foundation.dart";
|
||||
import "package:nexus/controllers/account_data.dart";
|
||||
import "package:nexus/controllers/app_paused.dart";
|
||||
import "package:nexus/controllers/client.dart";
|
||||
import "package:nexus/controllers/client_state.dart";
|
||||
import "package:nexus/controllers/init_complete.dart";
|
||||
|
|
@ -141,6 +143,20 @@ class GomuksListenerController extends AsyncNotifier<void> {
|
|||
if (errorCode != 0) {
|
||||
throw Exception("GomuksStart returned error code $errorCode");
|
||||
}
|
||||
|
||||
if (!ref.mounted) return;
|
||||
|
||||
if (Platform.isAndroid || Platform.isIOS) {
|
||||
// Mobile OSes block network access for backgrounded apps, so pause
|
||||
// syncing instead of letting it fail.
|
||||
ref.listen(AppPausedController.provider, (_, paused) {
|
||||
if (paused) {
|
||||
GomuksPauseSync(handle);
|
||||
} else {
|
||||
GomuksResumeSync(handle);
|
||||
}
|
||||
}, fireImmediately: true);
|
||||
}
|
||||
}
|
||||
|
||||
/// Waits for gomuks to report the client's device ID
|
||||
|
|
|
|||
Loading…
Reference in a new issue