Skip to content

Commit

Permalink
Optimize navigator animate
Browse files Browse the repository at this point in the history
Optimize save
  • Loading branch information
chen08209 committed Oct 27, 2024
1 parent 526ccdf commit c7a9159
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 126 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version-file: 'core/go.mod'
go-version: 'stable'
cache-dependency-path: |
core/go.sum
Expand Down
17 changes: 13 additions & 4 deletions lib/application.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'package:animations/animations.dart';
import 'package:dynamic_color/dynamic_color.dart';
import 'package:fl_clash/l10n/l10n.dart';
import 'package:fl_clash/common/common.dart';
Expand Down Expand Up @@ -58,10 +59,18 @@ class ApplicationState extends State<Application> {

final _pageTransitionsTheme = const PageTransitionsTheme(
builders: <TargetPlatform, PageTransitionsBuilder>{
TargetPlatform.android: CupertinoPageTransitionsBuilder(),
TargetPlatform.windows: CupertinoPageTransitionsBuilder(),
TargetPlatform.linux: CupertinoPageTransitionsBuilder(),
TargetPlatform.macOS: CupertinoPageTransitionsBuilder(),
TargetPlatform.android: SharedAxisPageTransitionsBuilder(
transitionType: SharedAxisTransitionType.horizontal,
),
TargetPlatform.windows: SharedAxisPageTransitionsBuilder(
transitionType: SharedAxisTransitionType.horizontal,
),
TargetPlatform.linux: SharedAxisPageTransitionsBuilder(
transitionType: SharedAxisTransitionType.horizontal,
),
TargetPlatform.macOS: SharedAxisPageTransitionsBuilder(
transitionType: SharedAxisTransitionType.horizontal,
),
},
);

Expand Down
3 changes: 2 additions & 1 deletion lib/common/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ export 'scroll.dart';
export 'icons.dart';
export 'http.dart';
export 'keyboard.dart';
export 'network.dart';
export 'network.dart';
export 'navigator.dart';
11 changes: 11 additions & 0 deletions lib/common/navigator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:flutter/material.dart';

class BaseNavigator {
static Future<T?> push<T>(BuildContext context, Widget child) async {
return await Navigator.of(context).push<T>(
MaterialPageRoute(
builder: (context) => child,
),
);
}
}
23 changes: 8 additions & 15 deletions lib/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'dart:typed_data';
import 'package:archive/archive.dart';
import 'package:fl_clash/common/archive.dart';
import 'package:fl_clash/enum/enum.dart';
import 'package:fl_clash/plugins/app.dart';
import 'package:fl_clash/state.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
Expand All @@ -29,6 +30,7 @@ class AppController {
late Function updateGroupDebounce;
late Function addCheckIpNumDebounce;
late Function applyProfileDebounce;
late Function savePreferencesDebounce;

AppController(this.context) {
appState = context.read<AppState>();
Expand All @@ -38,6 +40,9 @@ class AppController {
updateClashConfigDebounce = debounce<Function()>(() async {
await updateClashConfig();
});
savePreferencesDebounce = debounce<Function()>(() async {
await savePreferences();
});
applyProfileDebounce = debounce<Function()>(() async {
await applyProfile(isPrue: true);
});
Expand All @@ -51,10 +56,7 @@ class AppController {

updateStatus(bool isStart) async {
if (isStart) {
await globalState.handleStart(
config: config,
clashConfig: clashConfig,
);
await globalState.handleStart();
updateRunTime();
updateTraffic();
globalState.updateFunctionLists = [
Expand Down Expand Up @@ -202,17 +204,8 @@ class AppController {
}

savePreferences() async {
await saveConfigPreferences();
await saveClashConfigPreferences();
}

saveConfigPreferences() async {
debugPrint("saveConfigPreferences");
debugPrint("[APP] savePreferences");
await preferences.saveConfig(config);
}

saveClashConfigPreferences() async {
debugPrint("saveClashConfigPreferences");
await preferences.saveClashConfig(clashConfig);
}

Expand All @@ -231,7 +224,7 @@ class AppController {
handleBackOrExit() async {
if (config.appSetting.minimizeOnExit) {
if (system.isDesktop) {
await savePreferences();
await savePreferencesDebounce();
}
await system.back();
} else {
Expand Down
27 changes: 16 additions & 11 deletions lib/fragments/profiles/add_profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import 'package:flutter/material.dart';
class AddProfile extends StatelessWidget {
final BuildContext context;

const AddProfile({super.key, required this.context,});
const AddProfile({
super.key,
required this.context,
});

_handleAddProfileFormFile() async {
globalState.appController.addProfileFormFile();
Expand All @@ -18,14 +21,16 @@ class AddProfile extends StatelessWidget {
}

_toScan() async {
if(system.isDesktop){
if (system.isDesktop) {
globalState.appController.addProfileFormQrCode();
return;
}
final url = await Navigator.of(context)
.push<String>(MaterialPageRoute(builder: (_) => const ScanPage()));
final url = await BaseNavigator.push(
context,
const ScanPage(),
);
if (url != null) {
WidgetsBinding.instance.addPostFrameCallback((_){
WidgetsBinding.instance.addPostFrameCallback((_) {
_handleAddProfileFormURL(url);
});
}
Expand All @@ -44,12 +49,12 @@ class AddProfile extends StatelessWidget {
Widget build(context) {
return ListView(
children: [
ListItem(
leading: const Icon(Icons.qr_code),
title: Text(appLocalizations.qrcode),
subtitle: Text(appLocalizations.qrcodeDesc),
onTap: _toScan,
),
ListItem(
leading: const Icon(Icons.qr_code),
title: Text(appLocalizations.qrcode),
subtitle: Text(appLocalizations.qrcodeDesc),
onTap: _toScan,
),
ListItem(
leading: const Icon(Icons.upload_file),
title: Text(appLocalizations.file),
Expand Down
2 changes: 1 addition & 1 deletion lib/fragments/theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class _ThemeColorsBoxState extends State<ThemeColorsBox> {
);
},
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
Expand Down
5 changes: 1 addition & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@ Future<void> vpnService() async {
WidgetsBinding.instance.platformDispatcher.locale,
);
await app?.tip(appLocalizations.startVpn);
await globalState.handleStart(
config: config,
clashConfig: clashConfig,
);
await globalState.handleStart();

tile?.addListener(
TileListenerWithVpn(
Expand Down
27 changes: 23 additions & 4 deletions lib/manager/app_state_manager.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';

import 'package:fl_clash/common/common.dart';
import 'package:fl_clash/models/models.dart';
import 'package:fl_clash/state.dart';
Expand All @@ -18,7 +20,6 @@ class AppStateManager extends StatefulWidget {

class _AppStateManagerState extends State<AppStateManager>
with WidgetsBindingObserver {

_updateNavigationsContainer(Widget child) {
return Selector2<AppState, Config, UpdateNavigationsSelector>(
selector: (_, appState, config) {
Expand All @@ -45,6 +46,22 @@ class _AppStateManagerState extends State<AppStateManager>
);
}

_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,
);
}

@override
void initState() {
super.initState();
Expand All @@ -61,7 +78,7 @@ class _AppStateManagerState extends State<AppStateManager>
Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
final isPaused = state == AppLifecycleState.paused;
if (isPaused) {
await globalState.appController.savePreferences();
globalState.appController.savePreferencesDebounce();
}
}

Expand All @@ -73,8 +90,10 @@ class _AppStateManagerState extends State<AppStateManager>

@override
Widget build(BuildContext context) {
return _updateNavigationsContainer(
widget.child,
return _cacheStateChange(
_updateNavigationsContainer(
widget.child,
),
);
}
}
5 changes: 3 additions & 2 deletions lib/manager/window_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class WindowManager extends StatefulWidget {
State<WindowManager> createState() => _WindowContainerState();
}

class _WindowContainerState extends State<WindowManager> with WindowListener, WindowExtListener {
class _WindowContainerState extends State<WindowManager>
with WindowListener, WindowExtListener {
Function? updateLaunchDebounce;

_autoLaunchContainer(Widget child) {
Expand Down Expand Up @@ -82,7 +83,7 @@ class _WindowContainerState extends State<WindowManager> with WindowListener, Wi

@override
void onWindowMinimize() async {
await globalState.appController.savePreferences();
globalState.appController.savePreferencesDebounce();
super.onWindowMinimize();
}

Expand Down
5 changes: 5 additions & 0 deletions lib/models/clash_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,9 @@ class ClashConfig extends ChangeNotifier {
factory ClashConfig.fromJson(Map<String, dynamic> json) {
return _$ClashConfigFromJson(json);
}

@override
String toString() {
return 'ClashConfig{_mixedPort: $_mixedPort, _allowLan: $_allowLan, _ipv6: $_ipv6, _geodataLoader: $_geodataLoader, _logLevel: $_logLevel, _externalController: $_externalController, _mode: $_mode, _findProcessMode: $_findProcessMode, _keepAliveInterval: $_keepAliveInterval, _unifiedDelay: $_unifiedDelay, _tcpConcurrent: $_tcpConcurrent, _tun: $_tun, _dns: $_dns, _geoXUrl: $_geoXUrl, _rules: $_rules, _globalRealUa: $_globalRealUa, _hosts: $_hosts}';
}
}
8 changes: 7 additions & 1 deletion lib/models/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ class ThemeProps with _$ThemeProps {
@Default(FontFamily.system) FontFamily fontFamily,
}) = _ThemeProps;

factory ThemeProps.fromJson(Map<String, Object?> json) => _$ThemePropsFromJson(json);
factory ThemeProps.fromJson(Map<String, Object?> json) =>
_$ThemePropsFromJson(json);

factory ThemeProps.realFromJson(Map<String, Object?>? json) {
if (json == null) {
Expand Down Expand Up @@ -479,4 +480,9 @@ class Config extends ChangeNotifier {
factory Config.fromJson(Map<String, dynamic> json) {
return _$ConfigFromJson(json);
}

@override
String toString() {
return 'Config{_appSetting: $_appSetting, _profiles: $_profiles, _currentProfileId: $_currentProfileId, _isAccessControl: $_isAccessControl, _accessControl: $_accessControl, _dav: $_dav, _windowProps: $_windowProps, _themeProps: $_themeProps, _vpnProps: $_vpnProps, _desktopProps: $_desktopProps, _overrideDns: $_overrideDns, _hotKeyActions: $_hotKeyActions, _proxiesStyle: $_proxiesStyle}';
}
}
61 changes: 0 additions & 61 deletions lib/router/fade_page.dart

This file was deleted.

7 changes: 1 addition & 6 deletions lib/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,14 @@ class GlobalState {
appState.versionInfo = clashCore.getVersionInfo();
}

handleStart({
required Config config,
required ClashConfig clashConfig,
}) async {
handleStart() async {
clashCore.start();
if (globalState.isVpnService) {
await vpn?.startVpn();
startListenUpdate();
return;
}
startTime ??= DateTime.now();
await preferences.saveClashConfig(clashConfig);
await preferences.saveConfig(config);
await service?.init();
startListenUpdate();
}
Expand Down
Loading

0 comments on commit c7a9159

Please sign in to comment.