Skip to content

refactor: 提取Dio单例,常量#5

Merged
lxdklp merged 24 commits intolxdklp:mainfrom
futurw4v:refactor/constants
Feb 12, 2026
Merged

refactor: 提取Dio单例,常量#5
lxdklp merged 24 commits intolxdklp:mainfrom
futurw4v:refactor/constants

Conversation

@futurw4v
Copy link
Contributor

@futurw4v futurw4v commented Feb 11, 2026

一些小重构

  1. 添加了一个存放常量的constants.dart,提取了部分重复常量
  2. 提取了一个DioClient单例,统一输出日志,设置UA等便于管理,替换创建Dio实例为调用DioClient

(自动格式化)

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

该 PR 主要做了网络层与常量的重构:引入统一的 DioClient 单例来集中管理 Dio 配置/日志/UA,并新增 constants.dart 抽取重复字符串与常量,以减少各处重复代码并便于后续维护。

Changes:

  • 新增 lib/constants.dart,集中管理应用名、URL、路由、字体字重等常量。
  • 新增 lib/function/dio_client.dart(Dio 单例),并将大量网络请求从“临时创建 Dio”迁移为复用 DioClient().dio
  • 多处页面/逻辑做了格式化与小幅重构(UI 文案/URL 常量替换、代码排版等)。

Reviewed changes

Copilot reviewed 33 out of 33 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
macos/Flutter/GeneratedPluginRegistrant.swift 注册新增的 path_provider_foundation 插件。
lib/pages/setting/theme.dart 适配 FMLBaseApp,并做格式化。
lib/pages/setting/about.dart 使用 constants.dart 中的 app 名与 URL 常量,减少硬编码。
lib/pages/online/member.dart 主要为格式化与日志输出排版调整。
lib/pages/online.dart 通过 DioClient 统一进行 GitHub API 请求,移除本地 UA 拼接逻辑。
lib/pages/home.dart 仅格式化导航代码。
lib/pages/download/modrinth/type/shader.dart Modrinth 请求改为 DioClient,并做格式化。
lib/pages/download/modrinth/type/resourcepack.dart 同上,改用 DioClient 并格式化。
lib/pages/download/modrinth/type/modpack.dart 同上,改用 DioClient
lib/pages/download/modrinth/type/mod.dart 同上,改用 DioClient 并格式化。
lib/pages/download/modrinth/info.dart 移除版本读取/自定义 UA,改用 DioClient
lib/pages/download/download_version/download_game.dart BMCLAPI 请求改为 DioClient,移除版本读取逻辑。
lib/pages/download/download_version.dart 版本清单请求改为 DioClient,移除版本读取逻辑。
lib/pages/download/download_resources.dart 资源列表请求改为 DioClient,请求头简化。
lib/pages/download/download_modrinth.dart Modrinth 请求改为 DioClient,移除版本读取逻辑。
lib/pages/download/download_curseforge.dart CurseForge 请求改为 DioClient,请求头简化。
lib/pages/download/curseforge/type/shader.dart CurseForge 文件列表请求改为 DioClient(仍保留 _appVersion/UA 拼接)。
lib/pages/download/curseforge/type/resourcepack.dart CurseForge 文件列表请求改为 DioClient,移除版本读取/UA 拼接。
lib/pages/download/curseforge/type/modpack.dart CurseForge 文件列表请求改为 DioClient,移除版本读取/UA 拼接。
lib/pages/download/curseforge/type/mod.dart CurseForge 文件列表请求改为 DioClient,移除版本读取/UA 拼接。
lib/pages/download/curseforge/type/download_modpack/loader/curseforge_fabric_modpack.dart 多处请求改为 DioClient,并进行大量格式化。
lib/pages/download/curseforge/info.dart 项目详情/描述请求改为 DioClient,简化请求头。
lib/main.dart 引入 FMLBaseApp 命名与常量替换、接入 DioClient,并调整部分 URL/路由常量。
lib/function/launcher/login/microsoft_login.dart 登录相关请求改为 DioClient,减少重复 UA 拼接。
lib/function/launcher/login/external_login.dart 外置登录/校验相关请求改为 DioClient
lib/function/download.dart 下载逻辑改为复用 DioClient().dio,移除原先专用 Dio/HttpClientAdapter 配置。
lib/function/dio_client.dart 新增 Dio 单例封装(超时、UA、调试日志拦截)。
lib/function/account/microsoft.dart 微软账号设备码/令牌请求改为 DioClient
lib/function/account/external.dart 外置登录认证请求改为 DioClient 并格式化。
lib/constants.dart 新增全局常量集中定义(App 名、URL、路由、MethodChannel 等)。
Comments suppressed due to low confidence (1)

lib/main.dart:214

  • onGenerateRoute casts settings.arguments to both int and String, which will throw at runtime if this route is ever used. If this route is needed, pass a single arguments object (e.g. a Map/record/class) and decode it accordingly; if it’s unused (current navigation uses SlidePageRoute directly), consider removing this route branch to avoid a latent crash.
        if (settings.name == kOnlineOwnerRoute) {
          final int port = settings.arguments as int;
          final String etServer = settings.arguments as String;
          return SlidePageRoute(
            page: OwnerPage(port: port, etServer: etServer),

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 33 out of 33 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

if (existingUserAgent == null ||
(existingUserAgent is String && existingUserAgent.isEmpty)) {
final userAgent =
'$kAppNameAbb/${Platform.operatingSystem}/$_appVersion ${kDebugMode ? 'debug' : ''}';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ua不能被固定为'$kAppNameAbb/${Platform.operatingSystem}/$_appVersion ${kDebugMode ? 'debug' : ''}'; 不同的平台有不同的ua规范,如
bmclapi、LittleSkin: 访问BMCLAPI时,使用${NAME}/${VERSION}作为UA的开头
modrinth: 最佳:User-Agent: github_username/project_name/1.56.0 (launcher.com)或User-Agent: github_username/project_name/1.56.0 (contact@launcher.com)
以及我自己的api也要根据ua进行统计:FML/${Platform.operatingSystem}/$appVersion
虽然说ua不规范也不影响使用,但是用到了人家的东西,还是按照人家的规范来吧 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉NAME/VERSION是最通用的了,固定ua主要是避免重复造轮子

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉NAME/VERSION是最通用的了,固定ua主要是避免重复造轮子

ua最好默认为modrinth格式,然后bmclapi、LittleSkin以及我自己的API处再写入特别的ua

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉NAME/VERSION是最通用的了,固定ua主要是避免重复造轮子

ua最好默认为modrinth格式,然后bmclapi、LittleSkin以及我自己的API处再写入特别的ua

我存到静态变量里然后给它改成了类似FML/1.8.0了,有需求再提 <3

@futurw4v
Copy link
Contributor Author

还有一个,我看到version存在了shared preferences,读取也是用shared preferences读的,这个为啥

@lxdklp
Copy link
Owner

lxdklp commented Feb 12, 2026

还有一个,我看到version存在了shared preferences,读取也是用shared preferences读的,这个为啥

最开始只是想shared preferences方便一点,不过后面我看从PackageInfo和shared preferences代码数量差不多,但是也懒得改了,新的代码直接用PackageInfo就行了

@futurw4v
Copy link
Contributor Author

还有一个,我看到version存在了shared preferences,读取也是用shared preferences读的,这个为啥

最开始只是想shared preferences方便一点,不过后面我看从PackageInfo和shared preferences代码数量差不多,但是也懒得改了,新的代码直接用PackageInfo就行了

其实换成PackageInfo也会有同样的问题,因为也是Future,比如在download.dart:96这种并发量大的工具类里,如果每个任务都去await获取版本可能导致竞态和性能问题,我晚点搞个静态变量

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 35 out of 35 changed files in this pull request and generated 3 comments.

Comments suppressed due to low confidence (1)

lib/main.dart:208

  • onGenerateRoute里把 settings.arguments 同时强转成 intString(同一个对象)会在运行时直接抛 TypeError。如果需要同时传 portetServer,建议把 arguments 设计成 Map/record/自定义对象(例如 {port: ..., etServer: ...}),并分别取值;或者移除这段路由逻辑,继续使用当前的 SlidePageRoute(page: OwnerPage(...)) 方式。
        if (settings.name == kOnlineOwnerRoute) {
          final int port = settings.arguments as int;
          final String etServer = settings.arguments as String;
          return SlidePageRoute(
            page: OwnerPage(port: port, etServer: etServer),
          );

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 35 out of 35 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

// 登录
Future<String> login(String refreshToken) async {
LogUtil.log(refreshToken,level: 'INFO');
LogUtil.log(refreshToken, level: 'INFO');
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里把 refreshToken 以 INFO 级别写入日志会泄露敏感凭据(本地日志/崩溃上报/截图等场景都可能暴露)。建议删除该日志或至少打码(仅记录前后少量字符)并确保默认不落盘。

Copilot uses AI. Check for mistakes.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个我还是之前写微软登录功能时拿来debug用的,我居然忘了删(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个我还是之前写微软登录功能时拿来debug用的,我居然忘了删(

刚刚没看到 这个还没改也

Comment on lines 204 to 205
final int port = settings.arguments as int;
final String etServer = settings.arguments as String;
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onGenerateRoute里把 settings.arguments 同时强转为 intString(同一个对象)会在运行时直接抛 TypeError。建议把 arguments 统一封装成一个对象(例如 Map/record/专用参数类),再分别取 portetServer;或者如果当前没有命名路由调用方,直接移除这段路由分支避免潜在崩溃。

Suggested change
final int port = settings.arguments as int;
final String etServer = settings.arguments as String;
final args = settings.arguments as Map<String, dynamic>;
final int port = args['port'] as int;
final String etServer = args['etServer'] as String;

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以直接把onGenerateRoute直接删掉其实,项目里的路由跳转都是匿名路由,这个只和命名路由有关系

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@dvdamghier dvdamghier requested a review from Copilot February 12, 2026 15:55
@lxdklp lxdklp merged commit e6b96d0 into lxdklp:main Feb 12, 2026
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants