diff --git a/lib/kit/core/build.dart b/lib/kit/core/build.dart deleted file mode 100644 index ad051ec..0000000 --- a/lib/kit/core/build.dart +++ /dev/null @@ -1,21 +0,0 @@ -enum BuildMode { - release, - debug, - profile; - - static final isDebug = _buildMode == BuildMode.debug; - static final isProfile = _buildMode == BuildMode.profile; - static final isRelease = _buildMode == BuildMode.release; -} - -final _buildMode = () { - if (const bool.fromEnvironment('dart.vm.product')) { - return BuildMode.release; - } - var result = BuildMode.profile; - assert(() { - result = BuildMode.debug; - return true; - }()); - return result; -}(); diff --git a/lib/kit/core/ext/obj.dart b/lib/kit/core/ext/obj.dart index 09c25a3..6636b32 100644 --- a/lib/kit/core/ext/obj.dart +++ b/lib/kit/core/ext/obj.dart @@ -5,9 +5,5 @@ extension ObjectX on T { } extension ObjectXNullable on T? { - A? nullOr(A Function(T) f) => this != null ? f(this!) : null; - VNode get vn => VNode(this); } - -VNode nvn() => VNode(null); diff --git a/lib/kit/core/ext/string.dart b/lib/kit/core/ext/string.dart deleted file mode 100644 index 13be728..0000000 --- a/lib/kit/core/ext/string.dart +++ /dev/null @@ -1,7 +0,0 @@ -import 'package:url_launcher/url_launcher_string.dart'; - -extension StringX on String { - Future launchUrl() async { - return await launchUrlString(this); - } -} diff --git a/lib/kit/core/func.dart b/lib/kit/core/func.dart deleted file mode 100644 index 5ffc3a1..0000000 --- a/lib/kit/core/func.dart +++ /dev/null @@ -1,23 +0,0 @@ -import 'dart:async'; - -abstract final class Fns { - static const _defaultDurationTime = 377; - static const _defaultThrottleId = 'default'; - static final startTimeMap = {_defaultThrottleId: 0}; - - static FutureOr throttle( - FutureOr Function() func, { - String id = _defaultThrottleId, - int duration = _defaultDurationTime, - void Function()? continueClick, - }) async { - final currentTime = DateTime.now().millisecondsSinceEpoch; - if (currentTime - (startTimeMap[id] ?? 0) > duration) { - startTimeMap[id] = DateTime.now().millisecondsSinceEpoch; - return await func(); - } else { - continueClick?.call(); - return null; - } - } -} diff --git a/lib/kit/core/logger.dart b/lib/kit/core/logger.dart index 43d5dde..ce38ab6 100644 --- a/lib/kit/core/logger.dart +++ b/lib/kit/core/logger.dart @@ -1,15 +1,6 @@ import 'dart:io'; -import 'package:logging/logging.dart'; - -import 'build.dart'; - abstract final class Loggers { - static final root = Logger('Root'); - static final store = Logger('Store'); - static final route = Logger('Route'); - static final app = Logger('App'); - static final sourceReg = RegExp(r'\((.+):(\d+):(\d+)\)'); static void log(Object message, {int skipFrames = 1}) { @@ -35,31 +26,3 @@ abstract final class Loggers { print(message); } } - -void dprint(Object? msg, [Object? msg2, Object? msg3, Object? msg4]) { - if (!BuildMode.isDebug) return; - lprint(msg, msg2, msg3, msg4, 3); -} - -void lprint( - Object? msg, [ - Object? msg2, - Object? msg3, - Object? msg4, - int skipFrames = 2, -]) { - final sb = StringBuffer(); - sb.write(msg.toString()); - - if (msg2 != null) { - sb.write('\n$msg2'); - if (msg3 != null) { - sb.write('\n$msg3'); - if (msg4 != null) { - sb.write('\n$msg4'); - } - } - } - final str = sb.toString(); - Loggers.log(str, skipFrames: skipFrames); -} diff --git a/lib/kit/core/platform.dart b/lib/kit/core/platform.dart index 476f334..693f692 100644 --- a/lib/kit/core/platform.dart +++ b/lib/kit/core/platform.dart @@ -38,12 +38,8 @@ enum Pfs { }(); } -final isAndroid = Pfs.type == Pfs.android; -final isIOS = Pfs.type == Pfs.ios; final isLinux = Pfs.type == Pfs.linux; final isMacOS = Pfs.type == Pfs.macos; final isWindows = Pfs.type == Pfs.windows; -final isWeb = Pfs.type == Pfs.web; -final isMobile = Pfs.type == Pfs.ios || Pfs.type == Pfs.android; final isDesktop = Pfs.type == Pfs.linux || Pfs.type == Pfs.macos || Pfs.type == Pfs.windows; diff --git a/lib/kit/core/rnode.dart b/lib/kit/core/rnode.dart index 1117271..77291f1 100644 --- a/lib/kit/core/rnode.dart +++ b/lib/kit/core/rnode.dart @@ -50,12 +50,6 @@ class RNode implements ChangeNotifier { } } -extension RNodeX on RNode { - ListenBuilder listen(Widget Function() builder) { - return ListenBuilder(listenable: this, builder: builder); - } -} - class VNode extends RNode implements ValueNotifier { T _value; @@ -74,45 +68,3 @@ class VNode extends RNode implements ValueNotifier { @override String toString() => 'VNode($value)'; } - -extension ValueListenableX on ValueListenable { - ValBuilder listenVal(Widget Function(T) builder) { - return ValBuilder(listenable: this, builder: builder); - } -} - -final class ValBuilder extends ValueListenableBuilder { - final ValueListenable listenable; - - ValBuilder({ - super.key, - required this.listenable, - required Widget Function(T) builder, - }) : super(valueListenable: listenable, builder: (_, val, _) => builder(val)); -} - -final class ListenBuilder extends ListenableBuilder { - ListenBuilder({ - super.key, - required super.listenable, - required Widget Function() builder, - }) : super(builder: (_, _) => builder()); -} - -final class EmptyListenable implements ValueListenable { - @override - void addListener(VoidCallback listener) {} - - @override - void removeListener(VoidCallback listener) {} - - @override - T? get value => null; - - const EmptyListenable(); -} - -abstract final class RNodes { - static final app = RNode(); - static final dark = VNode(false); -} diff --git a/lib/kit/kit.dart b/lib/kit/kit.dart index fd07b76..a633916 100644 --- a/lib/kit/kit.dart +++ b/lib/kit/kit.dart @@ -1,11 +1,8 @@ library; -export 'core/build.dart'; export 'core/ext/datetime.dart'; export 'core/ext/obj.dart'; -export 'core/ext/string.dart'; export 'core/ext/widget.dart'; -export 'core/func.dart'; export 'core/logger.dart'; export 'core/platform.dart'; export 'core/rnode.dart'; diff --git a/lib/kit/res/ui.dart b/lib/kit/res/ui.dart index 5e87635..ac10599 100644 --- a/lib/kit/res/ui.dart +++ b/lib/kit/res/ui.dart @@ -1,55 +1,10 @@ import 'package:flutter/material.dart'; abstract final class UIs { - static const text11 = TextStyle(fontSize: 11); - static const text11Bold = TextStyle( - fontSize: 11, - fontWeight: FontWeight.w500, - ); - static const text11Grey = TextStyle(color: Colors.grey, fontSize: 11); - static const text12 = TextStyle(fontSize: 12); - static const text12Bold = TextStyle( - fontSize: 12, - fontWeight: FontWeight.bold, - ); - static const text12Grey = TextStyle(color: Colors.grey, fontSize: 12); - static const text13 = TextStyle(fontSize: 13); - static const text13Bold = TextStyle( - fontSize: 13, - fontWeight: FontWeight.bold, - ); - static const text13Grey = TextStyle(color: Colors.grey, fontSize: 13); static const text15 = TextStyle(fontSize: 15); - static const text15Bold = TextStyle( - fontSize: 15, - fontWeight: FontWeight.bold, - ); - static const text18 = TextStyle(fontSize: 18); - static const text27 = TextStyle(fontSize: 27); static const textGrey = TextStyle(color: Colors.grey); static const textRed = TextStyle(color: Colors.red); static const placeholder = SizedBox(); - static const height7 = SizedBox(height: 7); static const height13 = SizedBox(height: 13); - static const height77 = SizedBox(height: 77); - static const width7 = SizedBox(width: 7); - static const width13 = SizedBox(width: 13); - - static Widget dot({Color? color, double? size}) => Container( - width: size ?? 7, - height: size ?? 7, - decoration: BoxDecoration( - color: color ?? primaryColor, - shape: BoxShape.circle, - ), - ); - - static const centerLoading = Padding( - padding: EdgeInsets.symmetric(vertical: 7), - child: Center(child: CircularProgressIndicator()), - ); - - static var colorSeed = const Color.fromARGB(255, 72, 15, 15); - static var primaryColor = colorSeed; } diff --git a/lib/kit/widgets/virtual_window_frame.dart b/lib/kit/widgets/virtual_window_frame.dart index 581422f..bd1086d 100644 --- a/lib/kit/widgets/virtual_window_frame.dart +++ b/lib/kit/widgets/virtual_window_frame.dart @@ -5,13 +5,7 @@ import '../core/platform.dart'; import 'appbar.dart'; abstract final class WindowFrameConfig { - static bool _showCaption = true; - - static bool get showCaption => _showCaption && isDesktop; - - static void setShowCaption(bool value) { - _showCaption = value; - } + static bool get showCaption => isDesktop; } class VirtualWindowFrame extends StatelessWidget { diff --git a/lib/pages/download_page/components/task_details_dialog.dart b/lib/pages/download_page/components/task_details_dialog.dart index 48e9754..0b6284a 100644 --- a/lib/pages/download_page/components/task_details_dialog.dart +++ b/lib/pages/download_page/components/task_details_dialog.dart @@ -203,6 +203,8 @@ class TaskDetailsDialog { isLoadingPeers = true; lastPeersFetchTime = now; + List>? peersResult; + String? peersErrorLocal; try { final instanceManager = outerContext .read(); @@ -210,25 +212,31 @@ class TaskDetailsDialog { currentTask.instanceId, ); if (instance == null) { - peersError = l10n.targetInstanceNotConnected; - return; - } - final nextClientKey = - '${instance.id}_${instance.protocol}_${instance.host}_${instance.port}_${instance.secret}'; - if (peersClientKey != nextClientKey || - peersClient == null) { - peersClient?.close(); - peersClient = Aria2RpcClient(instance); - peersClientKey = nextClientKey; + peersErrorLocal = l10n.targetInstanceNotConnected; + } else { + final nextClientKey = + '${instance.id}_${instance.protocol}_${instance.host}_${instance.port}_${instance.secret}'; + if (peersClientKey != nextClientKey || + peersClient == null) { + peersClient?.close(); + peersClient = Aria2RpcClient(instance); + peersClientKey = nextClientKey; + } + peersResult = await peersClient!.getPeers( + currentTask.id, + ); } - peers = await peersClient!.getPeers(currentTask.id); - peersError = null; } catch (error) { - peersError = '$error'; + peersErrorLocal = '$error'; } finally { - isLoadingPeers = false; if (context.mounted) { - setState(() {}); + setState(() { + peersError = peersErrorLocal; + isLoadingPeers = false; + if (peersResult != null) { + peers = peersResult; + } + }); } } } @@ -242,14 +250,13 @@ class TaskDetailsDialog { activeTabController = tabController; activeTabListener = () { if (!tabController.indexIsChanging) { - currentTabIndex = tabController.index; + setState(() { + currentTabIndex = tabController.index; + }); unawaited( requestPeersIfNeeded?.call(force: true) ?? Future.value(), ); - if (context.mounted) { - setState(() {}); - } } }; activeTabController!.addListener(activeTabListener!); diff --git a/lib/pages/download_page/download_page.dart b/lib/pages/download_page/download_page.dart index ce122cc..f45a86d 100644 --- a/lib/pages/download_page/download_page.dart +++ b/lib/pages/download_page/download_page.dart @@ -317,11 +317,9 @@ class DownloadPageState extends State if (downloadDataService == null || _selectedTaskKeys.isEmpty) return; final validKeys = downloadDataService!.tasks.map(_taskKey).toSet(); - final before = _selectedTaskKeys.length; - _selectedTaskKeys.removeWhere((key) => !validKeys.contains(key)); - if (before != _selectedTaskKeys.length && mounted) { - setState(() {}); - } + setState(() { + _selectedTaskKeys.removeWhere((key) => !validKeys.contains(key)); + }); } List _filterTasks() { diff --git a/lib/pages/instance_page/components/instance_dialog.dart b/lib/pages/instance_page/components/instance_dialog.dart index c09ddb9..f9c1d5f 100644 --- a/lib/pages/instance_page/components/instance_dialog.dart +++ b/lib/pages/instance_page/components/instance_dialog.dart @@ -7,9 +7,8 @@ import '../../../services/aria2_rpc_client.dart'; class InstanceDialog extends StatefulWidget { final Aria2Instance? instance; - final void Function(Aria2Instance instance)? onSave; - const InstanceDialog({super.key, this.instance, this.onSave}); + const InstanceDialog({super.key, this.instance}); @override State createState() => _InstanceDialogState(); @@ -240,16 +239,9 @@ class _InstanceDialogState extends State { void _submit() { final instance = _buildDraftInstance(requireName: true); - if (instance == null) { - return; - } + if (instance == null) return; - if (widget.onSave != null) { - widget.onSave!(instance); - if (mounted) { - Navigator.of(context).pop(); - } - } else if (mounted) { + if (mounted) { Navigator.of(context).pop(instance); } } @@ -396,8 +388,6 @@ class _InstanceDialogState extends State { setState(() { _hostError = null; }); - } else { - setState(() {}); } }, decoration: InputDecoration( @@ -418,8 +408,6 @@ class _InstanceDialogState extends State { setState(() { _portError = null; }); - } else { - setState(() {}); } final port = int.tryParse(value); if (port != null) { diff --git a/lib/services/download_data_service.dart b/lib/services/download_data_service.dart index eb32b92..0d83e4b 100644 --- a/lib/services/download_data_service.dart +++ b/lib/services/download_data_service.dart @@ -211,14 +211,15 @@ class DownloadDataService extends ChangeNotifier with Loggable { } } + static const _statusOrder = { + DownloadStatus.active: 0, + DownloadStatus.waiting: 1, + DownloadStatus.stopped: 2, + }; + int _compareTasks(DownloadTask left, DownloadTask right) { - final statusOrder = { - DownloadStatus.active: 0, - DownloadStatus.waiting: 1, - DownloadStatus.stopped: 2, - }; - final leftOrder = statusOrder[left.status] ?? 99; - final rightOrder = statusOrder[right.status] ?? 99; + final leftOrder = _statusOrder[left.status] ?? 99; + final rightOrder = _statusOrder[right.status] ?? 99; if (leftOrder != rightOrder) { return leftOrder.compareTo(rightOrder); } @@ -227,7 +228,13 @@ class DownloadDataService extends ChangeNotifier with Loggable { return left.instanceId.compareTo(right.instanceId); } - return left.name.toLowerCase().compareTo(right.name.toLowerCase()); + return _compareIgnoreCase(left.name, right.name); + } + + static int _compareIgnoreCase(String a, String b) { + final aLower = a.toLowerCase(); + final bLower = b.toLowerCase(); + return aLower.compareTo(bLower); } void _restartTimer() {