remove global connection state

This commit is contained in:
otsmr 2026-04-21 02:24:47 +02:00
parent bd012a363e
commit 715774bd7f
5 changed files with 33 additions and 26 deletions

View file

@ -4,7 +4,6 @@ import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:twonly/globals.dart'; import 'package:twonly/globals.dart';
import 'package:twonly/src/localization/generated/app_localizations.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/purchases.provider.dart';
import 'package:twonly/src/providers/routing.provider.dart'; import 'package:twonly/src/providers/routing.provider.dart';
import 'package:twonly/src/providers/settings.provider.dart'; import 'package:twonly/src/providers/settings.provider.dart';
@ -36,29 +35,16 @@ class _AppState extends State<App> with WidgetsBindingObserver {
globalIsAppInBackground = false; globalIsAppInBackground = false;
WidgetsBinding.instance.addObserver(this); WidgetsBinding.instance.addObserver(this);
globalCallbackConnectionState = ({required isConnected}) async {
await context.read<CustomChangeProvider>().updateConnectionState(
isConnected,
);
await setUserPlan();
};
unawaited(initAsync()); unawaited(initAsync());
} }
Future<void> setUserPlan() async { Future<void> initAsync() async {
final user = await getUser(); final user = await getUser();
if (user != null && mounted) { if (user != null && mounted) {
if (mounted) { context.read<PurchasesProvider>().updatePlan(
context.read<PurchasesProvider>().updatePlan( planFromString(user.subscriptionPlan),
planFromString(user.subscriptionPlan), );
);
}
} }
}
Future<void> initAsync() async {
await setUserPlan();
await apiService.connect(); await apiService.connect();
await apiService.listenToNetworkChanges(); await apiService.listenToNetworkChanges();
} }
@ -81,7 +67,6 @@ class _AppState extends State<App> with WidgetsBindingObserver {
@override @override
void dispose() { void dispose() {
WidgetsBinding.instance.removeObserver(this); WidgetsBinding.instance.removeObserver(this);
globalCallbackConnectionState = ({required isConnected}) {};
super.dispose(); super.dispose();
} }

View file

@ -31,10 +31,6 @@ late UserData gUser;
// App widget. // App widget.
// This callback called by the apiProvider // This callback called by the apiProvider
void Function({required bool isConnected}) globalCallbackConnectionState =
({
required isConnected,
}) {};
void Function() globalCallbackAppIsOutdated = () {}; void Function() globalCallbackAppIsOutdated = () {};
void Function() globalCallbackNewDeviceRegistered = () {}; void Function() globalCallbackNewDeviceRegistered = () {};

View file

@ -1,8 +1,23 @@
import 'dart:async';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:twonly/globals.dart';
class CustomChangeProvider with ChangeNotifier, DiagnosticableTreeMixin { class CustomChangeProvider with ChangeNotifier, DiagnosticableTreeMixin {
CustomChangeProvider() {
_connSub = apiService.onConnectionStateUpdated.listen(
updateConnectionState,
);
}
bool _isConnected = false; bool _isConnected = false;
bool get isConnected => _isConnected; bool get isConnected => _isConnected;
late StreamSubscription<bool> _connSub;
@override
void dispose() {
_connSub.cancel();
super.dispose();
}
Future<void> updateConnectionState(bool update) async { Future<void> updateConnectionState(bool update) async {
_isConnected = update; _isConnected = update;
notifyListeners(); notifyListeners();

View file

@ -39,6 +39,12 @@ class PurchasesProvider with ChangeNotifier, DiagnosticableTreeMixin {
); );
_planSub = apiService.onPlanUpdated.listen(updatePlan); _planSub = apiService.onPlanUpdated.listen(updatePlan);
_connSub = apiService.onConnectionStateUpdated.listen((_) async {
final user = await getUser();
if (user != null) {
updatePlan(planFromString(user.subscriptionPlan));
}
});
loadPurchases(); loadPurchases();
} }
@ -51,6 +57,7 @@ class PurchasesProvider with ChangeNotifier, DiagnosticableTreeMixin {
final InAppPurchase iapConnection = IAPConnection.instance; final InAppPurchase iapConnection = IAPConnection.instance;
late StreamSubscription<SubscriptionPlan> _planSub; late StreamSubscription<SubscriptionPlan> _planSub;
late StreamSubscription<bool> _connSub;
bool _userTriggeredBuyButton = false; bool _userTriggeredBuyButton = false;
void updatePlan(SubscriptionPlan newPlan) { void updatePlan(SubscriptionPlan newPlan) {
@ -229,6 +236,7 @@ class PurchasesProvider with ChangeNotifier, DiagnosticableTreeMixin {
@override @override
void dispose() { void dispose() {
_planSub.cancel(); _planSub.cancel();
_connSub.cancel();
_subscription.cancel(); _subscription.cancel();
super.dispose(); super.dispose();
} }

View file

@ -61,6 +61,9 @@ class ApiService {
final _planUpdateController = StreamController<SubscriptionPlan>.broadcast(); final _planUpdateController = StreamController<SubscriptionPlan>.broadcast();
Stream<SubscriptionPlan> get onPlanUpdated => _planUpdateController.stream; Stream<SubscriptionPlan> get onPlanUpdated => _planUpdateController.stream;
final _connectionStateController = StreamController<bool>.broadcast();
Stream<bool> get onConnectionStateUpdated => _connectionStateController.stream;
bool appIsOutdated = false; bool appIsOutdated = false;
bool isAuthenticated = false; bool isAuthenticated = false;
@ -92,7 +95,7 @@ class ApiService {
// Function is called after the user is authenticated at the server // Function is called after the user is authenticated at the server
Future<void> onAuthenticated() async { Future<void> onAuthenticated() async {
await initFCMAfterAuthenticated(); await initFCMAfterAuthenticated();
globalCallbackConnectionState(isConnected: true); _connectionStateController.add(true);
if (globalIsInBackgroundTask) { if (globalIsInBackgroundTask) {
await retransmitRawBytes(); await retransmitRawBytes();
@ -125,13 +128,13 @@ class ApiService {
Future<void> onConnected() async { Future<void> onConnected() async {
await authenticate(); await authenticate();
_reconnectionDelay = 1; _reconnectionDelay = 1;
globalCallbackConnectionState(isConnected: true); _connectionStateController.add(true);
} }
Future<void> onClosed() async { Future<void> onClosed() async {
_channel = null; _channel = null;
isAuthenticated = false; isAuthenticated = false;
globalCallbackConnectionState(isConnected: false); _connectionStateController.add(false);
await twonlyDB.mediaFilesDao.resetPendingDownloadState(); await twonlyDB.mediaFilesDao.resetPendingDownloadState();
await startReconnectionTimer(); await startReconnectionTimer();
} }