Skip to content

Commit

Permalink
fix(win): save data in exe path
Browse files Browse the repository at this point in the history
  • Loading branch information
MuZhou233 committed Mar 20, 2024
1 parent f64a1a4 commit e384057
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 17 deletions.
38 changes: 30 additions & 8 deletions lib/bloc/deeplink_bloc.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic> json) =>
_$DeepLinkStateFromJson(json);

Map<String, dynamic> toJson() => _$DeepLinkStateToJson(this);
}

class DeepLinkConnectState extends DeepLinkState {
final ServerConfig serverConfig;
Expand All @@ -21,8 +33,8 @@ class DeepLinkConnectState extends DeepLinkState {
}

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

DeepLinkBloc(Uri? initialUri) : super(DeepLinkState()) {
on<DeepLinkInitEvent>((event, emit) async {
Expand All @@ -48,7 +60,7 @@ class DeepLinkBloc extends Bloc<DeepLinkEvent, DeepLinkState> {
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');
Expand All @@ -58,8 +70,18 @@ class DeepLinkBloc extends Bloc<DeepLinkEvent, DeepLinkState> {
add(DeepLinkInitEvent());
}

void dispose() {
_sub?.cancel();
super.close();
Future<void> dispose() async {
await _sub?.cancel();
await super.close();
}

@override
DeepLinkState? fromJson(Map<String, dynamic> json) {
return DeepLinkState.fromJson(json);
}

@override
Map<String, dynamic>? toJson(DeepLinkState state) {
return state.toJson();
}
}
13 changes: 13 additions & 0 deletions lib/bloc/deeplink_bloc.g.dart

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

14 changes: 11 additions & 3 deletions lib/init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ Future<MyApp> 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;
Expand Down Expand Up @@ -60,6 +65,9 @@ Future<MyApp> 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);

Expand Down
1 change: 1 addition & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
17 changes: 11 additions & 6 deletions lib/model/yesod_model.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
enum FeedListType {
table,
magazine,
card,
}

@Deprecated('delete')
class RssSubscription {
final String title;
final String link;
Expand All @@ -12,6 +19,7 @@ class RssSubscription {
});
}

@Deprecated('delete')
class RssPostItem {
String? title;
String? link;
Expand All @@ -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,
}
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit e384057

Please sign in to comment.