Skip to content

Instantly share code, notes, and snippets.

@kaiyan910
Last active November 28, 2018 05:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaiyan910/585c9ae46e2e6c34fa7bd740ce47fc83 to your computer and use it in GitHub Desktop.
Save kaiyan910/585c9ae46e2e6c34fa7bd740ce47fc83 to your computer and use it in GitHub Desktop.
override fun queryClassifyShopsByPrefix(shopCategory: ShopCategory,
filterParams: FilterParams): Observable<List<ShopClassifyByPrefix>> {
return checkIsInit().flatMap { init ->
Observable
// 先把所有東西拿出來
.fromCallable {
// TODO: 要加一個 Dao 方法: "用 shopCategory 拿所有 ShopEntity"
mShopDao.queryAllShop()
}
// 轉成 Observable<ShopEntity>
.flatMapIterable { list -> list }
// 做一次 "floor" Filter
.filter {
if (filterParams.floor == Constant.FILTER_TYPE_ALL) {
true
} else {
it.floor == filterParams.floor
}
}
// 做一次 "type" Filter
.filter {
if (filterParams.type == Constant.FILTER_TYPE_ALL) {
true
} else {
it.type_en.contains(filterParams.type)
|| it.type_tc.contains(filterParams.type)
|| it.type_sc.contains(filterParams.type)
}
}
// 用 ShopEntity 的英文名字的第一個字母做 Grouping
.groupBy { it.name_en[0].toString() }
.flatMap {
Timber.d("<DEBUG> group = %s", it.key)
var row = 0
it
// 開始 Observe 接收上一層的 GroupedObservable 的 Observable<ShopEntity>,
// 然後把他轉成一個 ShopClassifyByPrefix
.reduce(ShopClassifyByPrefix(it.key!!, mutableMapOf())) { model, shop ->
if (!model.shopsByRow.containsKey(row)) {
model.shopsByRow[row] = mutableListOf()
}
model.shopsByRow[row]?.add(shop)
if (model.shopsByRow[row]?.size == 3) {
row++
}
model
}
.toObservable()
}
// 接上一層的 ShopClassifyByPrefix 變成 List<ShopClassifyByPrefix>
.toList()
.toObservable()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment