本周完成了如下工作:
(1)初始化
通过NimClient的getService接口获取到UserService(用户资料操作相关接口)服务实例,调用getUserInfo方法从本地数据库中获取用户资料
通过NimClient的getService接口获取到UserServiceObserve(用户资料变更通知)服务实例,调用observeUserInfoUpdate方法进行用户资料变更观察者通知
public void init(){
mUserInfo = NIMClient.getService(UserService.class).getUserInfo(mMyAccount);
mUpdateListeners = new ArrayList<>();
// 初始化监听
//用户资料托管接口,提供获取用户资料,修改个人资料等
NIMClient.getService(UserServiceObserve.class)
.observeUserInfoUpdate(new Observer>() {
@Override
public void onEvent(List userInfoList) {
NimUserInfo userInfo = userInfoList.get(0);
Log.e(TAG,"UserInfoUpdate"+userInfo.toString());
}
},true);
}
(2)从服务器账户数据到本地数据库
通过NimClient的getService接口获取到UserService(用户资料操作相关接口)服务实例,调用fetchUserInfo从服务器获取用户资料
三种情况:同步成功,同步失败,同步出错
public void fetchAccountInfo(){
List accounts = new ArrayList<>();
accounts.add(mMyAccount);
NIMClient.getService(UserService.class).fetchUserInfo(accounts)
.setCallback(new RequestCallback>() {
@Override
public void onSuccess(List param) {
Log.e(TAG,"fetchAccountInfo onSuccess ");
mUserInfo = param.get(0);
// 同步成功,通知刷新
for (OnInfoUpdateListener listener : mUpdateListeners){
listener.myInfoUpdate();
}
}
@Override
public void onFailed(int code) {
Log.e(TAG,"fetchAccountInfo onFailed code " + code);
}
@Override
public void onException(Throwable exception) {
Log.e(TAG,"fetchAccountInfo onException message " + exception.getMessage());
}
});
}
(3)将数据更新到服务器
将各项属性值(头像、生日、地址、签名、昵称、性别)存储到fields
再通过NimClient的getService接口获取到UserService(用户资料操作相关接口)服务实例,调用updateUserInfo更新本人用户资料
上传更新过程同样有三种情况:上传成功(更新本地数据库(调用fetchAccountInfo)),上传失败,上传出错
public void syncChange2Service(){
Map fields = new HashMap<>();
if(!TextUtils.isEmpty(mLocalAccount.getHeadImgUrl())){
fields.put(UserInfoFieldEnum.AVATAR,mLocalAccount.getHeadImgUrl());
}
if (!TextUtils.isEmpty(mLocalAccount.getBirthDay())){
fields.put(UserInfoFieldEnum.BIRTHDAY,mLocalAccount.getBirthDay());
}
if (!TextUtils.isEmpty(mLocalAccount.getLocation())){
fields.put(UserInfoFieldEnum.EXTEND,mLocalAccount.getLocation());
}
if (!TextUtils.isEmpty(mLocalAccount.getSignature())){
fields.put(UserInfoFieldEnum.SIGNATURE,mLocalAccount.getSignature());
}
fields.put(UserInfoFieldEnum.Name,mLocalAccount.getNick());
fields.put(UserInfoFieldEnum.GENDER,mLocalAccount.getGenderEnum().getValue());
NIMClient.getService(UserService.class).updateUserInfo(fields)
.setCallback(new RequestCallback() {
@Override
public void onSuccess(Void param) {
Log.e(TAG,"syncChange2Service onSuccess");
// 上传成功,更新本地数据库
fetchAccountInfo();
}
@Override
public void onFailed(int code) {
Log.e(TAG,"syncChange2Service onFailed code " + code);
//TODO 同步失败应当后台服务上传
}
@Override
public void onException(Throwable exception) {
Log.e(TAG,"syncChange2Service onException message " + exception.getMessage());
}
});
}
其中mLocalAccount为LocalAccountBean类,LocalAccountBean.java:
设置本地账户中的各项属性以及获取属性的方法
package com.ezreal.ezchat.bean;
import com.netease.nimlib.sdk.uinfo.constant.GenderEnum;
import java.io.Serializable;
/**
* Created by 张静.
*/
public class LocalAccountBean implements Serializable {
private String mHeadImgUrl;
private String mAccount;
private String mNick;
private GenderEnum mGenderEnum;
private String mBirthDay;
private String mLocation;
private String mSignature;
public String getHeadImgUrl() {
return mHeadImgUrl;
}
public void setHeadImgUrl(String headImgUrl) {
mHeadImgUrl = headImgUrl;
}
public String getAccount() {
return mAccount;
}
public void setAccount(String account) {
mAccount = account;
}
public String getNick() {
return mNick;
}
public void setNick(String nick) {
mNick = nick;
}
public GenderEnum getGenderEnum() {
return mGenderEnum;
}
public void setGenderEnum(GenderEnum genderEnum) {
mGenderEnum = genderEnum;
}
public String getBirthDay() {
return mBirthDay;
}
public void setBirthDay(String birthDay) {
mBirthDay = birthDay;
}
public String getLocation() {
return mLocation;
}
public void setLocation(String location) {
mLocation = location;
}
public String getSignature() {
return mSignature;
}
public void setSignature(String signature) {
mSignature = signature;
}
}
(4)获取本地账户
public LocalAccountBean getLocalAccount() {
mLocalAccount = new LocalAccountBean();
mLocalAccount.setAccount(mUserInfo.getAccount());
mLocalAccount.setHeadImgUrl(mUserInfo.getAvatar());
mLocalAccount.setBirthDay(mUserInfo.getBirthday());
mLocalAccount.setNick(mUserInfo.getName());
mLocalAccount.setSignature(mUserInfo.getSignature());
mLocalAccount.setGenderEnum(mUserInfo.getGenderEnum());
String extension = mUserInfo.getExtension();
if (!TextUtils.isEmpty(extension)){
mLocalAccount.setLocation(extension);
}
return mLocalAccount;
}
附上完整NimUserHandler.java
package com.ezreal.ezchat.handler;
import android.util.Log;
import com.ezreal.ezchat.bean.LocalAccountBean;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.Observer;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.uinfo.UserService;
import com.netease.nimlib.sdk.uinfo.UserServiceObserve;
import com.netease.nimlib.sdk.uinfo.constant.UserInfoFieldEnum;
import com.netease.nimlib.sdk.uinfo.model.NimUserInfo;
import com.suntek.commonlibrary.utils.TextUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by 张静.
*/
public class NimUserHandler {
private static final String TAG = NimUserHandler.class.getSimpleName();
private static NimUserHandler instance;
private String mMyAccount;
private NimUserInfo mUserInfo;
private LocalAccountBean mLocalAccount;
private List mUpdateListeners;
public static NimUserHandler getInstance(){
if (instance == null){
synchronized (NimUserHandler.class){
if (instance == null){
instance = new NimUserHandler();
}
}
}
return instance;
}
public void init(){
mUserInfo = NIMClient.getService(UserService.class).getUserInfo(mMyAccount);
mUpdateListeners = new ArrayList<>();
// 初始化监听
//用户资料托管接口,提供获取用户资料,修改个人资料等
NIMClient.getService(UserServiceObserve.class)
.observeUserInfoUpdate(new Observer>() {
@Override
public void onEvent(List userInfoList) {
NimUserInfo userInfo = userInfoList.get(0);
Log.e(TAG,"UserInfoUpdate"+userInfo.toString());
}
},true);
}
/**
* 从服务器账户数据到本地数据库
*/
public void fetchAccountInfo(){
List accounts = new ArrayList<>();
accounts.add(mMyAccount);
NIMClient.getService(UserService.class).fetchUserInfo(accounts)
.setCallback(new RequestCallback>() {
@Override
public void onSuccess(List param) {
Log.e(TAG,"fetchAccountInfo onSuccess ");
mUserInfo = param.get(0);
// 同步成功,通知刷新
for (OnInfoUpdateListener listener : mUpdateListeners){
listener.myInfoUpdate();
}
}
@Override
public void onFailed(int code) {
Log.e(TAG,"fetchAccountInfo onFailed code " + code);
}
@Override
public void onException(Throwable exception) {
Log.e(TAG,"fetchAccountInfo onException message " + exception.getMessage());
}
});
}
public void syncChange2Service(){
Map fields = new HashMap<>();
if(!TextUtils.isEmpty(mLocalAccount.getHeadImgUrl())){
fields.put(UserInfoFieldEnum.AVATAR,mLocalAccount.getHeadImgUrl());
}
if (!TextUtils.isEmpty(mLocalAccount.getBirthDay())){
fields.put(UserInfoFieldEnum.BIRTHDAY,mLocalAccount.getBirthDay());
}
if (!TextUtils.isEmpty(mLocalAccount.getLocation())){
fields.put(UserInfoFieldEnum.EXTEND,mLocalAccount.getLocation());
}
if (!TextUtils.isEmpty(mLocalAccount.getSignature())){
fields.put(UserInfoFieldEnum.SIGNATURE,mLocalAccount.getSignature());
}
fields.put(UserInfoFieldEnum.Name,mLocalAccount.getNick());
fields.put(UserInfoFieldEnum.GENDER,mLocalAccount.getGenderEnum().getValue());
NIMClient.getService(UserService.class).updateUserInfo(fields)
.setCallback(new RequestCallback() {
@Override
public void onSuccess(Void param) {
Log.e(TAG,"syncChange2Service onSuccess");
// 上传成功,更新本地数据库
fetchAccountInfo();
}
@Override
public void onFailed(int code) {
Log.e(TAG,"syncChange2Service onFailed code " + code);
//TODO 同步失败应当后台服务上传
}
@Override
public void onException(Throwable exception) {
Log.e(TAG,"syncChange2Service onException message " + exception.getMessage());
}
});
}
public LocalAccountBean getLocalAccount() {
mLocalAccount = new LocalAccountBean();
mLocalAccount.setAccount(mUserInfo.getAccount());
mLocalAccount.setHeadImgUrl(mUserInfo.getAvatar());
mLocalAccount.setBirthDay(mUserInfo.getBirthday());
mLocalAccount.setNick(mUserInfo.getName());
mLocalAccount.setSignature(mUserInfo.getSignature());
mLocalAccount.setGenderEnum(mUserInfo.getGenderEnum());
String extension = mUserInfo.getExtension();
if (!TextUtils.isEmpty(extension)){
mLocalAccount.setLocation(extension);
}
return mLocalAccount;
}
public void setLocalAccount(LocalAccountBean account){
this.mLocalAccount = account;
}
public String getMyAccount() {
return mMyAccount;
}
public void setMyAccount(String account) {
mMyAccount = account;
}
public NimUserInfo getUserInfo() {
return mUserInfo;
}
public void setUpdateListeners(OnInfoUpdateListener listeners){
mUpdateListeners.add(listeners);
}
public interface OnInfoUpdateListener{
void myInfoUpdate();
}
}