A framework for building convergent cross-platform Nextcloud clients using Flutter.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
5.0 KiB

part of '../neon_news.dart';
2 years ago
abstract interface class NewsBlocEvents {
void addFeed(final String url, final int? folderId);
2 years ago
void removeFeed(final int feedId);
2 years ago
void renameFeed(final int feedId, final String feedTitle);
2 years ago
void moveFeed(final int feedId, final int? folderId);
2 years ago
void markFeedAsRead(final int feedId);
2 years ago
void createFolder(final String name);
void deleteFolder(final int folderId);
2 years ago
void renameFolder(final int folderId, final String name);
2 years ago
void markFolderAsRead(final int folderId);
2 years ago
}
abstract interface class NewsBlocStates {
BehaviorSubject<Result<List<NewsFolder>>> get folders;
2 years ago
BehaviorSubject<Result<List<NewsFeed>>> get feeds;
2 years ago
BehaviorSubject<int> get unreadCounter;
2 years ago
}
class NewsBloc extends InteractiveBloc implements NewsBlocEvents, NewsBlocStates, NewsMainArticlesBloc {
2 years ago
NewsBloc(
this.options,
this.account,
2 years ago
) {
mainArticlesBloc.articles.listen((final result) {
if (result.hasData) {
final type = mainArticlesBloc.filterType.valueOrNull;
unreadCounter
.add(result.requireData.where((final a) => type == FilterType.starred ? a.starred : a.unread).length);
}
});
unawaited(mainArticlesBloc.refresh());
2 years ago
}
@override
NewsBloc get _newsBloc => this;
@override
final NewsAppSpecificOptions options;
@override
@override
final Account account;
late final mainArticlesBloc = NewsMainArticlesBloc(
this,
options,
account,
);
2 years ago
late int _newestItemId;
@override
int? id;
@override
ListType? listType;
2 years ago
@override
void dispose() {
unawaited(feeds.close());
unawaited(folders.close());
unawaited(unreadCounter.close());
unawaited(articles.close());
unawaited(filterType.close());
mainArticlesBloc.dispose();
super.dispose();
2 years ago
}
@override
BehaviorSubject<Result<List<NewsFeed>>> feeds = BehaviorSubject<Result<List<NewsFeed>>>();
@override
BehaviorSubject<Result<List<NewsFolder>>> folders = BehaviorSubject<Result<List<NewsFolder>>>();
@override
BehaviorSubject<int> unreadCounter = BehaviorSubject<int>();
@override
late BehaviorSubject<Result<List<NewsArticle>>> articles = mainArticlesBloc.articles;
@override
late BehaviorSubject<FilterType> filterType = mainArticlesBloc.filterType;
2 years ago
@override
Future<void> refresh() async {
await Future.wait([
RequestManager.instance.wrapNextcloud<List<NewsFolder>, NewsListFolders>(
account.id,
'news-folders',
folders,
() async => account.client.news.listFolders(),
(final response) => response.folders.toList(),
),
RequestManager.instance.wrapNextcloud<List<NewsFeed>, NewsListFeeds>(
account.id,
'news-feeds',
feeds,
() async => account.client.news.listFeeds(),
(final response) {
if (response.newestItemId != null) {
_newestItemId = response.newestItemId!;
}
return response.feeds.toList();
},
),
mainArticlesBloc.reload(),
]);
2 years ago
}
@override
void addFeed(final String url, final int? folderId) {
wrapAction(() async => account.client.news.addFeed(url: url, folderId: folderId));
}
2 years ago
@override
void createFolder(final String name) {
wrapAction(() async => account.client.news.createFolder(name: name));
}
2 years ago
@override
void deleteFolder(final int folderId) {
wrapAction(() async => account.client.news.deleteFolder(folderId: folderId));
}
2 years ago
@override
void markFeedAsRead(final int feedId) {
wrapAction(() async => account.client.news.markFeedAsRead(feedId: feedId, newestItemId: _newestItemId));
}
@override
void markFolderAsRead(final int folderId) {
wrapAction(() async => account.client.news.markFolderAsRead(folderId: folderId, newestItemId: _newestItemId));
2 years ago
}
@override
void moveFeed(final int feedId, final int? folderId) {
wrapAction(() async => account.client.news.moveFeed(feedId: feedId, folderId: folderId));
}
@override
void removeFeed(final int feedId) {
wrapAction(() async => account.client.news.deleteFeed(feedId: feedId));
}
@override
void renameFeed(final int feedId, final String feedTitle) {
wrapAction(() async => account.client.news.renameFeed(feedId: feedId, feedTitle: feedTitle));
}
@override
void renameFolder(final int folderId, final String name) {
wrapAction(() async => account.client.news.renameFolder(folderId: folderId, name: name));
}
@override
void markArticleAsRead(final NewsArticle article) {
mainArticlesBloc.markArticleAsRead(article);
}
2 years ago
@override
void markArticleAsUnread(final NewsArticle article) {
mainArticlesBloc.markArticleAsUnread(article);
}
2 years ago
@override
void setFilterType(final FilterType type) {
mainArticlesBloc.setFilterType(type);
}
@override
void starArticle(final NewsArticle article) {
mainArticlesBloc.starArticle(article);
}
@override
void unstarArticle(final NewsArticle article) {
mainArticlesBloc.unstarArticle(article);
}
@override
Future<void> reload() async {
await mainArticlesBloc.reload();
}
2 years ago
}