-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcontroller.dart
139 lines (123 loc) · 3.37 KB
/
controller.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import 'package:e1547/client/client.dart';
import 'package:e1547/interface/interface.dart';
import 'package:e1547/post/post.dart';
import 'package:flutter/foundation.dart';
class PostController extends PageClientDataController<Post>
with PostActionController, PostFilterableController {
PostController({
required this.client,
QueryMap? query,
bool orderFavorites = false,
bool orderPools = true,
bool denying = true,
this.canSearch = true,
this.filterMode = PostFilterMode.filtering,
}) : _query = query ?? {},
_orderFavorites = orderFavorites,
_orderPools = orderPools {
this.denying = denying;
client.traits.addListener(applyFilter);
}
@override
final Client client;
final bool canSearch;
QueryMap _query;
QueryMap get query => _query;
set query(QueryMap value) {
if (mapEquals(_query, value)) return;
_query = Map.of(value);
refresh();
}
@override
final PostFilterMode filterMode;
bool _orderFavorites;
/// Order posts by when they were added to favorites.
bool get orderFavorites => _orderFavorites;
set orderFavorites(bool value) {
if (value == _orderFavorites) return;
_orderFavorites = value;
refresh();
}
bool _orderPools;
/// Order posts by pool order.
bool get orderPools => _orderPools;
set orderPools(bool value) {
if (value == _orderPools) return;
_orderPools = value;
refresh();
}
@override
@protected
Future<List<Post>> fetch(int page, bool force) async => client.posts.page(
page: page,
query: query,
force: force,
orderPoolsByOldest: orderPools,
orderFavoritesByAdded: orderFavorites,
cancelToken: cancelToken,
);
@override
void dispose() {
client.traits.removeListener(applyFilter);
super.dispose();
}
}
class SinglePostController extends PostController {
SinglePostController({
required this.id,
required super.client,
super.filterMode = PostFilterMode.plain,
}) : super(canSearch: false);
final int id;
@override
Future<List<Post>> fetch(int page, bool force) async => [
if (page == firstPageKey)
await client.posts.get(
id: id,
force: force,
cancelToken: cancelToken,
),
];
}
class PostProvider extends SubChangeNotifierProvider<Client, PostController> {
PostProvider({
QueryMap? query,
bool orderFavorites = false,
bool orderPools = true,
bool denying = true,
bool canSearch = true,
PostFilterMode filterMode = PostFilterMode.filtering,
super.child,
super.builder,
}) : super(
create: (context, client) => PostController(
client: client,
query: query,
orderPools: orderPools,
denying: denying,
canSearch: canSearch,
filterMode: filterMode,
),
);
// ignore: use_key_in_widget_constructors
PostProvider.builder({
required super.create,
super.keys,
super.child,
super.builder,
});
}
class SinglePostProvider extends PostProvider {
SinglePostProvider({
required int id,
PostFilterMode filterMode = PostFilterMode.plain,
super.child,
super.builder,
}) : super.builder(
create: (context, client) => SinglePostController(
id: id,
client: client,
filterMode: filterMode,
),
);
}