Skip to content

Commit fe4d0b8

Browse files
committed
1、增加收藏子页->网址收藏部分
1 parent 4e5c890 commit fe4d0b8

11 files changed

+606
-27
lines changed

lib/entity/collect_web_entity.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import 'package:json_annotation/json_annotation.dart';
2+
3+
part 'collect_web_entity.g.dart';
4+
5+
6+
@JsonSerializable()
7+
class CollectWebEntity extends Object {
8+
9+
@JsonKey(name: 'desc')
10+
String desc;
11+
12+
@JsonKey(name: 'icon')
13+
String icon;
14+
15+
@JsonKey(name: 'id')
16+
int id;
17+
18+
@JsonKey(name: 'link')
19+
String link;
20+
21+
@JsonKey(name: 'name')
22+
String name;
23+
24+
@JsonKey(name: 'order')
25+
int order;
26+
27+
@JsonKey(name: 'userId')
28+
int userId;
29+
30+
@JsonKey(name: 'visible')
31+
int visible;
32+
33+
CollectWebEntity(this.desc,this.icon,this.id,this.link,this.name,this.order,this.userId,this.visible,);
34+
35+
factory CollectWebEntity.fromJson(Map<String, dynamic> srcJson) => _$CollectWebEntityFromJson(srcJson);
36+
37+
Map<String, dynamic> toJson() => _$CollectWebEntityToJson(this);
38+
39+
}
40+
41+

lib/entity/collect_web_entity.g.dart

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/http/api.dart

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ class ArticleApi {
229229
return dio.get(ARTICLE_LIST(page - 1, id));
230230
}
231231

232-
static Future<Response> getTopArticles(){
232+
static Future<Response> getTopArticles() {
233233
return dio.get(TOP_ARTICLE);
234234
}
235235
}
@@ -260,10 +260,15 @@ class WXArticleApi {
260260
class CollectApi {
261261
static String COLLECT(int id) => '/lg/collect/$id/json';
262262

263+
///收藏站外文章
264+
static String COLLECT_OUTTER = '/lg/collect/add/json';
265+
263266
static String UN_COLLECT(int id) => '/lg/uncollect_originId/$id/json';
264267

265268
static String UN_COLLECT_WITH_ORIGIN_ID(int id) => '/lg/uncollect/$id/json';
266269

270+
static String COLLECT_LIST(int page) => '/lg/collect/list/$page/json';
271+
267272
static Future<Response> collect(int id) {
268273
return dio.post(COLLECT(id));
269274
}
@@ -276,35 +281,93 @@ class CollectApi {
276281
static Future<Response> unCollectWithOriginId(int id, int originId) {
277282
return dio.post(
278283
UN_COLLECT_WITH_ORIGIN_ID(id),
279-
queryParameters: {'originId': originId},
284+
queryParameters: {'originId': originId ?? -1},
280285
);
281286
}
287+
288+
///获取收藏列表,page从1开始
289+
static Future<Response> getCollectList(int page) {
290+
//老接口原因,实际输入页码是从0开始
291+
return dio.get(COLLECT_LIST(page - 1));
292+
}
293+
294+
static Future<Response> collectOutter(
295+
{String title, String author, String link}) {
296+
Map<String, dynamic> query = {};
297+
if (title != null) {
298+
query['title'] = title;
299+
}
300+
if (author != null) {
301+
query['author'] = author;
302+
}
303+
if (link != null) {
304+
query['link'] = link;
305+
}
306+
return dio.post(COLLECT_OUTTER, queryParameters: query);
307+
}
282308
}
283309

284-
///其他接口
285-
class CommonApi{
310+
class CollectWebApi {
311+
static const String COLLECT_WEB_LIST = '/lg/collect/usertools/json';
312+
static const String COLLECT_WEB = '/lg/collect/addtool/json';
313+
static const String UPDATE_WEB = '/lg/collect/updatetool/json';
314+
static const String DELETE_WEB = '/lg/collect/deletetool/json';
315+
316+
static Future<Response> getCollectWebList() {
317+
return dio.get(COLLECT_WEB_LIST);
318+
}
319+
320+
static Future<Response> collectWeb({String name, String link}) {
321+
Map<String, dynamic> query = {};
322+
if (name != null) {
323+
query['name'] = name;
324+
}
325+
if (link != null) {
326+
query['link'] = link;
327+
}
328+
return dio.post(COLLECT_WEB, queryParameters: query);
329+
}
330+
331+
static Future<Response> updateWeb({int id, String name, String link}) {
332+
Map<String, dynamic> query = {};
333+
if (id != null) {
334+
query['id'] = id;
335+
}
336+
if (name != null) {
337+
query['name'] = name;
338+
}
339+
if (link != null) {
340+
query['link'] = link;
341+
}
342+
return dio.post(UPDATE_WEB, queryParameters: query);
343+
}
344+
345+
static Future<Response> deleteWeb(int id) {
346+
return dio.post(DELETE_WEB, queryParameters: {'id': id});
347+
}
348+
}
286349

350+
///其他接口
351+
class CommonApi {
287352
static String SEARCH(int page) => '/article/query/$page/json';
288353

289354
static String HOT_SEARCH_KEY = '/hotkey/json';
290355

291356
static String NAVIGATION = '/navi/json';
292357

293-
294358
///搜索文章。页码从1开始
295-
static Future<Response> searchArticles(int page,String searchKey){
359+
static Future<Response> searchArticles(int page, String searchKey) {
296360
//老接口原因,实际输入页码是从0开始
297-
return dio.post(SEARCH(page-1),queryParameters: {'k':searchKey});
361+
return dio.post(SEARCH(page - 1), queryParameters: {'k': searchKey});
298362
}
299363

300364
///热搜词
301-
static Future<Response> getHotKey(){
365+
static Future<Response> getHotKey() {
302366
return dio.get(HOT_SEARCH_KEY);
303367
}
304368

305369
///导航
306-
static Future<Response> getNavigations(){
370+
static Future<Response> getNavigations() {
307371
return dio.get(NAVIGATION);
308372
}
309-
310373
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:provider/provider.dart';
3+
4+
import 'collect_web_model.dart';
5+
import 'collect_web_view.dart';
6+
7+
///收藏子页
8+
class CollectSubPage extends StatefulWidget {
9+
@override
10+
_CollectSubPageState createState() => _CollectSubPageState();
11+
}
12+
13+
class _CollectSubPageState extends State<CollectSubPage> with AutomaticKeepAliveClientMixin {
14+
GlobalKey<AnimatedListState> animatedListKey;
15+
16+
@override
17+
void initState() {
18+
super.initState();
19+
animatedListKey = GlobalKey();
20+
}
21+
22+
@override
23+
Widget build(BuildContext context) {
24+
super.build(context);
25+
return Column(
26+
children: <Widget>[
27+
CollectWebView(),
28+
],
29+
);
30+
}
31+
32+
@override
33+
bool get wantKeepAlive => true;
34+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import 'package:flutter/foundation.dart';
2+
import 'package:wanandroid_flutter/entity/base_entity.dart';
3+
import 'package:wanandroid_flutter/entity/collect_web_entity.dart';
4+
import 'package:wanandroid_flutter/http/index.dart';
5+
6+
///收藏网址提供者
7+
class CollectWebModel extends ChangeNotifier {
8+
bool isFirst;
9+
bool isEditMode;
10+
bool isLoading;
11+
Function(Exception e) onError;
12+
List<CollectWebEntity> _datas;
13+
14+
List<CollectWebEntity> get datas => _datas;
15+
16+
CollectWebModel(this.onError) {
17+
isFirst = true;
18+
isEditMode = false;
19+
isLoading = false;
20+
_datas = [];
21+
}
22+
23+
//收藏网址的增删改查方法:
24+
25+
getDatas() async {
26+
isLoading = true;
27+
notifyListeners();
28+
29+
try {
30+
Response response = await CollectWebApi.getCollectWebList();
31+
BaseEntity<List> baseEntity = BaseEntity.fromJson(response.data);
32+
_datas =
33+
baseEntity.data.map((e) => CollectWebEntity.fromJson(e)).toList();
34+
} catch (e) {
35+
print(e);
36+
onError(e);
37+
}
38+
39+
isFirst = false;
40+
isLoading = false;
41+
notifyListeners();
42+
}
43+
44+
addWeb(
45+
{String name,
46+
String link,
47+
ValueChanged<CollectWebEntity> onAddSuccess}) async {
48+
isLoading = true;
49+
notifyListeners();
50+
51+
try {
52+
Response response =
53+
await CollectWebApi.collectWeb(name: name, link: link);
54+
CollectWebEntity entity =
55+
CollectWebEntity.fromJson(response.data['data']);
56+
_datas.add(entity);
57+
onAddSuccess?.call(entity);
58+
} catch (e) {
59+
print(e);
60+
onError(e);
61+
}
62+
63+
isLoading = false;
64+
notifyListeners();
65+
}
66+
67+
deleteWeb(int id, {VoidCallback onDeleteSuccess}) async {
68+
isLoading = true;
69+
notifyListeners();
70+
71+
try {
72+
await CollectWebApi.deleteWeb(id);
73+
datas.removeWhere((e) => e.id == id);
74+
onDeleteSuccess?.call();
75+
} catch (e) {
76+
print(e);
77+
onError(e);
78+
}
79+
80+
isLoading = false;
81+
notifyListeners();
82+
}
83+
84+
toggleEdit() {
85+
isEditMode = !isEditMode;
86+
notifyListeners();
87+
}
88+
}

0 commit comments

Comments
 (0)