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
sealed class Action
sealed class UpdateAction : Action() {
data class ReorderItemsAction(val items: List<Item>) : UpdateAction()
data class UpdateItemAction(val localId: String,
val text: String,
val color: Color) : UpdateAction()
}
abstract class ViewActivity<T : ControllerView> : AppCompatActivity() {
lateinit var controllerView: T
override fun onStart() {
super.onStart()
controllerView.onStart()
}
override fun onPause() {
abstract class ViewActivity<T : ControllerView> : AppCompatActivity() {
lateinit var controllerView: T
override fun onStart() {
super.onStart()
controllerView.onStart()
}
//^^ We will do the same with every activity/fragment
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)
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)
}
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)
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()
}
abstract class ControllerView(
val store: Store,
mainThread: ThreadExecutor? = null)
: LifecycleCallbacks, StateHandler(mainThread) {
override fun onStart() {
store.stateHandlers.add(this)
}
override fun onDestroy() {
class ItemsControllerView(
val itemsViewCallback: WeakReference<ItemsViewCallback>,
store: Store,
mainThread: ThreadExecutor? = null)
: ControllerView(store, mainThread) {
fun fetchItems() =
store.dispatch(FetchItemsAction())
fun toEditItemScreen(item: Item) =
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()