package com.netease.nim.uikit.session.activity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.netease.nim.uikit.R;
import com.netease.nim.uikit.cache.FriendDataCache;
import com.netease.nim.uikit.cache.SimpleCallback;
import com.netease.nim.uikit.cache.TeamDataCache;
import com.netease.nim.uikit.model.ToolBarOptions;
import com.netease.nim.uikit.session.SessionCustomization;
import com.netease.nim.uikit.session.constant.Extras;
import com.netease.nim.uikit.session.fragment.MessageFragment;
import com.netease.nim.uikit.session.fragment.TeamMessageFragment;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.netease.nimlib.sdk.team.TeamService;
import com.netease.nimlib.sdk.team.constant.TeamTypeEnum;
import com.netease.nimlib.sdk.team.model.Team;
import com.netease.nimlib.sdk.team.model.TeamMember;
import java.util.List;
/**
* 群聊界面
* <p/>
* Created by huangjun on 2015/3/5.
*/
public class TeamMessageActivity extends BaseMessageActivity {
// model
private Team team;
private View invalidTeamTipView;
private TextView invalidTeamTipText;
private TeamMessageFragment fragment;
private Class<? extends Activity> backToClass;
public static void start(Context context, String tid, SessionCustomization customization,
Class<? extends Activity> backToClass, IMMessage anchor) {
Intent intent = new Intent();
intent.putExtra(Extras.EXTRA_ACCOUNT, tid);
intent.putExtra(Extras.EXTRA_CUSTOMIZATION, customization);
intent.putExtra(Extras.EXTRA_BACK_TO_CLASS, backToClass);
if (anchor != null) {
intent.putExtra(Extras.EXTRA_ANCHOR, anchor);
}
intent.setClass(context, TeamMessageActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(intent);
}
protected void findViews() {
invalidTeamTipView = findView(R.id.invalid_team_tip);
invalidTeamTipText = findView(R.id.invalid_team_text);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
backToClass = (Class<? extends Activity>) getIntent().getSerializableExtra(Extras.EXTRA_BACK_TO_CLASS);
findViews();
registerTeamUpdateObserver(true);
}
@Override
protected void onDestroy() {
super.onDestroy();
registerTeamUpdateObserver(false);
}
@Override
protected void onResume() {
super.onResume();
requestTeamInfo();
}
/**
* 请求群基本信息
*/
private void requestTeamInfo() {
// 请求群基本信息
Team t = NIMClient.getService(TeamService.class).queryTeamBlock(sessionId);
if (t != null) {
TeamDataCache.getInstance().addOrUpdateTeam(t);
updateTeamInfo(t);
} else {
TeamDataCache.getInstance().fetchTeamById(sessionId, new SimpleCallback<Team>() {
@Override
public void onResult(boolean success, Team result) {
if (success && result != null) {
updateTeamInfo(result);
} else {
onRequestTeamInfoFailed();
}
}
});
}
}
private void onRequestTeamInfoFailed() {
Toast.makeText(TeamMessageActivity.this, "获取群组信息失败!", Toast.LENGTH_SHORT).show();
finish();
}
/**
* 更新群信息
*
* @param d
*/
private void updateTeamInfo(final Team d) {
if (d == null) {
return;
}
team = d;
fragment.setTeam(team);
setTitle(team == null ? sessionId : team.getName() + "(" + team.getMemberCount() + "人)");
invalidTeamTipText.setText(team.getType() == TeamTypeEnum.Normal ? R.string.normal_team_invalid_tip : R.string.team_invalid_tip);
invalidTeamTipView.setVisibility(team.isMyTeam() ? View.GONE : View.VISIBLE);
}
/**
* 注册群信息更新监听
*
* @param register
*/
private void registerTeamUpdateObserver(boolean register) {
if (register) {
TeamDataCache.getInstance().registerTeamDataChangedObserver(teamDataChangedObserver);
TeamDataCache.getInstance().registerTeamMemberDataChangedObserver(teamMemberDataChangedObserver);
} else {
TeamDataCache.getInstance().unregisterTeamDataChangedObserver(teamDataChangedObserver);
TeamDataCache.getInstance().unregisterTeamMemberDataChangedObserver(teamMemberDataChangedObserver);
}
FriendDataCache.getInstance().registerFriendDataChangedObserver(friendDataChangedObserver, register);
}
/**
* 群资料变动通知和移除群的通知(包括自己退群和群被解散)
*/
TeamDataCache.TeamDataChangedObserver teamDataChangedObserver = new TeamDataCache.TeamDataChangedObserver() {
@Override
public void onUpdateTeams(List<Team> teams) {
if (team == null) {
return;
}
for (Team t : teams) {
if (t.getId().equals(team.getId())) {
updateTeamInfo(t);
break;
}
}
}
@Override
public void onRemoveTeam(Team team) {
if (team == null) {
return;
}
if (team.getId().equals(TeamMessageActivity.this.team.getId())) {
updateTeamInfo(team);
}
}
};
/**
* 群成员资料变动通知和移除群成员通知
*/
TeamDataCache.TeamMemberDataChangedObserver teamMemberDataChangedObserver = new TeamDataCache.TeamMemberDataChangedObserver() {
@Override
public void onUpdateTeamMember(List<TeamMember> members) {
fragment.refreshMessageList();
}
@Override
public void onRemoveTeamMember(TeamMember member) {
}
};
FriendDataCache.FriendDataChangedObserver friendDataChangedObserver = new FriendDataCache.FriendDataChangedObserver() {
@Override
public void onAddedOrUpdatedFriends(List<String> accounts) {
fragment.refreshMessageList();
}
@Override
public void onDeletedFriends(List<String> accounts) {
fragment.refreshMessageList();
}
@Override
public void onAddUserToBlackList(List<String> account) {
fragment.refreshMessageList();
}
@Override
public void onRemoveUserFromBlackList(List<String> account) {
fragment.refreshMessageList();
}
};
@Override
protected MessageFragment fragment() {
// 添加fragment
Bundle arguments = getIntent().getExtras();
arguments.putSerializable(Extras.EXTRA_TYPE, SessionTypeEnum.Team);
fragment = new TeamMessageFragment();
fragment.setArguments(arguments);
fragment.setContainerId(R.id.message_fragment_container);
return fragment;
}
@Override
protected int getContentViewId() {
return R.layout.nim_team_message_activity;
}
@Override
protected void initToolBar() {
ToolBarOptions options = new ToolBarOptions();
options.titleString = "群聊";
setToolBar(R.id.toolbar, options);
}
@Override
public void onBackPressed() {
super.onBackPressed();
if (backToClass != null) {
Intent intent = new Intent();
intent.setClass(this, backToClass);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
finish();
}
}
}