user_friend_model.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import 'dart:convert';
  2. import 'dart:math';
  3. import 'package:azlistview/azlistview.dart';
  4. import 'package:sport/bean/user_friend.dart';
  5. import 'package:sport/provider/lib/view_state_refresh_list_model.dart';
  6. import 'package:sport/services/api/inject_api.dart';
  7. import 'package:sport/services/api/resp.dart';
  8. class UserFriendModel extends ViewStateRefreshListModel<UserFriend>
  9. with InjectApi {
  10. int type;
  11. String kw;
  12. // Map map;
  13. List<SuspensionBean> items = [];
  14. UserFriendModel(this.type);
  15. @override
  16. Future<List<UserFriend>> loadData({int pageNum}) async {
  17. List<UserFriend> results = [];
  18. switch (type) {
  19. case 2:
  20. results =
  21. (await api.userFansList(kw: kw, page: max(1, pageNum))).results;
  22. break;
  23. case 1:
  24. results =
  25. (await api.userFollowList(kw: kw, page: max(1, pageNum))).results;
  26. break;
  27. default:
  28. ResultsTest<UserFriend> data = (await api.userFriendsTest(
  29. kw: kw, page: max(1, pageNum), test: "1"))
  30. .results;
  31. // map = data.map;
  32. // items = data.map.keys.map((e) => SuspensionBean(e.toString(), data.map[e] as List)).toList();
  33. items = [];
  34. data.map.forEach((key, value) {
  35. value.forEach((element) {
  36. items.add(SuspensionBean(key, UserFriend.fromJson(element)));
  37. });
  38. });
  39. items.forEach((element) {
  40. print(element.toJson());
  41. });
  42. SuspensionUtil.setShowSuspensionStatus(items);
  43. SuspensionUtil.sortListBySuspensionTag(items);
  44. // print("$items----------------${items.length}");
  45. break;
  46. }
  47. return results;
  48. }
  49. void submitValue(String value) {
  50. this.kw = value;
  51. refresh(init: true);
  52. }
  53. }
  54. class SuspensionBean extends ISuspensionBean {
  55. final String tag;
  56. final UserFriend user;
  57. SuspensionBean(this.tag, this.user);
  58. @override
  59. String getSuspensionTag() => tag;
  60. Map<String, dynamic> toJson() {
  61. final Map<String, dynamic> data = new Map<String, dynamic>();
  62. data['tag'] = this.tag;
  63. data['user'] = this.user.toJson();
  64. return data;
  65. }
  66. }
  67. class UserFriendListModel extends ISuspensionBean {
  68. String name;
  69. String tagIndex;
  70. String namePinyin;
  71. UserFriendListModel({
  72. this.name,
  73. this.tagIndex,
  74. this.namePinyin,
  75. });
  76. UserFriendListModel.fromJson(Map<String, dynamic> json) : name = json['name'];
  77. Map<String, dynamic> toJson() => {
  78. 'name': name,
  79. // 'tagIndex': tagIndex,
  80. // 'namePinyin': namePinyin,
  81. // 'isShowSuspension': isShowSuspension
  82. };
  83. @override
  84. String getSuspensionTag() => tagIndex;
  85. @override
  86. String toString() => json.encode(this);
  87. }