Skip to content

Instantly share code, notes, and snippets.

@javier-delgado
Last active September 26, 2017 19:41
Show Gist options
  • Select an option

  • Save javier-delgado/7a45372e2c7f632b678849d9c28b6a57 to your computer and use it in GitHub Desktop.

Select an option

Save javier-delgado/7a45372e2c7f632b678849d9c28b6a57 to your computer and use it in GitHub Desktop.

Revisions

  1. javier-delgado revised this gist Sep 26, 2017. 1 changed file with 42 additions and 28 deletions.
    70 changes: 42 additions & 28 deletions BaseActivity.java
    Original file line number Diff line number Diff line change
    @@ -1,32 +1,36 @@
    public abstract class BaseActivity extends AppCompatActivity {
    private boolean autoBusRegistration = true;
    public final static int CLEAR_STACK = 1;

    @Override
    public boolean onSupportNavigateUp() {
    onBackPressed();
    return true;
    }
    public static final int CLEAR_STACK = 1;
    private ViewGroup overlayContainerView;
    /**
    * Si autoRegisterBus es falso la activity no sera registrada del bus automaticamente
    */
    private boolean autoRegisterBus = true;

    @Override
    protected void onResume() {
    super.onResume();
    if (autoBusRegistration) getBus().register(this);
    if (autoRegisterBus) getBus().register(this);
    }

    @Override
    protected void onPause() {
    super.onPause();
    if (autoBusRegistration) getBus().unregister(this);
    if (autoRegisterBus) getBus().unregister(this);
    }

    @Override
    public boolean onSupportNavigateUp() {
    onBackPressed();
    return super.onSupportNavigateUp();
    }

    //<editor-fold desc="Current user methods">
    protected Usuario currentUser() {
    return RegistroHelper.getUsuario();
    return LoginManager.getUsuario();
    }

    protected boolean userSignedIn() {
    return RegistroHelper.userSignedIn();
    return LoginManager.userSignedIn();
    }
    //</editor-fold>

    @@ -38,20 +42,28 @@ protected void goToActivity(Class<? extends Activity> activityClass) {
    protected void goToActivity(Class<? extends Activity> activityClass, int flag) {
    Intent intent = new Intent(this, activityClass);
    switch (flag) {
    case 1:
    case CLEAR_STACK:
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    }
    startActivity(intent);
    }
    //</editor-fold>

    //<editor-fold desc="Overlay methods">
    /**
    * Setea la vista sobre la que se mostrara el overlay para esta activity.
    * Si no se setea o se pasa null, el overlay se mostrara sobre toda la pantalla.
    * @param overlayContainerView la vista donde se mostrara el overlay
    */
    protected void setActivityOverlayContainer(ViewGroup overlayContainerView) {
    this.overlayContainerView = overlayContainerView;
    }

    protected void showActivityOverlay() {
    View overlay = findViewById(R.id.lytNetworkIndicatorOverlay);
    if (overlay == null) {
    overlay = createNetworkOverlay();
    }
    if (overlay.getVisibility() == View.VISIBLE) return;

    Animation anim = AnimationUtils.loadAnimation(this, R.anim.fade_in);
    overlay.startAnimation(anim);
    @@ -83,33 +95,35 @@ public void onAnimationRepeat(Animation animation) {
    }

    private View createNetworkOverlay() {
    View overlay = getLayoutInflater().inflate(R.layout.network_indicator_overlay, null);
    ViewGroup vg = (ViewGroup)(getWindow().getDecorView().getRootView());
    vg.addView(overlay, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    if (overlayContainerView == null) {
    overlayContainerView = (ViewGroup)(getWindow().getDecorView().getRootView());
    }
    View overlay = getLayoutInflater().inflate(R.layout.network_indicator_overlay, overlayContainerView, false);
    overlayContainerView.addView(
    overlay,
    new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
    );
    return overlay;
    }
    //</editor-fold>


    //<editor-fold desc="Event bus">
    /**
    * Este metodo indica si la activity hija se va a hacer cargo del register/unregister al bus.
    * Esto es útil cuando se desea realizar el register/unregister en un metodo distinto a resume/pause.
    * @param autoBusRegistration indica si se debe registrar automaticamente o no. (default: true)
    */
    protected final void setAutoBusRegistration(boolean autoBusRegistration) {
    this.autoBusRegistration = autoBusRegistration;
    @SuppressWarnings("ConstantConditions")
    protected void showUpButton(boolean show) {
    getSupportActionBar().setDisplayHomeAsUpEnabled(show);
    }

    protected EventBus getBus() {
    return EventBus.getDefault();
    }

    public void setAutoRegisterBus(boolean autoRegisterBus) {
    this.autoRegisterBus = autoRegisterBus;
    }

    /**
    * Este metodo evita el error de que no hay suscriptores de eventbus
    * @param event
    */
    @Subscribe
    public void dummyEvent(Void event) {}
    //</editor-fold>
    }
  2. javier-delgado revised this gist Sep 5, 2017. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions BaseActivity.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,12 @@
    public abstract class BaseActivity extends AppCompatActivity {
    private boolean autoBusRegistration = true;
    public final static int CLEAR_STACK = 1;

    @Override
    public boolean onSupportNavigateUp() {
    onBackPressed();
    return true;
    }

    @Override
    protected void onResume() {
  3. javier-delgado revised this gist Sep 5, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions BaseActivity.java
    Original file line number Diff line number Diff line change
    @@ -44,6 +44,7 @@ protected void showActivityOverlay() {
    if (overlay == null) {
    overlay = createNetworkOverlay();
    }
    if (overlay.getVisibility() == View.VISIBLE) return;

    Animation anim = AnimationUtils.loadAnimation(this, R.anim.fade_in);
    overlay.startAnimation(anim);
  4. javier-delgado created this gist Aug 30, 2017.
    107 changes: 107 additions & 0 deletions BaseActivity.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,107 @@
    public abstract class BaseActivity extends AppCompatActivity {
    private boolean autoBusRegistration = true;

    @Override
    protected void onResume() {
    super.onResume();
    if (autoBusRegistration) getBus().register(this);
    }

    @Override
    protected void onPause() {
    super.onPause();
    if (autoBusRegistration) getBus().unregister(this);
    }

    //<editor-fold desc="Current user methods">
    protected Usuario currentUser() {
    return RegistroHelper.getUsuario();
    }

    protected boolean userSignedIn() {
    return RegistroHelper.userSignedIn();
    }
    //</editor-fold>

    //<editor-fold desc="Utility methods">
    protected void goToActivity(Class<? extends Activity> activityClass) {
    goToActivity(activityClass, -1);
    }

    protected void goToActivity(Class<? extends Activity> activityClass, int flag) {
    Intent intent = new Intent(this, activityClass);
    switch (flag) {
    case 1:
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    }
    startActivity(intent);
    }
    //</editor-fold>

    //<editor-fold desc="Overlay methods">
    protected void showActivityOverlay() {
    View overlay = findViewById(R.id.lytNetworkIndicatorOverlay);
    if (overlay == null) {
    overlay = createNetworkOverlay();
    }

    Animation anim = AnimationUtils.loadAnimation(this, R.anim.fade_in);
    overlay.startAnimation(anim);
    overlay.setVisibility(View.VISIBLE);
    }

    protected void hideActivityOverlay() {
    final View overlay = findViewById(R.id.lytNetworkIndicatorOverlay);
    if (overlay != null && overlay.getVisibility() == View.VISIBLE) {
    Animation anim = AnimationUtils.loadAnimation(this, R.anim.fade_out);
    anim.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {
    overlay.setVisibility(View.GONE);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
    });
    overlay.startAnimation(anim);
    }
    }

    private View createNetworkOverlay() {
    View overlay = getLayoutInflater().inflate(R.layout.network_indicator_overlay, null);
    ViewGroup vg = (ViewGroup)(getWindow().getDecorView().getRootView());
    vg.addView(overlay, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    return overlay;
    }
    //</editor-fold>


    //<editor-fold desc="Event bus">
    /**
    * Este metodo indica si la activity hija se va a hacer cargo del register/unregister al bus.
    * Esto es útil cuando se desea realizar el register/unregister en un metodo distinto a resume/pause.
    * @param autoBusRegistration indica si se debe registrar automaticamente o no. (default: true)
    */
    protected final void setAutoBusRegistration(boolean autoBusRegistration) {
    this.autoBusRegistration = autoBusRegistration;
    }

    protected EventBus getBus() {
    return EventBus.getDefault();
    }

    /**
    * Este metodo evita el error de que no hay suscriptores de eventbus
    * @param event
    */
    @Subscribe
    public void dummyEvent(Void event) {}
    //</editor-fold>
    }