public class App extends Application{
    private static App instance;
    private static int dayNightTheme;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        dayNightTheme= (int)SharedPreferencesUtil.getParam(this, Constants.THEME,R.style.SunAppTheme);
        initBugly();
    }

    public static App getInstance(){
        return instance;
    }

    public static int getDayNightTheme() { return dayNightTheme; }

    public static void setDayNightTheme(int theme) {dayNightTheme = theme; }

    private void initBugly(){

        Beta.autoInit = true;

        /**
         * true表示初始化时自动检查升级; false表示不会自动检查升级,需要手动调用Beta.checkUpgrade()方法;
         */
        Beta.autoCheckUpgrade = (boolean)SharedPreferencesUtil.getParam(this, Constants.AUTO_CHECK_UPGRADE,true);

        /**
         * 设置升级检查周期为60s(默认检查周期为0s)，60s内SDK不重复向后台请求策略);
         */
        Beta.upgradeCheckPeriod = 60 * 1000;

        /**
         * 设置启动延时为5s（默认延时3s），APP启动1s后初始化SDK，避免影响APP启动速度;
         */
        Beta.initDelay = 5 * 1000;
        /**
         * 设置通知栏大图标，largeIconId为项目中的图片资源;
         */
        Beta.largeIconId = R.mipmap.icon_night;

        /**
         * 设置状态栏小图标，smallIconId为项目中的图片资源Id;
         */
        Beta.largeIconId = R.mipmap.icon_night;

        /**
         * 设置更新弹窗默认展示的banner，defaultBannerId为项目中的图片资源Id;
         * 当后台配置的banner拉取失败时显示此banner，默认不设置则展示“loading“;
         */
        Beta.largeIconId = R.mipmap.icon_night;

        /**
         * 设置sd卡的Download为更新资源保存目录;
         * 后续更新资源会保存在此目录，需要在manifest中添加WRITE_EXTERNAL_STORAGE权限;

        Beta.storageDir = Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
         **/

        Bugly.init(getApplicationContext(), Configs.BUGLY_APP_ID, true);//最后一个参数代表是否是debug模式

    }

    public static void checkUpgrade(){
        Beta.checkUpgrade();
    }
} public class PatternStatus {
    public static final int DATI_PATTERN = -11;//答题模式
    public static final int BETI_PATTERN = -12; //背题模式
} public enum QuestionDoneType {
    NOT_DONE,//未做
    DONE_RIGHT,//做对了
    DONE_WRONG//做错了
} public class QuestionModel {
    private int questionNum;
    private QuestionType questionType;
    private String catogory;
    private String question;
    private int optionCount;
    private List<String> optionList;
    private List<Integer> answerList;
    private QuestionDoneType newDone;//是否做了
    private QuestionDoneType done;//上次是否做了
    private List<Integer> userAnswerList;
    private boolean isCollected;
    private boolean isWrong;

    public QuestionModel(int questionNum,QuestionType questionType, String catogory,String question, List<String> optionList, List<Integer> answerList){
        this.questionNum = questionNum;
        this.questionType = questionType;
        this.catogory = catogory;
        this.question = question;
        this.optionList = optionList;
        this.optionCount = optionList!=null?optionList.size():0;
        this.answerList = answerList;
        this.newDone = QuestionDoneType.NOT_DONE;
        this.done = QuestionDoneType.NOT_DONE;
        this.userAnswerList = new ArrayList<>();
        this.isCollected = false;
        this.isWrong = false;
    }

    public QuestionModel(int questionNum,QuestionType questionType,QuestionDoneType done, String catogory,String question, List<String> optionList, List<Integer> answerList,boolean isCollected,boolean isWrong){
        this.questionNum = questionNum;
        this.questionType = questionType;
        this.question = question;
        this.catogory = catogory;
        this.optionList = optionList;
        this.optionCount = optionList!=null?optionList.size():0;
        this.answerList = answerList;
        this.done = done;
        this.userAnswerList = new ArrayList<>();
        this.isCollected = isCollected;
        this.isWrong = isWrong;
        this.newDone = QuestionDoneType.NOT_DONE;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public int getOptionCount() {
        return optionCount;
    }

    public void setOptionCount(int optionCount) {
        this.optionCount = optionCount;
    }

    public QuestionType getQuestionType() {
        return questionType;
    }

    public void setQuestionType(QuestionType questionType) {
        this.questionType = questionType;
    }

    public List<String> getOptionList() {
        return optionList;
    }

    public void setOptionList(List<String> optionList) {
        this.optionList = optionList;
    }

    public List<Integer> getAnswerList() {
        return answerList;
    }

    public void setAnswerList(List<Integer> answerList) {
        this.answerList = answerList;
    }

    public QuestionDoneType getNewDone() {
        return newDone;
    }

    public void setNewDone(QuestionDoneType newDone) {
        this.newDone = newDone;
    }

    public QuestionDoneType getDone() {
        return done;
    }

    public void setDone(QuestionDoneType done) {
        this.done = done;
    }

    public int getQuestionNum() {
        return questionNum;
    }

    public void setQuestionNum(int questionNum) {
        this.questionNum = questionNum;
    }

    public List<Integer> getUserAnswerList() {
        return userAnswerList;
    }

    public void setUserAnswerList(List<Integer> userAnswerList) {
        this.userAnswerList = userAnswerList;
    }

    public boolean isCollected() {
        return isCollected;
    }

    public void setCollected(boolean collected) {
        isCollected = collected;
    }

    public boolean isWrong() {
        return isWrong;
    }

    public void setWrong(boolean wrong) {
        isWrong = wrong;
    }

    public String getCatogory() {
        return catogory;
    }

    public void setCatogory(String catogory) {
        this.catogory = catogory;
    }

    public String toString(){
        return questionNum+" "+question;
    }
} public class QuestionModelLoadUtil {

    public static List<QuestionJsonModel> getQuestionJsonModels(Context mContext,String filename)throws IOException {
        InputStream inputstream = mContext.getClass().getClassLoader().getResourceAsStream("assets/" + filename);
        ArrayList<QuestionJsonModel> jsonModelList = null;
        if (inputstream != null) {
            String jsonString = JsonUtil.inputStreamToStr(inputstream);
            jsonModelList = JsonUtil.jsonToArrayList(QuestionJsonModel.class, jsonString);
        }
        return jsonModelList;
    }

    public static int getQuestionDoneId(Context mContext, String catogory){
        String key = catogory+"_done_id";
        int num = (Integer) SharedPreferencesUtil.getParam(mContext,key,0);
        return num;
    }

    public static void saveQuestionDoneId(Context mContext, String catogory, Integer id){
        String key = catogory+"_done_id";
        SharedPreferencesUtil.setParam(mContext,key,id);
    }

    public static int getQuestionWrongDoneId(Context mContext, String catogory){
        String key = catogory+"_wrong_done_id";
        int num = (Integer) SharedPreferencesUtil.getParam(mContext,key,0);
        return num;
    }

    public static void saveQuetionWrongDoneId(Context mContext, String catogory, Integer id){
        String key = catogory+"_wrong_done_id";
        SharedPreferencesUtil.setParam(mContext,key,id);
    }

    public static int getQuestionCollectDoneId(Context mContext, String catogory){
        String key = catogory+"_collect_done_id";
        int num = (Integer) SharedPreferencesUtil.getParam(mContext,key,0);
        return num;
    }

    public static void saveQuetionCollectDoneId(Context mContext, String catogory, Integer id){
        String key = catogory+"_collect_done_id";
        SharedPreferencesUtil.setParam(mContext,key,id);
    }

    public static void saveQuestionModelToDB(Context mContext, List<QuestionModel> modelList, QuestionDbUtil.DbProgressListener listener) {
        QuestionDbUtil questionDbUtil = new QuestionDbUtil(mContext);
        questionDbUtil.insertList(modelList,listener);
    }

    public static void saveQuestionModelToDB(Context mContext, List<QuestionModel> modelList) {
        QuestionDbUtil questionDbUtil = new QuestionDbUtil(mContext);
        questionDbUtil.insertList(modelList,null);
    }

    public static List<QuestionModel> getQuestionModelsfromDB(Context mContext,String catogory) {
        try {
            QuestionDbUtil questionDbUtil = new QuestionDbUtil(mContext);
            return questionDbUtil.getAllData(catogory);
        }catch (Exception e){
            Logger.e(e);
        }
        return new ArrayList<>();
    }

    public static List<QuestionModel> searchQuestionModelsfromDB(Context mContext,String searchText) {
        try {
            QuestionDbUtil questionDbUtil = new QuestionDbUtil(mContext);
            return questionDbUtil.getSearchData(searchText);
        }catch (Exception e){
            Logger.e(e);
        }
        return new ArrayList<>();
    }

    public static List<QuestionModel> searchQuestionModelsfromDB(Context mContext,String searchText,int limitNum, int offsetNum) {
        try {
            QuestionDbUtil questionDbUtil = new QuestionDbUtil(mContext);
            return questionDbUtil.getSearchData(searchText,limitNum, offsetNum);
        }catch (Exception e){
            Logger.e(e);
        }
        return new ArrayList<>();
    }

    public static List<QuestionModel> searchQuestionModelsfromDBWithCatagory(Context mContext,String catagory,String searchText,int limitNum, int offsetNum) {
        List<QuestionModel> results = new ArrayList();
        try {
            QuestionDbUtil questionDbUtil = new QuestionDbUtil(mContext);
            for (QuestionModel model : questionDbUtil.getSearchData(searchText,limitNum, offsetNum)){
                if (model.getCatogory().equals(catagory)){
                    results.add(model);
                }
            }
            return results;
        }catch (Exception e){
            Logger.e(e);
        }
        return results;
    }

    public static void setQuestionModelDone(Context mContext,QuestionModel questionModel){
        try{
            QuestionDbUtil questionDbUtil = new QuestionDbUtil(mContext);
            questionDbUtil.update(questionModel);
        }catch (Exception e){
            Logger.e(e);
        }
    }

    public static void setQuestionModelToCollect(Context mContext,QuestionModel questionModel){
        try{
            questionModel.setCollected(true);
            QuestionDbUtil questionDbUtil = new QuestionDbUtil(mContext);
            questionDbUtil.update(questionModel);

            String key = questionModel.getCatogory()+"_collect_count";
            int num = (int)SharedPreferencesUtil.getParam(mContext,key,0);
            SharedPreferencesUtil.setParam(mContext,key,num+1);
        }catch (Exception e){
            Logger.e(e);
        }
    }

    public static void setQuestionModelOutCollect(Context mContext,QuestionModel questionModel){
        try{
            questionModel.setCollected(false);
            QuestionDbUtil questionDbUtil = new QuestionDbUtil(mContext);
            questionDbUtil.update(questionModel);

            String key = questionModel.getCatogory()+"_collect_count";
            int num = (int)SharedPreferencesUtil.getParam(mContext,key,1);
            SharedPreferencesUtil.setParam(mContext,key,num-1);
        }catch (Exception e){
            Logger.e(e);
        }
    }

    public static void setQuestionModelToWrong(Context mContext,QuestionModel questionModel){
        try{
            questionModel.setWrong(true);
            QuestionDbUtil questionDbUtil = new QuestionDbUtil(mContext);
            questionDbUtil.update(questionModel);

            String key = questionModel.getCatogory()+"_wrong_count";
            int num = (int)SharedPreferencesUtil.getParam(mContext,key,0);
            SharedPreferencesUtil.setParam(mContext,key,num+1);
        }catch (Exception e){
            Logger.e(e);
        }
    }

    public static void setQuestionModelOutWrong(Context mContext,QuestionModel questionModel){
        try{
            questionModel.setWrong(false);
            QuestionDbUtil questionDbUtil = new QuestionDbUtil(mContext);
            questionDbUtil.update(questionModel);

            String key = questionModel.getCatogory()+"_wrong_count";
            int num = (int)SharedPreferencesUtil.getParam(mContext,key,1);
            SharedPreferencesUtil.setParam(mContext,key,num-1);
        }catch (Exception e){
            Logger.e(e);
        }
    }

    public static List<QuestionModel> getCollectQuestionModel(Context mContext,String catogory){
        try{
            QuestionDbUtil questionDbUtil = new QuestionDbUtil(mContext);
            return questionDbUtil.getCollectedData(catogory);
        }catch (Exception e){
            Logger.e(e);
        }
        return new ArrayList<>();
    }

    public static List<QuestionModel> getWrongQuestionModel(Context mContext,String catogory){
        try{
            QuestionDbUtil questionDbUtil = new QuestionDbUtil(mContext);
            return questionDbUtil.getWrongData(catogory);
        }catch (Exception e){
            Logger.e(e);
        }
        return new ArrayList<>();
    }

    public static Integer getWrongQuestionModelCount(Context mContext, String catogory){
        String key = catogory+"_wrong_count";
        int num = (int)SharedPreferencesUtil.getParam(mContext,key,0);
        return num;
    }

    public static Integer getCollectQuestionModelCount(Context mContext, String catogory){
        String key = catogory+"_collect_count";
        int num = (int)SharedPreferencesUtil.getParam(mContext,key,0);
        return num;
    }

    public static void removeWrongQuestionModels(Context mContext,String catogory){
        try{
            QuestionDbUtil questionDbUtil = new QuestionDbUtil(mContext);
            questionDbUtil.removeWrongQuestion(catogory);

            String key = catogory+"_wrong_count";
            SharedPreferencesUtil.setParam(mContext,key,0);
        }catch (Exception e){
            Logger.e(e);
        }
    }

    public static void removeCollectQuestionModels(Context mContext,String catogory){
        try{
            QuestionDbUtil questionDbUtil = new QuestionDbUtil(mContext);
            questionDbUtil.removeCollectQuestion(catogory);

            String key = catogory+"_collect_count";
            SharedPreferencesUtil.setParam(mContext,key,0);
        }catch (Exception e){
            Logger.e(e);
        }
    }
} public interface PracticePresenter extends MvpPresenter, AnswerViewPaperAdapter.AnswerPageChangeListener,QuestionDailog.QuestionDailogListener {
    void onClickDatiPattern();
    void onClickBeitiPattern();
    void changeTheme();
    void onClickQuestionsLayout();
    void onClickCollect();
    void onClickRubbishLayout();
} public class PracticeActivity extends BaseActivity implements PracticeView {

    @BindView(R.id.viewpaper)
    ViewPager answerViewPager;
    @BindView(R.id.dati_pattern_view)
    Button datiPatternView;
    @BindView(R.id.beiti_pattern_view)
    Button beitiPatternView;
    @BindView(R.id.practice_wrong_num_text)
    TextView wrongNumTextView;
    @BindView(R.id.practice_right_num_text)
    TextView rightNUmTextView;
    @BindView(R.id.practice_page_num)
    TextView PageNumTextView;
    @BindView(R.id.practise_title_back)
    ImageButton titleBack;
    @BindView(R.id.sun_night)
    ImageButton sunNight;
    @BindView(R.id.collect_iv)
    ImageView collectIamgeView;
    @BindView(R.id.circle_loading_view)
    AnimatedCircleLoadingView loadingView;
    @BindView(R.id.rubbish_layout)
    RelativeLayout rubbish_layout;

    private PracticeHandler changeViewHandler;
    private PracticeHandler progressViewHandler;
    private PracticePresenter practicePresenter;
    private AnswerViewPaperAdapter answerViewPaperAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_practice);
        ButterKnife.bind(this);
        initPresenter();
        initHandler();
    }

    private void initPresenter() {
        Intent intent = getIntent();
        String type = intent.getStringExtra(Constants.PRACTICE_TYPE);
        if (type.equals(Constants.MAIN_PRACTICE))
            practicePresenter = new MainPracticePresenterImpl(this,intent);
        else if (type.equals(Constants.COLLECT_PRACTICE))
            practicePresenter = new CollectPracticePresenterImpl(this,intent);
        else if (type.equals(Constants.WRONG_PRACTICE))
            practicePresenter = new WrongPracticePresenterImpl(this,intent);
        else if (type.equals(Constants.SEARCH_PRACTICE))
            practicePresenter = new SearchPracticePresenterImpl(this,intent);

        practicePresenter.attachView(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(practicePresenter !=null) {
            practicePresenter.detachView(false);
            if(answerViewPaperAdapter!=null){
                answerViewPaperAdapter.removeAnswerPageListener(practicePresenter);
            }
        }
    }

    private void initHandler() {
        changeViewHandler = new PracticeHandler(this, new PracticeHandler.PracticeCallback() {
            @Override
            public void handleMessge(Message msg) {
                if(answerViewPager!=null)
                    answerViewPager.setCurrentItem(msg.what,true);
            }
        });
        progressViewHandler = new PracticeHandler(this, new PracticeHandler.PracticeCallback() {
            @Override
            public void handleMessge(Message msg) {
                loadingView.setPercent(msg.what);
            }
        });
    }

    @Override
    public void initPaperAdapter(List<QuestionModel> questionModelList, int status) {
        answerViewPaperAdapter = new AnswerViewPaperAdapter(this, questionModelList,status);
        answerViewPager.setAdapter(answerViewPaperAdapter);
        answerViewPager.addOnPageChangeListener(answerViewPaperAdapter.new AnswerViewPaperListener());
        answerViewPaperAdapter.addAnswerPageListener(practicePresenter);
    }

    @Override
    public void beginProcessing() {
        loadingView.setVisibility(View.VISIBLE);
        loadingView.startDeterminate();
    }

    @Override
    public void showProcessing(int progress){
        progressViewHandler.sendEmptyMessage(progress);
    }

    @Override
    public void sucessProcessing() {
        loadingView.stopOk();
        loadingView.setVisibility(View.GONE);
    }

    @Override
    public void failProcessing(){
        loadingView.stopFailure();
        loadingView.setVisibility(View.GONE);
    }

    @Override
    public void changeToBeitiView() {
        datiPatternView.setSelected(false);
        beitiPatternView.setSelected(true);
    }

    @Override
    public void changeToDatiView() {
        datiPatternView.setSelected(true);
        beitiPatternView.setSelected(false);
    }

    @Override
    public void showError(String s) {

    }

    @Override
    public void showInfo(String s) {
        Toast.makeText(this,s,Toast.LENGTH_LONG).show();
    }

    @OnClick(R.id.dati_pattern_view)
    public void onClickDatiPatternView(){
        practicePresenter.onClickDatiPattern();
        AppEventAgent.onEvent(this,AppEvent.PRACTICE_DATI_PATTERN);
    }

    @OnClick(R.id.beiti_pattern_view)
    public void onClickBeitiPatternView(){
        practicePresenter.onClickBeitiPattern();
        AppEventAgent.onEvent(this,AppEvent.PRACTICE_BEITI_PATTERN);
    }

    @Override
    public void changeAdapterPattern(int status){
        if(answerViewPaperAdapter!=null)
            answerViewPaperAdapter.changePatternStatus(status);
    }

    @Override
    public void changePaperView(int pageNum,boolean smooth,int delaytime) {
        answerViewPaperAdapter.notifyDataSetChanged();
        if (delaytime > 0) {
            changeViewHandler.sendEmptyMessageDelayed(pageNum, delaytime);
        } else {
            answerViewPager.setCurrentItem(pageNum, smooth);
        }
    }

    @Override
    public void showRightNumView(int rightNum) {
        rightNUmTextView.setText(rightNum+"");
    }

    @Override
    public void showWrongNumView(int wrongNum) {
        wrongNumTextView.setText(wrongNum+"");
    }

    @Override
    public void showPageNumView(int pageNum,int totalNum){
        PageNumTextView.setText(pageNum+"/"+totalNum);
    }

    @Override
    public void changeToNightTheme(Intent intent) {
        restartActivity(intent);
    }

    public void changeToSunTheme(Intent intent){
        restartActivity(intent);
    }

    private void restartActivity(Intent intent){
        startActivity(intent);
        super.finish();
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }

    @OnClick(R.id.sun_night)
    public void onClickSunNight() {
        practicePresenter.changeTheme();
        AppEventAgent.onEvent(this,AppEvent.PRACTICE_THEME);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @OnClick(R.id.practise_title_back)
    public void TitleBack(){
        onBackPressed();
    }

    @Override
    public void onBackPressed() {
        practicePresenter.detachView(false);

        finish();
    }

    @OnClick(R.id.questions_layout)
    public void onClickQuestionsLayout(){
        practicePresenter.onClickQuestionsLayout();
        AppEventAgent.onEvent(this,AppEvent.PRACTICE_QUESTION_LAYOUT);
    }

    public void showQuestionDailog(List<QuestionModel> modelList,int currentPosition){
        QuestionDailog dialog = new QuestionDailog(this,modelList,R.style.questionDailog,currentPosition);
        dialog.addQuestionDailogItemListener(practicePresenter);
        Window window = dialog.getWindow();
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);
        dialog.show();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.BOTTOM;
        params.dimAmount =0.5f;
        window.setAttributes(params);
    }

    @OnClick(R.id.collect_layout)
    public void onClickCollect(){
        practicePresenter.onClickCollect();
        AppEventAgent.onEvent(this,AppEvent.PRACTICE_COLLECT);
    }

    @Override
    public void showCollectView(boolean isShow) {
        if(isShow)
            collectIamgeView.setImageResource(R.mipmap.collect_icon_yes);
        else
            collectIamgeView.setImageResource(R.mipmap.collect_icon_no);
    }

    public void hideRemoveLayout(){
        rubbish_layout.setVisibility(View.GONE);
    }

    public void showRemoveLayout(){
        rubbish_layout.setVisibility(View.VISIBLE);
    }

    @OnClick(R.id.rubbish_layout)
    public void onClickRubbish_layout(){
        practicePresenter.onClickRubbishLayout();
        AppEventAgent.onEvent(this,AppEvent.PRACTICE_RUBBISH);
    }

    private static class PracticeHandler extends Handler {
        private final WeakReference<PracticeActivity> mActivity;
        private final PracticeCallback callback;

        public PracticeHandler(PracticeActivity activity,PracticeCallback callback) {
            mActivity = new WeakReference<PracticeActivity>(activity);
            this.callback = callback;
        }

        @Override
        public void handleMessage(Message msg) {
            PracticeActivity activity = mActivity.get();
            if (activity != null && callback!=null) {
                callback.handleMessge(msg);
            }
        }

        interface PracticeCallback{
            void handleMessge(Message msg);
        }
    }

    public void finishActivity(){
        super.finish();
    }

    @Override
    public void finish() {
        practicePresenter.detachView(false);
        this.setResult(1,new Intent());
        super.finish();
    }

} public class Constants {
    /**
     *shareperference文件名
     */
    public static final String SHAREPERFERENCE_FILE_NAME = "Mr_Horse";

    /**
     * 思修道德
     */
    public static final String SXDD = "sxdd";

    public static final String SXDD_JSON_NAME = "sxdd.json";

    public static final String SXDD_NAME = "思修道德";

    public static final String SXDD_SHORT_NAME = "思修";
    /**
     * 法律基础
     */
    public static final String FLJC = "fljc";

    public static final String FLJC_JSON_NAME = "fljc.json";

    public static final String FLJC_NAME = "法律基础";

    public static final String FLJC_SHORT_NAME = "法律";
    /**
     * 马克思主义
     */
    public static final String MKSZY = "mkszy";

    public static final String MKSZY_JSON_NAME = "mkszy.json";

    public static final String MKSZY_NAME = "马克思主义";

    public static final String MKSZY_SHORT_NAME = "马克思";

    /**
     * 中国近代史
     */
    public static final String ZGJDS = "zgjds";

    public static final String ZGJDS_JSON_NAME = "zgjds.json";

    public static final String ZGJDS_NAME = "中国近代史";

    public static final String ZGJDS_SHORT_NAME = "近代史";

    /**
     *毛泽东思想
     */
    public static final String MZDSX = "mzdsx";

    public static final String MZDSX_JSON_NAME = "mzdsx.json";

    public static final String MZDSX_NAME = "毛概理论";

    public static final String MZDSX_SHORT_NAME = "毛概";

    public static final String UNKNOWN = "未知";
    /**
     * 收藏
     */
    public static final String COLLECT = "collect";

    public static final String COLLECT_NAME = "收藏题目";
    /**
     *  错题训练
     */
    public static final String WRONG = "wrong";

    public static final String WRONG_NAME = "错题训练";
    /**
     * 主题模式
     */
    public static final String THEME ="theme";
    /**
     * 做过的题目数量
     */
    public static final String HAVE_DONE_NUM = "have_done_num";
    /**
     * 上次登录日期
     */
    public static final String LAST_DAY="last_day";
    /**
     * 打卡的日数
     */
    public static final String PUNCH_DAY_NUM="punch_day_num";
    /**
     * catogory
     */
    public static final String CATOGORY = "catogory";
    /**
     * filename
     */
    public static final String FILENAME = "filename";
    /**
     * page_num
     */
    public static final String PAGE_NUM = "page_num";
    /**
     * dowloadUrl
     */
    public static final String DOWNLOAD_URL = "http://demo.pre.im/naocan";

    public static final String BACKUP_DOWNLOAD_URL = " https://beta.bugly.qq.com/naocan";

    /**
     * feedbackUrl
     */
    public static final String FEEDBACK_URL = "http://demo.pre.im/aps/";
    /**
     * feedbackKeyId
     */
    public static final String FEEDBACK_KEY_ID = "7625f978a991dcf9b680b5d997d085e1";
    /**
     * feedbackHost
     */
    public static final String FEEDBACK_HOST = "demo.pre.im";
    /**
     * feedbackOrigin
     */
    public static final String FEEDBACK_ORIGIN = "http://demo.pre.im";
    /**
     * feedbackReferer
     */
    public static final String FEEDBACK_REFERER = "http://demo.pre.im/naocan";
    /**
     * 是否自动更新
     */
    public static final String AUTO_CHECK_UPGRADE = "autoCheckUpgrade";
    /**
     * Practice种类
     */
    public static final String PRACTICE_TYPE = "practice_type";
    public static final String MAIN_PRACTICE = "main_practice";
    public static final String WRONG_PRACTICE = "wrong_practice";
    public static final String COLLECT_PRACTICE = "collect_practice";
    public static final String SEARCH_PRACTICE = "search_practice";
    /**
     * 答题或者背题模式
     */
    public static final String PATTERN_STATUS = "pattern_status";

    /**
     * 搜索
     */
    public static final String DEFAULT_CHOOSE = "全部";
} public class SharedPreferencesUtil {
    private static final String FILE_NAME = Constants.SHAREPERFERENCE_FILE_NAME;
    /**
     * 保存数据的方法
     * @param context
     * @param key
     * @param object
     */
    public static void setParam(Context context , String key, Object object){

        String type = object.getClass().getSimpleName();
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();

        if("String".equals(type)){
            editor.putString(key, (String)object);
        }
        else if("Integer".equals(type)){
            editor.putInt(key, (Integer)object);
        }
        else if("Boolean".equals(type)){
            editor.putBoolean(key, (Boolean)object);
        }
        else if("Float".equals(type)){
            editor.putFloat(key, (Float)object);
        }
        else if("Long".equals(type)){
            editor.putLong(key, (Long)object);
        }

        editor.commit();
    }

    /**
     * 得到保存数据的方法，我们根据默认值得到保存的数据的具体类型，然后调用相对于的方法获取值
     * @param context
     * @param key
     * @param defaultObject
     * @return
     */
    public static Object getParam(Context context , String key, Object defaultObject){
        String type = defaultObject.getClass().getSimpleName();
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);

        if("String".equals(type)){
            return sp.getString(key, (String)defaultObject);
        }
        else if("Integer".equals(type)){
            return sp.getInt(key, (Integer)defaultObject);
        }
        else if("Boolean".equals(type)){
            return sp.getBoolean(key, (Boolean)defaultObject);
        }
        else if("Float".equals(type)){
            return sp.getFloat(key, (Float)defaultObject);
        }
        else if("Long".equals(type)){
            return sp.getLong(key, (Long)defaultObject);
        }
        return null;
    }
} public interface MvpView {

} public interface PracticeView extends MvpView {
    void initPaperAdapter(List<QuestionModel> questionModelList, int status);
    void changeToBeitiView();
    void changeToDatiView();
    void showError(String s);
    void showInfo(String s);
    void changeAdapterPattern(int status);
    void changePaperView(int pageNum,boolean smooth,int delaytime);
    void showRightNumView(int rightNum);
    void showWrongNumView(int wrongNum);
    void showPageNumView(int pageNum,int totalNum);
    void changeToNightTheme(Intent intent);
    void changeToSunTheme(Intent intent);
    void showQuestionDailog(List<QuestionModel> modelList,int currentPosition);
    void showCollectView(boolean isShow);
    void beginProcessing();
    void showProcessing(int progress);
    void sucessProcessing();
    void failProcessing();
    void hideRemoveLayout();
    void showRemoveLayout();
    void finishActivity();
}
import android.content.Context;
import android.content.Intent;
import com.jscheng.mr_horse.App;
import com.jscheng.mr_horse.R;
import com.jscheng.mr_horse.model.PatternStatus;
import com.jscheng.mr_horse.model.QuestionDoneType;
import com.jscheng.mr_horse.model.QuestionModel;
import com.jscheng.mr_horse.model.QuestionModelLoadUtil;
import com.jscheng.mr_horse.presenter.PracticePresenter;
import com.jscheng.mr_horse.ui.PracticeActivity;
import com.jscheng.mr_horse.utils.Constants;
import com.jscheng.mr_horse.utils.SharedPreferencesUtil;
import com.jscheng.mr_horse.view.MvpView;
import com.jscheng.mr_horse.view.PracticeView;
import java.util.ArrayList;
import java.util.List;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
package com.jscheng.mr_horse.presenter.impl;


/**
 * Created by cheng on 2017/4/19.
 */
public class WrongPracticePresenterImpl implements PracticePresenter {
    private List<QuestionModel> questionModelList;
    private int patternStatus;
    private PracticeView mPracticeView;
    private Context mContext;
    private int pageNum;
    private int rightNum;
    private int wrongNum;
    private boolean loading;
    private String catogory;
    private Observable loadSubscription;
    private Subscriber subscriber;

    public WrongPracticePresenterImpl(Context context, Intent intent) {
        this.mContext = context;
        this.patternStatus = intent.getIntExtra(Constants.PATTERN_STATUS,PatternStatus.DATI_PATTERN);//默认是答题模式