Skip to content

Commit

Permalink
feat(assets): display specific progress when updating assets
Browse files Browse the repository at this point in the history
And fix show bug.

popd code: show progress
  • Loading branch information
xchacha20-poly1305 committed Dec 31, 2023
1 parent 2f0591b commit 897efa2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
47 changes: 33 additions & 14 deletions app/src/main/java/io/nekohasekai/sagernet/ui/AssetsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ class AssetsActivity : ThemedActivity() {

init {
reloadAssets()
updating.visibility = View.GONE
}

fun reloadAssets() {
Expand All @@ -174,11 +175,11 @@ class AssetsActivity : ThemedActivity() {
assets.add(File(filesDir, "geosite.version.txt"))
// if (files != null) assets.addAll(files)

updating.visibility = View.GONE

layout.refreshLayout.post {
notifyDataSetChanged()
}

updating.setProgressCompat(0, true)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AssetHolder {
Expand Down Expand Up @@ -239,23 +240,27 @@ class AssetsActivity : ThemedActivity() {

private suspend fun updateAsset() {
val filesDir = getExternalFilesDir(null) ?: filesDir

var progress = 0
fun setProgress() {
updating.setProgressCompat(progress, true)
}

val repos: List<String> = when (DataStore.rulesProvider) {
0 -> listOf("SagerNet/sing-geoip", "SagerNet/sing-geosite")
1 -> listOf("xchacha20-poly1305/sing-geoip", "xchacha20-poly1305/sing-geosite")
2 -> listOf("Chocolate4U/Iran-sing-box-rules")
else -> listOf("SagerNet/sing-geoip", "SagerNet/sing-geosite")
}

progress = 15
onMainDispatcher {
setProgress()
updating.visibility = View.VISIBLE
}

val geoDir = File(filesDir, "geo")
var cacheFiles: Array<File> = arrayOf()

for ((list, repo) in repos.withIndex()) {
for ((i, repo) in repos.withIndex()) {

val client = Libcore.newHttpClient().apply {
modernTLS()
Expand All @@ -270,37 +275,44 @@ class AssetsActivity : ThemedActivity() {
setURL("https://codeload.github.com/$repo/zip/refs/heads/rule-set")
}.execute()

val cacheFile = File(filesDir.parentFile, filesDir.name + list + ".tmp")
val cacheFile = File(filesDir.parentFile, filesDir.name + i + ".tmp")
cacheFile.parentFile?.mkdirs()
response.writeTo(cacheFile.canonicalPath)
cacheFiles += cacheFile

adapter.reloadAssets()

} catch (e: Exception) {
onMainDispatcher {
e.message?.let { snackbar(it).show() }
}
} finally {
client.close()
}

progress += 20
onMainDispatcher {
setProgress()
}

}

for (cacheFile in cacheFiles) {
progress += 15
onMainDispatcher {
setProgress()
}
Libcore.unzipWithoutDir(cacheFile.absolutePath, geoDir.absolutePath)
cacheFile.delete()
}

onMainDispatcher {
snackbar(R.string.route_asset_updated).show()
updating.visibility = View.GONE
}

val versionFileList: List<File> = listOf(
File(filesDir, "geoip.version.txt"),
File(filesDir, "geosite.version.txt")
)
for (versionFile in versionFileList) {
progress += 5
onMainDispatcher {
setProgress()
}
versionFile.writeText(
LocalDate.now()
.format(
Expand All @@ -309,7 +321,14 @@ class AssetsActivity : ThemedActivity() {
)
)
}
adapter.reloadAssets()

progress = 100
onMainDispatcher {
setProgress()
updating.visibility = View.GONE
adapter.reloadAssets()
snackbar(R.string.route_asset_updated).show()
}
}

override fun onSupportNavigateUp(): Boolean {
Expand Down
2 changes: 1 addition & 1 deletion husi.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
PACKAGE_NAME=fr.husi
VERSION_NAME=0.1.1
VERSION_CODE=6
VERSION_CODE=7

0 comments on commit 897efa2

Please sign in to comment.