-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposts_cubit.dart
68 lines (57 loc) · 2.48 KB
/
posts_cubit.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import 'package:common_domain/mapper/mapper.dart';
import 'package:common_presentation/mixins/cubit_alert_mixin.dart';
import 'package:common_presentation/mixins/cubit_screen_state_pagination_mixin.dart';
import 'package:common_presentation/mixins/cubit_pagination_mixin.dart';
import 'package:common_presentation/widgets/screen_state/bloc_screen_state_mixin.dart';
import 'package:common_presentation/widgets/screen_state/generic_screen_state.dart';
import 'package:common_presentation/widgets/screen_state/screen_state.dart';
import 'package:features_reddit_posts_domain/features_reddit_posts_domain.dart';
import 'package:features_reddit_posts_presentation/features_reddit_posts_presentation.dart';
import 'package:features_reddit_posts_presentation/src/data/post_state.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_modular/flutter_modular.dart';
typedef PostsPageState = GenericScreenState<Iterable<PostItemState>>;
class PostsCubit extends Cubit<ScreenState<PostsPageState>>
with
BlocScreenStateMixin,
CubitPaginationMixin<Iterable<PostItemState>, GenericScreenState<Iterable<PostItemState>>>,
CubitScreenStatePaginationMixin,
CubitScreenStatePaginationIterableMixin<PostItemState, GenericScreenState<Iterable<PostItemState>>>,
CubitAlertMixin {
PostsCubit() : super(ScreenStateEmptyLoading()){
refresh();
}
late final PostsInteractor _interactor = Modular.get();
late final PostsNavigator _navigator = Modular.get();
late final Mapper<PostsModel, Iterable<PostItemState>> _postModelMapper = Modular.get();
String? _lastAfter;
@override
onRefresh() async {
_lastAfter = null;
onBeforeFirstPageLoad();
await _interactor
.getPosts(after: null)
.then(_saveLastAfter)
.then(_postModelMapper.map)
.then(_newPaginationState)
.then(emitData);
}
onPostTap(PostItemState state) => _navigator.toPostDetails(state);
//region CubitPaginationMixin
@override
Future<Iterable<PostItemState>> loadPage(int pageNumber) => _interactor
.getPosts(
after: _lastAfter,
)
.then(_saveLastAfter)
.then(_postModelMapper.map);
@override
Iterable<PostItemState>? getPagesIterable() => stateData?.data;
// endregion
GenericScreenState<Iterable<PostItemState>> _newPaginationState(Iterable<PostItemState> data) =>
GenericScreenState(data: data);
PostsModel _saveLastAfter(PostsModel model) {
_lastAfter = model.after;
return model;
}
}