contant_show_model.dart
4.76 KB
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
// import 'package:contacts_service/contacts_service.dart';
import 'package:flutter/material.dart';
import 'package:one_key/constant/constant.dart';
import 'package:one_key/constant/error.dart';
import 'package:one_key/constant/state.dart';
import 'package:one_key/models/user_history_entity.dart';
import 'package:one_key/net/base_resp.dart';
import 'package:one_key/net/dio_util.dart';
import 'package:one_key/net/http_url.dart';
import 'package:lpinyin/lpinyin.dart';
class ContantShowModel extends ChangeNotifier {
PageState pageStateLeft = PageState.Loading;
PageState pageStateRight = PageState.Loading;
List<UserHistoryEntity> userHistorys = []; //历史录入(总)
List<UserHistoryEntity> contacts = []; //手机通讯录(总)
List<UserHistoryEntity> userHistorysShow = []; //历史录入展示
List<UserHistoryEntity> contactsShow = []; //手机通讯录展示
TextEditingController controller = TextEditingController(); //搜索框
//获取历史录入
getUserHistorys(BuildContext context, Map<String, String?> params) async {
try {
BaseResp<List?> baseResp = await DioUtil().request<List>(
context, Method.post, HttpUrl.get_reserve_user,
data: params, showLoading: false);
if (baseResp.code == Constant.status_success) {
userHistorys = baseResp.data!.map<UserHistoryEntity>((value) {
return UserHistoryEntity.fromJson(value);
}).toList();
if (userHistorys.length == 0) {
pageStateLeft = PageState.Empty;
} else {
for (int i = 0; i < userHistorys.length; i++) {
UserHistoryEntity item = userHistorys[i];
item.pinyin =
PinyinHelper.getPinyin(item.rname!).replaceAll(' ', '');
List<String> pinyinList =
PinyinHelper.getPinyin(item.rname!).split(' ');
item.pinyinFirst = '';
item.pinyinList = [];
String pinyinAll = ''; //组装全拼组合
for (int j = 0; j < pinyinList.length; j++) {
String letter = pinyinList[j];
pinyinAll += pinyinList[j];
item.pinyinList!.add(pinyinAll);
if (letter.length > 0) {
item.pinyinFirst =
item.pinyinFirst! + pinyinList[j].substring(0, 1);
} else {
item.pinyinFirst = item.pinyinFirst! + '';
}
}
userHistorys[i] = item;
}
userHistorysShow = userHistorys;
pageStateLeft = PageState.Success;
}
notifyListeners();
}
} catch (error) {
print(error.toString());
if (error is MyError) {
switch (error.type) {
case 1:
pageStateLeft = PageState.ConnectivityError;
break;
case 2:
pageStateLeft = PageState.ConnectivityTimeOut;
break;
default:
pageStateLeft = PageState.ConnectivityError;
break;
}
}
notifyListeners();
}
}
searchData(String key) {
if (key == '') {
userHistorysShow = userHistorys;
contactsShow = contacts;
pageStateRight = PageState.Success;
pageStateLeft = PageState.Success;
} else {
userHistorysShow = [];
contactsShow = [];
for (int i = 0; i < userHistorys.length; i++) {
String? name = userHistorys[i].rname;
String? pinyinFirst = userHistorys[i].pinyinFirst;
// String pinyin = userHistorys[i].pinyin;
String? phone = userHistorys[i].phone;
List<String>? pinyinList = userHistorys[i].pinyinList;
if ((name != null && name.contains(key)) ||
(pinyinFirst != null && pinyinFirst.contains(key)) ||
(phone != null && phone.contains(key)) ||
(pinyinList != null && pinyinList.contains(key))) {
userHistorysShow.add(userHistorys[i]);
}
}
for (int i = 0; i < contacts.length; i++) {
String? name = contacts[i].rname;
String? pinyinFirst = contacts[i].pinyinFirst;
List<String>? pinyinList = contacts[i].pinyinList;
// String pinyin = contacts[i].pinyin;
String? phone = contacts[i].phone;
if ((name != null && name.contains(key)) ||
(pinyinFirst != null && pinyinFirst.contains(key)) ||
(phone != null && phone.contains(key)) ||
(pinyinList != null && pinyinList.contains(key))) {
contactsShow.add(contacts[i]);
}
}
if (userHistorysShow.length == 0) {
pageStateLeft = PageState.Empty;
} else {
pageStateLeft = PageState.Success;
}
if (contactsShow.length == 0) {
pageStateRight = PageState.Empty;
} else {
pageStateRight = PageState.Success;
}
}
notifyListeners();
}
}