package me.mattlogan.library;

import android.view.View;
import android.view.ViewGroup;

import java.util.Stack;

public final class ViewStack {

    private final Stack<View> stack;
    private final ViewGroup container;
    private final ViewStackListener listener;

    public static ViewStack create(ViewGroup container) {
        return new ViewStack(new Stack<View>(), container, ViewStackListener.DEFAULT);
    }

    public static ViewStack create(ViewGroup container, ViewStackListener listener) {
        return new ViewStack(new Stack<View>(), container, listener);
    }

    private ViewStack(Stack<View> stack, ViewGroup container, ViewStackListener listener) {
        this.stack = stack;
        this.container = container;
        this.listener = listener;
    }

    public View push(View view) {
        stack.push(view);
        updateContainer();
        listener.onViewStackChanged(stack.size());
        return view;
    }

    public View pop() {
        boolean shouldPop = listener.onPopRequested(stack.size());
        if (!shouldPop) return null;
        View view = stack.pop();
        updateContainer();
        listener.onViewStackChanged(stack.size());
        return view;
    }

    public View peek() {
        return stack.peek();
    }

    public int size() {
        return stack.size();
    }

    private void updateContainer() {
        container.removeAllViews();
        if (stack.size() > 0) {
            container.addView(stack.peek());
        }
    }
}

@@ -349,35 +349,16 @@
 ewStack(
-new Stack%3CView%3E(), 
 containe
@@ -514,27 +514,8 @@
 ack(
-new Stack%3CView%3E(), 
 cont
@@ -565,27 +565,8 @@
 ack(
-Stack%3CView%3E stack, 
 View
@@ -633,21 +633,29 @@
 stack = 
-stack
+new Stack%3C%3E()
 ;%0A      

Remove Stack from ViewStack constructor