package com.davidstemmer.screenplay.scene;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import mortar.Blueprint;
import mortar.Mortar;
import mortar.MortarScope;

/**
 * Created by weefbellington on 10/20/14.
 */
public abstract class ScopedScene extends StandardScene implements Blueprint {

    private MortarScope scope;

    protected ScopedScene() {
    }

    protected ScopedScene(Component... components) {
        super(components);
    }

    @Override
    public View setUp(Context context, ViewGroup parent) {
        MortarScope parentScope = Mortar.getScope(context);
        scope = parentScope.requireChild(this);
        Context childContext = scope.createContext(context);
        return super.setUp(childContext, parent);
    }

    @Override
    public View tearDown(Context context, ViewGroup parent) {
        MortarScope parentScope = Mortar.getScope(context);
        parentScope.destroyChild(scope);
        scope = null;
        return super.tearDown(context, parent);
    }
}

@@ -372,90 +372,73 @@
     
-protected ScopedScene() %7B%0A    %7D%0A%0A    protected ScopedScene(Component... compon
+@Override%0A    public View setUp(Context context, ViewGroup par
 ent
-s
 ) %7B%0A
@@ -449,24 +449,55 @@
     
-super(compon
+return super.setUp(createScope(context), par
 ent
-s
 );%0A 
@@ -524,37 +524,40 @@
     public View 
-setUp
+tearDown
 (Context context
@@ -570,32 +570,206 @@
 Group parent) %7B%0A
+        View destroyed = super.tearDown(context, parent);%0A        destroyScope(context);%0A        return destroyed;%0A    %7D%0A%0A%0A    private Context createScope(Context context) %7B%0A
         MortarSc
@@ -917,32 +917,33 @@
 t(context);%0A
+%0A
         
 return super
@@ -934,128 +934,153 @@
     
-return super.setUp(childContext, parent
+Mortar.inject(childContext, this);%0A        onCreateScope(scope
 );%0A    
-%7D%0A%0A
     
-@Override%0A    public View tearDown(Context context, ViewGroup paren
+return childContext;%0A    %7D%0A%0A    private void destroyScope(Context contex
 t) %7B
@@ -1215,52 +1215,120 @@
     
-return super.tearDown(context, parent);%0A    
+onDestroyScope();%0A    %7D%0A%0A    public void onCreateScope(MortarScope scope) %7B%7D%0A%0A    public void onDestroyScope() %7B
 %7D%0A%7D%0A

update the ScopedScene to also call Mortar.inject. Add callback methods so that subclasses know when the scope has been created and injection has occured.