com.umeng.common.ui.presenter.impl.UserSettingPresenter.java Source code

Java tutorial

Introduction

Here is the source code for com.umeng.common.ui.presenter.impl.UserSettingPresenter.java

Source

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2015 Umeng, Inc
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.umeng.common.ui.presenter.impl;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

import com.umeng.comm.core.beans.CommConfig;
import com.umeng.comm.core.beans.CommUser;
import com.umeng.comm.core.constants.Constants;
import com.umeng.comm.core.constants.ErrorCode;
import com.umeng.comm.core.db.ctrl.impl.DatabaseAPI;
import com.umeng.comm.core.impl.CommunityFactory;
import com.umeng.comm.core.listeners.Listeners;
import com.umeng.comm.core.listeners.Listeners.CommListener;
import com.umeng.comm.core.listeners.Listeners.FetchListener;
import com.umeng.comm.core.nets.Response;
import com.umeng.comm.core.nets.responses.ConfigResponse;
import com.umeng.comm.core.nets.responses.LoginResponse;
import com.umeng.comm.core.utils.CommonUtils;
import com.umeng.comm.core.utils.LoginHelper;
import com.umeng.comm.core.utils.ToastMsg;
import com.umeng.common.ui.mvpview.MvpUserProfileSettingView;
import com.umeng.common.ui.presenter.BaseFragmentPresenter;
import com.umeng.common.ui.util.BroadcastUtils;

public class UserSettingPresenter extends BaseFragmentPresenter<CommUser> {
    Activity mActivity;
    MvpUserProfileSettingView mProfileSettingView;
    private boolean isFirstSetting = false;// ??

    public UserSettingPresenter(Activity context, MvpUserProfileSettingView view) {
        mActivity = context;
        mCommunitySDK = CommunityFactory.getCommSDK(context);
        mProfileSettingView = view;
    }

    private void initCommConfig(final Context context) {
        if (!CommonUtils.isLogin(context)) {
            return;
        }
        mCommunitySDK.initCommConfig(new Listeners.SimpleFetchListener<ConfigResponse>() {

            @Override
            public void onComplete(ConfigResponse response) {
                Intent intent = new Intent();
                intent.setAction(Constants.ACTION_INIT_SUCCESS);
                LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

            }
        });
    }

    public void registertowsq(final CommUser user) {
        FetchListener<LoginResponse> wrapListener = new FetchListener<LoginResponse>() {

            @Override
            public void onStart() {
                mProfileSettingView.showLoading(true);
            }

            @Override
            public void onComplete(LoginResponse response) {
                mProfileSettingView.showLoading(false);
                if (response.errCode == ErrorCode.NO_ERROR) {

                    LoginHelper.loginSuccess(mActivity, response.result, user.source);
                    initCommConfig(mActivity);
                    // ??????
                    Intent intentb = new Intent();
                    intentb.setAction(Constants.ACTION_LOGIN_SUCCESS);
                    // LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
                    mActivity.sendBroadcast(intentb);
                    if (getIntentClass() != null && Constants.VERSION != 3) {
                        Intent intent = new Intent(mActivity, getIntentClass());
                        mActivity.startActivity(intent);
                    }
                    mActivity.finish();
                }

                showResponseToast(response);
            }
        };
        //        if (user.source == null) {
        //            mCommunitySDK.registerBySelfAccount(user, wrapListener);
        //
        //        } else {
        mCommunitySDK.registerByWsq(user, Constants.USER_PASSWORD, wrapListener);
        //        }

    }

    public void register(final CommUser user) {
        FetchListener<LoginResponse> wrapListener = new FetchListener<LoginResponse>() {

            @Override
            public void onStart() {
                mProfileSettingView.showLoading(true);
            }

            @Override
            public void onComplete(LoginResponse response) {
                mProfileSettingView.showLoading(false);
                if (response.errCode == ErrorCode.NO_ERROR) {

                    LoginHelper.loginSuccess(mActivity, response.result, user.source);
                    initCommConfig(mActivity);
                    // ??????
                    Intent intentb = new Intent();
                    intentb.setAction(Constants.ACTION_LOGIN_SUCCESS);
                    // LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
                    mActivity.sendBroadcast(intentb);
                    if (getIntentClass() != null && Constants.VERSION != 3) {
                        Intent intent = new Intent(mActivity, getIntentClass());
                        mActivity.startActivity(intent);
                    }
                    mActivity.finish();
                }

                showResponseToast(response);
            }
        };
        if (user.source == null) {
            mCommunitySDK.registerBySelfAccount(user, wrapListener);

        } else {
            mCommunitySDK.register(user, wrapListener);
        }

    }

    private Class<?> getIntentClass() {
        Class<?> guideclass = null;
        if (Constants.VERSION == 2) {
            try {
                guideclass = Class.forName("com.umeng.comm.ui.activities.GuideActivity");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            if (guideclass == null) {
                try {
                    guideclass = Class.forName("com.umeng.commm.ui.activities.GuideActivity");
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        } else if (Constants.VERSION == 1) {
            try {
                guideclass = Class.forName("com.umeng.commm.ui.activities.GuideActivity");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            if (guideclass == null) {
                try {
                    guideclass = Class.forName("com.umeng.comm.ui.activities.GuideActivity");
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        } else {
            try {
                guideclass = Class.forName("com.umeng.comm.ui.activities.GuideActivity");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            if (guideclass == null) {
                try {
                    guideclass = Class.forName("com.umeng.commm.ui.activities.GuideActivity");
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
        return guideclass;
    }

    public void updateUserProfile(final CommUser user) {

        mCommunitySDK.updateUserProfile(user, new CommListener() {

            @Override
            public void onStart() {
                mProfileSettingView.showLoading(true);
            }

            @Override
            public void onComplete(Response response) {
                mProfileSettingView.showLoading(false);
                showResponseToast(response);

                if (response.errCode == ErrorCode.NO_ERROR) {
                    CommConfig.getConfig().loginedUser = user;
                    saveUserInfo(response, user);
                    BroadcastUtils.sendUserUpdateBroadcast(mActivity, user);
                } else {
                    showResponseToast(response);
                }
            }
        });
    }

    /**
     * ??</br>
     * 
     * @param data
     * @param newUser
     */
    //// TODO: 16/1/25 ###
    private void saveUserInfo(Response data, CommUser newUser) {
        DatabaseAPI.getInstance().getUserDBAPI().saveUserInfoToDB(newUser);
        CommonUtils.saveLoginUserInfo(mActivity, newUser);

        if (isFirstSetting) {
            isFirstSetting = false;
            if (Constants.VERSION != 3 && getIntentClass() != null) {
                Intent intent = new Intent(mActivity, getIntentClass());
                mActivity.startActivity(intent);
            }
            mActivity.finish();
        }
    }

    public void setFirstSetting(boolean isFirstSetting) {
        this.isFirstSetting = isFirstSetting;
    }

    /**
     * ??Toast?</br>
     * 
     * @param data
     */
    private void showResponseToast(Response data) {
        if (data.errCode == ErrorCode.NO_ERROR) {
            ToastMsg.showShortMsgByResName("umeng_comm_update_info_success");
        } else if (data.errCode == ErrorCode.SENSITIVE_ERR_CODE) { // ???
            ToastMsg.showShortMsgByResName("umeng_comm_username_sensitive");
        } else if (data.errCode == ErrorCode.ERR_CODE_USER_NAME_DUPLICATE) { // ??
            ToastMsg.showShortMsgByResName("umeng_comm_duplicate_name");
        } else if (data.errCode == ErrorCode.ERR_CODE_USER_NAME_ILLEGAL_CHAR) {
            ToastMsg.showShortMsgByResName("umeng_comm_user_name_illegal_char");
        } else {
            ToastMsg.showShortMsgByResName("umeng_comm_update_userinfo_failed");
        }
    }
}