Skip to content

Commit

Permalink
feat: rebuild yesod config add page
Browse files Browse the repository at this point in the history
  • Loading branch information
MuZhou233 committed Nov 14, 2023
1 parent 549b736 commit 5760fd8
Show file tree
Hide file tree
Showing 10 changed files with 355 additions and 714 deletions.
122 changes: 122 additions & 0 deletions lib/bloc/yesod/yesod_cubit.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import 'package:bloc/bloc.dart';
import 'package:flutter/widgets.dart';
import 'package:html/parser.dart';
import 'package:http/http.dart' as http;
import 'package:tuihub_protos/librarian/sephirah/v1/yesod.pb.dart';
import 'package:tuihub_protos/librarian/v1/common.pb.dart';
import 'package:webfeed_revised/domain/rss_feed.dart';

import '../../common/api/api_helper.dart';
import '../../model/yesod_model.dart';

class YesodState {
final List<ListFeedConfigsResponse_FeedWithConfig> feedConfigs;
final int? feedConfigEditIndex;
final RssPostItem? feedPreview;
final YesodRequestStatus configLoadStatus;
final YesodRequestStatus configPreviewStatus;
final YesodRequestStatus configAddStatus;
final YesodRequestStatus configEditStatus;

final List<FeedItemDigest> feedItemDigests;
Expand All @@ -16,7 +24,10 @@ class YesodState {
YesodState(
this.feedConfigs,
this.feedConfigEditIndex,
this.feedPreview,
this.configLoadStatus,
this.configPreviewStatus,
this.configAddStatus,
this.configEditStatus,
this.feedItemDigests,
this.feedItems,
Expand All @@ -25,15 +36,21 @@ class YesodState {
YesodState copyWith({
List<ListFeedConfigsResponse_FeedWithConfig>? feedConfigs,
int? feedConfigEditIndex,
RssPostItem? feedPreview,
YesodRequestStatus? configLoadStatus,
YesodRequestStatus? configPreviewStatus,
YesodRequestStatus? configAddStatus,
YesodRequestStatus? configEditStatus,
List<FeedItemDigest>? feedItemDigests,
Map<InternalID, FeedItem>? feedItems,
}) {
return YesodState(
feedConfigs ?? this.feedConfigs,
feedConfigEditIndex ?? this.feedConfigEditIndex,
feedPreview ?? this.feedPreview,
configLoadStatus ?? this.configLoadStatus,
configPreviewStatus ?? this.configPreviewStatus,
configAddStatus ?? this.configAddStatus,
configEditStatus ?? this.configEditStatus,
feedItemDigests ?? this.feedItemDigests,
feedItems ?? this.feedItems,
Expand All @@ -59,6 +76,9 @@ enum YesodRequestStatusCode {
YesodState _initialState() => YesodState(
[],
null,
null,
YesodRequestStatus(YesodRequestStatusCode.initial),
YesodRequestStatus(YesodRequestStatusCode.initial),
YesodRequestStatus(YesodRequestStatusCode.initial),
YesodRequestStatus(YesodRequestStatusCode.initial),
[],
Expand Down Expand Up @@ -133,6 +153,108 @@ class YesodCubit extends Cubit<YesodState> {
}
}

Future<void> previewFeedConfig(String url) async {
emit(state.copyWith(
feedPreview: null,
configPreviewStatus:
YesodRequestStatus(YesodRequestStatusCode.processing),
));
try {
final response = await http.get(Uri.parse(url));
final data = RssFeed.parse(response.body);

debugPrint(data.title);

debugPrint(data.description);

final subscription = RssSubscription(
title: data.title!,
link: url,
description: data.description!,
iconUrl: '');

final item = data.items!.first;

String? imgUrl;

String description = '';
if (item.description != null) {
try {
final doc = parse(item.description);
final imgElements = doc.getElementsByTagName('img');
if (imgElements.isNotEmpty) {
imgUrl = imgElements.first.attributes['src'];
}
doc.querySelectorAll('p,h1,h2,h3,h4,h5,span').forEach((element) {
description = description + element.text;
});
description.replaceAll('\n', ' ');
debugPrint(description);
} catch (e) {
description = item.description!;
}
}

final example = RssPostItem(
title: item.title,
link: item.link,
description: description,
subscription: subscription,
image: imgUrl,
);
emit(state.copyWith(
feedPreview: example,
configPreviewStatus: YesodRequestStatus(YesodRequestStatusCode.success),
));
} catch (e) {
emit(state.copyWith(
feedPreview: RssPostItem(
subscription: RssSubscription(
title: '', link: '', iconUrl: '', description: '')),
configPreviewStatus: YesodRequestStatus(
YesodRequestStatusCode.failed,
msg: '解析失败, $e',
),
));
}
}

Future<void> addFeedConfig(FeedConfig config) async {
emit(state.copyWith(
configAddStatus: YesodRequestStatus(YesodRequestStatusCode.processing),
));
final index = state.feedConfigEditIndex;
if (index == null) {
return;
}
final resp = await api.doRequest((client, option) {
return client.createFeedConfig(
CreateFeedConfigRequest(
config: config,
),
options: option,
);
});
if (resp.status != ApiStatus.success) {
emit(state.copyWith(
configAddStatus:
YesodRequestStatus(YesodRequestStatusCode.failed, msg: resp.error),
));
return;
}
final configs = [
ListFeedConfigsResponse_FeedWithConfig(
config: config,
feed: null,
)
];
configs.addAll(state.feedConfigs);
emit(state.copyWith(
feedConfigs: configs,
configAddStatus: YesodRequestStatus(YesodRequestStatusCode.success),
));
}

void setFeedConfigEditIndex(int index) {
emit(state.copyWith(
feedConfigEditIndex: index,
Expand Down
4 changes: 2 additions & 2 deletions lib/route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import 'view/pages/settings/user/user_add_page.dart';
import 'view/pages/settings/user/user_edit_page.dart';
import 'view/pages/settings/user/user_manage_page.dart';
import 'view/pages/tiphereth/tiphereth_frame_page.dart';
import 'view/pages/yesod/yesod_config_add/yesod_config_add.dart';
import 'view/pages/yesod/yesod_config_add_page.dart';
import 'view/pages/yesod/yesod_config_edit_page.dart';
import 'view/pages/yesod/yesod_config_page.dart';
import 'view/pages/yesod/yesod_nav.dart';
Expand Down Expand Up @@ -288,7 +288,7 @@ GoRouter getRouter() {

final yesodActions = {
_YesodActions.configEdit: const YesodConfigEditPage(),
_YesodActions.configAdd: const YesodConfigAdd(),
_YesodActions.configAdd: const YesodConfigAddPage(),
};
return NoTransitionPage(
child: BlocProvider(
Expand Down
158 changes: 0 additions & 158 deletions lib/view/pages/yesod/yesod_config_add/bloc/yesod_add_bloc.dart

This file was deleted.

Loading

0 comments on commit 5760fd8

Please sign in to comment.