Added caching repositories to device

This commit is contained in:
Viktor De Pasquale 2019-05-12 20:21:55 +02:00
parent c1c677e161
commit 92789c3113
3 changed files with 24 additions and 11 deletions

View File

@ -1,12 +1,15 @@
package com.topjohnwu.magisk.data.repository package com.topjohnwu.magisk.data.repository
import android.content.Context 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.GithubApiServices
import com.topjohnwu.magisk.data.network.GithubRawApiServices import com.topjohnwu.magisk.data.network.GithubRawApiServices
import com.topjohnwu.magisk.data.network.GithubServices import com.topjohnwu.magisk.data.network.GithubServices
import com.topjohnwu.magisk.model.entity.GithubRepo import com.topjohnwu.magisk.model.entity.GithubRepo
import com.topjohnwu.magisk.model.entity.toRepository import com.topjohnwu.magisk.model.entity.toRepository
import com.topjohnwu.magisk.utils.Utils import com.topjohnwu.magisk.utils.Utils
import com.topjohnwu.magisk.utils.toSingle
import com.topjohnwu.magisk.utils.writeToFile import com.topjohnwu.magisk.utils.writeToFile
import com.topjohnwu.magisk.utils.writeToString import com.topjohnwu.magisk.utils.writeToString
import io.reactivex.Single import io.reactivex.Single
@ -15,10 +18,15 @@ class ModuleRepository(
private val context: Context, private val context: Context,
private val apiRaw: GithubRawApiServices, private val apiRaw: GithubRawApiServices,
private val api: GithubApiServices, 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 { .map {
it.mapNotNull { it.mapNotNull {
runCatching { runCatching {
@ -26,6 +34,7 @@ class ModuleRepository(
}.getOrNull() }.getOrNull()
} }
} }
.doOnSuccess { repoDao.insert(it) }
fun fetchInstalledModules() = Single.fromCallable { Utils.loadModulesLeanback() } fun fetchInstalledModules() = Single.fromCallable { Utils.loadModulesLeanback() }
.map { it.values.toList() } .map { it.values.toList() }

View File

@ -6,7 +6,7 @@ import org.koin.dsl.module
val repositoryModule = module { val repositoryModule = module {
single { MagiskRepository(get(), get(), get()) } single { MagiskRepository(get(), get(), get()) }
single { ModuleRepository(get(), get(), get(), get()) } single { ModuleRepository(get(), get(), get(), get(), get()) }
single { LogRepository(get()) } single { LogRepository(get()) }
single { AppRepository(get()) } single { AppRepository(get()) }
single { SettingRepository(get()) } single { SettingRepository(get()) }

View File

@ -5,6 +5,7 @@ import android.database.Cursor
import androidx.annotation.StringRes import androidx.annotation.StringRes
import com.skoumal.teanity.databinding.ComparableRvItem import com.skoumal.teanity.databinding.ComparableRvItem
import com.skoumal.teanity.extensions.addOnPropertyChangedCallback import com.skoumal.teanity.extensions.addOnPropertyChangedCallback
import com.skoumal.teanity.extensions.doOnSuccessUi
import com.skoumal.teanity.extensions.subscribeK import com.skoumal.teanity.extensions.subscribeK
import com.skoumal.teanity.util.DiffObservableList import com.skoumal.teanity.util.DiffObservableList
import com.skoumal.teanity.util.KObservableField 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.ui.base.MagiskViewModel
import com.topjohnwu.magisk.utils.toSingle import com.topjohnwu.magisk.utils.toSingle
import com.topjohnwu.magisk.utils.update import com.topjohnwu.magisk.utils.update
import com.topjohnwu.magisk.utils.zip
import io.reactivex.disposables.Disposable import io.reactivex.disposables.Disposable
import me.tatarka.bindingcollectionadapter2.OnItemBind import me.tatarka.bindingcollectionadapter2.OnItemBind
@ -54,7 +56,16 @@ class ModuleViewModel(
fun downloadPressed(item: RepoRvItem) = InstallModuleEvent(item.item).publish() fun downloadPressed(item: RepoRvItem) = InstallModuleEvent(item.item).publish()
fun refresh() { 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 } .flattenAsFlowable { it }
.map { RepoRvItem(it) } .map { RepoRvItem(it) }
.toList() .toList()
@ -62,13 +73,6 @@ class ModuleViewModel(
.flatMap { queryRaw() } .flatMap { queryRaw() }
.applyViewModel(this) .applyViewModel(this)
.subscribeK { itemsRemote.update(it.first, it.second) } .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() private fun query() = queryRaw()