Skip to content

Commit

Permalink
refactor: clean redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
MuZhou233 committed Feb 16, 2024
1 parent 25d0ea5 commit 80b44ba
Show file tree
Hide file tree
Showing 22 changed files with 333 additions and 828 deletions.
3 changes: 3 additions & 0 deletions l10n_arb/intl_zh_CN.arb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"continueInWebVersion": "继续使用网页版",
"changeServer": "切换服务器",
"continueInCurrentServer": "继续使用",
"loadFailed": "加载失败{reason}",

"durationJustNow": "刚刚",
"durationSeconds": "{number} 秒前",
Expand Down Expand Up @@ -52,6 +53,8 @@
"uploadImageFailed": "上传图像失败",

"gebura": "Gebura",
"store": "商店",
"library": "库",
"pleaseSetupApplicationPath": "请设置应用程序路径",
"pleaseDontReRunApplication": "请不要重复运行应用程序",
"applicationExitAbnormally": "应用程序异常退出",
Expand Down
1 change: 1 addition & 0 deletions lib/bloc/deeplink_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class DeepLinkConnectState extends DeepLinkState {
DeepLinkConnectState(this.serverConfig);
}

// DeepLinkBloc manages deep link events and states.
class DeepLinkBloc extends Bloc<DeepLinkEvent, DeepLinkState> {
StreamSubscription? _sub;

Expand Down
2 changes: 2 additions & 0 deletions lib/bloc/main_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import 'yesod/yesod_bloc.dart';
part 'main_event.dart';
part 'main_state.dart';

// MainBloc manage the basic state of the app,
// including login, logout, and server switching.
class MainBloc extends Bloc<MainEvent, MainState> {
final ApiHelper _api;
final ClientCommonRepo _repo;
Expand Down
8 changes: 4 additions & 4 deletions lib/init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Future<MyApp> init() async {
final api = ApiHelper();

// system tray
await initSystemTray();
await _initSystemTray();

WidgetsFlutterBinding.ensureInitialized();

Expand All @@ -39,7 +39,7 @@ Future<MyApp> init() async {
Bloc.observer = SimpleBlocObserver();
}
final packageInfo = await PackageInfo.fromPlatform();
final deviceInfo = await genClientDeviceInfo();
final deviceInfo = await _genClientDeviceInfo();
final clientSettingBloc = ClientSettingBloc(common);
final deepLinkBloc = DeepLinkBloc(initialUri);
final mainBloc = MainBloc(api, common, clientSettingBloc, deepLinkBloc,
Expand All @@ -51,7 +51,7 @@ Future<MyApp> init() async {
return MyApp(router, mainBloc);
}

Future<void> initSystemTray() async {
Future<void> _initSystemTray() async {
if (!PlatformHelper.isWindowsApp()) {
return;
}
Expand Down Expand Up @@ -92,7 +92,7 @@ Future<void> initSystemTray() async {
});
}

Future<ClientDeviceInfo> genClientDeviceInfo() async {
Future<ClientDeviceInfo> _genClientDeviceInfo() async {
final deviceInfo = DeviceInfoPlugin();
if (PlatformHelper.isAndroidApp()) {
final androidInfo = await deviceInfo.androidInfo;
Expand Down
5 changes: 5 additions & 0 deletions lib/l10n/intl/messages_zh-CN.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class MessageLookup extends MessageLookupByLibrary {
static String m7(success, failed) =>
"导入Steam应用程序完成,成功 ${success} 个,失败 ${failed} 个。";

static String m11(reason) => "加载失败${reason}";

static String m8(server) => "登录到 ${server}";

static String m9(reason) => "登录失败,${reason}";
Expand Down Expand Up @@ -123,6 +125,8 @@ class MessageLookup extends MessageLookupByLibrary {
"importingSteamApplications":
MessageLookupByLibrary.simpleMessage("正在导入Steam应用程序"),
"launcherError": MessageLookupByLibrary.simpleMessage("启动器错误"),
"library": MessageLookupByLibrary.simpleMessage("库"),
"loadFailed": m11,
"loggingInTo": m8,
"login": MessageLookupByLibrary.simpleMessage("登录"),
"loginFailed": m9,
Expand All @@ -141,6 +145,7 @@ class MessageLookup extends MessageLookupByLibrary {
"scanningLocalFiles": MessageLookupByLibrary.simpleMessage("正在扫描本地文件"),
"serverSetup": MessageLookupByLibrary.simpleMessage("服务器设置"),
"showServerConfig": MessageLookupByLibrary.simpleMessage("查看服务器信息"),
"store": MessageLookupByLibrary.simpleMessage("商店"),
"title": MessageLookupByLibrary.simpleMessage("TuiHub"),
"tls": MessageLookupByLibrary.simpleMessage("TLS"),
"unknownErrorOccurred": MessageLookupByLibrary.simpleMessage("发生未知错误"),
Expand Down
30 changes: 30 additions & 0 deletions lib/l10n/l10n.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 54 additions & 29 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:bitsdojo_window/bitsdojo_window.dart';
import 'package:chinese_font_library/chinese_font_library.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flex_color_scheme/flex_color_scheme.dart';
import 'package:flutter/foundation.dart';
Expand Down Expand Up @@ -33,9 +34,11 @@ import 'model/common_model.dart';
import 'repo/grpc/api_helper.dart';
import 'repo/local/common.dart';
import 'route.dart';
import 'view/helper/spacing.dart';

part 'init.dart';

// main function is the entry point of the app
void main(List<String> args) async {
// Required for Android App
await Hive.initFlutter();
Expand Down Expand Up @@ -148,6 +151,39 @@ class _MyAppWidget extends StatelessWidget {
if (state is DefaultAppState) {
context.read<ClientSettingBloc>().add(InitClientSettingEvent());
}

final lightTheme = _customizeTheme(
Brightness.light,
FlexThemeData.light(
scheme: state.theme.scheme,
useMaterial3: true,
surfaceMode: FlexSurfaceMode.highSurfaceLowScaffold,
blendLevel: 10,
subThemesData: const FlexSubThemesData(
blendOnLevel: 7,
useTextTheme: true,
useM2StyleDividerInM3: true,
),
visualDensity: FlexColorScheme.comfortablePlatformDensity,
swapLegacyOnMaterial3: true,
));

final darkTheme = _customizeTheme(
Brightness.dark,
FlexThemeData.dark(
scheme: state.theme.scheme,
surfaceMode: FlexSurfaceMode.highScaffoldLevelSurface,
blendLevel: 13,
subThemesData: const FlexSubThemesData(
blendOnLevel: 20,
useTextTheme: true,
useM2StyleDividerInM3: true,
),
visualDensity: FlexColorScheme.comfortablePlatformDensity,
useMaterial3: true,
swapLegacyOnMaterial3: true,
));

return MaterialApp.router(
onGenerateTitle: (context) => S.of(context).title,
localizationsDelegates: const [
Expand All @@ -157,39 +193,28 @@ class _MyAppWidget extends StatelessWidget {
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: S.delegate.supportedLocales,
// The Mandy red, light theme.
theme: FlexThemeData.light(
scheme: state.theme.scheme,
useMaterial3: true,
surfaceMode: FlexSurfaceMode.highSurfaceLowScaffold,
blendLevel: 10,
subThemesData: const FlexSubThemesData(
blendOnLevel: 7,
useTextTheme: true,
useM2StyleDividerInM3: true,
),
visualDensity: FlexColorScheme.comfortablePlatformDensity,
swapLegacyOnMaterial3: true,
),
// The Mandy red, dark theme.
darkTheme: FlexThemeData.dark(
scheme: state.theme.scheme,
surfaceMode: FlexSurfaceMode.highScaffoldLevelSurface,
blendLevel: 13,
subThemesData: const FlexSubThemesData(
blendOnLevel: 20,
useTextTheme: true,
useM2StyleDividerInM3: true,
),
visualDensity: FlexColorScheme.comfortablePlatformDensity,
useMaterial3: true,
swapLegacyOnMaterial3: true,
),
theme: lightTheme,
darkTheme: darkTheme,
themeMode: state.themeMode,
// Use dark or light theme based on system setting.
routerConfig: router,
);
},
);
}
}

ThemeData _customizeTheme(Brightness brightness, ThemeData theme) {
// Temporary fix for font issue
// https://github.com/flutter/flutter/issues/48381
return theme
.copyWith(
listTileTheme: theme.listTileTheme.copyWith(
selectedColor: theme.colorScheme.onPrimary,
selectedTileColor: theme.colorScheme.primary,
shape: RoundedRectangleBorder(
borderRadius: SpacingHelper.defaultBorderRadius,
),
),
)
.useSystemChineseFont(brightness);
}
1 change: 1 addition & 0 deletions lib/main_window.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'view/pages/server_select_overlay.dart';
import 'view/specialized/theme_mode_toggle.dart';
import 'view/specialized/title_bar.dart';

// MainWindow is the top-level widget of the app
class MainWindow extends StatelessWidget {
const MainWindow({super.key, required this.child});

Expand Down
14 changes: 7 additions & 7 deletions lib/route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ class AppRoutes {
static const String _gebura = '$_module/${ModuleName._gebura}';
static const AppRoutes gebura = AppRoutes._(_gebura);
static const AppRoutes geburaStore =
AppRoutes._('$_gebura/${_GeburaFunctions.store}');
AppRoutes._('$_gebura/${GeburaFunctions.store}');
static const AppRoutes geburaLibrary =
AppRoutes._('$_gebura/${_GeburaFunctions.library}');
AppRoutes._('$_gebura/${GeburaFunctions.library}');
static const AppRoutes geburaLibrarySettings =
AppRoutes._('$_gebura/${_GeburaFunctions.librarySettings}');
AppRoutes._('$_gebura/${GeburaFunctions.librarySettings}');
static AppRoutes geburaLibraryDetail(int id) =>
AppRoutes._('$geburaLibrary?id=$id');

Expand Down Expand Up @@ -230,7 +230,7 @@ class _YesodActions {
static const String recentFilter = 'filterRecent';
}

class _GeburaFunctions {
class GeburaFunctions {
static const String store = 'store';
static const String library = 'library';
static const String librarySettings = 'librarySettings';
Expand Down Expand Up @@ -427,9 +427,9 @@ GoRouter getRouter(MainBloc mainBloc, ApiHelper apiHelper) {
final function = state.pathParameters['function'] ??
AppRoutes.geburaStore.toString();
final geburaPages = {
_GeburaFunctions.store: const GeburaStorePage(),
_GeburaFunctions.library: const GeburaLibraryOverview(),
_GeburaFunctions.librarySettings:
GeburaFunctions.store: const GeburaStorePage(),
GeburaFunctions.library: const GeburaLibraryOverview(),
GeburaFunctions.librarySettings:
const GeburaLibrarySettings(),
};
Widget? page = geburaPages[function];
Expand Down
Loading

0 comments on commit 80b44ba

Please sign in to comment.