IOS
hefeixia
2021-02-18 49f3c1374873f73dbde2983ca0fcf1fb10bfedbf
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * Copyright (c) 2020 WildFireChat. All rights reserved.
 */
 
package cn.wildfire.chat.kit.contact.pick;
 
import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
 
import java.util.ArrayList;
import java.util.List;
 
import cn.wildfire.chat.kit.contact.model.UIUserInfo;
import cn.wildfire.chat.kit.utils.PinyinUtils;
import cn.wildfirechat.model.UserInfo;
import cn.wildfirechat.remote.ChatManager;
 
public class PickUserViewModel extends ViewModel {
    private List<UIUserInfo> users;
    private List<String> uncheckableIds;
    private List<String> initialCheckedIds;
    private MutableLiveData<UIUserInfo> userCheckStatusUpdateLiveData;
    private int maxPickCount = Integer.MAX_VALUE;
 
    public PickUserViewModel() {
        super();
    }
 
    public MutableLiveData<UIUserInfo> userCheckStatusUpdateLiveData() {
        if (userCheckStatusUpdateLiveData == null) {
            userCheckStatusUpdateLiveData = new MutableLiveData<>();
        }
        return userCheckStatusUpdateLiveData;
    }
 
    public void setMaxPickCount(int maxPickCount) {
        this.maxPickCount = maxPickCount;
    }
 
    public int getMaxPickCount() {
        return maxPickCount;
    }
 
    public void setUncheckableIds(List<String> uncheckableIds) {
        this.uncheckableIds = uncheckableIds;
        updateUserStatus();
    }
 
    public void addUncheckableIds(List<String> uncheckableIds) {
        if (uncheckableIds == null || uncheckableIds.isEmpty()) {
            return;
        }
        if (this.uncheckableIds == null) {
            this.uncheckableIds = new ArrayList<>();
        }
        this.uncheckableIds.addAll(uncheckableIds);
    }
 
    public void setInitialCheckedIds(List<String> checkedIds) {
        this.initialCheckedIds = checkedIds;
        updateUserStatus();
    }
 
    public List<String> getInitialCheckedIds() {
        return initialCheckedIds;
    }
 
    public void setUsers(List<UIUserInfo> users) {
        this.users = users;
        updateUserStatus();
    }
 
    private void updateUserStatus() {
        if (users == null || users.isEmpty()) {
            return;
        }
        for (UIUserInfo info : users) {
            if (initialCheckedIds != null && !initialCheckedIds.isEmpty()) {
                if (initialCheckedIds.contains(info.getUserInfo().uid)) {
                    info.setChecked(true);
                }
            }
            if (uncheckableIds != null && !uncheckableIds.isEmpty()) {
                if (uncheckableIds.contains(info.getUserInfo().uid)) {
                    info.setCheckable(false);
                }
            }
        }
    }
 
    public List<UIUserInfo> searchUser(String keyword) {
        if (users == null || users.isEmpty()) {
            return null;
        }
 
        List<UIUserInfo> resultList = new ArrayList<>();
        for (UIUserInfo info : users) {
            UserInfo userInfo = info.getUserInfo();
            String name = ChatManager.Instance().getUserDisplayName(userInfo);
            String pinyin = PinyinUtils.getPinyin(name);
            if (name.contains(keyword) || pinyin.contains(keyword.toUpperCase())) {
                resultList.add(info);
                if (uncheckableIds != null && uncheckableIds.contains(userInfo.uid)) {
                    info.setCheckable(false);
                }
                if (initialCheckedIds != null && initialCheckedIds.contains(userInfo.uid)) {
                    info.setChecked(true);
                }
            }
        }
        if (resultList.isEmpty()) {
            return null;
        }
        resultList.get(0).setShowCategory(true);
        resultList.get(0).setCategory("搜索结果");
        return resultList;
    }
 
    /**
     * include initial checked users
     *
     * @return
     */
    public @NonNull
    List<UIUserInfo> getCheckedUsers() {
        List<UIUserInfo> checkedUsers = new ArrayList<>();
        if (users == null) {
            return checkedUsers;
        }
        for (UIUserInfo info : users) {
            if (info.isCheckable() && info.isChecked()) {
                checkedUsers.add(info);
            }
        }
        return checkedUsers;
    }
 
    /**
     * @param userInfo
     * @param checked
     * @return 选择成功,则返回true;否则,失败
     */
    public boolean checkUser(UIUserInfo userInfo, boolean checked) {
        if (checked && getCheckedUsers() != null && getCheckedUsers().size() >= maxPickCount) {
            return false;
        }
        userInfo.setChecked(checked);
        if (userCheckStatusUpdateLiveData != null) {
            userCheckStatusUpdateLiveData.setValue(userInfo);
        }
        return true;
    }
 
    @Override
    protected void onCleared() {
        super.onCleared();
    }
}