From 92789c3113a5ab93634795fafbd7bafaedf454c1 Mon Sep 17 00:00:00 2001 From: Viktor De Pasquale Date: Sun, 12 May 2019 20:21:55 +0200 Subject: [PATCH] Added caching repositories to device --- .../data/repository/ModuleRepository.kt | 13 ++++++++++-- .../topjohnwu/magisk/di/RepositoryModule.kt | 2 +- .../magisk/ui/module/ModuleViewModel.kt | 20 +++++++++++-------- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/data/repository/ModuleRepository.kt b/app/src/main/java/com/topjohnwu/magisk/data/repository/ModuleRepository.kt index a47fafd08..05106b5ab 100644 --- a/app/src/main/java/com/topjohnwu/magisk/data/repository/ModuleRepository.kt +++ b/app/src/main/java/com/topjohnwu/magisk/data/repository/ModuleRepository.kt @@ -1,12 +1,15 @@ package com.topjohnwu.magisk.data.repository import android.content.Context +import com.skoumal.teanity.extensions.subscribeK +import com.topjohnwu.magisk.data.database.RepositoryDao import com.topjohnwu.magisk.data.network.GithubApiServices import com.topjohnwu.magisk.data.network.GithubRawApiServices import com.topjohnwu.magisk.data.network.GithubServices import com.topjohnwu.magisk.model.entity.GithubRepo import com.topjohnwu.magisk.model.entity.toRepository import com.topjohnwu.magisk.utils.Utils +import com.topjohnwu.magisk.utils.toSingle import com.topjohnwu.magisk.utils.writeToFile import com.topjohnwu.magisk.utils.writeToString import io.reactivex.Single @@ -15,10 +18,15 @@ class ModuleRepository( private val context: Context, private val apiRaw: GithubRawApiServices, private val api: GithubApiServices, - private val apiWeb: GithubServices + private val apiWeb: GithubServices, + private val repoDao: RepositoryDao ) { - fun fetchModules() = fetchAllRepos() + fun fetchModules() = Single.fromCallable { repoDao.fetchAll() } + .flatMap { if (it.isEmpty()) fetchRemoteRepos() else it.toSingle() } + .doOnSuccess { fetchRemoteRepos().subscribeK() } // cache changed for next time or next hot reload + + private fun fetchRemoteRepos() = fetchAllRepos() .map { it.mapNotNull { runCatching { @@ -26,6 +34,7 @@ class ModuleRepository( }.getOrNull() } } + .doOnSuccess { repoDao.insert(it) } fun fetchInstalledModules() = Single.fromCallable { Utils.loadModulesLeanback() } .map { it.values.toList() } diff --git a/app/src/main/java/com/topjohnwu/magisk/di/RepositoryModule.kt b/app/src/main/java/com/topjohnwu/magisk/di/RepositoryModule.kt index 1c1383d52..f124f193e 100644 --- a/app/src/main/java/com/topjohnwu/magisk/di/RepositoryModule.kt +++ b/app/src/main/java/com/topjohnwu/magisk/di/RepositoryModule.kt @@ -6,7 +6,7 @@ import org.koin.dsl.module val repositoryModule = module { single { MagiskRepository(get(), get(), get()) } - single { ModuleRepository(get(), get(), get(), get()) } + single { ModuleRepository(get(), get(), get(), get(), get()) } single { LogRepository(get()) } single { AppRepository(get()) } single { SettingRepository(get()) } diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleViewModel.kt index a360330e8..9d898b2a9 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleViewModel.kt @@ -5,6 +5,7 @@ import android.database.Cursor import androidx.annotation.StringRes import com.skoumal.teanity.databinding.ComparableRvItem import com.skoumal.teanity.extensions.addOnPropertyChangedCallback +import com.skoumal.teanity.extensions.doOnSuccessUi import com.skoumal.teanity.extensions.subscribeK import com.skoumal.teanity.util.DiffObservableList import com.skoumal.teanity.util.KObservableField @@ -20,6 +21,7 @@ import com.topjohnwu.magisk.model.events.OpenFilePickerEvent import com.topjohnwu.magisk.ui.base.MagiskViewModel import com.topjohnwu.magisk.utils.toSingle import com.topjohnwu.magisk.utils.update +import com.topjohnwu.magisk.utils.zip import io.reactivex.disposables.Disposable import me.tatarka.bindingcollectionadapter2.OnItemBind @@ -54,7 +56,16 @@ class ModuleViewModel( fun downloadPressed(item: RepoRvItem) = InstallModuleEvent(item.item).publish() fun refresh() { - moduleRepo.fetchModules() + val updateInstalled = moduleRepo.fetchInstalledModules() + .flattenAsFlowable { it } + .map { ModuleRvItem(it) } + .toList() + .map { it to itemsInstalled.calculateDiff(it) } + .doOnSuccessUi { itemsInstalled.update(it.first, it.second) } + + val updateRemote = moduleRepo.fetchModules() + + zip(updateInstalled, updateRemote) { _, remote -> remote } .flattenAsFlowable { it } .map { RepoRvItem(it) } .toList() @@ -62,13 +73,6 @@ class ModuleViewModel( .flatMap { queryRaw() } .applyViewModel(this) .subscribeK { itemsRemote.update(it.first, it.second) } - - moduleRepo.fetchInstalledModules() - .flattenAsFlowable { it } - .map { ModuleRvItem(it) } - .toList() - .map { it to itemsInstalled.calculateDiff(it) } - .subscribeK { itemsInstalled.update(it.first, it.second) } } private fun query() = queryRaw()