From 657056e6369ba7c6c52c1105d2decadefdb97710 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 24 Jan 2021 06:55:43 -0800 Subject: [PATCH] Cache changelog files --- .../main/java/com/topjohnwu/magisk/core/Const.kt | 4 ++++ .../magisk/events/dialog/ManagerInstallDialog.kt | 14 +++++++++++++- .../magisk/ui/install/InstallViewModel.kt | 16 +++++++++++++++- app/src/main/res/layout/fragment_install_md2.xml | 1 + 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/core/Const.kt b/app/src/main/java/com/topjohnwu/magisk/core/Const.kt index 0298cfded..e3cbc690b 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/Const.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/Const.kt @@ -2,6 +2,7 @@ package com.topjohnwu.magisk.core import android.os.Build import android.os.Process +import com.topjohnwu.magisk.BuildConfig import java.io.File @Suppress("DEPRECATION") @@ -61,6 +62,9 @@ object Const { const val PATREON_URL = "https://www.patreon.com/topjohnwu" const val SOURCE_CODE_URL = "https://github.com/topjohnwu/Magisk" + val CHANGELOG_URL = if (Version.isCanary()) Info.remote.magisk.note + else "https://topjohnwu.github.io/Magisk/releases/${BuildConfig.VERSION_CODE}.md" + const val GITHUB_RAW_URL = "https://raw.githubusercontent.com/" const val GITHUB_API_URL = "https://api.github.com/" const val GITHUB_PAGE_URL = "https://topjohnwu.github.io/magisk_files/" diff --git a/app/src/main/java/com/topjohnwu/magisk/events/dialog/ManagerInstallDialog.kt b/app/src/main/java/com/topjohnwu/magisk/events/dialog/ManagerInstallDialog.kt index 78a2c053f..6e7063f74 100644 --- a/app/src/main/java/com/topjohnwu/magisk/events/dialog/ManagerInstallDialog.kt +++ b/app/src/main/java/com/topjohnwu/magisk/events/dialog/ManagerInstallDialog.kt @@ -1,18 +1,30 @@ package com.topjohnwu.magisk.events.dialog +import android.content.Context import com.topjohnwu.magisk.R import com.topjohnwu.magisk.core.Info import com.topjohnwu.magisk.core.download.DownloadService import com.topjohnwu.magisk.core.download.Subject import com.topjohnwu.magisk.data.repository.NetworkService import com.topjohnwu.magisk.view.MagiskDialog +import org.koin.core.get import org.koin.core.inject +import java.io.File class ManagerInstallDialog : MarkDownDialog() { private val svc: NetworkService by inject() - override suspend fun getMarkdownText() = svc.fetchString(Info.remote.magisk.note) + override suspend fun getMarkdownText(): String { + val text = svc.fetchString(Info.remote.magisk.note) + // Cache the changelog + val context = get() + context.cacheDir.listFiles { _, name -> name.endsWith(".md") }.orEmpty().forEach { + it.delete() + } + File(context.cacheDir, "${Info.remote.magisk.versionCode}.md").writeText(text) + return text + } override fun build(dialog: MagiskDialog) { super.build(dialog) diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/install/InstallViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/install/InstallViewModel.kt index a24435933..b7bed34ba 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/install/InstallViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/install/InstallViewModel.kt @@ -1,12 +1,15 @@ package com.topjohnwu.magisk.ui.install import android.app.Activity +import android.content.Context import android.net.Uri import androidx.databinding.Bindable import androidx.lifecycle.viewModelScope import com.topjohnwu.magisk.BR +import com.topjohnwu.magisk.BuildConfig import com.topjohnwu.magisk.R import com.topjohnwu.magisk.arch.BaseViewModel +import com.topjohnwu.magisk.core.Const import com.topjohnwu.magisk.core.Info import com.topjohnwu.magisk.data.repository.NetworkService import com.topjohnwu.magisk.events.MagiskInstallFileEvent @@ -15,7 +18,9 @@ import com.topjohnwu.magisk.ui.flash.FlashFragment import com.topjohnwu.magisk.utils.set import com.topjohnwu.superuser.Shell import kotlinx.coroutines.launch +import org.koin.core.get import timber.log.Timber +import java.io.File import java.io.IOException class InstallViewModel( @@ -60,7 +65,16 @@ class InstallViewModel( init { viewModelScope.launch { try { - notes = svc.fetchString(Info.remote.magisk.note) + val context = get() + File(context.cacheDir, "${BuildConfig.VERSION_CODE}.md").run { + notes = if (exists()) + readText() + else { + val text = svc.fetchString(Const.Url.CHANGELOG_URL) + writeText(text) + text + } + } } catch (e: IOException) { Timber.e(e) } diff --git a/app/src/main/res/layout/fragment_install_md2.xml b/app/src/main/res/layout/fragment_install_md2.xml index 5fc64ea12..f1eb7848f 100644 --- a/app/src/main/res/layout/fragment_install_md2.xml +++ b/app/src/main/res/layout/fragment_install_md2.xml @@ -214,6 +214,7 @@