package com.sxjs.common.base;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.sxjs.common.AppComponent;
import com.sxjs.common.GlobalAppComponent;
import com.sxjs.common.model.DataManager;
import com.sxjs.common.receiver.NetWorkChangeBroadcastReceiver;
import com.sxjs.common.util.DialogUtil;
import butterknife.Unbinder;
/**
* Created by admin on 2017/3/12.
*/
public abstract class BaseActivity extends AppCompatActivity {
protected DataManager mDataManager;
protected Context mContext;
protected Dialog loadingDialog;
protected Unbinder unbinder;
private NetWorkChangeBroadcastReceiver receiver;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDataManager = getAppComponent().getDataManager();
mContext = getAppComponent().getContext();
registerNetChangeReceiver();
}
private void registerNetChangeReceiver() {
receiver = new NetWorkChangeBroadcastReceiver(this);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(receiver , intentFilter);
}
protected AppComponent getAppComponent() {
return GlobalAppComponent.getAppComponent();
}
protected void addFragment(int containerViewId, Fragment fragment , String tag) {
final FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(containerViewId, fragment , tag);
fragmentTransaction.commit();
}
protected void showShortToast(String message){
Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();
}
protected void showLongToast(String message){
Toast.makeText(getApplicationContext(),message,Toast.LENGTH_LONG).show();
}
protected void showProgressDialog(){
this.showProgressDialog(null,null);
}
protected void showProgressDialog(String msg){
this.showProgressDialog(msg , null);
}
protected void showProgressDialog(DialogInterface.OnCancelListener listener){
this.showProgressDialog(null ,listener);
}
protected void showProgressDialog(String msg , DialogInterface.OnCancelListener listener){
if(loadingDialog == null){
loadingDialog = DialogUtil.createLoadingDialog(this, msg, listener);
}else if(!loadingDialog.isShowing()){
loadingDialog.show();
}
}
protected void hiddenProgressDialog(){
if(loadingDialog != null && loadingDialog.isShowing()){
loadingDialog.dismiss();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(loadingDialog != null && loadingDialog.isShowing()){
loadingDialog.dismiss();
loadingDialog = null;
}
if(unbinder != null){
unbinder.unbind();
}
if(null != receiver){
receiver.onDestroy();
unregisterReceiver(receiver);
receiver = null;
}
}
}