2024-04-30 23:38:49 +08:00
|
|
|
import 'package:fl_clash/clash/clash.dart';
|
2024-12-09 01:40:39 +08:00
|
|
|
import 'package:fl_clash/common/common.dart';
|
2024-08-23 18:33:40 +08:00
|
|
|
import 'package:fl_clash/enum/enum.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
import 'package:fl_clash/models/models.dart';
|
2025-02-09 18:39:38 +08:00
|
|
|
import 'package:fl_clash/providers/app.dart';
|
|
|
|
|
import 'package:fl_clash/providers/config.dart';
|
|
|
|
|
import 'package:fl_clash/providers/state.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
import 'package:fl_clash/state.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
2025-02-09 18:39:38 +08:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
|
2025-02-09 18:39:38 +08:00
|
|
|
class ClashManager extends ConsumerStatefulWidget {
|
2024-04-30 23:38:49 +08:00
|
|
|
final Widget child;
|
|
|
|
|
|
2024-09-08 21:21:21 +08:00
|
|
|
const ClashManager({
|
2024-04-30 23:38:49 +08:00
|
|
|
super.key,
|
|
|
|
|
required this.child,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@override
|
2025-02-09 18:39:38 +08:00
|
|
|
ConsumerState<ClashManager> createState() => _ClashContainerState();
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
|
2025-02-09 18:39:38 +08:00
|
|
|
class _ClashContainerState extends ConsumerState<ClashManager>
|
|
|
|
|
with AppMessageListener {
|
2024-04-30 23:38:49 +08:00
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2025-02-09 18:39:38 +08:00
|
|
|
return widget.child;
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
clashMessage.addListener(this);
|
2025-02-09 18:39:38 +08:00
|
|
|
ref.listenManual(currentProfileIdProvider, (prev, next) {
|
|
|
|
|
if (prev != next) {
|
|
|
|
|
globalState.appController.handleChangeProfile();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
ref.listenManual(coreStateProvider, (prev, next) async {
|
|
|
|
|
if (prev != next) {
|
|
|
|
|
await clashCore.setState(next);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
ref.listenManual(clashConfigStateProvider, (prev, next) {
|
|
|
|
|
if (prev != next) {
|
|
|
|
|
globalState.appController.updateClashConfigDebounce();
|
|
|
|
|
}
|
|
|
|
|
});
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<void> dispose() async {
|
|
|
|
|
clashMessage.removeListener(this);
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
2024-08-05 19:25:35 +08:00
|
|
|
Future<void> onDelay(Delay delay) async {
|
2025-01-13 19:08:17 +08:00
|
|
|
super.onDelay(delay);
|
2024-05-11 17:02:34 +08:00
|
|
|
final appController = globalState.appController;
|
2024-05-06 19:03:49 +08:00
|
|
|
appController.setDelay(delay);
|
2024-12-09 01:40:39 +08:00
|
|
|
debouncer.call(
|
|
|
|
|
DebounceTag.updateDelay,
|
|
|
|
|
() async {
|
|
|
|
|
await appController.updateGroupsDebounce();
|
|
|
|
|
},
|
|
|
|
|
duration: const Duration(milliseconds: 5000),
|
|
|
|
|
);
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void onLog(Log log) {
|
2025-02-09 18:39:38 +08:00
|
|
|
ref.watch(logsProvider.notifier).addLog(log);
|
2024-08-23 18:33:40 +08:00
|
|
|
if (log.logLevel == LogLevel.error) {
|
2024-12-09 01:40:39 +08:00
|
|
|
globalState.showNotifier(log.payload ?? '');
|
2024-08-23 18:33:40 +08:00
|
|
|
}
|
2024-04-30 23:38:49 +08:00
|
|
|
super.onLog(log);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-13 23:43:42 +08:00
|
|
|
@override
|
|
|
|
|
void onRequest(Connection connection) async {
|
2025-02-09 18:39:38 +08:00
|
|
|
ref.watch(requestsProvider.notifier).addRequest(connection);
|
2024-06-13 23:43:42 +08:00
|
|
|
super.onRequest(connection);
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
2024-07-02 08:08:31 +08:00
|
|
|
|
|
|
|
|
@override
|
2024-10-14 10:03:23 +08:00
|
|
|
Future<void> onLoaded(String providerName) async {
|
2025-02-09 18:39:38 +08:00
|
|
|
ref.watch(providersProvider.notifier).setProvider(
|
|
|
|
|
await clashCore.getExternalProvider(
|
|
|
|
|
providerName,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
await globalState.appController.updateGroupsDebounce();
|
2024-08-04 08:21:14 +08:00
|
|
|
super.onLoaded(providerName);
|
2024-07-02 08:08:31 +08:00
|
|
|
}
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|