private void handleIntent(Intent intent) { mUserid = intent.getStringExtra(KEY_USERID); if (mUserid == null) { throw new IllegalArgumentException("Extra KEY_USERID is null"); } // 先加载UserInfo mUserInfo = mUserService.getUserInfo(mUserid); if (mUserInfo == null) { mUserService.syncUserInfo(mUserid, mMsgHandler); } else { updateUserInfo(); mUserService.syncUserInfo(mUserid, new Handler()); } }
private void updateUserInfo() { mUserNameView.setText(mUserInfo.getName()); mHeaderPicView.setImageUrl(mUserInfo.getProtrait(), getImageLoder()); Integer post = 0; // TODO 获取文章数 mPostsView.setText(String.valueOf(post)); Integer goods = 0; // TODO 获取赞数 mGoodsView.setText(String.valueOf(goods)); String age = new String(); try { Date birthday = new SimpleDateFormat("yyyy-MM-dd").parse(mUserInfo.getBirthday()); Calendar ca = Calendar.getInstance(); ca.setTime(birthday); int birthYear = ca.get(Calendar.YEAR); ca.setTime(new Date()); int currentYear = ca.get(Calendar.YEAR); age = String.valueOf(currentYear - birthYear); } catch (Exception e) { } mAgeView.setText(age); mCityView.setText(mUserService.loadPlaceName(mUserInfo.getCity())); mDescriptionView.setText(mUserInfo.getDescription()); mSexIconView.setImageResource(UserService.SEX_DRAWABLE_MAP.get(mUserInfo.getSex())); }
@Override public boolean handleMessage(Message msg) { switch (msg.what) { case MSG_GET_USERINFO_COMPLATE: mUserInfo = mUserService.getUserInfo(mUserid); if (mUserInfo == null) { // 如果没有找到这个userInfo,就发送错误消息 mMsgHandler.obtainMessage(BaseService.MSG_ERROR).sendToTarget(); return true; } updateUserInfo(); break; case UserService.MSG_SYNC_USERINFO_SUCCESS: case UserService.MSG_SYNC_USERINFO_FAILD: mMsgHandler.obtainMessage(MSG_GET_USERINFO_COMPLATE).sendToTarget(); break; case BaseService.MSG_ERROR: // 当没有网络的时候允许 if (msg.obj != null && msg.obj instanceof NoConnectionError) { mMsgHandler.obtainMessage(MSG_GET_USERINFO_COMPLATE).sendToTarget(); break; } // TODO 发生错误! showToast(getString(R.string.personalinfo_get_userinfo_faild)); finish(); break; case UserService.MSG_UPDATE_USERINFO_FAILD: showToast(R.string.personalinfo_update_faild); break; default: break; } return true; }
/** * 是否当前登录用户 * * @return */ private boolean isCurrentLoginUser() { LoginUserInfo currentUser = mUserService.getCurrentLoginUser(); if (currentUser == null) return false; if (mUserid.equals(currentUser.getUserId())) { return true; } else { return false; } }
@Override public void onClick(View v) { // 不是当前登录用户 if (!mUserInfo.getId().equals(mUserService.getCurrentLoginUser().getUserId())) { return; } switch (v.getId()) { case R.id.personalinfo_headerpic: if (isCurrentLoginUser()) { startAvatarPicker(); } break; case R.id.personalinfo_item_age: if (isCurrentLoginUser()) { startEditorDialog(REQUEST_CODE_AGE); } break; case R.id.personalinfo_item_city: if (isCurrentLoginUser()) { startEditorDialog(REQUEST_CODE_PLACE); } break; case R.id.personalinfo_item_description: if (isCurrentLoginUser()) { startEditorDialog(REQUEST_CODE_DESCRIPTION); } break; case R.id.personalinfo_item_sex: if (isCurrentLoginUser()) { startEditorDialog(REQUEST_CODE_SEX); } break; default: break; } }
/** 更新 当前登陆用户信息,并刷新界面 */ private void updateCurrentLoginUserInfo() { // TODO 需要检查登录? mUserService.saveUserInfo(mUserInfo, mMsgHandler); mUserService.getCurrentLoginUser().setUserInfo(mUserInfo); updateUserInfo(); }