package com.nispok.snackbar;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.ViewGroup;

/**
 * A handler for multiple {@link Snackbar}s
 */
public class SnackbarManager {

    private static final String TAG = SnackbarManager.class.getSimpleName();
    private static final Handler MAIN_THREAD = new Handler(Looper.getMainLooper());

    private static Snackbar currentSnackbar;

    private SnackbarManager() {
    }

    /**
     * Displays a {@link com.nispok.snackbar.Snackbar} in the current {@link Activity}, dismissing
     * the current Snackbar being displayed, if any. Note that the Activity will be obtained from
     * the Snackbar's {@link android.content.Context}. If the Snackbar was created with
     * {@link android.app.Activity#getApplicationContext()} then you must explicitly pass the target
     * Activity using {@link #show(Snackbar, android.app.Activity)}
     *
     * @param snackbar instance of {@link com.nispok.snackbar.Snackbar} to display
     */
    public static void show(@NonNull Snackbar snackbar) {
        try {
            show(snackbar, (Activity) snackbar.getContext());
        } catch (ClassCastException e) {
            Log.e(TAG, "Couldn't get Activity from the Snackbar's Context. Try calling " +
                    "#show(Snackbar, Activity) instead", e);
        }
    }

    /**
     * Displays a {@link com.nispok.snackbar.Snackbar} in the current {@link Activity}, dismissing
     * the current Snackbar being displayed, if any
     *
     * @param snackbar instance of {@link com.nispok.snackbar.Snackbar} to display
     * @param activity target {@link Activity} to display the Snackbar
     */
    public static void show(@NonNull final Snackbar snackbar, @NonNull final Activity activity) {
        MAIN_THREAD.post(new Runnable() {
            @Override
            public void run() {
                if (currentSnackbar != null) {
                    if (currentSnackbar.isShowing() && !currentSnackbar.isDimissing()) {
                        currentSnackbar.dismissAnimation(false);
                        currentSnackbar.dismissByReplace();
                        currentSnackbar = snackbar;
                        currentSnackbar.showAnimation(false);
                        currentSnackbar.showByReplace(activity);
                        return;
                    }
                    currentSnackbar.dismiss();
                }
                currentSnackbar = snackbar;
                currentSnackbar.show(activity);
            }
        });
    }

    /**
     * Displays a {@link com.nispok.snackbar.Snackbar} in the specified {@link ViewGroup}, dismissing
     * the current Snackbar being displayed, if any
     *
     * @param snackbar instance of {@link com.nispok.snackbar.Snackbar} to display
     * @param parent   parent {@link ViewGroup} to display the Snackbar
     */
    public static void show(@NonNull Snackbar snackbar, @NonNull ViewGroup parent) {
        show(snackbar, parent, Snackbar.shouldUsePhoneLayout(snackbar.getContext()));
    }

    /**
     * Displays a {@link com.nispok.snackbar.Snackbar} in the specified {@link ViewGroup}, dismissing
     * the current Snackbar being displayed, if any
     *
     * @param snackbar       instance of {@link com.nispok.snackbar.Snackbar} to display
     * @param parent         parent {@link ViewGroup} to display the Snackbar
     * @param usePhoneLayout true: use phone layout, false: use tablet layout
     */
    public static void show(@NonNull final Snackbar snackbar, @NonNull final ViewGroup parent,
                            final boolean usePhoneLayout) {
        MAIN_THREAD.post(new Runnable() {
            @Override
            public void run() {
                if (currentSnackbar != null) {
                    if (currentSnackbar.isShowing() && !currentSnackbar.isDimissing()) {
                        currentSnackbar.dismissAnimation(false);
                        currentSnackbar.dismissByReplace();
                        currentSnackbar = snackbar;
                        currentSnackbar.showAnimation(false);
                        currentSnackbar.showByReplace(parent, usePhoneLayout);
                        return;
                    }
                    currentSnackbar.dismiss();
                }
                currentSnackbar = snackbar;
                currentSnackbar.show(parent, usePhoneLayout);
            }
        });
    }

    /**
     * Dismisses the {@link com.nispok.snackbar.Snackbar} shown by this manager.
     */
    public static void dismiss() {
        if (currentSnackbar != null) {
            MAIN_THREAD.post(new Runnable() {
                @Override
                public void run() {
                    currentSnackbar.dismiss();
                }
            });
        }
    }

    /**
     * Return the current Snackbar
     */
    public static Snackbar getCurrentSnackbar() {
        return currentSnackbar;
    }
}

@@ -23,16 +23,53 @@
 ckbar;%0A%0A
+import java.lang.ref.WeakReference;%0A%0A
 import a
@@ -511,32 +511,49 @@
 tic 
-Snackbar currentSnackbar
+WeakReference%3CSnackbar%3E snackbarReference
 ;%0A%0A 
@@ -2008,32 +2008,97 @@
 ic void run() %7B%0A
+                Snackbar currentSnackbar = getCurrentSnackbar();%0A
                 
@@ -2370,34 +2370,56 @@
     
-currentSnackbar = 
+snackbarReference = new WeakReference%3C%3E(
 snackbar
 ;%0A  
@@ -2406,32 +2406,33 @@
 rence%3C%3E(snackbar
+)
 ;%0A              
@@ -2437,32 +2437,25 @@
             
-currentS
+s
 nackbar.show
@@ -2492,32 +2492,25 @@
             
-currentS
+s
 nackbar.show
@@ -2669,34 +2669,56 @@
     
-currentSnackbar = 
+snackbarReference = new WeakReference%3C%3E(
 snackbar
 ;%0A  
@@ -2705,32 +2705,33 @@
 rence%3C%3E(snackbar
+)
 ;%0A              
@@ -2728,32 +2728,25 @@
             
-currentS
+s
 nackbar.show
@@ -3965,32 +3965,97 @@
 ic void run() %7B%0A
+                Snackbar currentSnackbar = getCurrentSnackbar();%0A
                 
@@ -4327,34 +4327,56 @@
     
-currentSnackbar = 
+snackbarReference = new WeakReference%3C%3E(
 snackbar
 ;%0A  
@@ -4363,32 +4363,33 @@
 rence%3C%3E(snackbar
+)
 ;%0A              
@@ -4394,32 +4394,25 @@
             
-currentS
+s
 nackbar.show
@@ -4449,32 +4449,25 @@
             
-currentS
+s
 nackbar.show
@@ -4640,34 +4640,56 @@
     
-currentSnackbar = 
+snackbarReference = new WeakReference%3C%3E(
 snackbar
 ;%0A  
@@ -4680,24 +4680,25 @@
 e%3C%3E(snackbar
+)
 ;%0A          
@@ -4699,32 +4699,25 @@
             
-currentS
+s
 nackbar.show
@@ -4903,24 +4903,87 @@
 dismiss() %7B%0A
+        final Snackbar currentSnackbar = getCurrentSnackbar();%0A
         if (
@@ -5328,30 +5328,114 @@
     
-return currentSnackbar
+if (snackbarReference != null) %7B%0A            return snackbarReference.get();%0A        %7D%0A        return null
 ;%0A  

Use a WeakReference to fix activity leaks.