/** 重新验证当前ChannelList是否是对的 */
  private void checkChannelList() {
    ArrayList<SubItem> groupSubItems = adapter.getSubLists().get(1);
    groupSubItems.clear();
    if (UserAPI.isLoggedIn()) {
      groupSubItems.add(
          new SubItem(SubItem.Section_Post, SubItem.Type_Private_Channel, "我的小组", "user_group"));
    }
    if (GroupHelper.getMyGroupsNumber() > 0) {
      // 如果已经加载了栏目
      groupSubItems.add(
          new SubItem(SubItem.Section_Post, SubItem.Type_Collections, "小组热贴", "hot_posts"));
      groupSubItems.addAll(GroupHelper.getSelectedGroupSubItems());
    } else {
      groupSubItems.addAll(ChannelHelper.getPosts());
    }

    ArrayList<SubItem> questionSubItems = adapter.getSubLists().get(2);
    questionSubItems.clear();
    if (AskTagHelper.getAskTagsNumber() > 0) {
      // 如果已经加载了栏目
      questionSubItems.add(
          new SubItem(SubItem.Section_Question, SubItem.Type_Collections, "热门问答", "hottest"));
      questionSubItems.add(
          new SubItem(SubItem.Section_Question, SubItem.Type_Collections, "精彩回答", "highlight"));
      questionSubItems.addAll(AskTagHelper.getSelectedQuestionSubItems());
    } else {
      questionSubItems.addAll(ChannelHelper.getQuestions());
    }
    adapter.notifyDataSetInvalidated();
  }
 @Override
 protected ResponseObject doInBackground(String[] params) {
   ResponseObject<ArrayList<SubItem>> result = QuestionAPI.getAllMyTags();
   if (TextUtils.isEmpty(UserAPI.getUserID())) {
     result.error_message = "无法获得用户id";
     result.error = ResponseError.NO_USER_ID;
   } else if (result.ok) {
     ArrayList<SubItem> subItems = result.result;
     ArrayList<AskTag> myTags = new ArrayList<>();
     for (int i = 0; i < subItems.size(); i++) {
       SubItem item = subItems.get(i);
       AskTag myTag = new AskTag();
       myTag.setName(item.getName());
       myTag.setValue(item.getValue());
       myTag.setType(item.getType());
       myTag.setSection(item.getSection());
       myTag.setSelected(i < defaultTagsNumber);
       myTag.setOrder(i);
       myTags.add(myTag);
     }
     mergeMyGroups(myTags);
     AskTagHelper.putAllMyTags(myTags);
     getButtons();
   }
   return result;
 }
 private void mergeMyGroups(ArrayList<AskTag> myTags) {
   if (AskTagHelper.getAskTagsNumber() > 0) {
     List<AskTag> selectedGroups = AskTagHelper.getSelectedTags();
     for (int i = 0; i < myTags.size(); i++) {
       AskTag tmpTag = myTags.get(i);
       boolean selected = false;
       for (int j = 0; j < selectedGroups.size(); j++) {
         if (selectedGroups.get(j).getValue().equals(tmpTag.getValue())) {
           selected = true;
           break;
         }
       }
       tmpTag.setSelected(selected);
       if (selected) {
         tmpTag.setOrder(i);
       } else {
         tmpTag.setOrder(1024 + i);
       }
     }
   }
 }
  public void getButtons() {

    List<AskTag> selectedSections = AskTagHelper.getSelectedTags();
    List<AskTag> unselectedSections = AskTagHelper.getUnselectedTags();

    ArrayList<MovableButton> selectedButtons = new ArrayList<>();
    for (int i = 0; i < selectedSections.size(); i++) {
      AskTag section = selectedSections.get(i);
      AskTagMovableButton button = new AskTagMovableButton(this);
      button.setSection(section);
      selectedButtons.add(button);
    }

    ArrayList<MovableButton> unselectedButtons = new ArrayList<>();
    for (int i = 0; i < unselectedSections.size(); i++) {
      AskTag section = unselectedSections.get(i);
      AskTagMovableButton button = new AskTagMovableButton(this);
      button.setSection(section);
      unselectedButtons.add(button);
    }
    desk.setSelectedButtons(selectedButtons);
    desk.setUnselectedButtons(unselectedButtons);
  }
 private void commitChanges() {
   if (desk.getSenator().getList() != null && desk.getSenator().getList().size() > 0) {
     ArrayList<MovableButton> buttons = desk.getButtons();
     ArrayList<AskTag> sections = new ArrayList<>();
     for (int i = 0; i < buttons.size(); i++) {
       AskTag askTag = (AskTag) buttons.get(i).getSection();
       if (!askTag.getSelected()) {
         askTag.setOrder(1024 + askTag.getOrder());
       }
       sections.add(askTag);
     }
     if (sections.size() > 0) {
       AskTagHelper.putAllMyTags(sections);
     }
     setResult(RESULT_OK);
   }
 }
  private void recheckData() {
    if (isFirstLoad) {
      if (UserAPI.isLoggedIn()) {
        String nameString = SharedPreferencesUtil.readString(Consts.Key_User_Name, "");
        if (!TextUtils.isEmpty(nameString)) {
          userName.setText(nameString);
        }
        String avatarString = SharedPreferencesUtil.readString(Consts.Key_User_Avatar, "");
        if (!TextUtils.isEmpty(avatarString)) {
          if (Config.shouldLoadImage()) {
            Picasso.with(getActivity())
                .load(avatarString)
                .resizeDimen(
                    R.dimen.list_standard_comment_avatar_dimen,
                    R.dimen.list_standard_comment_avatar_dimen)
                .placeholder(R.drawable.default_avatar)
                .transform(new RoundTransformation(Color.parseColor("#00000000"), 0, true))
                .into(avatarView);
          } else {
            avatarView.setImageResource(R.drawable.default_avatar);
          }
        }
        testLogin();
      }
      isFirstLoad = false;
    } else {
      if (loginState != UserAPI.isLoggedIn()) {
        checkChannelList();
        if (UserAPI.isLoggedIn()) {
          loadUserInfo();
        } else {
          back2UnLogged();
        }
      } else {
        if (UserAPI.isLoggedIn()) {
          if (userKey != null
              && userKey.equals(SharedPreferencesUtil.readString(Consts.Key_Ukey, ""))) {
            String avatarString = SharedPreferencesUtil.readString(Consts.Key_User_Avatar, "");
            if (!TextUtils.isEmpty(avatarString)) {
              if (Config.shouldLoadImage()) {
                Picasso.with(getActivity())
                    .load(avatarString)
                    .placeholder(R.drawable.default_avatar)
                    .resizeDimen(
                        R.dimen.list_standard_comment_avatar_dimen,
                        R.dimen.list_standard_comment_avatar_dimen)
                    .transform(new RoundTransformation(Color.parseColor("#00000000"), 0, true))
                    .into(avatarView);
              } else {
                avatarView.setImageResource(R.drawable.default_avatar);
              }
            }
            // 重新加载小组数据库
            long lastGroupDBVersion =
                SharedPreferencesUtil.readLong(Consts.Key_Last_Post_Groups_Version, 0);
            if (currentGroupDBVersion != lastGroupDBVersion) {
              ArrayList<SubItem> groupSubItems = adapter.getSubLists().get(1);
              groupSubItems.clear();
              groupSubItems.add(
                  new SubItem(
                      SubItem.Section_Post, SubItem.Type_Private_Channel, "我的小组", "user_group"));
              if (GroupHelper.getMyGroupsNumber() > 0) {
                // 如果已经加载了栏目
                groupSubItems.add(
                    new SubItem(
                        SubItem.Section_Post, SubItem.Type_Collections, "小组热贴", "hot_posts"));
                groupSubItems.addAll(GroupHelper.getSelectedGroupSubItems());
              } else {
                groupSubItems.addAll(ChannelHelper.getPosts());
              }
            }
            currentGroupDBVersion = lastGroupDBVersion;

            // 重新加载标签数据库
            long lastTagDBVersion =
                SharedPreferencesUtil.readLong(Consts.Key_Last_Ask_Tags_Version, 0);
            if (currentTagDBVersion != lastTagDBVersion) {
              ArrayList<SubItem> questionSubItems = adapter.getSubLists().get(2);
              if (AskTagHelper.getAskTagsNumber() > 0) {
                // 如果已经加载了栏目
                questionSubItems.clear();
                questionSubItems.add(
                    new SubItem(
                        SubItem.Section_Question, SubItem.Type_Collections, "热门问答", "hottest"));
                questionSubItems.add(
                    new SubItem(
                        SubItem.Section_Question, SubItem.Type_Collections, "精彩回答", "highlight"));
                questionSubItems.addAll(AskTagHelper.getSelectedQuestionSubItems());
              } else {
                questionSubItems.clear();
                questionSubItems.addAll(ChannelHelper.getQuestions());
              }
            }
            currentTagDBVersion = lastTagDBVersion;
            adapter.notifyDataSetChanged();
          } else {
            // 切换了用户的话
            checkChannelList();
            loadUserInfo();
          }
        }
      }
    }
    loginState = UserAPI.isLoggedIn();
    userKey = SharedPreferencesUtil.readString(Consts.Key_Ukey, "");
    if (loginState) {
      loadMessages();
    } else {
      noticeView.setVisibility(View.GONE);
    }
  }