Skip to content

Instantly share code, notes, and snippets.

View CesarValiente's full-sized avatar
🏠
Working from home

Cesar Valiente Gordo CesarValiente

🏠
Working from home
View GitHub Profile
enum class Navigation {
ITEMS_LIST,
EDIT_ITEM
}
data class ItemsListScreen(
val items: List<Item> = emptyList())
data class EditItemScreen(val currentItem: Item = Item())
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
}
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
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()
class ItemsControllerView(
val itemsViewCallback: WeakReference<ItemsViewCallback>,
store: Store,
mainThread: ThreadExecutor? = null)
: ControllerView(store, mainThread) {
fun fetchItems() =
store.dispatch(FetchItemsAction())
fun toEditItemScreen(item: Item) =
abstract class ControllerView(
val store: Store,
mainThread: ThreadExecutor? = null)
: LifecycleCallbacks, StateHandler(mainThread) {
override fun onStart() {
store.stateHandlers.add(this)
}
override fun onDestroy() {
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()
}
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)
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)
}
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)