Skip to content
This repository was archived by the owner on Jul 18, 2026. It is now read-only.

Commit 84304ea

Browse files
authored
Merge pull request #179 from misaka10032w/develop
[Fix] 修复侧边栏在非预期位置打开的问题、修复更新下载进度条卡在0假死的问题
2 parents 7c323fa + cf48194 commit 84304ea

5 files changed

Lines changed: 43 additions & 10 deletions

File tree

app/src/main/java/com/yenaly/han1meviewer/logic/network/HUpdater.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ object HUpdater {
8181
*
8282
* @param url update url
8383
*/
84-
suspend fun File.injectUpdate(url: String, progress: (suspend (Int) -> Unit)? = null) {
84+
suspend fun File.injectUpdate(url: String, progress: (suspend (Int, Long, Long) -> Unit)? = null) {
8585
val res = HanimeNetwork.githubService.request(url)
8686
if (url.endsWith("zip")) {
8787
Log.d(TAG, "Injecting update from zip ($url)")
@@ -90,7 +90,9 @@ object HUpdater {
9090
ZipInputStream(stream).use { zip ->
9191
zip.nextEntry
9292
this.outputStream().use {
93-
zip.copyTo(it, body.contentLength(), progress = progress)
93+
Log.i(TAG, "content length: ${body.contentLength()}")
94+
// 估摸着压缩率为0.56左右,稍微估算解压后大小,防止进度卡在100%时间过长
95+
zip.copyTo(it, (body.contentLength() * 1.79).toLong(), progress = progress)
9496
}
9597
}
9698
}
@@ -99,6 +101,7 @@ object HUpdater {
99101
Log.d(TAG, "Injecting update from release ($url)")
100102
this.outputStream().use {
101103
res.body()?.use { body ->
104+
Log.i(TAG, "content length: ${body.contentLength()}")
102105
body.byteStream().copyTo(it, body.contentLength(), progress = progress)
103106
}
104107
}

app/src/main/java/com/yenaly/han1meviewer/logic/network/service/HGitHubService.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import retrofit2.Response
1010
import retrofit2.http.GET
1111
import retrofit2.http.Path
1212
import retrofit2.http.Query
13+
import retrofit2.http.Streaming
1314
import retrofit2.http.Url
1415

1516
/**
@@ -60,6 +61,7 @@ interface HGitHubService {
6061
* Typical request
6162
*/
6263
@GET
64+
@Streaming
6365
suspend fun request(
6466
@Url url: String,
6567
): Response<ResponseBody>

app/src/main/java/com/yenaly/han1meviewer/ui/activity/MainActivity.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
4040
import androidx.core.view.GravityCompat
4141
import androidx.core.view.ViewCompat
4242
import androidx.core.view.WindowInsetsCompat
43+
import androidx.drawerlayout.widget.DrawerLayout
4344
import androidx.drawerlayout.widget.DrawerLayout.DrawerListener
4445
import androidx.fragment.app.Fragment
4546
import androidx.fragment.app.FragmentActivity
@@ -171,6 +172,11 @@ class MainActivity : YenalyActivity<ActivityMainBinding>(), DrawerListener, Tool
171172
if (targetId != null && targetId != currentCheckedId) {
172173
binding.nvMain.setCheckedItem(targetId)
173174
}
175+
if (destination.id == R.id.nv_home_page){
176+
binding.dlMain.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
177+
} else {
178+
binding.dlMain.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
179+
}
174180
}
175181
}
176182

app/src/main/java/com/yenaly/han1meviewer/util/Files.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.content.ActivityNotFoundException
44
import android.content.ContentResolver
55
import android.content.Context
66
import android.content.Intent
7+
import android.util.Log
78
import androidx.core.content.FileProvider
89
import androidx.core.net.toUri
910
import com.yenaly.han1meviewer.FILE_PROVIDER_AUTHORITY
@@ -110,7 +111,7 @@ suspend fun InputStream.copyTo(
110111
out: OutputStream,
111112
contentLength: Long,
112113
bufferSize: Int = DEFAULT_BUFFER_SIZE,
113-
progress: (suspend (Int) -> Unit)? = null,
114+
progress: (suspend (Int, Long, Long) -> Unit)? = null,
114115
): Long {
115116
return withContext(Dispatchers.IO) {
116117
this@copyTo.use {
@@ -125,10 +126,11 @@ suspend fun InputStream.copyTo(
125126
val newPercent = (bytesCopied * 100 / contentLength).toInt()
126127
if (newPercent != percent) {
127128
percent = newPercent
128-
progress?.invoke(percent.coerceAtMost(100))
129+
progress?.invoke(percent.coerceAtMost(100), contentLength, bytesCopied)
129130
}
130131
bytes = read(buffer)
131132
}
133+
Log.i("progress",bytesCopied.toString())
132134
bytesCopied
133135
}
134136
}

app/src/main/java/com/yenaly/han1meviewer/worker/HUpdateWorker.kt

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.app.Notification
55
import android.content.Context
66
import android.content.pm.ServiceInfo
77
import android.os.Build
8+
import android.util.Log
89
import androidx.core.app.NotificationCompat
910
import androidx.core.app.NotificationManagerCompat
1011
import androidx.core.net.toFile
@@ -29,6 +30,7 @@ import com.yenaly.han1meviewer.util.installApkPackage
2930
import com.yenaly.han1meviewer.util.runSuspendCatching
3031
import com.yenaly.han1meviewer.util.updateFile
3132
import com.yenaly.yenaly_libs.utils.showShortToast
33+
import java.util.Locale
3234
import kotlin.random.Random
3335

3436
/**
@@ -105,8 +107,8 @@ class HUpdateWorker(
105107
val file = context.updateFile.apply { delete() }
106108
val inject = runSuspendCatching {
107109
setForeground(createForegroundInfo())
108-
file.injectUpdate(downloadLink) { progress ->
109-
updateNotification(progress)
110+
file.injectUpdate(downloadLink) { progress, fileSize, downloadedSize ->
111+
updateNotification(progress, fileSize, downloadedSize)
110112
}
111113
}
112114
if (inject.isSuccess) {
@@ -121,20 +123,38 @@ class HUpdateWorker(
121123
}
122124
}
123125

124-
private fun createNotification(progress: Int = 0): Notification {
126+
private fun createNotification(progress: Int = 0, fileSizeMB: String = "0", downloadedSizeMB: String = "0"): Notification {
125127
return NotificationCompat.Builder(context, UPDATE_NOTIFICATION_CHANNEL)
126128
.setSmallIcon(R.mipmap.ic_launcher)
127129
.setOngoing(true)
128-
.setContentTitle(context.getString(R.string.downloading_update_percent, progress))
130+
.setContentTitle(
131+
context.getString(
132+
R.string.downloading_update_percent, progress
133+
) + " ($downloadedSizeMB MB / $fileSizeMB MB)"
134+
)
129135
.setPriority(NotificationCompat.PRIORITY_HIGH)
130136
.setOnlyAlertOnce(true)
131137
.setProgress(100, progress, false)
132138
.build()
133139
}
134140

141+
private var lastNotifyTime = 0L // 节流,通知更新太快Android会抛异常
135142
@SuppressLint("MissingPermission")
136-
private fun updateNotification(progress: Int) {
137-
notificationManager.notify(downloadId, createNotification(progress))
143+
private fun updateNotification(progress: Int, fileSize: Long, downloadedSize: Long) {
144+
val now = System.currentTimeMillis()
145+
if (progress == 100 || now - lastNotifyTime > 1000) {
146+
lastNotifyTime = now
147+
val fileSizeMB = String.format(Locale.US, "%.2f", fileSize.toDouble() / (1024 * 1024))
148+
val downloadedSizeMB = String.format(Locale.US, "%.2f", downloadedSize.toDouble() / (1024 * 1024))
149+
notificationManager.notify(
150+
downloadId,
151+
createNotification(
152+
progress,
153+
fileSizeMB,
154+
downloadedSizeMB
155+
)
156+
)
157+
}
138158
}
139159

140160
private fun createForegroundInfo(progress: Int = 0): ForegroundInfo {

0 commit comments

Comments
 (0)