com.grass.caishi.cc.activity.main.SettingsFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.grass.caishi.cc.activity.main.SettingsFragment.java

Source

/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.grass.caishi.cc.activity.main;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.easemob.util.DeviceUuidFactory;
import com.grass.caishi.cc.Constant;
import com.grass.caishi.cc.MyApplication;
import com.grass.caishi.cc.R;
import com.grass.caishi.cc.activity.AdActivity;
import com.grass.caishi.cc.activity.purse.MoneyActivity;
import com.grass.caishi.cc.activity.MyMatchActivity;
import com.grass.caishi.cc.activity.MyNoticeMessageActivity;
import com.grass.caishi.cc.activity.MyPriceActivity;
import com.grass.caishi.cc.activity.OrderListActivity;
import com.grass.caishi.cc.activity.SettingMainActivity;
import com.grass.caishi.cc.activity.SettingUserActivity;
import com.grass.caishi.cc.activity.reply.AutoReplyActivity;
import com.grass.caishi.cc.utils.CommonUtils;
import com.grass.caishi.cc.utils.HttpRestClient;
import com.grass.caishi.cc.utils.ImageUtils;
import com.grass.caishi.cc.utils.JsonToMapList;
import com.grass.caishi.cc.utils.PreferenceUtils;
import com.grass.view.RoundImageView;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import org.apache.http.Header;

import java.util.Map;

/**
 * ?
 * 
 * @author Administrator
 * 
 */
public class SettingsFragment extends Fragment implements OnClickListener {

    private View btn_userinfo;
    private View btn_quan;
    private View btn_hot;
    private View btn_price;
    private View btn_ad;
    private View btn_money;
    private View btn_set;
    private View btn_order;
    private View btn_reply_auto;

    private RoundImageView user_logo;
    private ImageView user_sex;
    private TextView user_nick;
    private TextView user_name;
    private TextView user_vote;
    private TextView voteMsgNum;

    private SwipeRefreshLayout swipeRefreshLayout;

    private ProgressDialog dialog;

    private boolean isInit = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_conversation_setting, container, false);
    }

    /*
     * rl_user_pic rl_user_nicheng rl_user_xingbie rl_user_nianling rl_user_chengshi rl_user_zainadongtai iv_user_photo tv_user_nicheng tv_user_xingbie tv_user_nianling tv_user_chengshi tv_user_zainadongtai UserPic UserNickName UserSex UserAge UserArea UserZaina
     */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (savedInstanceState != null && savedInstanceState.getBoolean("isConflict", false))
            return;
        dialog = new ProgressDialog(getActivity());
        dialog.setMessage("?...");
        DeviceUuidFactory uuid = new DeviceUuidFactory(getActivity());
        InitView();
        InitData(false);

    }

    PreferenceUtils pu;

    public void InitData(boolean bool) {
        if (!isInit)
            return;
        if (bool) {
            // if(MainActivity){
            // dialog.setMessage("?...");
            // dialog.show();
            // }
            refreshData();
        } else {
            String nick = pu.getSettingUserNick();
            String name = pu.getSettingUserName();
            int vote = pu.getSettingUserVote();
            int sex = pu.getSettingUserSex();
            String logo = pu.getSettingUserLogo();

            ImageUtils.LoadThumbImage(user_logo, logo);
            user_name.setText(name);
            user_nick.setText(nick);
            user_vote.setText(vote + "");

            user_logo.setBorderColor(sex == 1);
            //         if (sex == 1) {
            //            user_sex.setImageResource(R.drawable.icon_boy_32);
            //         } else {
            //            user_sex.setImageResource(R.drawable.icon_girl_32);
            //         }
        }

        // refreshVoteMessage();
    }

    private void InitView() {
        pu = PreferenceUtils.getInstance(getActivity());
        btn_userinfo = getView().findViewById(R.id.setting_view_btn_userinfo);
        btn_quan = getView().findViewById(R.id.setting_view_btn_match_msg);
        btn_hot = getView().findViewById(R.id.setting_view_btn_hot);
        btn_price = getView().findViewById(R.id.setting_view_btn_price);
        btn_ad = getView().findViewById(R.id.setting_view_btn_ad);
        btn_money = getView().findViewById(R.id.setting_view_btn_money);
        btn_set = getView().findViewById(R.id.setting_view_btn_set);
        btn_order = getView().findViewById(R.id.setting_view_btn_order);
        btn_reply_auto = getView().findViewById(R.id.setting_view_btn_reply_auto);

        btn_userinfo.setOnClickListener(this);
        btn_quan.setOnClickListener(this);
        btn_hot.setOnClickListener(this);
        btn_price.setOnClickListener(this);
        btn_ad.setOnClickListener(this);
        btn_money.setOnClickListener(this);
        btn_set.setOnClickListener(this);
        btn_order.setOnClickListener(this);
        btn_reply_auto.setOnClickListener(this);

        user_logo = (RoundImageView) getView().findViewById(R.id.setting_main_image_logo);
        user_sex = (ImageView) getView().findViewById(R.id.setting_main_image_sex);
        user_nick = (TextView) getView().findViewById(R.id.setting_main_text_nick);
        user_name = (TextView) getView().findViewById(R.id.setting_main_text_name);
        user_vote = (TextView) getView().findViewById(R.id.setting_main_text_vote);
        voteMsgNum = (TextView) getView().findViewById(R.id.setting_text_vote_msgNum);
        voteMsgNum.setVisibility(View.GONE);

        swipeRefreshLayout = (SwipeRefreshLayout) getView().findViewById(R.id.swipeLayout);
        // swipeRefreshLayout.setColorSchemeResources(R.color.swipe_color_1,
        // R.color.swipe_color_2, R.color.swipe_color_3, R.color.swipe_color_4);
        swipeRefreshLayout.setSize(SwipeRefreshLayout.DEFAULT);
        // swipeRefreshLayout.setProgressBackgroundColor(R.color.swipe_background_color);
        swipeRefreshLayout.setProgressViewEndTarget(true, 100);

        swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                // if(!swipeRefreshLayout.isRefreshing()){
                refreshData();
            }
        });
        isInit = true;
    }

    protected void refreshData() {
        RequestParams params = new RequestParams();
        params.put("user_id", MyApplication.getInstance().getUserID());
        HttpRestClient.post(Constant.GET_USER_INFO_FROM_ID, params, new TextHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                //
                swipeRefreshLayout.setRefreshing(false);
                if (CommonUtils.isNullOrEmpty(responseString)) {
                    // dialog.setMessage("?,?");
                    // dialog.dismiss();
                    // toast.setTitleText("?,?").changeAlertType(MyToast.ERROR_TYPE).setBackMenu(false).showHide();
                    return;
                }
                Map<String, Object> lm = JsonToMapList.getMap(responseString);

                if (lm.get("ok") != null && lm.get("ok").toString().equals("1")) {
                    Map<String, Object> result = JsonToMapList.getMap(lm.get("data").toString());
                    /****  ****/
                    PreferenceUtils.getInstance(getActivity()).setSettingUserID(result.get("user_id").toString());
                    // String name = result.get("user_nick").toString();
                    PreferenceUtils.getInstance(getActivity())
                            .setSettingUserName(result.get("user_name").toString());
                    PreferenceUtils.getInstance(getActivity())
                            .setSettingUserLogo(result.get("user_logo").toString());
                    PreferenceUtils.getInstance(getActivity())
                            .setSettingUserNick(result.get("user_nick").toString());
                    PreferenceUtils.getInstance(getActivity())
                            .setSettingUserSex(Integer.valueOf(result.get("user_sex").toString()));
                    PreferenceUtils.getInstance(getActivity())
                            .setSettingUserVote(Integer.valueOf(result.get("user_vote").toString()));
                    PreferenceUtils.getInstance(getActivity()).setSettingUserBG(result.get("user_bg").toString());
                    PreferenceUtils.getInstance(getActivity())
                            .setSettingUserCity(result.get("user_add_name").toString());
                    PreferenceUtils.getInstance(getActivity())
                            .setSettingUserCityCode(result.get("user_add_code").toString());
                    PreferenceUtils.getInstance(getActivity()).setSettingQuanBG(result.get("quan_bg").toString());
                    // PreferenceUtils.getInstance(getActivity()).setSettingu(result.get("quan_bg").toString());
                    if (CommonUtils.isNullOrEmpty(result.get("user_money"))) {
                        PreferenceUtils.getInstance(getActivity()).setSettingUserMoney("0.00");
                    } else {
                        PreferenceUtils.getInstance(getActivity())
                                .setSettingUserMoney(result.get("user_money").toString());
                    }

                    /*
                     * ??? progressShow = false; pd.dismiss();
                     */
                    InitData(false);

                }
                // dialog.dismiss();

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                //
                // dialog.dismiss();
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    // public void refreshVoteMessage() {
    // if (!isInit)
    // return;
    // UserDao userDao = new UserDao(getActivity());
    // int size = userDao.getVoteMessageList(true).size();
    // if (size == 0) {
    // voteMsgNum.setVisibility(View.GONE);
    // } else {
    // voteMsgNum.setText(size + "");
    // voteMsgNum.setVisibility(View.VISIBLE);
    // }
    // }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (((MainActivity) getActivity()).isConflict) {
            outState.putBoolean("isConflict", true);
        } else if (((MainActivity) getActivity()).getCurrentAccountRemoved()) {
            outState.putBoolean(Constant.ACCOUNT_REMOVED, true);
        }
    }

    @Override
    public void onClick(View arg0) {
        //
        Intent it;
        switch (arg0.getId()) {
        case R.id.setting_view_btn_userinfo:
            it = new Intent(getActivity(), SettingUserActivity.class);
            startActivityForResult(it, 888);
            break;
        case R.id.setting_view_btn_match_msg:
            // UserDao userDao = new UserDao(getActivity());
            // userDao.clearVoteMessage();
            InitData(false);
            it = new Intent(getActivity(), MyNoticeMessageActivity.class);
            startActivityForResult(it, 888);
            break;
        case R.id.setting_view_btn_money:
            startActivity(new Intent(getActivity(), MoneyActivity.class));
            break;
        case R.id.setting_view_btn_ad:
            it = new Intent(getActivity(), AdActivity.class);
            startActivity(it);
            break;
        case R.id.setting_view_btn_set:
            it = new Intent(getActivity(), SettingMainActivity.class);
            startActivityForResult(it, MainActivity.MAIN_MESSAGE_SYSTEM_OUT);
            break;
        case R.id.setting_view_btn_hot:
            it = new Intent(getActivity(), MyMatchActivity.class);
            startActivity(it);
            break;
        case R.id.setting_view_btn_price:
            it = new Intent(getActivity(), MyPriceActivity.class);
            startActivity(it);
            break;
        case R.id.setting_view_btn_order:
            it = new Intent(getActivity(), OrderListActivity.class);
            startActivity(it);
            break;
        case R.id.setting_view_btn_reply_auto:
            it = new Intent(getActivity(), AutoReplyActivity.class);
            startActivity(it);
            break;
        default:
            break;
        }

    }

}