Last active
November 28, 2018 05:28
-
-
Save kaiyan910/585c9ae46e2e6c34fa7bd740ce47fc83 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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