package com.jiuqi.njt.register;
import java.util.ArrayList;
import java.util.List;
import com.jiuqi.mobile.nigo.comeclose.bean.base.RoleCode;
import com.jiuqi.njt.R;
import com.jiuqi.njt.data.MyApp;
import com.jiuqi.njt.ui.LoginActivityNew;
import com.jiuqi.njt.ui.NjtMainActivity;
import com.jiuqi.njt.util.TitleBarUtil;
import com.jiuqi.njt.util.UIUtil;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.GridView;
import android.widget.LinearLayout;
/**
* 新的选择角色页面
*
* @author joe
*
*/
public class SelectRole extends Activity implements OnClickListener {
private Button finish;
private MyApp application;
private GridView selectRole_GridView_body;
private ArrayList<RoleCode> selectRoleList;
public RoleCode[] allRoleCodes = new RoleCode[]{RoleCode.Cooper,RoleCode.Driver,RoleCode.Dealer,RoleCode.Gastation,RoleCode.Repair};
/**
* 注册页面返回的sessionId
*/
private String sessionId;
/**
* 是不是来自于其他用户注册
*/
private boolean isRegisterByOther;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
doinit();
}
private void doinit() {
initParam();
initWidgets();
initListeners();
initUI();
}
private void initParam() {
application = (MyApp) getApplication();
sessionId = getIntent().getStringExtra("sessionId");
isRegisterByOther = getIntent().getBooleanExtra("isRegisterByOther", false);
if(isRegisterByOther){
allRoleCodes = new RoleCode[]{RoleCode.Cooper,RoleCode.Dealer,RoleCode.Gastation,RoleCode.Repair};
}
}
private void initWidgets() {
setContentView(R.layout.select_role);
ViewStub rlTitleBar = (ViewStub) findViewById(R.id.titleBarStub);
TitleBarUtil.createTitleBar(this, rlTitleBar, "选择角色",
new OnClickListener() {
@Override
public void onClick(View v) {
SelectRole.this.finish();
}
});
finish = (Button) findViewById(R.id.selectRole_btn_finish);
selectRole_GridView_body = (GridView) findViewById(R.id.selectRole_GridView_body);
}
private void initListeners() {
finish.setOnClickListener(this);
}
private void initUI() {
selectRoleList = new ArrayList<RoleCode>();
GirdViewAdapter adapter = new GirdViewAdapter(SelectRole.this,
allRoleCodes, selectRoleList);
selectRole_GridView_body.setAdapter(adapter);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.selectRole_btn_finish:
if(null!=selectRoleList&&(selectRoleList.size()>0&&selectRoleList.size()<4)){
RegisterUtils.saveUserRolesByRoles(SelectRole.this, selectRoleList, sessionId,new AllTaskInterface() {
@Override
public <T> void taskFinishReturn(T... result) {
// // TODO Auto-generated method stub
// Intent intent = new Intent();
// intent.setClass(SelectRole.this,PerfectInformation.class);
// intent.putExtra("selectRoleList", selectRoleList);
// startActivity(intent);
// finish();
Intent jump = new Intent();
jump.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
String data = (String) result[0];
if ("perfectinfo".equals(data)) // 没有完善信息,就跳转到完善信息页面
{
// 如果是老用户先要插入角色,然后整合模块,(更新SessionID)
jump.setClass(SelectRole.this, PerfectInformation.class);
} else if ("index".equals(data))// 如果已经完善信息,就直接进入主页
{
// 如果是老用户先要插入角色,然后整合模块,(更新SessionID)
jump.setClass(SelectRole.this, NjtMainActivity.class);
new RegisterUtils().loginSuccess(SelectRole.this);
} else if ("chooserole".equals(data))// 如果是没选择角色,就跳转到完善信息页面
{
jump.setClass(SelectRole.this, SelectRole.class);
} else {
UIUtil.showMsg(SelectRole.this, "错误的状态");
jump.setClass(SelectRole.this, LoginActivityNew.class);
}
startActivity(jump);
finish();
}
},true);
}else{
UIUtil.showMsg(SelectRole.this, "至少选择一个角色(最多选3个角色)");
}
// Intent intent = new Intent();
// intent.setClass(SelectRole.this,PerfectInformation.class);
// intent.putExtra("selectRoleList", selectRoleList.toArray());
// startActivity(intent);
// Class[] parameterTypes = {UserRolesBean.class};
// UserBean userBean = application.getClientContext().getUser();
// for (int i = 0; i < selectRoleList.size(); i++) {
// UserRolesBean userRoleBean = new UserRolesBean();
// userRoleBean.setUserGuid(userBean.getGuid());
// userRoleBean.setCreateDate(new Date());
// RoleBean roleBean = new RoleBean();
// roleBean.setCode(selectRoleList.get(i).getCode());
// userRoleBean.setRoleBean(roleBean );
// Object[] args = {userRoleBean};
// RegisterUtils.executeTask(SelectRole.this,
// IUserRolesManager.class, "create", parameterTypes, args, new
// AllTaskInterface() {
//
// @Override
// public <T> void taskFinishReturn(T... result) {
// // TODO Auto-generated method stub
//
// }
// });
// }
break;
default:
break;
}
}
private class GirdViewAdapter extends BaseAdapter {
private Context context;
private RoleCode[] allRoleCodes;
private LayoutInflater inflater;
private List<RoleCode> roleCodeList;
public GirdViewAdapter(Context context, RoleCode[] allRoleCode,
List<RoleCode> roleCodeList) {
super();
this.context = context;
this.allRoleCodes = allRoleCode;
this.roleCodeList = roleCodeList;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return allRoleCodes.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return allRoleCodes[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
if (convertView == null) {
LinearLayout lay = new LinearLayout(context);
CheckBox roleCheckBox = new CheckBox(context);
lay.addView(roleCheckBox);
convertView = lay;
holder = new ViewHolder();
holder.roleCheckBox = roleCheckBox;
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
int width = UIUtil.getScreenWidth(SelectRole.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
int magin = UIUtil.converNumberToDIP(SelectRole.this, 10);
int maginMore = UIUtil.converNumberToDIP(SelectRole.this, 15);
params.setMargins(magin, maginMore, magin, maginMore);
holder.roleCheckBox.setLayoutParams(params);
holder.roleCheckBox.setGravity(Gravity.CENTER);
holder.roleCheckBox.setButtonDrawable(R.drawable.btn_check);
holder.roleCheckBox.setText(allRoleCodes[position].getName());
holder.roleCheckBox.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
final RoleCode item = allRoleCodes[position];
holder.roleCheckBox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
roleCodeList.add(item);
} else {
roleCodeList.remove(item);
}
}
});
return convertView;
}
private class ViewHolder {
private CheckBox roleCheckBox;
}
}
}