package com.littlesparkle.growler.library.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.littlesparkle.growler.library.R;
import com.littlesparkle.growler.library.http.BaseHttpSubscriber;

import cn.pedant.SweetAlert.SweetAlertDialog;

public abstract class BaseActivity extends AppCompatActivity
        implements BaseHttpSubscriber.IToast, BaseHttpSubscriber.IProgress {

    private static final int QUIT_CHECK_INTERNAL = 2000;
    protected boolean mIsDoubleClickToQuit = false;
    private long mQuitTimePoint = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResId());
        initData();
        initView();
    }

    protected void initView() {
    }

    protected void initData() {
    }

    protected abstract int getLayoutResId();

    @Override
    public void onBackPressed() {
        if (!mIsDoubleClickToQuit) {
            super.onBackPressed();
        } else {
            if ((System.currentTimeMillis() - mQuitTimePoint) > QUIT_CHECK_INTERNAL) {
                Toast.makeText(getApplicationContext(), R.string.quit_confirmation,
                        Toast.LENGTH_SHORT).show();
                mQuitTimePoint = System.currentTimeMillis();
            } else {
                super.onBackPressed();
            }
        }
    }

    private SweetAlertDialog mProgressDialog = null;

    @Override
    public void showProgress() {
        if (mProgressDialog == null) {
            mProgressDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE);
            mProgressDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
            mProgressDialog.setTitleText(getString(R.string.please_waiting));
            mProgressDialog.setCancelable(true);
        }
        mProgressDialog.show();
    }

    @Override
    public void dismissProgress() {
        if (mProgressDialog != null) {
            mProgressDialog.hide();
        }
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void dismissToast() {
    }
}

@@ -103,16 +103,70 @@
 Bundle;%0A
+import android.os.Handler;%0Aimport android.os.Message;%0A
 import a
@@ -345,16 +345,69 @@
 scriber;
+%0Aimport com.littlesparkle.growler.library.log.Logger;
 %0A%0Aimport
@@ -733,16 +733,1678 @@
 t = 0;%0A%0A
+    private static final int MSG_SHOW_PROGRESS = 1;%0A    private static final int MSG_HIDE_PROGRESS = 2;%0A    private static final int MSG_SHOW_TOAST = 3;%0A    private static final int MSG_HIDE_TOAST = 4;%0A    private Handler mTipsHandler = new Handler() %7B%0A        @Override%0A        public void handleMessage(Message msg) %7B%0A            switch (msg.what) %7B%0A                case MSG_SHOW_PROGRESS:%0A                    Logger.log(%22BaseActivity: show progress dialog%22);%0A                    if (mProgressDialog == null) %7B%0A                        mProgressDialog = new SweetAlertDialog(BaseActivity.this, SweetAlertDialog.PROGRESS_TYPE);%0A                        mProgressDialog.getProgressHelper().setBarColor(Color.parseColor(%22#A5DC86%22));%0A                        mProgressDialog.setTitleText(getString(R.string.please_waiting));%0A                        mProgressDialog.setCancelable(true);%0A                    %7D%0A                    if (!mProgressDialog.isShowing()) %7B%0A                        mProgressDialog.show();%0A                    %7D%0A                    break;%0A                case MSG_HIDE_PROGRESS:%0A                    Logger.log(%22BaseActivity: hide progress dialog%22);%0A                    if (mProgressDialog != null && mProgressDialog.isShowing()) %7B%0A                        mProgressDialog.dismiss();%0A                    %7D%0A                    break;%0A                case MSG_SHOW_TOAST:%0A                    Toast.makeText(BaseActivity.this, msg.obj.toString(), Toast.LENGTH_SHORT).show();%0A                    break;%0A                case MSG_HIDE_TOAST:%0A                    break;%0A                default:%0A                    break;%0A            %7D%0A        %7D%0A    %7D;%0A%0A
     @Ove
@@ -3348,121 +3348,53 @@
     
-if (mProgressDialog == null) %7B%0A            mProgressDialog = new SweetAlertDialog(this, SweetAlertDialog.
+mTipsHandler.removeMessages(MSG_SHOW_
 PROGRESS
 _TYP
@@ -3381,37 +3381,32 @@
 SG_SHOW_PROGRESS
-_TYPE
 );%0A            m
@@ -3404,521 +3404,423 @@
     
-    mProgressDialog.getProgressHelper().setBarColor(Color.parseColor(%22#A5DC86%22));%0A            mProgressDialog.setTitleText(getString(R.string.please_waiting));%0A            mProgressDialog.setCancelable(true);%0A        %7D%0A        mProgressDialog.show();%0A    %7D%0A%0A    @Override%0A    public void dismissProgress() %7B%0A        if (mProgressDialog != null) %7B%0A            mProgressDialog.hide();%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void showToast(String msg) %7B%0A        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(
+mTipsHandler.sendEmptyMessage(MSG_SHOW_PROGRESS);%0A    %7D%0A%0A    @Override%0A    public void dismissProgress() %7B%0A        mTipsHandler.removeMessages(MSG_HIDE_PROGRESS);%0A        mTipsHandler.sendEmptyMessage(MSG_HIDE_PROGRESS);%0A    %7D%0A%0A    @Override%0A    public void showToast(String msg) %7B%0A        mTipsHandler.removeMessages(MSG_SHOW_TOAST);%0A        mTipsHandler.sendMessage(mTipsHandler.obtainMessage(MSG_SHOW_TOAST, msg)
 );%0A 

refactor tips in base activity