PageRenderTime 2957ms CodeModel.GetById 40ms RepoModel.GetById 1ms app.codeStats 0ms

/app/src/main/java/com/uprint/android_pack/cloudprint4androidmanager/activity/MineInfoActivity.java

https://gitlab.com/zhangxiaang/CloudPrint4AndroidManager
Java | 493 lines | 440 code | 45 blank | 8 comment | 47 complexity | 3d7169e005a868e7a7dd5f65fb742f38 MD5 | raw file
  1. package com.uprint.android_pack.cloudprint4androidmanager.activity;
  2. import android.content.DialogInterface;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.support.v7.app.AlertDialog;
  6. import android.support.v7.widget.Toolbar;
  7. import android.text.Editable;
  8. import android.text.TextWatcher;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.widget.AdapterView;
  12. import android.widget.ArrayAdapter;
  13. import android.widget.RadioGroup;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16. import com.alibaba.fastjson.JSON;
  17. import com.alibaba.fastjson.JSONArray;
  18. import com.alibaba.fastjson.JSONObject;
  19. import com.rengwuxian.materialedittext.MaterialAutoCompleteTextView;
  20. import com.rengwuxian.materialedittext.MaterialEditText;
  21. import com.uprint.android_pack.cloudprint4androidmanager.CPBaseActivity;
  22. import com.uprint.android_pack.cloudprint4androidmanager.R;
  23. import com.uprint.android_pack.cloudprint4androidmanager.adapter.SimpleBuildingAdapter;
  24. import com.uprint.android_pack.cloudprint4androidmanager.dataType.ManagerInfo;
  25. import com.uprint.android_pack.cloudprint4androidmanager.dataType.SchoolInfo;
  26. import com.uprint.android_pack.cloudprint4androidmanager.network.ICallBack;
  27. import com.uprint.android_pack.cloudprint4androidmanager.network.NetValue;
  28. import com.uprint.android_pack.cloudprint4androidmanager.network.biz.CommonGetBiz;
  29. import com.uprint.android_pack.cloudprint4androidmanager.network.biz.CommonPostBiz;
  30. import com.uprint.android_pack.cloudprint4androidmanager.utils.EmptyViewUtil;
  31. import com.uprint.android_pack.cloudprint4androidmanager.utils.RegexUtils;
  32. import com.uprint.android_pack.cloudprint4androidmanager.utils.SharedPreferenceUtil;
  33. import java.lang.ref.WeakReference;
  34. import java.util.ArrayList;
  35. import java.util.HashMap;
  36. import java.util.List;
  37. import java.util.Map;
  38. import butterknife.Bind;
  39. import butterknife.ButterKnife;
  40. /**
  41. * Created by zhangxiaang on 15/10/13.
  42. * todo the roles group has some problem can't react for the role changed
  43. */
  44. public class MineInfoActivity extends CPBaseActivity {
  45. public static final String TAG = "MineInfoActivity";
  46. private TextView title;
  47. private Map<String, String> params_map;
  48. private HashMap update_params_map;
  49. @Bind(R.id.managerName)
  50. TextView managerName;
  51. @Bind(R.id.QQ)
  52. TextView qq;
  53. @Bind(R.id.managerId)
  54. TextView managerId;
  55. @Bind(R.id.buildingName)
  56. TextView buildingName;
  57. @Bind(R.id.dorm)
  58. TextView dorm;
  59. @Bind(R.id.idcard)
  60. TextView idcard;
  61. @Bind(R.id.sex)
  62. TextView sex;
  63. @Bind(R.id.email)
  64. TextView email;
  65. @Bind(R.id.graduated)
  66. TextView graduated;
  67. @Bind(R.id.alipay)
  68. TextView alipay;
  69. public ManagerInfo managerInfo;
  70. private boolean school_select;
  71. private ArrayList<SchoolInfo> schoolInfos;
  72. private ArrayList<SchoolInfo.DormInfo> infos;
  73. private SimpleBuildingAdapter adapter1;
  74. private int single_choice;
  75. private boolean getUserInfo;
  76. private int is_girl;
  77. MaterialEditText _name;
  78. MaterialEditText _qq;
  79. MaterialAutoCompleteTextView _school;
  80. MaterialEditText _email;
  81. MaterialEditText _alipay;
  82. MaterialEditText _graduatedYear;
  83. MaterialEditText _dromname;
  84. MaterialEditText _buildings;
  85. MaterialEditText _sendBuilding;
  86. MaterialEditText _idnum;
  87. RadioGroup _group;
  88. @Override
  89. protected void onCreate(Bundle savedInstanceState) {
  90. super.onCreate(savedInstanceState);
  91. setContentView(R.layout.mineinfolayout_2);
  92. EmptyViewUtil.showLoadingDialog(this, "Loading..", true);
  93. ButterKnife.bind(this);
  94. init();
  95. }
  96. private void init() {
  97. params_map = new HashMap<>();
  98. update_params_map = new HashMap();
  99. mToolbar = (Toolbar) findViewById(R.id.toolbar);
  100. mToolbar.setNavigationIcon(R.drawable.goback);
  101. title = (TextView) mToolbar.findViewById(R.id.toolbar_title);
  102. title.setText("个人信息");
  103. setSupportActionBar(mToolbar);
  104. if (getSupportActionBar() != null) {
  105. getSupportActionBar().setHomeButtonEnabled(false);
  106. getSupportActionBar().setDisplayShowHomeEnabled(false);
  107. getSupportActionBar().setDisplayShowTitleEnabled(false);
  108. }
  109. SharedPreferenceUtil.checkToken(this);
  110. params_map.put("token", SharedPreferenceUtil.get_Token(getApplicationContext()));
  111. initPage();
  112. }
  113. public void initPage() {
  114. SharedPreferenceUtil.checkToken(this);
  115. update_params_map.put("token", SharedPreferenceUtil.get_Token(getApplicationContext()));
  116. //update the info
  117. CommonGetBiz biz = new CommonGetBiz(this);
  118. biz.request(new ICallBack() {
  119. @Override
  120. public void displayResult(int status, Object... params) {
  121. EmptyViewUtil.closeLoadingDialog();
  122. JSONObject result = (JSONObject) params[0];
  123. JSONObject info = result.getJSONArray("results").getJSONObject(0);
  124. if (result.getInteger("error") != 0 || info.getInteger("state") != 2) {
  125. new AlertDialog.Builder(MineInfoActivity.this)
  126. .setMessage("尚未填写个人,去完善个人信息")
  127. .setCancelable(false)
  128. .setPositiveButton("确认", new DialogInterface.OnClickListener() {
  129. @Override
  130. public void onClick(DialogInterface dialog, int which) {
  131. startActivity(new Intent(getApplicationContext(), ImproveInfoActivity.class));
  132. }
  133. }).create().show();
  134. return;
  135. }
  136. getUserInfo = true;
  137. managerInfo = JSON.toJavaObject(info, ManagerInfo.class);
  138. managerInfo.setID_number(info.getString("ID_number"));
  139. managerInfo.getBuilding().setID(info.getJSONObject("building").getInteger("ID"));
  140. managerInfo.getSchool().setID(info.getJSONObject("school").getInteger("ID"));
  141. for (int i = 0; i < info.getJSONArray("manage_buildings").size(); i++) {
  142. managerInfo.getManage_buildings().get(i).setID(info.getJSONArray("manage_buildings")
  143. .getJSONObject(i).getInteger("ID"));
  144. }
  145. managerInfoView(managerInfo);
  146. }
  147. }, NetValue.IMPROVE_INFO(), params_map);
  148. }
  149. public void managerInfoView(ManagerInfo managerInfo) {
  150. managerName.setText(managerInfo.getManager_name());
  151. qq.setText(managerInfo.getQq());
  152. sex.setText(managerInfo.getSex() == 0 ? "男" : "女");
  153. email.setText(managerInfo.getEmail());
  154. alipay.setText(managerInfo.getAlipay_account());
  155. graduated.setText(managerInfo.getGraduate_year());
  156. idcard.setText(managerInfo.getID_number());
  157. dorm.setText(managerInfo.getDorm());
  158. buildingName.setText(managerInfo.getBuilding().getName());
  159. StringBuilder builder = new StringBuilder();
  160. for (int i = 0; i < managerInfo.getManage_buildings().size(); i++) {
  161. builder.append(managerInfo.getManage_buildings().get(i).getName() + "\n");
  162. }
  163. int size = builder.length();
  164. if (size > 0) {
  165. builder.deleteCharAt(size - 1);
  166. managerId.setText(builder.toString());
  167. } else managerId.setText("尚未选择");
  168. }
  169. public void initDialogManagerInfo(ManagerInfo managerInfo) {
  170. _name.setText(managerInfo.getManager_name());
  171. _qq.setText(managerInfo.getQq());
  172. _school.setHint("请重新输入您的学校信息");
  173. _email.setText(managerInfo.getEmail());
  174. _graduatedYear.setText(managerInfo.getGraduate_year());
  175. _dromname.setText(managerInfo.getDorm());
  176. _alipay.setText(managerInfo.getAlipay_account());
  177. _idnum.setText(managerInfo.getID_number());
  178. if (managerInfo.getSex() == 0) {
  179. _group.check(R.id.boy);
  180. } else _group.check(R.id.girl);
  181. }
  182. public void initContentView(View contentView) {
  183. _name = (MaterialEditText) contentView.findViewById(R.id.nickname);
  184. _qq = (MaterialEditText) contentView.findViewById(R.id.qqaccount);
  185. _school = (MaterialAutoCompleteTextView) contentView.findViewById(R.id.school);
  186. _email = (MaterialEditText) contentView.findViewById(R.id.email);
  187. _alipay = (MaterialEditText) contentView.findViewById(R.id.zhifubao);
  188. _graduatedYear = (MaterialEditText) contentView.findViewById(R.id.graduatedYear);
  189. _dromname = (MaterialEditText) contentView.findViewById(R.id.dormname);
  190. _buildings = (MaterialEditText) contentView.findViewById(R.id.select_shop);
  191. _sendBuilding = (MaterialEditText) contentView.findViewById(R.id.sendBuilding);
  192. _idnum = (MaterialEditText) contentView.findViewById(R.id.idnum);
  193. _group = (RadioGroup) contentView.findViewById(R.id.radioGroup);
  194. //init the value
  195. initDialogManagerInfo(managerInfo);
  196. _group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  197. @Override
  198. public void onCheckedChanged(RadioGroup group, int checkedId) {
  199. switch (checkedId) {
  200. case R.id.boy:
  201. is_girl = 0;
  202. break;
  203. case R.id.girl:
  204. is_girl = 1;
  205. break;
  206. default:
  207. break;
  208. }
  209. }
  210. });
  211. _school.addTextChangedListener(new TextWatcher() {
  212. WeakReference<MineInfoActivity> ref = new WeakReference<>(MineInfoActivity.this);
  213. @Override
  214. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  215. _school.dismissDropDown();
  216. }
  217. @Override
  218. public void onTextChanged(CharSequence s, int start, int before, int count) {
  219. _school.dismissDropDown();
  220. }
  221. @Override
  222. public void afterTextChanged(Editable s) {
  223. String keyWord = s.toString();
  224. if (keyWord != null && keyWord.trim().length() != 0 && !school_select) {
  225. Map<String, String> params = new HashMap<>();
  226. params.put("keyword", keyWord);
  227. CommonGetBiz biz = new CommonGetBiz(ref.get());
  228. biz.request(new ICallBack() {
  229. @Override
  230. public void displayResult(int status, Object... params) {
  231. List<String> school_name = new ArrayList<>();
  232. schoolInfos = new ArrayList<>();
  233. JSONObject result = (JSONObject) params[0];
  234. if (result.getInteger("error") != 0) {
  235. Toast.makeText(ref.get(), result.getString("msg"), Toast.LENGTH_SHORT).show();
  236. return;
  237. }
  238. JSONArray array = result.getJSONArray("results");
  239. if (array.size() == 0) {
  240. Toast.makeText(ref.get(), "抱歉,您的学校暂时未收录", Toast.LENGTH_SHORT).show();
  241. return;
  242. }
  243. for (int i = 0; i < array.size(); i++) {
  244. SchoolInfo school = JSON.parseObject(array.getJSONObject(i).toJSONString(), SchoolInfo.class);
  245. schoolInfos.add(school);
  246. }
  247. for (int i = 0; i < schoolInfos.size(); i++) {
  248. school_name.add(schoolInfos.get(i).getName());
  249. }
  250. ArrayAdapter<String> adapter = new ArrayAdapter<>(ref.get(), R.layout.school_item_layout, school_name);
  251. _school.setAdapter(adapter);
  252. _school.showDropDown();
  253. }
  254. }, NetValue.GET_SCHOOL(), params);
  255. }
  256. school_select = false;
  257. }
  258. });
  259. _school.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  260. @Override
  261. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  262. infos = schoolInfos.get(position).getBuildings();
  263. if (infos != null && infos.size() > 0) {
  264. HashMap schoolInfo = new HashMap();
  265. schoolInfo.put("ID", schoolInfos.get(position).getId());
  266. schoolInfo.put("name", schoolInfos.get(position).getName());
  267. update_params_map.put("school", schoolInfo);
  268. school_select = true;
  269. adapter1 = new SimpleBuildingAdapter(MineInfoActivity.this, infos);
  270. } else {
  271. AlertDialog.Builder builder = new AlertDialog.Builder(MineInfoActivity.this);
  272. builder.setMessage("您的学校尚无录入相应的宿舍,请联系我们的客服").setPositiveButton("确认", new DialogInterface.OnClickListener() {
  273. @Override
  274. public void onClick(DialogInterface dialog, int which) {
  275. dialog.dismiss();
  276. }
  277. }).create().show();
  278. }
  279. }
  280. });
  281. _buildings.setOnClickListener(new View.OnClickListener() {
  282. HashMap building = new HashMap();
  283. @Override
  284. public void onClick(View v) {
  285. if (infos == null) {
  286. Toast.makeText(MineInfoActivity.this, "请先选择学校", Toast.LENGTH_SHORT).show();
  287. return;
  288. }
  289. AlertDialog.Builder builder = new AlertDialog.Builder(MineInfoActivity.this);
  290. AlertDialog dialog = builder.setSingleChoiceItems(adapter1, -1, new DialogInterface.OnClickListener() {
  291. @Override
  292. public void onClick(DialogInterface dialog, int which) {
  293. single_choice = which;
  294. }
  295. }).setPositiveButton("确认", new DialogInterface.OnClickListener() {
  296. @Override
  297. public void onClick(DialogInterface dialog, int which) {
  298. building.put("ID", infos.get(single_choice).getId());
  299. building.put("name", infos.get(single_choice).getName());
  300. building.put("status", infos.get(single_choice).getStatus());
  301. update_params_map.put("building", building);
  302. _buildings.setText(infos.get(single_choice).getName());
  303. }
  304. }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
  305. @Override
  306. public void onClick(DialogInterface dialog, int which) {
  307. dialog.dismiss();
  308. }
  309. }).create();
  310. dialog.getWindow().getAttributes().windowAnimations = R.style.PauseDialogAnimation;
  311. dialog.show();
  312. }
  313. });
  314. _sendBuilding.setOnClickListener(new View.OnClickListener() {
  315. @Override
  316. public void onClick(View v) {
  317. if (infos == null) {
  318. Toast.makeText(MineInfoActivity.this, "请先选择学校", Toast.LENGTH_SHORT).show();
  319. return;
  320. }
  321. final ArrayList<SchoolInfo.DormInfo> list = new ArrayList<>(20);
  322. CharSequence[] names = new CharSequence[infos.size()];
  323. for (int i = 0; i < infos.size(); i++) {
  324. names[i] = infos.get(i).getName();
  325. }
  326. AlertDialog.Builder builder = new AlertDialog.Builder(MineInfoActivity.this);
  327. AlertDialog dialog = builder.setMultiChoiceItems(names, null, new DialogInterface.OnMultiChoiceClickListener() {
  328. @Override
  329. public void onClick(DialogInterface dialog, int which, boolean isChecked) {
  330. if (isChecked) {
  331. if (list.size() >= 20) {
  332. int index = infos.indexOf(list.get(0));
  333. ((AlertDialog) dialog).getListView().setItemChecked(index, false);
  334. list.remove(0);
  335. }
  336. list.add(infos.get(which));
  337. } else {
  338. list.remove(infos.get(which));
  339. }
  340. }
  341. }).setPositiveButton("确认", new DialogInterface.OnClickListener() {
  342. @Override
  343. public void onClick(DialogInterface dialog, int which) {
  344. ArrayList<HashMap> send_building = new ArrayList<>();
  345. for (int i = 0; i < list.size(); i++) {
  346. HashMap map = new HashMap<>();
  347. map.put("ID", list.get(i).getId());
  348. map.put("status", list.get(i).getStatus());
  349. map.put("name", list.get(i).getName());
  350. send_building.add(map);
  351. }
  352. StringBuilder builder = new StringBuilder();
  353. for (int i = 0; i < list.size(); i++) {
  354. builder.append(list.get(i).getName() + ",");
  355. }
  356. _sendBuilding.setText(getBuildingListName(list));
  357. update_params_map.put("manage_buildings", send_building);
  358. }
  359. }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
  360. @Override
  361. public void onClick(DialogInterface dialog, int which) {
  362. dialog.dismiss();
  363. }
  364. }).create();
  365. dialog.getWindow().getAttributes().windowAnimations = R.style.PauseDialogAnimation;
  366. dialog.show();
  367. }
  368. });
  369. }
  370. private String getBuildingListName(ArrayList<SchoolInfo.DormInfo> list) {
  371. StringBuilder builder = new StringBuilder();
  372. for (SchoolInfo.DormInfo info : list) {
  373. builder.append(info.getName() + ",");
  374. }
  375. int length = builder.length();
  376. builder.deleteCharAt(length - 1);
  377. return builder.toString();
  378. }
  379. public boolean validateForm() {
  380. if (_name.validateWith(RegexUtils.name()) &&
  381. _graduatedYear.validateWith(RegexUtils.year()) &&
  382. _qq.validateWith(RegexUtils.qq()) &&
  383. _idnum.validateWith(RegexUtils.idnum()) &&
  384. _email.validateWith(RegexUtils.email()) &&
  385. _alipay.validateWith(RegexUtils.zhifubao())) {
  386. return true;
  387. }
  388. return false;
  389. }
  390. public void changeInfo(View view) {
  391. if (getUserInfo) {
  392. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  393. View contentView = LayoutInflater.from(this).inflate(R.layout.changeinfo, null, false);
  394. initContentView(contentView);
  395. AlertDialog dialog = builder.setView(contentView).setCancelable(true).setPositiveButton("提交", new DialogInterface.OnClickListener() {
  396. @Override
  397. public void onClick(DialogInterface dialog, int which) {
  398. if (!validateForm()) {
  399. //todo 如何不取消当前的view?
  400. }
  401. postUpdateParams();
  402. }
  403. }).setNeutralButton("取消", new DialogInterface.OnClickListener() {
  404. @Override
  405. public void onClick(DialogInterface dialog, int which) {
  406. dialog.dismiss();
  407. }
  408. }).create();
  409. dialog.getWindow().getAttributes().windowAnimations = R.style.PauseDialogAnimation;
  410. dialog.show();
  411. } else Toast.makeText(this, "尚未获取到信息", Toast.LENGTH_SHORT).show();
  412. }
  413. public void postUpdateParams() {
  414. updateParamsMap();
  415. CommonPostBiz biz = new CommonPostBiz(this);
  416. biz.request(new ICallBack() {
  417. @Override
  418. public void displayResult(int status, Object... params) {
  419. JSONObject result = (JSONObject) params[0];
  420. if (result.getInteger("error") == 0) {
  421. Toast.makeText(MineInfoActivity.this, result.getString("msg"), Toast.LENGTH_SHORT).show();
  422. updatePageAgain();
  423. }
  424. }
  425. }, NetValue.UPDATE_MANAGER_INFO(), update_params_map);
  426. }
  427. private void updatePageAgain() {
  428. EmptyViewUtil.showLoadingDialog(this, "更新信息...", false);
  429. initPage();
  430. }
  431. //TODO 检测参数
  432. private void updateParamsMap() {
  433. update_params_map.put("manager_name", _name.getText().toString());
  434. update_params_map.put("qq", _qq.getText().toString());
  435. update_params_map.put("sex", is_girl);
  436. update_params_map.put("email", _email.getText().toString());
  437. update_params_map.put("graduate_year", _graduatedYear.getText().toString());
  438. update_params_map.put("alipay_account", _alipay.getText().toString());
  439. update_params_map.put("ID_number", _idnum.getText().toString());
  440. update_params_map.put("dorm", _dromname.getText().toString());
  441. update_params_map.put("state", managerInfo.getState());
  442. }
  443. @Override
  444. protected void onDestroy() {
  445. super.onDestroy();
  446. ButterKnife.unbind(this);
  447. }
  448. }