package com.qiniu.pili.droid.streaming;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.abooc.test.data.User;
import com.abooc.test.data.UserLoader;
import com.abooc.util.Debug;
import com.abooc.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.avos.avoscloud.im.v2.AVIMClient;
import com.avos.avoscloud.im.v2.AVIMException;
import com.avos.avoscloud.im.v2.AVIMMessageManager;
import com.avos.avoscloud.im.v2.callback.AVIMClientCallback;
import com.leancloud.im.chatroom.AVIMClientManager;
import com.leancloud.im.chatroom.ConversationEventHandler;
import com.leancloud.im.chatroom.R;
import com.leancloud.im.chatroom.activity.AVBaseActivity;

import java.util.ArrayList;

/**
 * Created by wli on 15/8/13.
 * 登陆页面，暂时未做自动登陆，每次重新进入都要再登陆一次
 */
public class AVLoginActivity extends AVBaseActivity implements OnClickListener {

    /**
     * 此处 xml 里限制了长度为 30，汉字算一个
     */
    protected EditText userNameView;
    protected Button loginButton;
    protected TextView logText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        userNameView = (EditText) findViewById(R.id.activity_login_et_username);
        logText = (TextView) findViewById(R.id.activity_login_text_log);
        loginButton = (Button) findViewById(R.id.activity_login_btn_login);
        loginButton.setOnClickListener(this);

        /**
         * 调试获取用户列表
         */
        UserLoader.loadUsers(new UserLoader.OnGetUsersListener() {
            @Override
            public void onGet(ArrayList<User> users) {
//                Object toJSON = JSONArray.toJSON(users);
                String text = JSON.toJSONString(users);
                Debug.out("content:\n" + text);
                logText.setText(text);
            }
        });

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.activity_login_btn_login) {
            openClient(userNameView.getText().toString().trim());
        }
    }

    private void openClient(String selfId) {
        if (TextUtils.isEmpty(selfId)) {
            showToast(R.string.login_null_name_tip);
            return;
        }

        loginButton.setEnabled(false);
        userNameView.setEnabled(false);
        AVIMClientManager.getInstance().open(selfId, new AVIMClientCallback() {
            @Override
            public void done(AVIMClient avimClient, AVIMException e) {
                loginButton.setEnabled(true);
                userNameView.setEnabled(true);
                if (Debug.printStackTrace(e)) {
                    Toast.show("登录失败");
                    return;
                }
                AVIMMessageManager.setConversationEventHandler(new ConversationEventHandler());
//                ChatRoomsActivity.launch(AVLoginActivity.this);
                startActivity(MainActivity.class);
                finish();
            }
        });
    }

}

@@ -258,80 +258,8 @@
 w;%0A%0A
-import com.abooc.test.data.User;%0Aimport com.abooc.test.data.UserLoader;%0A
 impo
@@ -318,42 +318,8 @@
 st;%0A
-import com.alibaba.fastjson.JSON;%0A
 impo
@@ -728,37 +728,8 @@
 y;%0A%0A
-import java.util.ArrayList;%0A%0A
 /**%0A
@@ -1472,425 +1472,8 @@
 );%0A%0A
-        /**%0A         * %E8%B0%83%E8%AF%95%E8%8E%B7%E5%8F%96%E7%94%A8%E6%88%B7%E5%88%97%E8%A1%A8%0A         */%0A        UserLoader.loadUsers(new UserLoader.OnGetUsersListener() %7B%0A            @Override%0A            public void onGet(ArrayList%3CUser%3E users) %7B%0A//                Object toJSON = JSONArray.toJSON(users);%0A                String text = JSON.toJSONString(users);%0A                Debug.out(%22content:%5Cn%22 + text);%0A                logText.setText(text);%0A            %7D%0A        %7D);%0A%0A
     

删除：去掉UserLoader；