2024-04-30 23:38:49 +08:00
|
|
|
import 'package:fl_clash/common/common.dart';
|
|
|
|
|
import 'package:fl_clash/models/models.dart';
|
2024-05-11 17:02:34 +08:00
|
|
|
import 'package:fl_clash/state.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
|
|
2024-09-08 21:21:21 +08:00
|
|
|
class AppStateManager extends StatefulWidget {
|
2024-04-30 23:38:49 +08:00
|
|
|
final Widget child;
|
|
|
|
|
|
2024-09-08 21:21:21 +08:00
|
|
|
const AppStateManager({
|
2024-04-30 23:38:49 +08:00
|
|
|
super.key,
|
|
|
|
|
required this.child,
|
|
|
|
|
});
|
|
|
|
|
|
2024-09-08 21:21:21 +08:00
|
|
|
@override
|
|
|
|
|
State<AppStateManager> createState() => _AppStateManagerState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _AppStateManagerState extends State<AppStateManager>
|
|
|
|
|
with WidgetsBindingObserver {
|
2024-04-30 23:38:49 +08:00
|
|
|
_updateNavigationsContainer(Widget child) {
|
2024-05-07 13:50:00 +08:00
|
|
|
return Selector2<AppState, Config, UpdateNavigationsSelector>(
|
|
|
|
|
selector: (_, appState, config) {
|
2025-02-03 23:32:00 +08:00
|
|
|
final group = appState.currentGroups;
|
2024-04-30 23:38:49 +08:00
|
|
|
final hasProfile = config.profiles.isNotEmpty;
|
|
|
|
|
return UpdateNavigationsSelector(
|
2024-09-26 14:29:04 +08:00
|
|
|
openLogs: config.appSetting.openLogs,
|
2025-02-03 23:32:00 +08:00
|
|
|
hasProxies: group.isNotEmpty && hasProfile,
|
2024-04-30 23:38:49 +08:00
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
builder: (context, state, child) {
|
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback(
|
|
|
|
|
(_) {
|
2024-05-11 17:02:34 +08:00
|
|
|
globalState.appController.appState.navigationItems =
|
2024-04-30 23:38:49 +08:00
|
|
|
navigation.getItems(
|
|
|
|
|
openLogs: state.openLogs,
|
|
|
|
|
hasProxies: state.hasProxies,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
return child!;
|
|
|
|
|
},
|
|
|
|
|
child: child,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-27 16:59:23 +08:00
|
|
|
_cacheStateChange(Widget child) {
|
|
|
|
|
return Selector2<Config, ClashConfig, String>(
|
|
|
|
|
selector: (_, config, clashConfig) => "$clashConfig $config",
|
|
|
|
|
shouldRebuild: (prev, next) {
|
|
|
|
|
if (prev != next) {
|
|
|
|
|
globalState.appController.savePreferencesDebounce();
|
|
|
|
|
}
|
|
|
|
|
return prev != next;
|
|
|
|
|
},
|
|
|
|
|
builder: (context, state, child) {
|
|
|
|
|
return child!;
|
|
|
|
|
},
|
|
|
|
|
child: child,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-08 21:21:21 +08:00
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
WidgetsBinding.instance.addObserver(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
WidgetsBinding.instance.removeObserver(this);
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
|
2025-01-13 19:08:17 +08:00
|
|
|
if (state == AppLifecycleState.paused ||
|
|
|
|
|
state == AppLifecycleState.inactive) {
|
2024-10-27 16:59:23 +08:00
|
|
|
globalState.appController.savePreferencesDebounce();
|
2025-01-13 19:08:17 +08:00
|
|
|
render?.pause();
|
|
|
|
|
} else {
|
|
|
|
|
render?.resume();
|
2024-09-08 21:21:21 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void didChangePlatformBrightness() {
|
|
|
|
|
globalState.appController.appState.brightness =
|
|
|
|
|
WidgetsBinding.instance.platformDispatcher.platformBrightness;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 23:38:49 +08:00
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2025-01-13 19:08:17 +08:00
|
|
|
return Listener(
|
2025-02-03 23:32:00 +08:00
|
|
|
onPointerHover: (_) {
|
2025-01-13 19:08:17 +08:00
|
|
|
render?.resume();
|
|
|
|
|
},
|
|
|
|
|
child: _cacheStateChange(
|
|
|
|
|
_updateNavigationsContainer(
|
|
|
|
|
widget.child,
|
|
|
|
|
),
|
2024-10-27 16:59:23 +08:00
|
|
|
),
|
2024-04-30 23:38:49 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|