Skip to content

Commit

Permalink
fix(yesod): move data from bloc to hive
Browse files Browse the repository at this point in the history
  • Loading branch information
MuZhou233 committed Mar 20, 2024
1 parent e384057 commit 45e4829
Show file tree
Hide file tree
Showing 17 changed files with 554 additions and 247 deletions.
3 changes: 1 addition & 2 deletions lib/bloc/deeplink_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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';
Expand Down Expand Up @@ -60,7 +59,7 @@ class DeepLinkBloc extends HydratedBloc<DeepLinkEvent, DeepLinkState> {
process(initialUri);
_sub = uriLinkStream.listen(process);
while (_sub != null) {
sleep(const Duration(seconds: 1));
await Future.delayed(const Duration(seconds: 1));
}
} catch (e) {
debugPrint('DeepLinkBloc: $e');
Expand Down
58 changes: 15 additions & 43 deletions lib/bloc/yesod/yesod_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ part 'yesod_state.dart';

class YesodBloc extends Bloc<YesodEvent, YesodState> {
final ApiHelper _api;
final YesodRepo _repo;
final YesodRepo repo;

YesodBloc(this._api, this._repo) : super(YesodState()) {
YesodBloc(this._api, this.repo) : super(YesodState()) {
on<YesodInitEvent>((event, emit) async {
if (state.feedConfigs == null) {
add(YesodConfigLoadEvent());
Expand Down Expand Up @@ -176,21 +176,11 @@ class YesodBloc extends Bloc<YesodEvent, YesodState> {
));
}, transformer: droppable());

on<YesodSetConfigEditIndexEvent>((event, emit) async {
emit(state.copyWith(
feedConfigEditIndex: event.index,
));
}, transformer: restartable());

on<YesodConfigEditEvent>((event, emit) async {
emit(YesodConfigEditState(
state,
EventStatus.processing,
));
final index = state.feedConfigEditIndex;
if (index == null) {
return;
}
final resp = await _api.doRequest(
(client) => client.updateFeedConfig,
UpdateFeedConfigRequest(
Expand All @@ -206,10 +196,7 @@ class YesodBloc extends Bloc<YesodEvent, YesodState> {
return;
}
final configs = state.feedConfigs ?? [];
configs[index] = ListFeedConfigsResponse_FeedWithConfig(
config: event.config,
feed: configs[index].feed,
);
add(YesodConfigLoadEvent());
emit(YesodConfigEditState(
state.copyWith(feedConfigs: configs),
EventStatus.success,
Expand All @@ -229,15 +216,17 @@ class YesodBloc extends Bloc<YesodEvent, YesodState> {
: state,
EventStatus.processing,
));
final listConfig = repo.getFeedItemListConfig();
final resp = await _api.doRequest(
(client) => client.listFeedItems,
ListFeedItemsRequest(
paging: PagingRequest(
pageSize: Int64(pageSize),
pageNum: Int64(pageNum),
),
feedIdFilter: state.feedItemFilter?.feedIdFilter,
categoryFilter: state.feedItemFilter?.categoryFilter,
feedIdFilter: listConfig.feedIdFilter
?.map((e) => InternalID(id: Int64.parseInt(e))),
categoryFilter: listConfig.categoryFilter,
),
);
if (resp.status != ApiStatus.success) {
Expand All @@ -260,23 +249,12 @@ class YesodBloc extends Bloc<YesodEvent, YesodState> {
));
}, transformer: droppable());

on<YesodFeedItemDigestsSetFilterEvent>((event, emit) async {
emit(state.copyWith(feedItemFilter: event.filter));
add(YesodFeedItemDigestsLoadEvent(1, refresh: true));
}, transformer: restartable());

on<YesodFeedItemLoadEvent>((event, emit) async {
emit(YesodFeedItemLoadState(
state,
EventStatus.processing,
));
if (_repo.existFeedItem(event.id)) {
state.feedItems ??= {};
final item = _repo.getFeedItem(event.id);
if (item != null) {
state.feedItems![event.id] = item;
}
} else {
if (!repo.existFeedItem(event.id)) {
final resp = await _api.doRequest(
(client) => client.getFeedItem,
GetFeedItemRequest(
Expand All @@ -292,15 +270,13 @@ class YesodBloc extends Bloc<YesodEvent, YesodState> {
return;
} else {
final item = resp.getData().item;
await _repo.setFeedItem(event.id, item);
state.feedItems ??= {};
state.feedItems![event.id] = item;
emit(YesodFeedItemLoadState(
state,
EventStatus.success,
));
await repo.setFeedItem(event.id, item);
}
}
emit(YesodFeedItemLoadState(
state,
EventStatus.success,
));
}, transformer: droppable());

on<YesodFeedCategoriesLoadEvent>((event, emit) async {
Expand All @@ -326,10 +302,6 @@ class YesodBloc extends Bloc<YesodEvent, YesodState> {
));
}, transformer: droppable());

on<YesodFeedListTypeSetEvent>((event, emit) async {
emit(state.copyWith(feedListType: event.type));
});

on<YesodFeedItemReadEvent>((event, emit) async {
await _api.doRequest(
(client) => client.readFeedItem,
Expand All @@ -339,10 +311,10 @@ class YesodBloc extends Bloc<YesodEvent, YesodState> {
}

int cacheSize() {
return _repo.cacheSize();
return repo.cacheSize();
}

Future<void> clearCache() async {
await _repo.clearCache();
await repo.clearCache();
}
}
18 changes: 0 additions & 18 deletions lib/bloc/yesod/yesod_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ final class YesodConfigAddEvent extends YesodEvent {
YesodConfigAddEvent(this.config);
}

final class YesodSetConfigEditIndexEvent extends YesodEvent {
final int index;

YesodSetConfigEditIndexEvent(this.index);
}

final class YesodConfigEditEvent extends YesodEvent {
final FeedConfig config;

Expand All @@ -38,12 +32,6 @@ final class YesodFeedItemDigestsLoadEvent extends YesodEvent {
YesodFeedItemDigestsLoadEvent(this.pageNum, {this.refresh});
}

final class YesodFeedItemDigestsSetFilterEvent extends YesodEvent {
final YesodFeedItemFilter filter;

YesodFeedItemDigestsSetFilterEvent(this.filter);
}

final class YesodFeedItemLoadEvent extends YesodEvent {
final InternalID id;

Expand All @@ -52,12 +40,6 @@ final class YesodFeedItemLoadEvent extends YesodEvent {

final class YesodFeedCategoriesLoadEvent extends YesodEvent {}

final class YesodFeedListTypeSetEvent extends YesodEvent {
final FeedListType type;

YesodFeedListTypeSetEvent(this.type);
}

final class YesodFeedItemReadEvent extends YesodEvent {
final InternalID id;

Expand Down
43 changes: 0 additions & 43 deletions lib/bloc/yesod/yesod_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,36 @@ class YesodState {
late List<ListFeedConfigsResponse_FeedWithConfig>? feedConfigs;
late List<FeedItemDigest>? feedItemDigests;
late List<String>? feedCategories;
late Map<InternalID, FeedItem>? feedItems;

// Data for UI
late int? feedConfigEditIndex;
late RssPostItem? feedPreview;
late InternalID? selectedFeedItemID;
late YesodFeedItemFilter? feedItemFilter;
late FeedListType? feedListType;

YesodState({
this.feedConfigs,
this.feedConfigEditIndex,
this.feedPreview,
this.feedItemDigests,
this.feedItems,
this.selectedFeedItemID,
this.feedItemFilter,
this.feedCategories,
this.feedListType,
});

YesodState copyWith({
List<ListFeedConfigsResponse_FeedWithConfig>? feedConfigs,
int? feedConfigEditIndex,
RssPostItem? feedPreview,
List<FeedItemDigest>? feedItemDigests,
Map<InternalID, FeedItem>? feedItems,
InternalID? selectedFeedItemID,
YesodFeedItemFilter? feedItemFilter,
List<String>? feedCategories,
FeedListType? feedListType,
}) {
return YesodState(
feedConfigs: feedConfigs ?? this.feedConfigs,
feedConfigEditIndex: feedConfigEditIndex ?? this.feedConfigEditIndex,
feedPreview: feedPreview ?? this.feedPreview,
feedItemDigests: feedItemDigests ?? this.feedItemDigests,
feedItems: feedItems ?? this.feedItems,
selectedFeedItemID: selectedFeedItemID ?? this.selectedFeedItemID,
feedItemFilter: feedItemFilter ?? this.feedItemFilter,
feedCategories: feedCategories ?? this.feedCategories,
feedListType: feedListType ?? this.feedListType,
);
}

void _from(YesodState other) {
feedConfigs = other.feedConfigs;
feedConfigEditIndex = other.feedConfigEditIndex;
feedPreview = other.feedPreview;
feedItemDigests = other.feedItemDigests;
feedItems = other.feedItems;
selectedFeedItemID = other.selectedFeedItemID;
feedItemFilter = other.feedItemFilter;
feedCategories = other.feedCategories;
feedListType = other.feedListType;
}
}

Expand Down Expand Up @@ -148,21 +123,3 @@ class YesodFeedCategoriesLoadState extends YesodState with EventStatusMixin {
@override
final EventStatus statusCode;
}

class YesodFeedItemFilter {
final Iterable<InternalID>? feedIdFilter;
final Iterable<InternalID>? authorIdFilter;
final Iterable<String>? publishPlatformFilter;
final Iterable<String>? categoryFilter;
final TimeRange? publishTimeRange;
final bool? hideRead;

YesodFeedItemFilter({
this.feedIdFilter,
this.authorIdFilter,
this.publishPlatformFilter,
this.categoryFilter,
this.publishTimeRange,
this.hideRead,
});
}
6 changes: 3 additions & 3 deletions lib/init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ Future<MyApp> init() async {
if (kDebugMode) {
Bloc.observer = SimpleBlocObserver();
}
final deviceInfo = await _genClientDeviceInfo();
final clientSettingBloc = ClientSettingBloc(common);
final deepLinkBloc = DeepLinkBloc(initialUri);
HydratedBloc.storage = await HydratedStorage.build(
storageDirectory: dataPath == null ? Directory('') : Directory(dataPath),
);
final deviceInfo = await _genClientDeviceInfo();
final clientSettingBloc = ClientSettingBloc(common);
final deepLinkBloc = DeepLinkBloc(initialUri);
final mainBloc = MainBloc(api, common, clientSettingBloc, deepLinkBloc,
packageInfo, deviceInfo, dataPath);

Expand Down
22 changes: 21 additions & 1 deletion lib/model/yesod_model.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
enum FeedListType {
import 'package:freezed_annotation/freezed_annotation.dart';

part 'yesod_model.freezed.dart';
part 'yesod_model.g.dart';

@freezed
class YesodFeedItemListConfig with _$YesodFeedItemListConfig {
const factory YesodFeedItemListConfig({
Iterable<String>? feedIdFilter,
Iterable<String>? authorIdFilter,
Iterable<String>? publishPlatformFilter,
Iterable<String>? categoryFilter,
bool? hideRead,
FeedItemListType? listType,
}) = _YesodFeedItemListConfig;

factory YesodFeedItemListConfig.fromJson(Map<String, dynamic> json) =>
_$YesodFeedItemListConfigFromJson(json);
}

enum FeedItemListType {
table,
magazine,
card,
Expand Down
Loading

0 comments on commit 45e4829

Please sign in to comment.