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.

156 lines
4.0 KiB

part of '../neon_news.dart';
3 years ago
enum FilterType {
all,
unread,
starred,
}
enum ListType {
feed,
folder,
}
abstract interface class NewsArticlesBlocEvents {
3 years ago
void setFilterType(final FilterType type);
void markArticleAsRead(final news.Article article);
3 years ago
void markArticleAsUnread(final news.Article article);
3 years ago
void starArticle(final news.Article article);
3 years ago
void unstarArticle(final news.Article article);
3 years ago
}
abstract interface class NewsArticlesBlocStates {
BehaviorSubject<Result<List<news.Article>>> get articles;
3 years ago
BehaviorSubject<FilterType> get filterType;
}
3 years ago
class NewsMainArticlesBloc extends NewsArticlesBloc {
NewsMainArticlesBloc(
super._newsBloc,
super.options,
super.account,
);
3 years ago
}
class NewsArticlesBloc extends InteractiveBloc implements NewsArticlesBlocEvents, NewsArticlesBlocStates {
3 years ago
NewsArticlesBloc(
this._newsBloc,
this.options,
this.account, {
3 years ago
this.id,
this.listType,
}) {
filterType.add(options.defaultArticlesFilterOption.value);
if (listType != null && filterType.value == FilterType.starred) {
filterType.add(FilterType.all);
3 years ago
}
if (this is! NewsMainArticlesBloc) {
unawaited(refresh());
}
3 years ago
}
final NewsBloc _newsBloc;
final NewsAppSpecificOptions options;
final Account account;
final int? id;
final ListType? listType;
@override
void dispose() {
unawaited(articles.close());
unawaited(filterType.close());
super.dispose();
}
@override
BehaviorSubject<Result<List<news.Article>>> articles = BehaviorSubject<Result<List<news.Article>>>();
@override
BehaviorSubject<FilterType> filterType = BehaviorSubject<FilterType>();
@override
Future<void> refresh() async {
if (this is! NewsMainArticlesBloc) {
await reload();
}
await _newsBloc.refresh();
3 years ago
}
Future<void> reload() async {
3 years ago
// The API for pagination is pretty useless in this case sadly. So no pagination for us :(
// https://github.com/nextcloud/news/blob/master/docs/api/api-v1-2.md#get-items
// https://github.com/nextcloud/news/blob/48ee5ce4d135da20079961a62ae37958d6a6b628/lib/Db/ListType.php#L21
late final news.ListType type;
3 years ago
bool? getRead;
if (listType != null) {
switch (filterType.value) {
3 years ago
case FilterType.all:
break;
case FilterType.unread:
getRead = false;
default:
throw Exception('FilterType ${filterType.value} not allowed');
3 years ago
}
}
switch (listType) {
case ListType.feed:
type = news.ListType.feed;
3 years ago
case ListType.folder:
type = news.ListType.folder;
3 years ago
case null:
switch (filterType.value) {
3 years ago
case FilterType.starred:
type = news.ListType.starred;
3 years ago
case FilterType.all:
type = news.ListType.allItems;
3 years ago
case FilterType.unread:
type = news.ListType.unread;
3 years ago
}
}
await RequestManager.instance.wrapNextcloud<List<news.Article>, news.ListArticles, void>(
account.id,
'news-articles-${type.index}-$id-$getRead',
articles,
account.client.news.listArticlesRaw(
type: type.index,
id: id ?? 0,
getRead: getRead ?? true ? 1 : 0,
),
(final response) => response.body.items.toList(),
3 years ago
);
}
@override
void markArticleAsRead(final news.Article article) {
wrapAction(() async => account.client.news.markArticleAsRead(itemId: article.id));
}
3 years ago
@override
void markArticleAsUnread(final news.Article article) {
wrapAction(() async => account.client.news.markArticleAsUnread(itemId: article.id));
3 years ago
}
@override
void setFilterType(final FilterType type) {
wrapAction(() async => filterType.add(type));
}
3 years ago
@override
void starArticle(final news.Article article) {
wrapAction(() async => account.client.news.starArticle(itemId: article.id));
}
3 years ago
@override
void unstarArticle(final news.Article article) {
wrapAction(() async => account.client.news.unstarArticle(itemId: article.id));
}
3 years ago
}