Skip to content

Commit c36a92d

Browse files
committed
Fix Compose API compatibility and improve download reliability
- Use lambda-based progress API for LinearProgressIndicator (BOM 2024.09.00) - Use mutableFloatStateOf/mutableIntStateOf to avoid autoboxing - Increase read timeout from 30s to 120s for large model files - Clean up temp files on download failure - Disconnect HttpURLConnection in finally block
1 parent 907a13a commit c36a92d

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

Yolo/android/app/src/main/java/com/example/executorchyolodemo/ModelDownloadScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ fun ModelDownloadScreen(
8484
if (isDownloading) {
8585
Spacer(modifier = Modifier.height(12.dp))
8686
LinearProgressIndicator(
87-
progress = progress,
87+
progress = { progress },
8888
modifier = Modifier.fillMaxWidth(),
8989
)
9090
Spacer(modifier = Modifier.height(8.dp))

Yolo/android/app/src/main/java/com/example/executorchyolodemo/ModelDownloadViewModel.kt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ package com.example.executorchyolodemo
1010

1111
import android.util.Log
1212
import androidx.compose.runtime.getValue
13+
import androidx.compose.runtime.mutableFloatStateOf
14+
import androidx.compose.runtime.mutableIntStateOf
1315
import androidx.compose.runtime.mutableStateOf
1416
import androidx.compose.runtime.setValue
1517
import androidx.lifecycle.ViewModel
@@ -63,13 +65,13 @@ class ModelDownloadViewModel : ViewModel() {
6365
var downloadStatus by mutableStateOf(DownloadStatus.NOT_STARTED)
6466
private set
6567

66-
var downloadProgress by mutableStateOf(0f)
68+
var downloadProgress by mutableFloatStateOf(0f)
6769
private set
6870

69-
var currentFileIndex by mutableStateOf(0)
71+
var currentFileIndex by mutableIntStateOf(0)
7072
private set
7173

72-
var totalFileCount by mutableStateOf(0)
74+
var totalFileCount by mutableIntStateOf(0)
7375
private set
7476

7577
var currentFileName by mutableStateOf("")
@@ -150,21 +152,22 @@ class ModelDownloadViewModel : ViewModel() {
150152
fileInfo: ModelFileInfo,
151153
targetFile: File
152154
): Boolean = withContext(Dispatchers.IO) {
155+
val tempFile = File(targetFile.absolutePath + ".tmp")
156+
var connection: HttpURLConnection? = null
153157
try {
154158
Log.i(TAG, "Downloading ${fileInfo.filename} from ${fileInfo.url}")
155159
val url = URL(fileInfo.url)
156-
val connection = url.openConnection() as HttpURLConnection
160+
connection = url.openConnection() as HttpURLConnection
157161
connection.requestMethod = "GET"
158162
connection.instanceFollowRedirects = true
159163
connection.connectTimeout = 30000
160-
connection.readTimeout = 30000
164+
connection.readTimeout = 120000
161165
connection.connect()
162166

163167
if (connection.responseCode != HttpURLConnection.HTTP_OK) {
164168
throw Exception("Server returned HTTP ${connection.responseCode}")
165169
}
166170

167-
val tempFile = File(targetFile.absolutePath + ".tmp")
168171
connection.inputStream.use { input ->
169172
FileOutputStream(tempFile).use { output ->
170173
val buffer = ByteArray(8192)
@@ -180,10 +183,13 @@ class ModelDownloadViewModel : ViewModel() {
180183
true
181184
} catch (e: Exception) {
182185
Log.e(TAG, "Failed to download ${fileInfo.filename}", e)
186+
tempFile.delete()
183187
withContext(Dispatchers.Main) {
184188
errorMessage = "Failed to download ${fileInfo.filename}: ${e.message}"
185189
}
186190
false
191+
} finally {
192+
connection?.disconnect()
187193
}
188194
}
189195
}

0 commit comments

Comments
 (0)