Skip to content

Commit

Permalink
fix: show product not found in main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
VaiTon committed May 11, 2021
1 parent 2280eac commit 1682f89
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 226 deletions.
Expand Up @@ -24,7 +24,6 @@ import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import dagger.hilt.android.AndroidEntryPoint
import io.reactivex.android.schedulers.AndroidSchedulers
Expand Down Expand Up @@ -147,7 +146,7 @@ class ProductViewActivity : BaseActivity(), OnRefreshListener {
*/
private fun initViews() {
adapterResult = setupViewPager(binding.pager)
TabLayoutMediator(binding.tabs, binding.pager) { tab: TabLayout.Tab, position: Int ->
TabLayoutMediator(binding.tabs, binding.pager) { tab, position ->
tab.text = adapterResult!!.getPageTitle(position)
}.attach()
binding.navigationBottomInclude.bottomNavigation.selectNavigationItem(0)
Expand Down
Expand Up @@ -7,9 +7,12 @@ import androidx.lifecycle.ViewModel
import com.jakewharton.rxrelay2.BehaviorRelay
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import io.reactivex.Completable
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.addTo
import io.reactivex.rxkotlin.flatMapIterable
import io.reactivex.schedulers.Schedulers
import openfoodfacts.github.scrachx.openfood.R
import openfoodfacts.github.scrachx.openfood.models.DaoSession
Expand All @@ -23,10 +26,10 @@ import javax.inject.Inject
@HiltViewModel
@SuppressLint("StaticFieldLeak")
class ScanHistoryViewModel @Inject constructor(
@ApplicationContext private val context: Context,
private val daoSession: DaoSession,
private val client: OpenFoodAPIClient,
private val localeManager: LocaleManager
@ApplicationContext private val context: Context,
private val daoSession: DaoSession,
private val client: OpenFoodAPIClient,
private val localeManager: LocaleManager
) : ViewModel() {

private val compositeDisposable = CompositeDisposable()
Expand All @@ -42,83 +45,74 @@ class ScanHistoryViewModel @Inject constructor(
super.onCleared()
}

fun observeFetchProductState(): Observable<FetchProductsState> = fetchProductsStateRelay
fun observeFetchProductState() = fetchProductsStateRelay

fun refreshItems() {
fetchProductsStateRelay.accept(FetchProductsState.Loading)
Observable
.fromCallable {
daoSession.historyProductDao.queryBuilder().list()
}
.flatMap { prods ->
client.getProductsByBarcode(prods.map { it.barcode }).toObservable()
}
.flatMapIterable { it }
.map { product ->
val historyProduct = daoSession.historyProductDao.queryBuilder()
.where(HistoryProductDao.Properties.Barcode.eq(product.code))
.build()
.unique()
product.productName?.let { historyProduct.title = it }
product.brands?.let { historyProduct.brands = it }
product.getImageSmallUrl(localeManager.getLanguage())?.let { historyProduct.url = it }
product.quantity?.let { historyProduct.quantity = it }
product.nutritionGradeFr?.let { historyProduct.nutritionGrade = it }
product.ecoscore?.let { historyProduct.ecoscore = it }
product.novaGroups?.let { historyProduct.novaGroup = it }
daoSession.historyProductDao.update(historyProduct)
}
.toList()
.map {
daoSession.historyProductDao.queryBuilder().list()
}
.map { it.customSort() }
.subscribeOn(Schedulers.io())
.subscribe(
{ items -> fetchProductsStateRelay.accept(FetchProductsState.Data(items)) },
{ fetchProductsStateRelay.accept(FetchProductsState.Error) }
)
.addTo(compositeDisposable)
Single.fromCallable { daoSession.historyProductDao.queryBuilder().list() }
.flatMap { products ->
client.getProductsByBarcode(products.map { it.barcode })
}
.toObservable()
.flatMapIterable()
.map { product ->
val historyProduct = daoSession.historyProductDao.queryBuilder()
.where(HistoryProductDao.Properties.Barcode.eq(product.code))
.build()
.unique()

product.productName?.let { historyProduct.title = it }
product.brands?.let { historyProduct.brands = it }
product.getImageSmallUrl(localeManager.getLanguage())?.let { historyProduct.url = it }
product.quantity?.let { historyProduct.quantity = it }
product.nutritionGradeFr?.let { historyProduct.nutritionGrade = it }
product.ecoscore?.let { historyProduct.ecoscore = it }
product.novaGroups?.let { historyProduct.novaGroup = it }
daoSession.historyProductDao.update(historyProduct)
}.toList()

.map { daoSession.historyProductDao.queryBuilder().list() }
.map { it.customSort() }
.subscribeOn(Schedulers.io())
.doOnError { fetchProductsStateRelay.accept(FetchProductsState.Error) }
.subscribe { items -> fetchProductsStateRelay.accept(FetchProductsState.Data(items)) }
.addTo(compositeDisposable)
}

fun clearHistory() {
fetchProductsStateRelay.accept(FetchProductsState.Loading)
Observable
.fromCallable {
daoSession.historyProductDao.deleteAll()
}
.subscribeOn(Schedulers.io())
.doOnError { fetchProductsStateRelay.accept(FetchProductsState.Error) }
.subscribe { fetchProductsStateRelay.accept(FetchProductsState.Data(emptyList())) }
.addTo(compositeDisposable)
Completable.fromCallable { daoSession.historyProductDao.deleteAll() }
.subscribeOn(Schedulers.io())
.doOnError { fetchProductsStateRelay.accept(FetchProductsState.Error) }
.subscribe { fetchProductsStateRelay.accept(FetchProductsState.Data(emptyList())) }
.addTo(compositeDisposable)
}

fun removeProductFromHistory(product: HistoryProduct) {
Observable
.fromCallable {
daoSession.historyProductDao.delete(product)
daoSession.historyProductDao.queryBuilder().list()
}
.map { it.customSort() }
.subscribeOn(Schedulers.io())
.doOnError { fetchProductsStateRelay.accept(FetchProductsState.Error) }
.subscribe { products -> fetchProductsStateRelay.accept(FetchProductsState.Data(products)) }
.addTo(compositeDisposable)
Observable.fromCallable {
daoSession.historyProductDao.delete(product)
daoSession.historyProductDao.queryBuilder().list()
}
.map { it.customSort() }
.subscribeOn(Schedulers.io())
.doOnError { fetchProductsStateRelay.accept(FetchProductsState.Error) }
.subscribe { products -> fetchProductsStateRelay.accept(FetchProductsState.Data(products)) }
.addTo(compositeDisposable)

}

fun updateSortType(type: SortType) {
sortType = type
fetchProductsStateRelay
.take(1)
.map {
(it as? FetchProductsState.Data)?.items?.customSort() ?: emptyList()
}
.filter { it.isNotEmpty() }
.subscribeOn(Schedulers.io())
.doOnError { fetchProductsStateRelay.accept(FetchProductsState.Error) }
.subscribe { products -> fetchProductsStateRelay.accept(FetchProductsState.Data(products)) }
.addTo(compositeDisposable)
.take(1)
.map {
(it as? FetchProductsState.Data)?.items?.customSort() ?: emptyList()
}
.filter { it.isNotEmpty() }
.subscribeOn(Schedulers.io())
.doOnError { fetchProductsStateRelay.accept(FetchProductsState.Error) }
.subscribe { products -> fetchProductsStateRelay.accept(FetchProductsState.Data(products)) }
.addTo(compositeDisposable)
}

fun openProduct(barcode: String, activity: Activity) {
Expand Down

0 comments on commit 1682f89

Please sign in to comment.