DownloadAuditionFragment.java 6.32 KB
package com.sunvote.xpadapp.fragments;

import android.annotation.SuppressLint;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sunvote.xpadapp.R;
import com.sunvote.xpadapp.audition.DownloadType;
import com.sunvote.xpadapp.audition.EvaluationRuleInfo;
import com.sunvote.xpadapp.audition.ScoreNameInfo;
import com.sunvote.xpadapp.audition.ScoreRuleDao;
import com.sunvote.xpadapp.audition.ScoreRuleExplainInfo;
import com.sunvote.xpadapp.audition.ScoreRuleInfo;
import com.sunvote.xpadapp.audition.dao.EvaluationRuleDao;
import com.sunvote.xpadapp.audition.dao.EvaluationRuleDaoImpl;
import com.sunvote.xpadapp.audition.dao.ScoreNameDao;
import com.sunvote.xpadapp.audition.dao.ScoreNameDaoImpl;
import com.sunvote.xpadapp.audition.dao.ScoreNameRuleDao;
import com.sunvote.xpadapp.audition.dao.ScoreNameRuleDaoImpl;
import com.sunvote.xpadapp.audition.dao.ScoreNameRuleInfo;
import com.sunvote.xpadapp.audition.dao.ScoreRuleDaoImpl;
import com.sunvote.xpadapp.audition.dao.ScoreRuleExplainDao;
import com.sunvote.xpadapp.audition.dao.ScoreRuleExplainDaoImpl;
import com.sunvote.xpadapp.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/11/13 0013.
 */

@SuppressLint("ValidFragment")
public class DownloadAuditionFragment extends BaseFragment {
    //界面
    private View view;
    private TextView tvDownloadTitle,tvName,tvProgress;
    private Object object;
    private int downloadType;

    public DownloadAuditionFragment(Object obj, int mDownloadType){
        object=obj;
        downloadType=mDownloadType;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_audition_fragment,container,false);
        initView();
        initData();
        return view;
    }

    private void initView(){
        tvDownloadTitle=view.findViewById(R.id.tv_download_title);
        tvName=view.findViewById(R.id.tv_name);
        tvProgress=view.findViewById(R.id.tv_progress);
        //tvProgress.setText(String.format("%s%s", "进度: ", "下载中"));
        tvDownloadTitle.setText(getString(R.string.download));

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ;
            }
        });
    }


    private void initData(){
        DownloadType type = DownloadType.getDownloadType(downloadType);
        tvName.setText(String.format("%s%s", "表名: ", type.toString()));
        if(object==null){
            return;
        }
        switch (type){
            case MultipleAssessItem:
                ScoreNameDao scoreNameDao=new ScoreNameDaoImpl();
                scoreNameDao.delScoreNameInfo();
                List<ScoreNameInfo> scoreNameInfoLs= (ArrayList<ScoreNameInfo>) object;
                for(ScoreNameInfo scoreNameInfo:scoreNameInfoLs){
                    scoreNameDao.addScoreNameInfo(scoreNameInfo);
                }
                break;
            case MultipleAssessRule:
                ScoreNameRuleDao scoreNameRuleDao=new ScoreNameRuleDaoImpl();
                scoreNameRuleDao.delScoreNameRuleInfo();
                List<ScoreNameRuleInfo> scoreNameRuleInfoLs= (ArrayList<ScoreNameRuleInfo>) object;
                for(ScoreNameRuleInfo scoreNameRuleInfo:scoreNameRuleInfoLs){
                    scoreNameRuleDao.addScoreNameRuleInfo(scoreNameRuleInfo);
                }
                break;
            case ScoreRule:
                ScoreRuleDao scoreRuleDao=new ScoreRuleDaoImpl();
                scoreRuleDao.delScoreRuleInfo();
                List<ScoreRuleInfo> scoreRuleInfoLs= (ArrayList<ScoreRuleInfo>) object;
                for(ScoreRuleInfo scoreRuleInfo:scoreRuleInfoLs){
                    scoreRuleDao.addScoreRuleInfo(scoreRuleInfo);
                }
                scoreRuleDao.addDefalutScoreRuleInfo();
                break;
            case EvaluationRule:
                EvaluationRuleDao evaluationRuleDao=new EvaluationRuleDaoImpl();
                evaluationRuleDao.delEvaluationRuleInfo();
                List<EvaluationRuleInfo> evaluationRuleInfoLs= (ArrayList<EvaluationRuleInfo>) object;
                for(EvaluationRuleInfo evaluationRuleInfo:evaluationRuleInfoLs){
                    evaluationRuleDao.addEvaluationRuleInfo(evaluationRuleInfo);
                }
                break;
            case ScoreRuleExplain:
                ScoreRuleExplainDao scoreRuleExplainDao=new ScoreRuleExplainDaoImpl();
                scoreRuleExplainDao.delScoreRuleExplainInfo();
                List<ScoreRuleExplainInfo> scoreRuleExplainInfoLs= (ArrayList<ScoreRuleExplainInfo>) object;
                for(ScoreRuleExplainInfo scoreRuleExplainInfo:scoreRuleExplainInfoLs){
                    scoreRuleExplainDao.addScoreRuleExplainInfo(scoreRuleExplainInfo);
                }
                break;
            default:
                    break;
        }
        //tvProgress.setText(String.format("%s%s", "进度: ", "下载完成"));
        tvDownloadTitle.setText("下载完成");
        Message message = new Message();
        myHandler.sendMessage(message);
    }

    public void onSubmitData(int type, Object obj) {
        object=obj;
        downloadType=type;
        initData();
    }

    /**
     * 消息处理
     */
    Handler myHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
        new Handler().postDelayed(new Runnable(){
            public void run() {
                 goBack();
            }
        }, 500);
        }
    };

    @SuppressLint("NewApi")
    private void goBack() {
        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction tx = fm.beginTransaction();
        tx.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
        tx.remove(this);
        tx.commit();
        mMainActivity.addCurrFragment=null;
        if(mMainActivity.currFragment != null){
            mMainActivity.currFragment.onDownUpdate();
        }
    }
}