jld3103
2 years ago
7 changed files with 253 additions and 72 deletions
@ -0,0 +1,103 @@
|
||||
import 'dart:async'; |
||||
|
||||
import 'package:neon/src/apps/news/blocs/articles.dart'; |
||||
import 'package:neon/src/neon.dart'; |
||||
import 'package:nextcloud/nextcloud.dart'; |
||||
import 'package:rx_bloc/rx_bloc.dart'; |
||||
import 'package:rxdart/rxdart.dart'; |
||||
|
||||
part 'article.rxb.g.dart'; |
||||
|
||||
abstract class NewsArticleBlocEvents { |
||||
void markArticleAsRead(); |
||||
|
||||
void markArticleAsUnread(); |
||||
|
||||
void starArticle(); |
||||
|
||||
void unstarArticle(); |
||||
} |
||||
|
||||
abstract class NewsArticleBlocStates { |
||||
BehaviorSubject<bool> get unread; |
||||
|
||||
BehaviorSubject<bool> get starred; |
||||
|
||||
Stream<Exception> get errors; |
||||
} |
||||
|
||||
@RxBloc() |
||||
class NewsArticleBloc extends $NewsArticleBloc { |
||||
NewsArticleBloc( |
||||
this._requestManager, |
||||
this._client, |
||||
this._newsArticlesBloc, |
||||
final NewsArticle article, |
||||
) { |
||||
_$markArticleAsReadEvent.listen((final _) { |
||||
_wrapArticleAction(() async { |
||||
await _client.news.markArticleAsRead(itemId: article.id); |
||||
_unreadSubject.add(false); |
||||
}); |
||||
}); |
||||
|
||||
_$markArticleAsUnreadEvent.listen((final _) { |
||||
_wrapArticleAction(() async { |
||||
await _client.news.markArticleAsUnread(itemId: article.id); |
||||
_unreadSubject.add(true); |
||||
}); |
||||
}); |
||||
|
||||
_$starArticleEvent.listen((final _) { |
||||
_wrapArticleAction(() async { |
||||
await _client.news.starArticle(itemId: article.id); |
||||
_starredSubject.add(true); |
||||
}); |
||||
}); |
||||
|
||||
_$unstarArticleEvent.listen((final _) { |
||||
_wrapArticleAction(() async { |
||||
await _client.news.unstarArticle(itemId: article.id); |
||||
_starredSubject.add(false); |
||||
}); |
||||
}); |
||||
|
||||
_unreadSubject.add(article.unread); |
||||
_starredSubject.add(article.starred); |
||||
url = article.url; |
||||
} |
||||
|
||||
void _wrapArticleAction(final Future Function() call) { |
||||
final stream = _requestManager.wrapWithoutCache(() async => call()).asBroadcastStream(); |
||||
stream.whereError().listen(_errorsStreamController.add); |
||||
stream.whereSuccess().listen((final _) async { |
||||
_newsArticlesBloc.refresh(); |
||||
}); |
||||
} |
||||
|
||||
final RequestManager _requestManager; |
||||
final NextcloudClient _client; |
||||
final NewsArticlesBloc _newsArticlesBloc; |
||||
|
||||
late final String url; |
||||
final _unreadSubject = BehaviorSubject<bool>(); |
||||
final _starredSubject = BehaviorSubject<bool>(); |
||||
final _errorsStreamController = StreamController<Exception>(); |
||||
|
||||
@override |
||||
void dispose() { |
||||
unawaited(_unreadSubject.close()); |
||||
unawaited(_starredSubject.close()); |
||||
unawaited(_errorsStreamController.close()); |
||||
super.dispose(); |
||||
} |
||||
|
||||
@override |
||||
BehaviorSubject<bool> _mapToUnreadState() => _unreadSubject; |
||||
|
||||
@override |
||||
BehaviorSubject<bool> _mapToStarredState() => _starredSubject; |
||||
|
||||
@override |
||||
Stream<Exception> _mapToErrorsState() => _errorsStreamController.stream.asBroadcastStream(); |
||||
} |
@ -0,0 +1,85 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND |
||||
|
||||
// ************************************************************************** |
||||
// Generator: RxBlocGeneratorForAnnotation |
||||
// ************************************************************************** |
||||
|
||||
part of 'article.dart'; |
||||
|
||||
/// Used as a contractor for the bloc, events and states classes |
||||
/// {@nodoc} |
||||
abstract class NewsArticleBlocType extends RxBlocTypeBase { |
||||
NewsArticleBlocEvents get events; |
||||
NewsArticleBlocStates get states; |
||||
} |
||||
|
||||
/// [$NewsArticleBloc] extended by the [NewsArticleBloc] |
||||
/// {@nodoc} |
||||
abstract class $NewsArticleBloc extends RxBlocBase |
||||
implements NewsArticleBlocEvents, NewsArticleBlocStates, NewsArticleBlocType { |
||||
final _compositeSubscription = CompositeSubscription(); |
||||
|
||||
/// Тhe [Subject] where events sink to by calling [markArticleAsRead] |
||||
final _$markArticleAsReadEvent = PublishSubject<void>(); |
||||
|
||||
/// Тhe [Subject] where events sink to by calling [markArticleAsUnread] |
||||
final _$markArticleAsUnreadEvent = PublishSubject<void>(); |
||||
|
||||
/// Тhe [Subject] where events sink to by calling [starArticle] |
||||
final _$starArticleEvent = PublishSubject<void>(); |
||||
|
||||
/// Тhe [Subject] where events sink to by calling [unstarArticle] |
||||
final _$unstarArticleEvent = PublishSubject<void>(); |
||||
|
||||
/// The state of [unread] implemented in [_mapToUnreadState] |
||||
late final BehaviorSubject<bool> _unreadState = _mapToUnreadState(); |
||||
|
||||
/// The state of [starred] implemented in [_mapToStarredState] |
||||
late final BehaviorSubject<bool> _starredState = _mapToStarredState(); |
||||
|
||||
/// The state of [errors] implemented in [_mapToErrorsState] |
||||
late final Stream<Exception> _errorsState = _mapToErrorsState(); |
||||
|
||||
@override |
||||
void markArticleAsRead() => _$markArticleAsReadEvent.add(null); |
||||
|
||||
@override |
||||
void markArticleAsUnread() => _$markArticleAsUnreadEvent.add(null); |
||||
|
||||
@override |
||||
void starArticle() => _$starArticleEvent.add(null); |
||||
|
||||
@override |
||||
void unstarArticle() => _$unstarArticleEvent.add(null); |
||||
|
||||
@override |
||||
BehaviorSubject<bool> get unread => _unreadState; |
||||
|
||||
@override |
||||
BehaviorSubject<bool> get starred => _starredState; |
||||
|
||||
@override |
||||
Stream<Exception> get errors => _errorsState; |
||||
|
||||
BehaviorSubject<bool> _mapToUnreadState(); |
||||
|
||||
BehaviorSubject<bool> _mapToStarredState(); |
||||
|
||||
Stream<Exception> _mapToErrorsState(); |
||||
|
||||
@override |
||||
NewsArticleBlocEvents get events => this; |
||||
|
||||
@override |
||||
NewsArticleBlocStates get states => this; |
||||
|
||||
@override |
||||
void dispose() { |
||||
_$markArticleAsReadEvent.close(); |
||||
_$markArticleAsUnreadEvent.close(); |
||||
_$starArticleEvent.close(); |
||||
_$unstarArticleEvent.close(); |
||||
_compositeSubscription.dispose(); |
||||
super.dispose(); |
||||
} |
||||
} |
Loading…
Reference in new issue