mirror of
https://github.com/twonlyapp/twonly-app.git
synced 2026-05-25 02:12:13 +00:00
remove global connection state
This commit is contained in:
parent
bd012a363e
commit
715774bd7f
5 changed files with 33 additions and 26 deletions
23
lib/app.dart
23
lib/app.dart
|
|
@ -4,7 +4,6 @@ import 'package:flutter_localizations/flutter_localizations.dart';
|
|||
import 'package:provider/provider.dart';
|
||||
import 'package:twonly/globals.dart';
|
||||
import 'package:twonly/src/localization/generated/app_localizations.dart';
|
||||
import 'package:twonly/src/providers/connection.provider.dart';
|
||||
import 'package:twonly/src/providers/purchases.provider.dart';
|
||||
import 'package:twonly/src/providers/routing.provider.dart';
|
||||
import 'package:twonly/src/providers/settings.provider.dart';
|
||||
|
|
@ -36,29 +35,16 @@ class _AppState extends State<App> with WidgetsBindingObserver {
|
|||
globalIsAppInBackground = false;
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
|
||||
globalCallbackConnectionState = ({required isConnected}) async {
|
||||
await context.read<CustomChangeProvider>().updateConnectionState(
|
||||
isConnected,
|
||||
);
|
||||
await setUserPlan();
|
||||
};
|
||||
|
||||
unawaited(initAsync());
|
||||
}
|
||||
|
||||
Future<void> setUserPlan() async {
|
||||
Future<void> initAsync() async {
|
||||
final user = await getUser();
|
||||
if (user != null && mounted) {
|
||||
if (mounted) {
|
||||
context.read<PurchasesProvider>().updatePlan(
|
||||
planFromString(user.subscriptionPlan),
|
||||
);
|
||||
}
|
||||
context.read<PurchasesProvider>().updatePlan(
|
||||
planFromString(user.subscriptionPlan),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> initAsync() async {
|
||||
await setUserPlan();
|
||||
await apiService.connect();
|
||||
await apiService.listenToNetworkChanges();
|
||||
}
|
||||
|
|
@ -81,7 +67,6 @@ class _AppState extends State<App> with WidgetsBindingObserver {
|
|||
@override
|
||||
void dispose() {
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
globalCallbackConnectionState = ({required isConnected}) {};
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -31,10 +31,6 @@ late UserData gUser;
|
|||
// App widget.
|
||||
|
||||
// This callback called by the apiProvider
|
||||
void Function({required bool isConnected}) globalCallbackConnectionState =
|
||||
({
|
||||
required isConnected,
|
||||
}) {};
|
||||
void Function() globalCallbackAppIsOutdated = () {};
|
||||
void Function() globalCallbackNewDeviceRegistered = () {};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,23 @@
|
|||
import 'dart:async';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:twonly/globals.dart';
|
||||
|
||||
class CustomChangeProvider with ChangeNotifier, DiagnosticableTreeMixin {
|
||||
CustomChangeProvider() {
|
||||
_connSub = apiService.onConnectionStateUpdated.listen(
|
||||
updateConnectionState,
|
||||
);
|
||||
}
|
||||
bool _isConnected = false;
|
||||
bool get isConnected => _isConnected;
|
||||
late StreamSubscription<bool> _connSub;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_connSub.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> updateConnectionState(bool update) async {
|
||||
_isConnected = update;
|
||||
notifyListeners();
|
||||
|
|
|
|||
|
|
@ -39,6 +39,12 @@ class PurchasesProvider with ChangeNotifier, DiagnosticableTreeMixin {
|
|||
);
|
||||
|
||||
_planSub = apiService.onPlanUpdated.listen(updatePlan);
|
||||
_connSub = apiService.onConnectionStateUpdated.listen((_) async {
|
||||
final user = await getUser();
|
||||
if (user != null) {
|
||||
updatePlan(planFromString(user.subscriptionPlan));
|
||||
}
|
||||
});
|
||||
|
||||
loadPurchases();
|
||||
}
|
||||
|
|
@ -51,6 +57,7 @@ class PurchasesProvider with ChangeNotifier, DiagnosticableTreeMixin {
|
|||
final InAppPurchase iapConnection = IAPConnection.instance;
|
||||
|
||||
late StreamSubscription<SubscriptionPlan> _planSub;
|
||||
late StreamSubscription<bool> _connSub;
|
||||
bool _userTriggeredBuyButton = false;
|
||||
|
||||
void updatePlan(SubscriptionPlan newPlan) {
|
||||
|
|
@ -229,6 +236,7 @@ class PurchasesProvider with ChangeNotifier, DiagnosticableTreeMixin {
|
|||
@override
|
||||
void dispose() {
|
||||
_planSub.cancel();
|
||||
_connSub.cancel();
|
||||
_subscription.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,6 +61,9 @@ class ApiService {
|
|||
final _planUpdateController = StreamController<SubscriptionPlan>.broadcast();
|
||||
Stream<SubscriptionPlan> get onPlanUpdated => _planUpdateController.stream;
|
||||
|
||||
final _connectionStateController = StreamController<bool>.broadcast();
|
||||
Stream<bool> get onConnectionStateUpdated => _connectionStateController.stream;
|
||||
|
||||
bool appIsOutdated = false;
|
||||
bool isAuthenticated = false;
|
||||
|
||||
|
|
@ -92,7 +95,7 @@ class ApiService {
|
|||
// Function is called after the user is authenticated at the server
|
||||
Future<void> onAuthenticated() async {
|
||||
await initFCMAfterAuthenticated();
|
||||
globalCallbackConnectionState(isConnected: true);
|
||||
_connectionStateController.add(true);
|
||||
|
||||
if (globalIsInBackgroundTask) {
|
||||
await retransmitRawBytes();
|
||||
|
|
@ -125,13 +128,13 @@ class ApiService {
|
|||
Future<void> onConnected() async {
|
||||
await authenticate();
|
||||
_reconnectionDelay = 1;
|
||||
globalCallbackConnectionState(isConnected: true);
|
||||
_connectionStateController.add(true);
|
||||
}
|
||||
|
||||
Future<void> onClosed() async {
|
||||
_channel = null;
|
||||
isAuthenticated = false;
|
||||
globalCallbackConnectionState(isConnected: false);
|
||||
_connectionStateController.add(false);
|
||||
await twonlyDB.mediaFilesDao.resetPendingDownloadState();
|
||||
await startReconnectionTimer();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue