MessageManager.java 4.81 KB
package com.shunzhi.parent.manager;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;

import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.Observer;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.StatusCode;
import com.netease.nimlib.sdk.auth.AuthService;
import com.netease.nimlib.sdk.auth.AuthServiceObserver;
import com.netease.nimlib.sdk.auth.LoginInfo;
import com.netease.nimlib.sdk.msg.MsgServiceObserve;
import com.netease.nimlib.sdk.msg.model.CustomNotification;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.share.mvpsdk.utils.ToastUtils;
import com.shunzhi.parent.AppContext;
import com.shunzhi.parent.bean.NIMLoginResultBean;
import com.shunzhi.parent.ui.activity.LoginAndRegistActivity;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.subjects.PublishSubject;
import timber.log.Timber;

/**
 * Created by 10501 on 2018/3/17.
 */

public class MessageManager {

    private static class InstanceHolder {
        static final MessageManager instance = new MessageManager();
    }

    public static MessageManager getInstance() {
        return InstanceHolder.instance;
    }

    private Observer<List<IMMessage>> messageObserver = new Observer<List<IMMessage>>() {
        @Override
        public void onEvent(List<IMMessage> imMessages) {
            onMessageReceive(imMessages);
        }
    };
    private Observer<CustomNotification> customNotificationObserver = new Observer<CustomNotification>() {
        @Override
        public void onEvent(CustomNotification customNotification) {
            onCustomNotificationReceive(customNotification);
        }
    };


    private MessageManager() {
        NIMClient.getService(MsgServiceObserve.class).observeReceiveMessage(messageObserver, true);
        NIMClient.getService(MsgServiceObserve.class).observeCustomNotification(customNotificationObserver, true);
        NIMClient.getService(AuthServiceObserver.class).observeOnlineStatus(new Observer<StatusCode>() {
            @Override
            public void onEvent(StatusCode statusCode) {
                if (statusCode == StatusCode.KICKOUT) {
                    Context context = AppContext.getInstance();
                    Intent i = new Intent(context, LoginAndRegistActivity.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(i);
                }
            }
        }, true);

    }

    private void onMessageReceive(List<IMMessage> messageList) {

    }

    private void onCustomNotificationReceive(CustomNotification customnotification) {

    }

    public Observable<NIMLoginResultBean> login(String account, String password) {
        final PublishSubject<NIMLoginResultBean> subject = PublishSubject.create();
        SharedPreferences sp = AppContext.getInstance().getApplicationContext().getSharedPreferences("info", Context.MODE_PRIVATE);
        sp.edit().putString("account", account).apply();
        sp.edit().putString("token", password).apply();
        NIMClient.getService(AuthService.class)
                .login(new LoginInfo(account, password))
                .setCallback(new RequestCallback() {
                    @Override
                    public void onSuccess(Object o) {
                        NIMLoginResultBean bean = new NIMLoginResultBean(true, 200, null);
                        subject.onNext(bean);
                        Timber.i("----===onSuccess : %s",o);
                    }

                    @Override
                    public void onFailed(int i) {
                        Timber.i("----===nim login failed : %s", i);
                        ToastUtils.showToast("云信服务器登录失败:" + i);
                        NIMLoginResultBean bean = new NIMLoginResultBean(false, i, null);
                        subject.onNext(bean);
                    }

                    @Override
                    public void onException(Throwable throwable) {
                        Timber.i("----===onException");
                        if (throwable != null) throwable.printStackTrace();
                        NIMLoginResultBean bean = new NIMLoginResultBean(false, 0, throwable);
                        subject.onNext(bean);
                    }
                });
        return subject;
    }


    public void logout() {
        NIMClient.getService(MsgServiceObserve.class).observeReceiveMessage(messageObserver, false);
        NIMClient.getService(MsgServiceObserve.class).observeCustomNotification(customNotificationObserver, false);
        NIMClient.getService(AuthService.class).logout();
    }

    public interface NIMLoginCallback {
        void onResult(boolean isSuccess, int resultCode, Throwable throwable);
    }
}