Skip to content

Commit 9004531

Browse files
committed
2 parents 9b57db5 + 0e98efb commit 9004531

File tree

10 files changed

+477
-93
lines changed

10 files changed

+477
-93
lines changed

chat/kit/src/main/java/cn/wildfire/chat/kit/group/GroupViewModel.java

Lines changed: 98 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,22 @@ public void onFail(int errorCode) {
233233
return result;
234234
}
235235

236+
public MutableLiveData<OperateResult<Boolean>> muteGroupMember(String groupId, boolean mute, List<String> memberIds, NotificationMessageContent notifyMsg, List<Integer> lines) {
237+
MutableLiveData<OperateResult<Boolean>> result = new MutableLiveData<>();
238+
ChatManager.Instance().muteGroupMember(groupId, mute, memberIds, lines, notifyMsg, new GeneralCallback() {
239+
@Override
240+
public void onSuccess() {
241+
result.setValue(new OperateResult<>(0));
242+
}
243+
244+
@Override
245+
public void onFail(int errorCode) {
246+
result.setValue(new OperateResult<>(errorCode));
247+
}
248+
});
249+
return result;
250+
}
251+
236252
public MutableLiveData<OperateResult<Boolean>> muteAll(String groupId, boolean mute, MessageContent notifyMsg, List<Integer> notifyLines) {
237253
MutableLiveData<OperateResult<Boolean>> result = new MutableLiveData<>();
238254
ChatManager.Instance().modifyGroupInfo(groupId, ModifyGroupInfoType.Modify_Group_Mute, mute ? "1" : "0", notifyLines, notifyMsg, new GeneralCallback() {
@@ -319,8 +335,19 @@ public MutableLiveData<List<GroupMember>> getGroupMembersLiveData(String groupId
319335
public MutableLiveData<List<UIUserInfo>> getGroupManagerUIUserInfosLiveData(String groupId, boolean refresh) {
320336
MutableLiveData<List<UIUserInfo>> data = new MutableLiveData<>();
321337
ChatManager.Instance().getWorkHandler().post(() -> {
322-
List<GroupMember> members = ChatManager.Instance().getGroupMembers(groupId, false);
323-
List<UIUserInfo> userInfos = memberToUIUserInfo(groupId, members);
338+
List<GroupMember> managers = getGroupManagers(groupId);
339+
List<UIUserInfo> userInfos = managerMemberToUIUserInfo(groupId, managers);
340+
data.postValue(userInfos);
341+
});
342+
343+
return data;
344+
}
345+
346+
public MutableLiveData<List<UIUserInfo>> getMutedMemberUIUserInfosLiveData(String groupId, boolean refresh) {
347+
MutableLiveData<List<UIUserInfo>> data = new MutableLiveData<>();
348+
ChatManager.Instance().getWorkHandler().post(() -> {
349+
List<GroupMember> mutedMembers = getMutedMembers(groupId);
350+
List<UIUserInfo> userInfos = mutedMemberToUIUserInfo(groupId, mutedMembers);
324351
data.postValue(userInfos);
325352
});
326353

@@ -332,7 +359,7 @@ public List<GroupMember> getGroupManagers(String groupId) {
332359
List<GroupMember> managers = new ArrayList<>();
333360
if (members != null) {
334361
for (GroupMember member : members) {
335-
if (member.type == GroupMember.GroupMemberType.Manager) {
362+
if (member.type == GroupMember.GroupMemberType.Manager || member.type == GroupMember.GroupMemberType.Owner) {
336363
managers.add(member);
337364
}
338365
}
@@ -351,24 +378,49 @@ public List<String> getGroupManagerIds(String groupId) {
351378
return mangerIds;
352379
}
353380

354-
private List<UIUserInfo> memberToUIUserInfo(String groupId, List<GroupMember> members) {
381+
382+
public List<GroupMember> getMutedMembers(String groupId) {
383+
List<GroupMember> members = ChatManager.Instance().getGroupMembers(groupId, false);
384+
List<GroupMember> managers = new ArrayList<>();
385+
if (members != null) {
386+
for (GroupMember member : members) {
387+
if (member.type == GroupMember.GroupMemberType.Silent) {
388+
managers.add(member);
389+
}
390+
}
391+
}
392+
return managers;
393+
}
394+
395+
396+
public List<String> getMutedMemberIds(String groupId) {
397+
List<GroupMember> mutedMembers = getMutedMembers(groupId);
398+
List<String> mutedIds = new ArrayList<>();
399+
if (mutedMembers != null) {
400+
for (GroupMember manager : mutedMembers) {
401+
mutedIds.add(manager.memberId);
402+
}
403+
}
404+
return mutedIds;
405+
406+
}
407+
408+
private List<UIUserInfo> managerMemberToUIUserInfo(String groupId, List<GroupMember> members) {
355409
if (members == null || members.isEmpty()) {
356410
return null;
357411
}
358412

359413
List<String> memberIds = new ArrayList<>(members.size());
360414
for (GroupMember member : members) {
361-
if (member.type == GroupMember.GroupMemberType.Owner || member.type == GroupMember.GroupMemberType.Manager) {
362-
memberIds.add(member.memberId);
363-
}
415+
memberIds.add(member.memberId);
364416
}
365417

366418
List<UIUserInfo> uiUserInfos = new ArrayList<>();
367419
List<UserInfo> userInfos = UserViewModel.getUsers(memberIds, groupId);
368420
boolean showManagerCategory = false;
369421
for (UserInfo userInfo : userInfos) {
370422
UIUserInfo info = new UIUserInfo(userInfo);
371-
String name = ChatManager.Instance().getGroupMemberDisplayName(groupId, userInfo.uid);
423+
String name = ChatManager.Instance().getGroupMemberDisplayName(userInfo);
372424
if (!TextUtils.isEmpty(name)) {
373425
String pinyin = PinyinUtils.getPinyin(name);
374426
char c = pinyin.toUpperCase().charAt(0);
@@ -403,6 +455,44 @@ private List<UIUserInfo> memberToUIUserInfo(String groupId, List<GroupMember> me
403455
return uiUserInfos;
404456
}
405457

458+
private List<UIUserInfo> mutedMemberToUIUserInfo(String groupId, List<GroupMember> members) {
459+
if (members == null || members.isEmpty()) {
460+
return null;
461+
}
462+
463+
List<String> memberIds = new ArrayList<>(members.size());
464+
for (GroupMember member : members) {
465+
memberIds.add(member.memberId);
466+
}
467+
468+
List<UIUserInfo> uiUserInfos = new ArrayList<>();
469+
List<UserInfo> userInfos = UserViewModel.getUsers(memberIds, groupId);
470+
boolean showManagerCategory = false;
471+
for (UserInfo userInfo : userInfos) {
472+
UIUserInfo info = new UIUserInfo(userInfo);
473+
String name = ChatManager.Instance().getGroupMemberDisplayName(userInfo);
474+
if (!TextUtils.isEmpty(name)) {
475+
String pinyin = PinyinUtils.getPinyin(name);
476+
char c = pinyin.toUpperCase().charAt(0);
477+
if (c >= 'A' && c <= 'Z') {
478+
info.setSortName(pinyin);
479+
} else {
480+
// 为了让排序排到最后
481+
info.setSortName("{" + pinyin);
482+
}
483+
} else {
484+
info.setSortName("");
485+
}
486+
info.setCategory("禁言列表");
487+
if (!showManagerCategory) {
488+
showManagerCategory = true;
489+
info.setShowCategory(true);
490+
}
491+
uiUserInfos.add(info);
492+
}
493+
return uiUserInfos;
494+
}
495+
406496
public GroupMember getGroupMember(String groupId, String memberId) {
407497
return ChatManager.Instance().getGroupMember(groupId, memberId);
408498
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package cn.wildfire.chat.kit.group.manage;
2+
3+
import android.content.Intent;
4+
import android.os.Bundle;
5+
import android.view.View;
6+
7+
import androidx.annotation.Nullable;
8+
import androidx.lifecycle.ViewModelProviders;
9+
10+
import com.afollestad.materialdialogs.MaterialDialog;
11+
12+
import java.util.ArrayList;
13+
import java.util.Collections;
14+
15+
import cn.wildfire.chat.kit.contact.BaseUserListFragment;
16+
import cn.wildfire.chat.kit.contact.UserListAdapter;
17+
import cn.wildfire.chat.kit.contact.model.FooterValue;
18+
import cn.wildfire.chat.kit.contact.model.UIUserInfo;
19+
import cn.wildfire.chat.kit.group.BasePickGroupMemberActivity;
20+
import cn.wildfire.chat.kit.group.GroupViewModel;
21+
import cn.wildfire.chat.kit.user.UserViewModel;
22+
import cn.wildfirechat.model.GroupInfo;
23+
import cn.wildfirechat.model.GroupMember;
24+
import cn.wildfirechat.remote.ChatManager;
25+
26+
public class GroupMemberMuteListFragment extends BaseUserListFragment {
27+
private GroupViewModel groupViewModel;
28+
private GroupInfo groupInfo;
29+
private GroupMember groupMember;
30+
31+
public static GroupMemberMuteListFragment newInstance(GroupInfo groupInfo) {
32+
Bundle args = new Bundle();
33+
args.putParcelable("groupInfo", groupInfo);
34+
GroupMemberMuteListFragment fragment = new GroupMemberMuteListFragment();
35+
fragment.setArguments(args);
36+
return fragment;
37+
}
38+
39+
@Override
40+
public void onCreate(@Nullable Bundle savedInstanceState) {
41+
super.onCreate(savedInstanceState);
42+
groupInfo = getArguments().getParcelable("groupInfo");
43+
showQuickIndexBar(false);
44+
45+
groupViewModel = ViewModelProviders.of(getActivity()).get(GroupViewModel.class);
46+
groupMember = groupViewModel.getGroupMember(groupInfo.target, ChatManager.Instance().getUserId());
47+
observerGroupMemberUpdate();
48+
}
49+
50+
@Override
51+
protected void afterViews(View view) {
52+
super.afterViews(view);
53+
loadAndShowGroupMembers(true);
54+
}
55+
56+
@Override
57+
public UserListAdapter onCreateUserListAdapter() {
58+
return new UserListAdapter(this);
59+
}
60+
61+
@Override
62+
public void initFooterViewHolders() {
63+
if (groupMember.type == GroupMember.GroupMemberType.Owner || groupMember.type == GroupMember.GroupMemberType.Manager) {
64+
addFooterViewHolder(MuteGroupMemberViewHolder.class, new FooterValue());
65+
}
66+
}
67+
68+
@Override
69+
public void onUserClick(UIUserInfo userInfo) {
70+
UserViewModel userViewModel = ViewModelProviders.of(this).get(UserViewModel.class);
71+
GroupMember me = groupViewModel.getGroupMember(groupInfo.target, userViewModel.getUserId());
72+
if (me == null || me.type != GroupMember.GroupMemberType.Owner) {
73+
return;
74+
}
75+
76+
new MaterialDialog.Builder(getActivity())
77+
.items(Collections.singleton("取消禁言"))
78+
.itemsCallback((dialog, itemView, position, text) -> {
79+
groupViewModel.setGroupManager(groupInfo.target, false, Collections.singletonList(userInfo.getUserInfo().uid), null, Collections.singletonList(0));
80+
})
81+
.cancelable(true)
82+
.build()
83+
.show();
84+
}
85+
86+
@Override
87+
public void onFooterClick(int index) {
88+
Intent intent = new Intent(getActivity(), MuteGroupMemberActivity.class);
89+
intent.putExtra(BasePickGroupMemberActivity.GROUP_INFO, groupInfo);
90+
91+
ArrayList<String> uncheckableMemberIds = new ArrayList<>();
92+
uncheckableMemberIds.add(groupInfo.owner);
93+
uncheckableMemberIds.addAll(groupViewModel.getMutedMemberIds(groupInfo.target));
94+
uncheckableMemberIds.addAll(groupViewModel.getGroupManagerIds(groupInfo.target));
95+
intent.putExtra(BasePickGroupMemberActivity.UNCHECKABLE_MEMBER_IDS, uncheckableMemberIds);
96+
startActivity(intent);
97+
}
98+
99+
private void observerGroupMemberUpdate() {
100+
groupViewModel.groupMembersUpdateLiveData().observe(this, groupMembers -> {
101+
if (groupInfo.target.equals(groupMembers.get(0).groupId)) {
102+
loadAndShowGroupMembers(false);
103+
}
104+
});
105+
}
106+
107+
private void loadAndShowGroupMembers(boolean refresh) {
108+
groupViewModel.getMutedMemberUIUserInfosLiveData(groupInfo.target, refresh).observe(this, uiUserInfos -> {
109+
showContent();
110+
userListAdapter.setUsers(uiUserInfos);
111+
userListAdapter.notifyDataSetChanged();
112+
});
113+
}
114+
}
Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,92 @@
11
package cn.wildfire.chat.kit.group.manage;
22

3+
import android.widget.Toast;
4+
5+
import androidx.lifecycle.Observer;
6+
import androidx.lifecycle.ViewModelProviders;
7+
8+
import com.kyleduo.switchbutton.SwitchButton;
9+
10+
import java.util.Collections;
11+
import java.util.List;
12+
13+
import butterknife.BindView;
314
import cn.wildfire.chat.kit.WfcBaseActivity;
15+
import cn.wildfire.chat.kit.group.GroupViewModel;
416
import cn.wildfirechat.chat.R;
517
import cn.wildfirechat.model.GroupInfo;
618

719
public class GroupMuteActivity extends WfcBaseActivity {
20+
private GroupInfo groupInfo;
21+
private GroupViewModel groupViewModel;
22+
23+
@BindView(R.id.muteSwitchButton)
24+
SwitchButton switchButton;
825

926
@Override
10-
protected void afterViews() {
11-
GroupInfo groupInfo = getIntent().getParcelableExtra("groupInfo");
12-
getSupportFragmentManager().beginTransaction()
13-
.replace(R.id.containerFrameLayout, GroupMuteFragment.newInstance(groupInfo))
14-
.commit();
27+
protected int contentLayout() {
28+
return R.layout.group_manage_mute_activity;
1529
}
1630

1731
@Override
18-
protected int contentLayout() {
19-
return R.layout.fragment_container_activity;
32+
protected void afterViews() {
33+
super.afterViews();
34+
groupInfo = getIntent().getParcelableExtra("groupInfo");
35+
init();
36+
}
37+
38+
private void init() {
39+
groupViewModel = ViewModelProviders.of(this).get(GroupViewModel.class);
40+
groupViewModel.groupInfoUpdateLiveData().observe(this, new Observer<List<GroupInfo>>() {
41+
@Override
42+
public void onChanged(List<GroupInfo> groupInfos) {
43+
if (groupInfos != null) {
44+
for (GroupInfo info : groupInfos) {
45+
if (info.target.equals(groupInfo.target)) {
46+
boolean oMuted = groupInfo.mute == 1;
47+
boolean nMuted = info.mute == 1;
48+
groupInfo = info;
49+
50+
if (oMuted != nMuted) {
51+
initGroupMemberMuteListFragment(!nMuted);
52+
}
53+
break;
54+
}
55+
}
56+
}
57+
58+
}
59+
});
60+
switchButton.setCheckedNoEvent(groupInfo.mute == 1);
61+
switchButton.setOnCheckedChangeListener((buttonView, isChecked) -> {
62+
groupViewModel.muteAll(groupInfo.target, isChecked, null, Collections.singletonList(0)).observe(this, booleanOperateResult -> {
63+
if (!booleanOperateResult.isSuccess()) {
64+
switchButton.setCheckedNoEvent(!isChecked);
65+
Toast.makeText(this, "禁言失败 " + booleanOperateResult.getErrorCode(), Toast.LENGTH_SHORT).show();
66+
}
67+
});
68+
});
69+
70+
if (groupInfo.mute == 0) {
71+
initGroupMemberMuteListFragment(true);
72+
}
73+
}
74+
75+
private GroupMemberMuteListFragment fragment;
76+
77+
private void initGroupMemberMuteListFragment(boolean show) {
78+
if (show) {
79+
if (fragment == null) {
80+
fragment = GroupMemberMuteListFragment.newInstance(groupInfo);
81+
}
82+
getSupportFragmentManager().beginTransaction()
83+
.replace(R.id.containerFrameLayout, fragment)
84+
.commit();
85+
} else {
86+
if (fragment != null) {
87+
getSupportFragmentManager().beginTransaction().remove(fragment).commit();
88+
fragment = null;
89+
}
90+
}
2091
}
2192
}

0 commit comments

Comments
 (0)