Skip to content

Commit

Permalink
store templatedviews in navigator-level map for teardown. This keeps …
Browse files Browse the repository at this point in the history
…them out of navigable's scope
  • Loading branch information
cmathew committed Feb 10, 2022
1 parent 266d4e2 commit eec6f72
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public void hide(@NotNull Context context) {
view.saveHierarchyState(viewState);
}
screen.setView(null);
screen.setTemplatedView(null);
screen.setActivity(null);
}

Expand Down
12 changes: 0 additions & 12 deletions magellan-legacy/src/main/java/com/wealthfront/magellan/Screen.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.wealthfront.magellan.coroutines.ShownLifecycleScope;
Expand Down Expand Up @@ -49,7 +48,6 @@ public abstract class Screen<V extends ViewGroup & ScreenView> extends Lifecycle

private @Nullable Activity activity;
private @Nullable V view;
private @Nullable View templatedView;
private boolean isTransitioning;
private final Queue<TransitionFinishedListener> transitionFinishedListeners = new LinkedList<>();
private Navigator navigator;
Expand All @@ -70,12 +68,6 @@ public V getView() {
return view;
}

@Nullable
@Override
public View getTemplatedView() {
return templatedView != null ? templatedView : view;
}

/**
* @return the Activity associated with this Screen or null if we are not in between {@link #onShow(Context)} and
* {@link #onHide(Context)}.
Expand Down Expand Up @@ -226,10 +218,6 @@ public final void setView(@Nullable V view) {
this.view = view;
}

public final void setTemplatedView(@Nullable View templatedView) {
this.templatedView = templatedView;
}

public final void setActivity(@Nullable Activity activity) {
this.activity = activity;
this.dialogComponent.setContext(activity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ public interface Displayable {

public val view: View?

public var templatedView: View?

public fun transitionStarted() {}

public fun transitionFinished() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.wealthfront.magellan.navigation.ViewTemplateApplier

public abstract class Journey<V : ViewBinding>(
inflateBinding: (LayoutInflater) -> V,
getContainer: V.() -> ScreenContainer,
protected val getContainer: V.() -> ScreenContainer,
navigationRequestHandler: NavigationRequestHandler? = Magellan.navigationRequestHandler,
templateApplier: ViewTemplateApplier? = null
) : Step<V>(inflateBinding) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.wealthfront.magellan.coroutines.ShownLifecycleScope
import com.wealthfront.magellan.lifecycle.LifecycleAwareComponent
import com.wealthfront.magellan.lifecycle.attachFieldToLifecycle
import com.wealthfront.magellan.lifecycle.createAndAttachFieldToLifecycleWhenShown
import com.wealthfront.magellan.lifecycle.createAndAttachOverridableFieldToLifecycleWhenShown
import kotlinx.coroutines.CoroutineScope

public abstract class Step<V : ViewBinding>(
Expand All @@ -26,9 +25,6 @@ public abstract class Step<V : ViewBinding>(
final override var view: View? by createAndAttachFieldToLifecycleWhenShown { viewBinding!!.root }
@VisibleForTesting set

final override var templatedView: View? by createAndAttachOverridableFieldToLifecycleWhenShown { view }
@VisibleForTesting set

public var shownScope: CoroutineScope by attachFieldToLifecycle(ShownLifecycleScope()) { it }
@VisibleForTesting set

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.wealthfront.magellan.lifecycle

import android.content.Context
import kotlin.reflect.KProperty

public class CreateAndAttachFieldToLifecycleWhenShownDelegate<Field>(
public val fieldSupplier: (Context) -> Field
Expand All @@ -21,40 +20,3 @@ public class CreateAndAttachFieldToLifecycleWhenShownDelegate<Field>(

public fun <Field> LifecycleOwner.createAndAttachFieldToLifecycleWhenShown(fieldSupplier: (Context) -> Field): AttachFieldToLifecycleDelegate<CreateAndAttachFieldToLifecycleWhenShownDelegate<Field>, Field?> =
attachFieldToLifecycle(CreateAndAttachFieldToLifecycleWhenShownDelegate(fieldSupplier), { it.field })


public class OverrideableShownFieldDelegate<PropertyType>(
parent: LifecycleOwner,
private val fieldSupplier: (Context) -> PropertyType,
): LifecycleAware {

private var field: PropertyType? = null

init {
parent.attachToLifecycle(this)
}

override fun show(context: Context) {
field = fieldSupplier(context)
}

override fun hide(context: Context) {
field = null
}

@Suppress("UNCHECKED_CAST")
public operator fun getValue(thisRef: Any?, property: KProperty<*>): PropertyType? {
return field
}

public operator fun setValue(thisRef: Any?, property: KProperty<*>, value: PropertyType) {
field = value
}
}

public fun <Field> LifecycleOwner.createAndAttachOverridableFieldToLifecycleWhenShown(fieldSupplier: (Context) -> Field): OverrideableShownFieldDelegate<Field> {
return OverrideableShownFieldDelegate(
parent = this,
fieldSupplier = fieldSupplier
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public open class NavigationDelegate(

public var currentNavigableSetup: ((NavigableCompat) -> Unit)? = null

private var templatedViewMap = HashMap<NavigableCompat, View>()

protected var containerView: ScreenContainer? = null
protected val navigationPropagator: NavigationPropagator = NavigationPropagator
public val backStack: Deque<NavigationEvent> = ArrayDeque()
Expand All @@ -51,14 +53,19 @@ public open class NavigationDelegate(
override fun onShow(context: Context) {
containerView = container()
currentNavigable?.let { currentNavigable ->
containerView!!.addView(currentNavigable.view!!)
val templatedView = templateApplier?.onViewCreated(currentState.context!!, currentNavigable.view!!)
if (templatedView != null) {
templatedViewMap[currentNavigable] = templatedView
}
containerView!!.addView(templatedView ?: currentNavigable.view!!)
currentNavigable.transitionStarted()
currentNavigable.transitionFinished()
}
}

override fun onHide(context: Context) {
containerView = null
templatedViewMap.clear()
}

override fun onDestroy(context: Context) {
Expand Down Expand Up @@ -150,21 +157,24 @@ public open class NavigationDelegate(
navigationPropagator.onNavigatedTo(currentNavigable)
when (currentState) {
is LifecycleState.Shown, is LifecycleState.Resumed -> {
val templatedView = templateApplier?.onViewCreated(currentState.context!!, currentNavigable.view!!) ?: currentNavigable.view!!
currentNavigable.templatedView = templatedView
containerView!!.addView(templatedView, direction.indexToAddView(containerView!!))
val templatedView = templateApplier?.onViewCreated(currentState.context!!, currentNavigable.view!!)
if (templatedView != null) {
templatedViewMap[currentNavigable] = templatedView
}
containerView!!.addView(templatedView ?: currentNavigable.view!!, direction.indexToAddView(containerView!!))
}
is LifecycleState.Destroyed, is Created -> {
}
}
return currentNavigable.templatedView
return templatedViewMap[currentNavigable] ?: currentNavigable.view
}

protected open fun navigateFrom(currentNavigable: NavigableCompat?): View? {
return currentNavigable?.let { oldNavigable ->
val currentView = oldNavigable.templatedView
val currentView = templatedViewMap[oldNavigable] ?: oldNavigable.view
lifecycleRegistry.updateMaxState(oldNavigable, CREATED)
navigationPropagator.onNavigatedFrom(oldNavigable)
templatedViewMap.remove(oldNavigable)
currentView
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
package com.wealthfront.magellan.sample.advanced.designcereal

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import androidx.annotation.VisibleForTesting
import com.wealthfront.magellan.core.SimpleJourney
import com.wealthfront.magellan.databinding.MagellanSimpleJourneyBinding
import com.wealthfront.magellan.init.Magellan
import com.wealthfront.magellan.lifecycle.attachFieldToLifecycle
import com.wealthfront.magellan.navigation.DefaultLinearNavigator
import com.wealthfront.magellan.navigation.LinearNavigator
import com.wealthfront.magellan.navigation.ViewTemplateApplier
import com.wealthfront.magellan.sample.advanced.SampleApplication.Companion.app
import com.wealthfront.magellan.sample.advanced.ToolbarHelper
import com.wealthfront.magellan.sample.advanced.databinding.DesignCerealTemplateBinding
import javax.inject.Inject

class DesignCerealJourney(private val cerealComplete: () -> Unit) : SimpleJourney() {

override var navigator: LinearNavigator by attachFieldToLifecycle(
DefaultLinearNavigator(
{ viewBinding!!.getContainer() },
Magellan.navigationRequestHandler,
object : ViewTemplateApplier {
override fun onViewCreated(context: Context, view: View): View {
val inflater = LayoutInflater.from(context)
val template = DesignCerealTemplateBinding.inflate(inflater).apply {
this.content.addView(view)
}
return template.root
}
}
)
)

@Inject lateinit var toolbarHelper: ToolbarHelper
private var customCereal: CustomCereal? = null

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/smallSpacing"
android:text="Design a cereal!"
android:textAppearance="@style/TextAppearance.MaterialComponents.Headline4"
/>

<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
1 change: 1 addition & 0 deletions magellan-sample-advanced/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="smallSpacing">8dp</dimen>
<dimen name="normalSpacing">16dp</dimen>
<dimen name="largeSpacing">24dp</dimen>
<dimen name="giantSpacing">32dp</dimen>
Expand Down

0 comments on commit eec6f72

Please sign in to comment.