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
enum class Navigation { | |
ITEMS_LIST, | |
EDIT_ITEM | |
} | |
data class ItemsListScreen( | |
val items: List<Item> = emptyList()) | |
data class EditItemScreen(val currentItem: Item = Item()) |
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
private fun reduce(action: Action, currentState: State): State { | |
val newState = when (action) { | |
is CreationAction -> CreationReducer.reduce(action, currentState) | |
is UpdateAction -> UpdateReducer.reduce(action, currentState) | |
is ReadAction -> ReadReducer.reduce(action, currentState) | |
is DeleteAction -> DeleteReducer.reduce(action, currentState) | |
is NavigationAction -> NavigationReducer.reduce(action, currentState) | |
} | |
return newState | |
} |
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
abstract class Store(private val storeThread: ThreadExecutor? = null) { | |
private var actions = LinkedBlockingQueue<Action>() | |
val stateHandlers: CopyOnWriteArrayList<StateHandler> = CopyOnWriteArrayList() | |
val sideEffects: CopyOnWriteArrayList<SideEffect> = CopyOnWriteArrayList() | |
var state = State() | |
protected set | |
@Synchronized |
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
sealed class Action | |
sealed class NavigationAction : Action() { | |
data class EditItemScreenAction(val item: Item) : NavigationAction() | |
class ItemsScreenAction : NavigationAction() | |
} | |
sealed class ReadAction : Action() { | |
class FetchItemsAction : ReadAction() | |
data class ItemsLoadedAction(val items: List<Item>) : ReadAction() |
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
class ItemsControllerView( | |
val itemsViewCallback: WeakReference<ItemsViewCallback>, | |
store: Store, | |
mainThread: ThreadExecutor? = null) | |
: ControllerView(store, mainThread) { | |
fun fetchItems() = | |
store.dispatch(FetchItemsAction()) | |
fun toEditItemScreen(item: Item) = |
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
abstract class ControllerView( | |
val store: Store, | |
mainThread: ThreadExecutor? = null) | |
: LifecycleCallbacks, StateHandler(mainThread) { | |
override fun onStart() { | |
store.stateHandlers.add(this) | |
} | |
override fun onDestroy() { |
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
class ItemsActivity : ViewActivity<ItemsControllerView>(), ItemsViewCallback { | |
//--- We set up the ControllerView --- // | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.items_layout) | |
setupControllerView() | |
bindViews() | |
} |
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
class StoreActionSubscriber(actionDispatcher: ActionDispatcher, | |
actionThread: ThreadExecutor? = null, | |
val stateDispatcher: StateDispatcher) | |
: ActionSubscriber(actionDispatcher, actionThread) { | |
override fun reduce(action: Action) { | |
val currentState = stateDispatcher.state | |
val newState = when (action) { | |
is CreationAction -> CreationReducer.reduce(action, currentState) | |
is UpdateAction -> UpdateReducer.reduce(action, currentState) |
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
abstract class ControllerView( | |
protected val actionDispatcher: ActionDispatcher, | |
protected val stateDispatcher: StateDispatcher, | |
private val handleStateDifferentThread: ThreadExecutor? = null) | |
: LifecycleCallbacks, Subscriber<State> { | |
protected fun <T : Action> dispatch(action: T) { | |
actionDispatcher.dispatch(action) | |
} |
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
interface Subscriber<in T> { | |
fun onNext(data: T) | |
} | |
abstract class Dispatcher<T> { | |
private val subscriptions = ArrayList<Subscriber<T>>() | |
fun subscribe(subscriber: Subscriber<T>): Boolean = | |
subscriptions.add(subscriber) |