Skip to content

Commit

Permalink
Use the Map API for data source.
Browse files Browse the repository at this point in the history
  • Loading branch information
pardom committed May 12, 2016
1 parent 1ac748e commit a9f16d0
Show file tree
Hide file tree
Showing 17 changed files with 90 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import clean.news.presentation.model.item.ItemDetailViewModel.Sources
import clean.news.ui.item.detail.ItemDetailKey.ItemDetailComponent
import com.jakewharton.rxbinding.support.v7.widget.itemClicks
import com.jakewharton.rxbinding.support.v7.widget.navigationClicks
import com.jakewharton.rxbinding.widget.text
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
import rx.subscriptions.CompositeSubscription
Expand Down Expand Up @@ -54,14 +55,16 @@ class ItemDetailView : RelativeLayout {

override fun onAttachedToWindow() {
super.onAttachedToWindow()

val sinks = model.setUp(Sources(
toolbar.navigationClicks(),
Observable.empty(),
toolbar.itemClicks().filter {it.itemId == R.id.item_share }
))

sinks.item
.subscribe { titleTextView.text = it.title }
.map { it.title.orEmpty() }
.subscribe(titleTextView.text())
.addTo(subscriptions)

sinks.children
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/kotlin/clean/news/app/data/DataSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package clean.news.app.data

import rx.Observable

interface DataSource<T, ID> {
fun getAll(): Observable<List<T>>
interface DataSource<K, V> {
fun getAll(): Observable<List<V>>

fun getById(id: ID): Observable<T>
fun get(key: K): Observable<V>

fun save(t: T): Observable<Boolean>
fun put(key: K, value: V): Observable<V>

fun remove(key: K): Observable<V>
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import clean.news.core.entity.Item
import clean.news.core.entity.Item.ListType
import rx.Observable

interface ItemDataSource : DataSource<Item, Long> {
interface ItemDataSource : DataSource<Long, Item> {

fun getItems(listType: ListType): Observable<List<Item>>

fun getChildren(item: Item): Observable<List<Item>>

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package clean.news.app.data.user
import clean.news.app.data.DataSource
import clean.news.core.entity.User

interface UserDataSource : DataSource<User, String>
interface UserDataSource : DataSource<String, User>
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
package clean.news.app.data.user

import clean.news.app.data.DataSource
import clean.news.core.entity.User

interface UserDiskDataSource : DataSource<User, String>
interface UserDiskDataSource : UserDataSource
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
package clean.news.app.data.user

import clean.news.app.data.DataSource
import clean.news.core.entity.User

interface UserMemoryDataSource : DataSource<User, String>
interface UserMemoryDataSource : UserDataSource
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
package clean.news.app.data.user

import clean.news.app.data.DataSource
import clean.news.core.entity.User

interface UserNetworkDataSource : DataSource<User, String>
interface UserNetworkDataSource : UserDataSource
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class GetItemById @Inject constructor(
override fun execute(request: Request): Observable<Response> {
return Strategy(request.flags)
.execute(
disk.getById(request.id),
memory.getById(request.id),
network.getById(request.id),
disk.get(request.id),
memory.get(request.id),
network.get(request.id),
save
)
.map { Response(it) }
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/kotlin/clean/news/app/usecase/item/SaveItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class SaveItem @Inject constructor(

override fun execute(request: Request): Observable<Response> {
val strategy = Strategy(request.flags)
val observable = Observable.empty<Boolean>()
val observable = Observable.empty<Item>()

if (strategy.useDisk) observable.mergeWith(disk.save(request.item))
if (strategy.useMemory) observable.mergeWith(memory.save(request.item))
if (strategy.useDisk) observable.mergeWith(disk.put(request.item.id, request.item))
if (strategy.useMemory) observable.mergeWith(memory.put(request.item.id, request.item))

return observable.map { Response(it) }
return observable.map { Response() }
}

class Request(val item: Item, flags: Int = Strategy.WARM) : Strategy.Request(flags)

class Response(val success: Boolean) : UseCase.Response
class Response() : UseCase.Response
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class GetUserById @Inject constructor(
override fun execute(request: Request): Observable<Response> {
return Strategy(request.flags)
.execute(
disk.getById(request.id),
memory.getById(request.id),
network.getById(request.id),
disk.get(request.id),
memory.get(request.id),
network.get(request.id),
save
)
.map { Response(it) }
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/kotlin/clean/news/app/usecase/user/SaveUser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ class SaveUser(

override fun execute(request: Request): Observable<Response> {
val strategy = Strategy(request.flags)
val observable = Observable.empty<Boolean>()
val observable = Observable.empty<User>()

if (strategy.useDisk) observable.mergeWith(disk.save(request.user))
if (strategy.useMemory) observable.mergeWith(memory.save(request.user))
if (strategy.useDisk) observable.mergeWith(disk.put(request.user.id, request.user))
if (strategy.useMemory) observable.mergeWith(memory.put(request.user.id, request.user))

return observable.map { Response(it) }
return observable.map { Response() }
}

class Request(val user: User, flags: Int = Strategy.WARM) : Strategy.Request(flags)

class Response(val success: Boolean) : UseCase.Response
class Response() : UseCase.Response
}
18 changes: 11 additions & 7 deletions data/src/main/kotlin/clean/news/data/lru/ItemLruDataSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import clean.news.core.entity.Item.Type
import rx.Observable

class ItemLruDataSource : ItemMemoryDataSource {

private val BUFFER = 10

private val lru = LruCache<Long, Item>(512)
Expand All @@ -29,22 +30,25 @@ class ItemLruDataSource : ItemMemoryDataSource {
return Observable.just(lru.snapshot().values.toList())
}

override fun getById(id: Long): Observable<Item> {
return lru[id]?.let { Observable.just(it) } ?: Observable.empty()
override fun get(key: Long): Observable<Item> {
return lru[key]?.let { Observable.just(it) } ?: Observable.empty()
}

override fun put(key: Long, value: Item): Observable<Item> {
return Observable.just(lru.put(key, value))
}

@Suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE")
override fun save(item: Item): Observable<Boolean> {
lru.put(item.id, item)
return Observable.just(true)
override fun remove(key: Long): Observable<Item> {
return Observable.just(lru.remove(key))
}

// Private functions

private fun getChildrenObservable(item: Item, level: Int): Observable<Item> {
return Observable.from(item.kids.orEmpty())
.concatMapEager { getById(it) }
.concatMapEager { get(it) }
.map { it.copy(level = level) }
.concatMapEager { getChildrenObservable(it, level + 1) }
}

}
16 changes: 10 additions & 6 deletions data/src/main/kotlin/clean/news/data/lru/UserLruDataSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@ import clean.news.core.entity.User
import rx.Observable

class UserLruDataSource : UserMemoryDataSource {

val lru = LruCache<String, User>(512)

override fun getAll(): Observable<List<User>> {
return Observable.just(lru.snapshot().values.toList())
}

override fun getById(id: String): Observable<User> {
return Observable.just(lru[id])
override fun get(key: String): Observable<User> {
return Observable.just(lru[key])
}

override fun put(key: String, value: User): Observable<User> {
return Observable.just(lru.put(key, value))
}

@Suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE")
override fun save(user: User): Observable<Boolean> {
lru.put(user.id, user)
return Observable.just(true)
override fun remove(key: String): Observable<User> {
return Observable.just(lru.remove(key))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import javax.inject.Inject

class ItemRetrofitDataSource @Inject constructor(
private val itemService: ItemService) : ItemNetworkDataSource {

private val BUFFER = 5

override fun getItems(listType: ListType): Observable<List<Item>> {
Expand All @@ -30,16 +29,19 @@ class ItemRetrofitDataSource @Inject constructor(
}

override fun getAll(): Observable<List<Item>> {
throw UnsupportedOperationException("Cannot get all items from network.")
throw UnsupportedOperationException("Cannot get all items from the network.")
}

override fun get(key: Long): Observable<Item> {
return itemService.getById(key)
}

override fun getById(id: Long): Observable<Item> {
return itemService.getById(id)
override fun put(key: Long, value: Item): Observable<Item> {
throw UnsupportedOperationException("Cannot put an item to the network.")
}

@Suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE")
override fun save(item: Item): Observable<Boolean> {
throw UnsupportedOperationException("Cannot save items to network.")
override fun remove(key: Long): Observable<Item> {
throw UnsupportedOperationException("Cannot remove an item from the network.")
}

// Private functions
Expand All @@ -51,7 +53,7 @@ class ItemRetrofitDataSource @Inject constructor(
}

val childObservable = Observable.from(item.kids.orEmpty())
.concatMapEager { getById(it) }
.concatMapEager { get(it) }
.map { it.copy(level = level) }
.filter { it.deleted != true }
.concatMapEager { getChildrenObservable(it, level + 1) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ class UserRetrofitDataSource @Inject constructor(
private val userService: UserService) : UserNetworkDataSource {

override fun getAll(): Observable<List<User>> {
throw UnsupportedOperationException("Cannot get all users from network API.")
throw UnsupportedOperationException("Cannot get all users from the network.")
}

override fun getById(id: String): Observable<User> {
return userService.getById(id)
override fun get(key: String): Observable<User> {
return userService.getById(key)
}

@Suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE")
override fun save(user: User): Observable<Boolean> {
throw UnsupportedOperationException("Cannot save a user to the network API.")
override fun put(key: String, value: User): Observable<User> {
throw UnsupportedOperationException("Cannot put a user to the network.")
}

override fun remove(key: String): Observable<User> {
throw UnsupportedOperationException("Cannot remove a user from the network.")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import clean.news.core.entity.Item.ListType
import rx.Observable

class ItemSqliteDataSource : ItemDiskDataSource {

override fun getItems(listType: ListType): Observable<List<Item>> {
return Observable.empty()
}
Expand All @@ -18,12 +19,16 @@ class ItemSqliteDataSource : ItemDiskDataSource {
return Observable.empty()
}

override fun getById(id: Long): Observable<Item> {
override fun get(key: Long): Observable<Item> {
return Observable.empty()
}

@Suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE")
override fun save(item: Item): Observable<Boolean> {
override fun put(key: Long, value: Item): Observable<Item> {
return Observable.empty()
}

override fun remove(key: Long): Observable<Item> {
return Observable.empty()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ import clean.news.core.entity.User
import rx.Observable

class UserSqliteDataSource : UserDiskDataSource {

override fun getAll(): Observable<List<User>> {
return Observable.empty()
}

override fun getById(id: String): Observable<User> {
override fun get(key: String): Observable<User> {
return Observable.empty()
}

@Suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE")
override fun save(user: User): Observable<Boolean> {
override fun put(key: String, value: User): Observable<User> {
return Observable.empty()
}

override fun remove(key: String): Observable<User> {
return Observable.empty()
}

}

0 comments on commit a9f16d0

Please sign in to comment.