MineFragment.java 6.9 KB
package com.hh.xuetubao.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hh.xuetubao.R;
import com.hh.xuetubao.Utils.SharedPrefrenceUtils;
import com.hh.xuetubao.activity.login.LoginActivity;
import com.hh.xuetubao.activity.personal.DeliveryActivity;
import com.hh.xuetubao.activity.personal.InfromationActivity;
import com.hh.xuetubao.activity.personal.MessageActivity;
import com.hh.xuetubao.activity.personal.MoneyActivity;
import com.hh.xuetubao.activity.personal.ResumeActivity;
import com.hh.xuetubao.activity.study.StudyNoteActivity;
import com.hh.xuetubao.bean.UserBean;
import com.hh.xuetubao.model.PersonalModel;
import com.hh.xuetubao.mvp.BaseMvpFragment;
import com.hh.xuetubao.mvp.CommonPresenter;
import com.hh.xuetubao.mvp.ICommonView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 我的Fragment
 */
public class MineFragment extends BaseMvpFragment<CommonPresenter, PersonalModel> implements ICommonView, View.OnClickListener {

    @BindView(R.id.rl_xinxi)
    RelativeLayout rlXinxi;
    @BindView(R.id.rl_jianli)
    RelativeLayout rlJianli;
    @BindView(R.id.rl_message)
    RelativeLayout rlMessage;
    @BindView(R.id.rl_money)
    RelativeLayout rlMoney;
    @BindView(R.id.rl_study)
    RelativeLayout rlStudy;
    @BindView(R.id.rl_toudi)
    RelativeLayout rlToudi;
    Unbinder unbinder;
    @BindView(R.id.message_num)
    TextView messageNum;
    @BindView(R.id.img_user)
    ImageView imgUser;
    @BindView(R.id.tv_user)
    TextView tvUser;
    @BindView(R.id.rl_cuoti)
    RelativeLayout rlCuoti;
    @BindView(R.id.rl_end)
    RelativeLayout rlEnd;
    private Intent intent;
    private FragmentTransaction transaction;
    private PraticeWrongFragment wrongFragment;
    private String accountOid;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        accountOid = SharedPrefrenceUtils.getString(getActivity(), "AccountOid");

        rlXinxi.setOnClickListener(this);
        rlJianli.setOnClickListener(this);
        rlMessage.setOnClickListener(this);
        rlMoney.setOnClickListener(this);
        rlToudi.setOnClickListener(this);
        rlStudy.setOnClickListener(this);
        rlCuoti.setOnClickListener(this);
        rlEnd.setOnClickListener(this);

    }

    @Override
    public void initData() {
        PersonalModel model = new PersonalModel();
        model.getModel(this, 1, 1, "HeadPic");
        model.getModel(this, 1, 2, "CoverPic");
        model.getModel(this, 1, 3, accountOid);
        model.getModel(this, 2, 1, accountOid);
    }

    @Override
    public CommonPresenter getPresenter() {
        return new CommonPresenter();
    }

    @Override
    public PersonalModel getModel() {
        return new PersonalModel();
    }

    @Override
    public void getData(Object o, int api, int intent) {
        if (api == 1 && intent == 1) {
            UserBean bean = (UserBean) o;
            String imgUrl = bean.getImgUrl();
            SharedPrefrenceUtils.saveString(getActivity(), "HeadPic", imgUrl);
        }
        if (api == 1 && intent == 2) {
            UserBean bean = (UserBean) o;
            String imgUrl = bean.getImgUrl();
            SharedPrefrenceUtils.saveString(getActivity(), "CoverPic", imgUrl);
        }
        if (api == 1 && intent == 3) {
            UserBean bean = (UserBean) o;
            int count = bean.getUnReadCount();
            messageNum.setText(String.valueOf(count));
        }

        if (api == 2 && intent == 1) {
            UserBean bean = (UserBean) o;
            if (bean.getUserName() != null)
                SharedPrefrenceUtils.saveString(getActivity(), "UserName", bean.getUserName());
            tvUser.setText(bean.getUserName());
            if (bean.getLoginName() != null)
                SharedPrefrenceUtils.saveString(getActivity(), "LoginName", bean.getLoginName());
            if (bean.getHeadPic() != null && bean.getHeadPic() != "") {
                SharedPrefrenceUtils.saveString(getActivity(), "ReallHeader", bean.getHeadPic());
                Glide.with(getActivity()).load(bean.getHeadPic()).into(imgUser);
            } else {
                String headPic = SharedPrefrenceUtils.getString(getActivity(), "HeadPic");
                Glide.with(getActivity()).load(headPic).into(imgUser);
            }

        }
    }

    @Override
    public void onComplete(int api) {

    }

    @Override
    public void onError(Throwable e, int api) {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onClick(View v) {

        transaction = getActivity().getSupportFragmentManager().beginTransaction();
        wrongFragment = new PraticeWrongFragment();

        switch (v.getId()) {
            case R.id.rl_xinxi:
                intent = new Intent(getActivity(), InfromationActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_jianli:
                intent = new Intent(getActivity(), ResumeActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_message:
                intent = new Intent(getActivity(), MessageActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_money:
                intent = new Intent(getActivity(), MoneyActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_toudi:
                intent = new Intent(getActivity(), DeliveryActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_study:
                intent = new Intent(getActivity(), StudyNoteActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_cuoti:
                transaction.replace(R.id.frame, wrongFragment);
                transaction.commit();
                break;
            case R.id.rl_end:
                SharedPrefrenceUtils.saveString(getActivity(),"AccountOid",null);
                startActivity(new Intent(getActivity(), LoginActivity.class));
                getActivity().finish();
                break;
        }
    }


}