Skip to content

Commit

Permalink
Convert list model to Redux. Remove model base class.
Browse files Browse the repository at this point in the history
  • Loading branch information
pardom committed Jul 14, 2016
1 parent a86d0f9 commit a1cf860
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 229 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import clean.news.ui.main.MainKey
class FlowNavigationFactory : NavigationFactory {
override fun main() = MainKey()

override fun itemDetail(item: Item) = ItemDetailKey(item)
override fun detail(item: Item) = ItemDetailKey(item)

override fun url(item: Item) = ItemUrlKey(item)

Expand Down
39 changes: 16 additions & 23 deletions android/src/main/kotlin/clean/news/ui/item/list/ItemListView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,26 @@ import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.AttributeSet
import clean.news.adapter.ItemAdapter
import clean.news.app.util.addTo
import clean.news.core.entity.Item
import clean.news.flow.service.DaggerService
import clean.news.presentation.collections.streamMapOf
import clean.news.presentation.model.item.ItemListViewModel
import clean.news.presentation.model.item.ItemListViewModel.Sinks
import clean.news.presentation.model.item.ItemListViewModel.Sources
import clean.news.presentation.model.item.ItemListViewModel.Action
import clean.news.ui.item.list.ItemListScreen.ItemListModule
import clean.news.ui.main.MainKey.MainComponent
import flow.Flow
import rx.android.schedulers.AndroidSchedulers
import rx.subjects.PublishSubject
import redux.Store
import redux.Store.Subscription
import rx.subscriptions.CompositeSubscription
import javax.inject.Inject

class ItemListView : RecyclerView {
class ItemListView : RecyclerView, Store.Subscriber {

@Inject
lateinit var model: ItemListViewModel

private val adapter: ItemAdapter

private val itemUrlClicks = PublishSubject.create<Item>()
private val itemDetailClicks = PublishSubject.create<Item>()
private val subscriptions = CompositeSubscription()
private lateinit var subscription: Subscription

@JvmOverloads
constructor(context: Context, attrs: AttributeSet? = null, defStyle: Int = 0) : super(context, attrs, defStyle) {
Expand All @@ -39,32 +35,29 @@ class ItemListView : RecyclerView {
override fun onFinishInflate() {
super.onFinishInflate()

adapter.itemUrlClickListener = { itemUrlClicks.onNext(it) }
adapter.itemDetailClickListener = { itemDetailClicks.onNext(it) }
adapter.itemUrlClickListener = { model.store.dispatch(Action.GoToUrl(it)) }
adapter.itemDetailClickListener = { model.store.dispatch(Action.GoToDetail(it)) }

setAdapter(adapter)
}

override fun onAttachedToWindow() {
super.onAttachedToWindow()

val sinks = model.attach(streamMapOf(
Sources.ITEM_URL_CLICKS to itemUrlClicks,
Sources.ITEM_DETAIL_CLICKS to itemDetailClicks
))

Sinks.ITEMS<List<Item>>(sinks)
.observeOn(AndroidSchedulers.mainThread())
.subscribe { adapter.setItems(it) }
.addTo(subscriptions)
subscription = model.store.subscribe(this)
onStateChanged()
}

override fun onDetachedFromWindow() {
subscriptions.unsubscribe()
model.detach()
subscription.unsubscribe()
super.onDetachedFromWindow()
}

override fun onStateChanged() {
val state = model.store.getState()
adapter.setItems(state.items)
}

fun inject(module: ItemListModule) {
Flow.getService<MainComponent>(DaggerService.NAME, context)
?.plus(module)
Expand Down
4 changes: 2 additions & 2 deletions android/src/main/kotlin/clean/news/ui/item/url/ItemUrlView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import clean.news.presentation.model.item.ItemUrlViewModel.Action
import clean.news.ui.item.url.ItemUrlKey.ItemUrlComponent
import com.jakewharton.rxbinding.support.v7.widget.itemClicks
import com.jakewharton.rxbinding.support.v7.widget.navigationClicks
import redux.Store.Subscriber
import redux.Store
import redux.Store.Subscription
import rx.subscriptions.CompositeSubscription
import javax.inject.Inject

class ItemUrlView : RelativeLayout, Subscriber {
class ItemUrlView : RelativeLayout, Store.Subscriber {
@Inject
lateinit var model: ItemUrlViewModel

Expand Down

This file was deleted.

This file was deleted.

124 changes: 0 additions & 124 deletions presentation/src/main/kotlin/clean/news/presentation/model/Model.kt

This file was deleted.

Loading

0 comments on commit a1cf860

Please sign in to comment.