Cache changelog files

This commit is contained in:
topjohnwu 2021-01-24 06:55:43 -08:00
parent 9d5efea66e
commit 657056e636
4 changed files with 33 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package com.topjohnwu.magisk.core
import android.os.Build import android.os.Build
import android.os.Process import android.os.Process
import com.topjohnwu.magisk.BuildConfig
import java.io.File import java.io.File
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
@ -61,6 +62,9 @@ object Const {
const val PATREON_URL = "https://www.patreon.com/topjohnwu" const val PATREON_URL = "https://www.patreon.com/topjohnwu"
const val SOURCE_CODE_URL = "https://github.com/topjohnwu/Magisk" 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_RAW_URL = "https://raw.githubusercontent.com/"
const val GITHUB_API_URL = "https://api.github.com/" const val GITHUB_API_URL = "https://api.github.com/"
const val GITHUB_PAGE_URL = "https://topjohnwu.github.io/magisk_files/" const val GITHUB_PAGE_URL = "https://topjohnwu.github.io/magisk_files/"

View File

@ -1,18 +1,30 @@
package com.topjohnwu.magisk.events.dialog package com.topjohnwu.magisk.events.dialog
import android.content.Context
import com.topjohnwu.magisk.R import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.core.Info import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.download.DownloadService import com.topjohnwu.magisk.core.download.DownloadService
import com.topjohnwu.magisk.core.download.Subject import com.topjohnwu.magisk.core.download.Subject
import com.topjohnwu.magisk.data.repository.NetworkService import com.topjohnwu.magisk.data.repository.NetworkService
import com.topjohnwu.magisk.view.MagiskDialog import com.topjohnwu.magisk.view.MagiskDialog
import org.koin.core.get
import org.koin.core.inject import org.koin.core.inject
import java.io.File
class ManagerInstallDialog : MarkDownDialog() { class ManagerInstallDialog : MarkDownDialog() {
private val svc: NetworkService by inject() 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>()
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) { override fun build(dialog: MagiskDialog) {
super.build(dialog) super.build(dialog)

View File

@ -1,12 +1,15 @@
package com.topjohnwu.magisk.ui.install package com.topjohnwu.magisk.ui.install
import android.app.Activity import android.app.Activity
import android.content.Context
import android.net.Uri import android.net.Uri
import androidx.databinding.Bindable import androidx.databinding.Bindable
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.topjohnwu.magisk.BR import com.topjohnwu.magisk.BR
import com.topjohnwu.magisk.BuildConfig
import com.topjohnwu.magisk.R import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.arch.BaseViewModel import com.topjohnwu.magisk.arch.BaseViewModel
import com.topjohnwu.magisk.core.Const
import com.topjohnwu.magisk.core.Info import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.data.repository.NetworkService import com.topjohnwu.magisk.data.repository.NetworkService
import com.topjohnwu.magisk.events.MagiskInstallFileEvent 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.magisk.utils.set
import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.koin.core.get
import timber.log.Timber import timber.log.Timber
import java.io.File
import java.io.IOException import java.io.IOException
class InstallViewModel( class InstallViewModel(
@ -60,7 +65,16 @@ class InstallViewModel(
init { init {
viewModelScope.launch { viewModelScope.launch {
try { try {
notes = svc.fetchString(Info.remote.magisk.note) val context = get<Context>()
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) { } catch (e: IOException) {
Timber.e(e) Timber.e(e)
} }

View File

@ -214,6 +214,7 @@
<com.google.android.material.card.MaterialCardView <com.google.android.material.card.MaterialCardView
style="@style/WidgetFoundation.Card" style="@style/WidgetFoundation.Card"
gone="@{viewModel.notes.empty}"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="@dimen/l1" android:layout_marginTop="@dimen/l1"