Added feature that prevents repositories from being downloaded every single time that user requests to show Module/Download fragment unless requested by user

This commit is contained in:
Viktor De Pasquale 2019-05-25 16:42:34 +02:00
parent 1c90b6eca3
commit 6e1aefe6d8
6 changed files with 11 additions and 10 deletions

View File

@ -2,6 +2,7 @@ package com.topjohnwu.magisk.data.database
import androidx.room.Dao
import androidx.room.Query
import androidx.room.Transaction
import com.skoumal.teanity.database.BaseDao
import com.topjohnwu.magisk.model.entity.Repository
@ -9,6 +10,7 @@ import com.topjohnwu.magisk.model.entity.Repository
interface RepositoryDao : BaseDao<Repository> {
@Query("DELETE FROM repos")
@Transaction
override fun deleteAll()
@Query("SELECT * FROM repos")

View File

@ -1,7 +1,6 @@
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
@ -23,9 +22,9 @@ class ModuleRepository(
private val repoDao: RepositoryDao
) {
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
fun fetchModules(force: Boolean = false) =
if (force) fetchRemoteRepos() else Single.fromCallable { repoDao.fetchAll() }
.flatMap { if (it.isEmpty()) fetchRemoteRepos() else it.toSingle() }
private fun fetchRemoteRepos() = fetchAllRepos()
.map {

View File

@ -48,14 +48,14 @@ class ModuleViewModel(
queryDisposable?.dispose()
queryDisposable = query()
}
refresh()
refresh(false)
}
fun fabPressed() = OpenFilePickerEvent().publish()
fun repoPressed(item: RepoRvItem) = OpenChangelogEvent(item.item).publish()
fun downloadPressed(item: RepoRvItem) = InstallModuleEvent(item.item).publish()
fun refresh() {
fun refresh(forceReload: Boolean) {
val updateInstalled = moduleRepo.fetchInstalledModules()
.flattenAsFlowable { it }
.map { ModuleRvItem(it) }
@ -63,7 +63,7 @@ class ModuleViewModel(
.map { it to itemsInstalled.calculateDiff(it) }
.doOnSuccessUi { itemsInstalled.update(it.first, it.second) }
val updateRemote = moduleRepo.fetchModules()
val updateRemote = moduleRepo.fetchModules(forceReload)
zip(updateInstalled, updateRemote) { _, remote -> remote }
.flattenAsFlowable { it }

View File

@ -56,7 +56,7 @@ class ReposFragment : MagiskFragment<ModuleViewModel, FragmentReposBinding>(),
Config.get<Int>(Config.Key.REPO_ORDER)!!
) { d, which ->
Config.set(Config.Key.REPO_ORDER, which)
viewModel.refresh()
viewModel.refresh(false)
d.dismiss()
}.show()
}

View File

@ -21,7 +21,7 @@
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:onRefreshListener="@{() -> viewModel.refresh()}"
app:onRefreshListener="@{() -> viewModel.refresh(false)}"
app:refreshing="@{viewModel.loading}">
<androidx.recyclerview.widget.RecyclerView

View File

@ -21,7 +21,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:onRefreshListener="@{() -> viewModel.refresh()}"
app:onRefreshListener="@{() -> viewModel.refresh(true)}"
app:refreshing="@{viewModel.loading}">
<androidx.recyclerview.widget.RecyclerView