-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename FragmentStoreBuilder to FeaturesBuilder.
- Loading branch information
Showing
16 changed files
with
125 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
formula-android/src/main/java/com/instacart/formula/android/FeaturesBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.instacart.formula.android | ||
|
||
import com.instacart.formula.android.internal.FeatureBinding | ||
import com.instacart.formula.android.internal.Features | ||
import com.instacart.formula.android.internal.MappedFeatureFactory | ||
import java.lang.IllegalStateException | ||
import kotlin.reflect.KClass | ||
|
||
/** | ||
* Helps to build a [Features] list that binds various fragment keys to their respective | ||
* feature factories. Each feature factory has a dependency type that needs to either match | ||
* [Dependencies] type defined here or map this root dependency type to the custom type. | ||
*/ | ||
class FeaturesBuilder<Dependencies> { | ||
companion object { | ||
inline fun <Dependencies> build( | ||
init: FeaturesBuilder<Dependencies>.() -> Unit | ||
): Features<Dependencies> { | ||
return FeaturesBuilder<Dependencies>().apply(init).build() | ||
} | ||
} | ||
|
||
private val types = mutableSetOf<Class<*>>() | ||
private val bindings: MutableList<FeatureBinding<Dependencies, *>> = mutableListOf() | ||
|
||
/** | ||
* Binds a [feature factory][FeatureFactory] for a specific [key][type]. | ||
* | ||
* @param type The class which describes the [key][Key]. | ||
* @param featureFactory Feature factory that provides state observable and view rendering logic. | ||
*/ | ||
fun <Key : FragmentKey> bind( | ||
type : KClass<Key>, | ||
featureFactory: FeatureFactory<Dependencies, Key>, | ||
) = apply { | ||
val binding = FeatureBinding(type.java, featureFactory) | ||
bind(type.java, binding) | ||
} | ||
|
||
/** | ||
* Binds a feature factory for a [Key]. | ||
* | ||
* @param featureFactory Feature factory that provides state observable and view rendering logic. | ||
*/ | ||
inline fun <reified Key: FragmentKey> bind( | ||
featureFactory: FeatureFactory<Dependencies, Key> | ||
) = apply { | ||
bind(Key::class, featureFactory) | ||
} | ||
|
||
/** | ||
* A convenience inline function that binds a feature factory for a specific [key][Key]. | ||
* | ||
* @param featureFactory Feature factory that provides state observable and view rendering logic. | ||
* @param toDependencies Maps [Dependencies] to feature factory [dependencies][CustomDependencyType]. | ||
*/ | ||
inline fun <CustomDependencyType, reified Key: FragmentKey> bind( | ||
featureFactory: FeatureFactory<CustomDependencyType, Key>, | ||
noinline toDependencies: (Dependencies) -> CustomDependencyType | ||
) = apply { | ||
val mapped = MappedFeatureFactory( | ||
delegate = featureFactory, | ||
toDependencies = toDependencies, | ||
) | ||
bind(Key::class, mapped) | ||
} | ||
|
||
@PublishedApi | ||
internal fun build(): Features<Dependencies> { | ||
return Features(bindings) | ||
} | ||
|
||
private fun bind(type: Class<*>, binding: FeatureBinding<Dependencies, *>) = apply { | ||
if (types.contains(type)) { | ||
throw IllegalStateException("Binding for $type already exists") | ||
} | ||
types += type | ||
bindings += binding | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 2 additions & 77 deletions
79
formula-android/src/main/java/com/instacart/formula/android/FragmentStoreBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,4 @@ | ||
package com.instacart.formula.android | ||
|
||
import com.instacart.formula.android.internal.FeatureBinding | ||
import com.instacart.formula.android.internal.MappedFeatureFactory | ||
import java.lang.IllegalStateException | ||
import kotlin.reflect.KClass | ||
|
||
/** | ||
* A class used by [FragmentFlowStore] to register [fragment keys][FragmentKey] and their | ||
* feature factories. | ||
*/ | ||
class FragmentStoreBuilder<Component> { | ||
companion object { | ||
@PublishedApi | ||
internal inline fun <Component> build( | ||
init: FragmentStoreBuilder<Component>.() -> Unit | ||
): List<FeatureBinding<Component, *>> { | ||
return FragmentStoreBuilder<Component>().apply(init).build() | ||
} | ||
} | ||
|
||
private val types = mutableSetOf<Class<*>>() | ||
private val bindings: MutableList<FeatureBinding<Component, *>> = mutableListOf() | ||
|
||
/** | ||
* Binds a [feature factory][FeatureFactory] for a specific [key][type]. | ||
* | ||
* @param type The class which describes the [key][Key]. | ||
* @param featureFactory Feature factory that provides state observable and view rendering logic. | ||
*/ | ||
fun <Key : FragmentKey> bind( | ||
type : KClass<Key>, | ||
featureFactory: FeatureFactory<Component, Key>, | ||
) = apply { | ||
val binding = FeatureBinding(type.java, featureFactory) | ||
bind(type.java, binding) | ||
} | ||
|
||
/** | ||
* Binds a feature factory for a [Key]. | ||
* | ||
* @param featureFactory Feature factory that provides state observable and view rendering logic. | ||
*/ | ||
inline fun <reified Key: FragmentKey> bind( | ||
featureFactory: FeatureFactory<Component, Key> | ||
) = apply { | ||
bind(Key::class, featureFactory) | ||
} | ||
|
||
/** | ||
* A convenience inline function that binds a feature factory for a specific [key][Key]. | ||
* | ||
* @param featureFactory Feature factory that provides state observable and view rendering logic. | ||
* @param toDependencies Maps [Component] to feature factory [dependencies][Dependencies]. | ||
*/ | ||
inline fun <Dependencies, reified Key: FragmentKey> bind( | ||
featureFactory: FeatureFactory<Dependencies, Key>, | ||
noinline toDependencies: (Component) -> Dependencies | ||
) = apply { | ||
val mapped = MappedFeatureFactory( | ||
delegate = featureFactory, | ||
toDependencies = toDependencies, | ||
) | ||
bind(Key::class, mapped) | ||
} | ||
|
||
@PublishedApi | ||
internal fun build(): List<FeatureBinding<Component, *>> { | ||
return bindings | ||
} | ||
|
||
private fun bind(type: Class<*>, binding: FeatureBinding<Component, *>) = apply { | ||
if (types.contains(type)) { | ||
throw IllegalStateException("Binding for $type already exists") | ||
} | ||
types += type | ||
bindings += binding | ||
} | ||
} | ||
@Deprecated("Use FeaturesBuilder directly") | ||
typealias FragmentStoreBuilder<Component> = FeaturesBuilder<Component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
formula-android/src/main/java/com/instacart/formula/android/internal/Features.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.instacart.formula.android.internal | ||
|
||
class Features<in Dependencies>( | ||
val bindings: List<FeatureBinding<Dependencies, *>>, | ||
) |
Oops, something went wrong.