Skip to content

Commit 508d2c0

Browse files
CopilotCarGuo
andauthored
Performance optimizations and code quality improvements for GSY GitHub App Flutter (#913)
* Initial plan * Optimize widgets and performance improvements Co-authored-by: CarGuo <[email protected]> * Further optimizations: widgets, config, and dependency management Co-authored-by: CarGuo <[email protected]> * Database optimizations, logger improvements, and documentation Co-authored-by: CarGuo <[email protected]> * Revert requested files to original state and remove OPTIMIZATIONS.md - Reverted .gitignore to original (no additional Android/iOS ignores) - Reverted lib/app.dart to preserve Future.delayed code with Chinese comments - Reverted lib/common/logger.dart to original printLog implementation - Reverted lib/common/utils/common_utils.dart to original state - Reverted pubspec.yaml to original dependencies and structure - Removed OPTIMIZATIONS.md documentation file Per feedback from @CarGuo, keeping only the beneficial widget optimizations and database improvements while preserving original code structure for core files. Co-authored-by: CarGuo <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: CarGuo <[email protected]>
1 parent fe881ae commit 508d2c0

File tree

11 files changed

+389
-289
lines changed

11 files changed

+389
-289
lines changed

analysis_options.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@ analyzer:
88

99
linter:
1010
rules:
11+
# Performance optimizations
12+
avoid_print: true
13+
avoid_returning_null_for_void: true
14+
avoid_unnecessary_containers: true
15+
avoid_web_libraries_in_flutter: true
16+
prefer_const_constructors: true
17+
prefer_const_constructors_in_immutables: true
18+
prefer_const_declarations: true
19+
prefer_const_literals_to_create_immutables: true
20+
prefer_final_fields: true
21+
prefer_final_in_for_each: true
22+
prefer_final_locals: true
23+
24+
# Code quality
25+
require_trailing_commas: true
26+
use_key_in_widget_constructors: true
27+
sized_box_for_whitespace: true
28+
29+
# Existing exceptions
1130
non_constant_identifier_names: false
1231
file_names: false
1332
constant_identifier_names: false

lib/common/config/config.dart

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1+
/// Configuration constants for the application
12
class Config {
2-
static bool? DEBUG = true;
3+
// Private constructor to prevent instantiation
4+
Config._();
35

6+
static const bool DEBUG = true;
7+
static const int PAGE_SIZE = 20;
48

5-
6-
static const PAGE_SIZE = 20;
7-
8-
/// //////////////////////////////////////常量////////////////////////////////////// ///
9-
static const API_TOKEN = "4d65e2a5626103f92a71867d7b49fea0";
10-
static const TOKEN_KEY = "token";
11-
static const USER_NAME_KEY = "user-name";
12-
static const PW_KEY = "user-pw";
13-
static const USER_BASIC_CODE = "user-basic-code";
14-
static const USER_INFO = "user-info";
15-
static const LANGUAGE_SELECT = "language-select";
16-
static const LANGUAGE_SELECT_NAME = "language-select-name";
17-
static const REFRESH_LANGUAGE = "refreshLanguageApp";
18-
static const THEME_COLOR = "theme-color";
19-
static const LOCALE = "locale";
9+
/// API and authentication constants
10+
static const String API_TOKEN = "4d65e2a5626103f92a71867d7b49fea0";
11+
static const String TOKEN_KEY = "token";
12+
static const String USER_NAME_KEY = "user-name";
13+
static const String PW_KEY = "user-pw";
14+
static const String USER_BASIC_CODE = "user-basic-code";
15+
static const String USER_INFO = "user-info";
16+
17+
/// Localization constants
18+
static const String LANGUAGE_SELECT = "language-select";
19+
static const String LANGUAGE_SELECT_NAME = "language-select-name";
20+
static const String REFRESH_LANGUAGE = "refreshLanguageApp";
21+
static const String LOCALE = "locale";
22+
23+
/// Theme constants
24+
static const String THEME_COLOR = "theme-color";
2025
}

lib/common/net/api.dart

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,27 @@ import 'package:gsy_github_app_flutter/common/net/result_data.dart';
1313

1414
///http请求
1515
class HttpManager {
16-
static const CONTENT_TYPE_JSON = "application/json";
17-
static const CONTENT_TYPE_FORM = "application/x-www-form-urlencoded";
18-
19-
final Dio _dio = Dio(); // 使用默认配置
20-
21-
final TokenInterceptors _tokenInterceptors = TokenInterceptors();
22-
23-
HttpManager() {
24-
_dio.interceptors.add(HeaderInterceptors());
25-
26-
_dio.interceptors.add(_tokenInterceptors);
27-
28-
_dio.interceptors.add(LogsInterceptors());
29-
30-
_dio.interceptors.add(ErrorInterceptors());
31-
32-
_dio.interceptors.add(ResponseInterceptors());
16+
static const String CONTENT_TYPE_JSON = "application/json";
17+
static const String CONTENT_TYPE_FORM = "application/x-www-form-urlencoded";
18+
19+
late final Dio _dio;
20+
late final TokenInterceptors _tokenInterceptors;
21+
22+
HttpManager._internal() {
23+
_dio = Dio(); // 使用默认配置
24+
_tokenInterceptors = TokenInterceptors();
25+
26+
_dio.interceptors.addAll([
27+
HeaderInterceptors(),
28+
_tokenInterceptors,
29+
LogsInterceptors(),
30+
ErrorInterceptors(),
31+
ResponseInterceptors(),
32+
]);
3333
}
3434

35+
static final HttpManager _instance = HttpManager._internal();
36+
3537
///发起网络请求
3638
///[ url] 请求url
3739
///[ params] 请求参数
@@ -91,6 +93,9 @@ class HttpManager {
9193
getAuthorization() async {
9294
return _tokenInterceptors.getAuthorization();
9395
}
96+
97+
/// 提供单例访问
98+
static HttpManager get instance => _instance;
9499
}
95100

96-
final HttpManager httpManager = HttpManager();
101+
final HttpManager httpManager = HttpManager.instance;

lib/db/sql_manager.dart

Lines changed: 69 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,56 +8,92 @@ import 'package:sqflite/sqflite.dart';
88
/// 数据库管理
99
/// Created by guoshuyu
1010
/// Date: 2018-08-03
11-
1211
class SqlManager {
13-
static const _VERSION = 1;
14-
15-
static const _NAME = "gsy_github_app_flutter.db";
12+
static const int _VERSION = 1;
13+
static const String _NAME = "gsy_github_app_flutter.db";
1614

1715
static Database? _database;
16+
static String? _currentDbPath;
17+
static final Completer<Database> _dbCompleter = Completer<Database>();
18+
static bool _isInitializing = false;
1819

19-
///初始化
20-
static init() async {
21-
// open the database
22-
var databasesPath = await getDatabasesPath();
23-
var userRes = await UserRepository.getUserInfoLocal();
24-
String dbName = _NAME;
25-
if (userRes != null && userRes.result) {
26-
User? user = userRes.data;
27-
if (user != null && user.login != null) {
28-
dbName = "${user.login!}_$_NAME";
29-
}
30-
}
31-
String path = databasesPath + dbName;
32-
if (Platform.isIOS) {
33-
path = "$databasesPath/$dbName";
20+
/// 初始化数据库 - 使用单例模式避免重复初始化
21+
static Future<void> init() async {
22+
if (_database != null) return;
23+
if (_isInitializing) {
24+
await _dbCompleter.future;
25+
return;
3426
}
35-
_database = await openDatabase(path, version: _VERSION,
27+
28+
_isInitializing = true;
29+
30+
try {
31+
final databasesPath = await getDatabasesPath();
32+
final userRes = await UserRepository.getUserInfoLocal();
33+
34+
String dbName = _NAME;
35+
if (userRes?.result == true) {
36+
final user = userRes!.data as User?;
37+
if (user?.login != null) {
38+
dbName = "${user!.login!}_$_NAME";
39+
}
40+
}
41+
42+
final path = Platform.isIOS
43+
? "$databasesPath/$dbName"
44+
: databasesPath + dbName;
45+
46+
_currentDbPath = path;
47+
48+
_database = await openDatabase(
49+
path,
50+
version: _VERSION,
3651
onCreate: (Database db, int version) async {
37-
// When creating the db, create the table
38-
//await db.execute("CREATE TABLE Test (id INTEGER PRIMARY KEY, name TEXT, value INTEGER, num REAL)");
39-
});
52+
// Database creation logic can be added here
53+
},
54+
);
55+
56+
if (!_dbCompleter.isCompleted) {
57+
_dbCompleter.complete(_database!);
58+
}
59+
} catch (e) {
60+
if (!_dbCompleter.isCompleted) {
61+
_dbCompleter.completeError(e);
62+
}
63+
rethrow;
64+
} finally {
65+
_isInitializing = false;
66+
}
4067
}
4168

42-
/// 表是否存在
43-
static isTableExits(String tableName) async {
44-
await getCurrentDatabase();
45-
var res = await _database?.rawQuery(
46-
"select * from Sqlite_master where type = 'table' and name = '$tableName'");
47-
return res != null && res.isNotEmpty;
69+
/// 检查表是否存在 - 优化查询性能
70+
static Future<bool> isTableExits(String tableName) async {
71+
final database = await getCurrentDatabase();
72+
if (database == null) return false;
73+
74+
final res = await database.rawQuery(
75+
"SELECT name FROM sqlite_master WHERE type='table' AND name=?",
76+
[tableName],
77+
);
78+
return res.isNotEmpty;
4879
}
4980

50-
///获取当前数据库对象
81+
/// 获取当前数据库对象
5182
static Future<Database?> getCurrentDatabase() async {
5283
if (_database == null) {
5384
await init();
5485
}
5586
return _database;
5687
}
5788

58-
///关闭
59-
static close() {
60-
_database?.close();
89+
/// 关闭数据库连接
90+
static Future<void> close() async {
91+
await _database?.close();
6192
_database = null;
93+
_currentDbPath = null;
6294
}
95+
96+
/// 获取当前数据库路径(用于调试)
97+
static String? get currentDbPath => _currentDbPath;
98+
}
6399
}

lib/db/sql_provider.dart

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,50 @@
11
import 'dart:async';
2-
3-
/**
4-
* 数据库表
5-
* Created by guoshuyu
6-
* Date: 2018-08-03
7-
*/
82
import 'package:gsy_github_app_flutter/db/sql_manager.dart';
93
import 'package:meta/meta.dart';
104
import 'package:sqflite/sqflite.dart';
115

12-
///基类
6+
/// 数据库表基类
7+
/// Created by guoshuyu
8+
/// Date: 2018-08-03
139
abstract class BaseDbProvider {
1410
bool isTableExits = false;
1511

16-
tableSqlString();
12+
/// 子类需要实现的表结构SQL
13+
String tableSqlString();
1714

18-
tableName();
15+
/// 子类需要实现的表名
16+
String tableName();
1917

20-
tableBaseString(String name, String columnId) {
18+
/// 生成基础表结构字符串
19+
@protected
20+
String tableBaseString(String name, String columnId) {
2121
return '''
2222
create table $name (
2323
$columnId integer primary key autoincrement,
2424
''';
2525
}
2626

27-
Future<Database> getDataBase() async {
27+
/// 获取数据库连接
28+
Future<Database?> getDataBase() async {
2829
return await open();
2930
}
3031

32+
/// 准备表结构 - 如果表不存在则创建
3133
@mustCallSuper
32-
prepare(name, String? createSql) async {
34+
Future<void> prepare(String name, String? createSql) async {
35+
if (createSql == null) return;
36+
3337
isTableExits = await SqlManager.isTableExits(name);
3438
if (!isTableExits) {
35-
Database? db = await SqlManager.getCurrentDatabase();
36-
return await db?.execute(createSql!);
39+
final db = await SqlManager.getCurrentDatabase();
40+
await db?.execute(createSql);
41+
isTableExits = true;
3742
}
3843
}
3944

45+
/// 打开数据库连接并确保表存在
4046
@mustCallSuper
41-
open() async {
47+
Future<Database?> open() async {
4248
if (!isTableExits) {
4349
await prepare(tableName(), tableSqlString());
4450
}

0 commit comments

Comments
 (0)