From e384057a501c7548c9fc97bceafecef8d696f98b Mon Sep 17 00:00:00 2001 From: MuZhou233 Date: Wed, 20 Mar 2024 00:48:08 +0000 Subject: [PATCH] fix(win): save data in exe path --- lib/bloc/deeplink_bloc.dart | 38 +++++++++++++++++++++++++++-------- lib/bloc/deeplink_bloc.g.dart | 13 ++++++++++++ lib/init.dart | 14 ++++++++++--- lib/main.dart | 1 + lib/model/yesod_model.dart | 17 ++++++++++------ pubspec.yaml | 1 + 6 files changed, 67 insertions(+), 17 deletions(-) create mode 100644 lib/bloc/deeplink_bloc.g.dart diff --git a/lib/bloc/deeplink_bloc.dart b/lib/bloc/deeplink_bloc.dart index 6accef1..5870c51 100644 --- a/lib/bloc/deeplink_bloc.dart +++ b/lib/bloc/deeplink_bloc.dart @@ -1,18 +1,30 @@ import 'dart:async'; -import 'package:bloc/bloc.dart'; import 'package:flutter/foundation.dart'; +import 'package:freezed_annotation/freezed_annotation.dart'; +import 'package:hydrated_bloc/hydrated_bloc.dart'; import 'package:uni_links/uni_links.dart'; +import 'package:universal_io/io.dart'; import '../common/platform.dart'; import '../model/common_model.dart'; +part 'deeplink_bloc.g.dart'; + @immutable sealed class DeepLinkEvent {} class DeepLinkInitEvent extends DeepLinkEvent {} -class DeepLinkState {} +@JsonSerializable() +class DeepLinkState { + DeepLinkState(); + + factory DeepLinkState.fromJson(Map json) => + _$DeepLinkStateFromJson(json); + + Map toJson() => _$DeepLinkStateToJson(this); +} class DeepLinkConnectState extends DeepLinkState { final ServerConfig serverConfig; @@ -21,8 +33,8 @@ class DeepLinkConnectState extends DeepLinkState { } // DeepLinkBloc manages deep link events and states. -class DeepLinkBloc extends Bloc { - StreamSubscription? _sub; +class DeepLinkBloc extends HydratedBloc { + StreamSubscription? _sub; DeepLinkBloc(Uri? initialUri) : super(DeepLinkState()) { on((event, emit) async { @@ -48,7 +60,7 @@ class DeepLinkBloc extends Bloc { process(initialUri); _sub = uriLinkStream.listen(process); while (_sub != null) { - await Future.delayed(const Duration(seconds: 1)); + sleep(const Duration(seconds: 1)); } } catch (e) { debugPrint('DeepLinkBloc: $e'); @@ -58,8 +70,18 @@ class DeepLinkBloc extends Bloc { add(DeepLinkInitEvent()); } - void dispose() { - _sub?.cancel(); - super.close(); + Future dispose() async { + await _sub?.cancel(); + await super.close(); + } + + @override + DeepLinkState? fromJson(Map json) { + return DeepLinkState.fromJson(json); + } + + @override + Map? toJson(DeepLinkState state) { + return state.toJson(); } } diff --git a/lib/bloc/deeplink_bloc.g.dart b/lib/bloc/deeplink_bloc.g.dart new file mode 100644 index 0000000..c9c736d --- /dev/null +++ b/lib/bloc/deeplink_bloc.g.dart @@ -0,0 +1,13 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'deeplink_bloc.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +DeepLinkState _$DeepLinkStateFromJson(Map json) => + DeepLinkState(); + +Map _$DeepLinkStateToJson(DeepLinkState instance) => + {}; diff --git a/lib/init.dart b/lib/init.dart index c39e492..9411e16 100644 --- a/lib/init.dart +++ b/lib/init.dart @@ -5,9 +5,14 @@ Future init() async { final packageInfo = await PackageInfo.fromPlatform(); // https://github.com/hivedb/hive/issues/1044 - final dataPath = PlatformHelper.isWeb() - ? null - : (await getApplicationSupportDirectory()).path; + late String? dataPath; + if (PlatformHelper.isWeb()) { + dataPath = null; + } else if (PlatformHelper.isWindowsApp() && !kDebugMode) { + dataPath = path.join(path.dirname(Platform.resolvedExecutable), 'data'); + } else { + dataPath = (await getApplicationSupportDirectory()).path; + } // dotenv var enableSentry = false; @@ -60,6 +65,9 @@ Future init() async { final deviceInfo = await _genClientDeviceInfo(); final clientSettingBloc = ClientSettingBloc(common); final deepLinkBloc = DeepLinkBloc(initialUri); + HydratedBloc.storage = await HydratedStorage.build( + storageDirectory: dataPath == null ? Directory('') : Directory(dataPath), + ); final mainBloc = MainBloc(api, common, clientSettingBloc, deepLinkBloc, packageInfo, deviceInfo, dataPath); diff --git a/lib/main.dart b/lib/main.dart index cce6381..2e3a09e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -13,6 +13,7 @@ import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:go_router/go_router.dart'; import 'package:hive_flutter/hive_flutter.dart'; +import 'package:hydrated_bloc/hydrated_bloc.dart'; import 'package:package_info_plus/package_info_plus.dart'; import 'package:path/path.dart' as path; import 'package:path_provider/path_provider.dart'; diff --git a/lib/model/yesod_model.dart b/lib/model/yesod_model.dart index 943ceed..e1e219d 100644 --- a/lib/model/yesod_model.dart +++ b/lib/model/yesod_model.dart @@ -1,3 +1,10 @@ +enum FeedListType { + table, + magazine, + card, +} + +@Deprecated('delete') class RssSubscription { final String title; final String link; @@ -12,6 +19,7 @@ class RssSubscription { }); } +@Deprecated('delete') class RssPostItem { String? title; String? link; @@ -30,24 +38,21 @@ class RssPostItem { }); } +@Deprecated('delete') class RssDescriptionContent { final String content; RssDescriptionContent(this.content); } +@Deprecated('delete') class TextContent extends RssDescriptionContent { TextContent(super.content); } +@Deprecated('delete') class ImgTextContent extends RssDescriptionContent { final String imgUrl; ImgTextContent(super.content, this.imgUrl); } - -enum FeedListType { - table, - magazine, - card, -} diff --git a/pubspec.yaml b/pubspec.yaml index ef393da..02e9744 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -39,6 +39,7 @@ dependencies: bloc: ^8.1.3 bloc_concurrency: ^0.2.4 flutter_bloc: ^8.1.4 + hydrated_bloc: ^9.1.4 # https://github.com/flutter/flutter/issues/140869 go_router: ^12.1.3