Skip to content

Commit acc64e4

Browse files
committed
Refactor ApkDownloader to use dependency injection and handle errors during APK download
1 parent 9356773 commit acc64e4

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

app/src/main/java/com/hamzaazman/birthdayreminder/common/ApkDownloader.kt renamed to app/src/main/java/com/hamzaazman/birthdayreminder/di/ApkDownloader.kt

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
package com.hamzaazman.birthdayreminder.common
1+
package com.hamzaazman.birthdayreminder.di
22

33
import android.content.Context
4+
import dagger.hilt.android.qualifiers.ApplicationContext
45
import kotlinx.coroutines.CoroutineScope
56
import kotlinx.coroutines.Dispatchers
67
import kotlinx.coroutines.launch
@@ -10,15 +11,19 @@ import java.io.File
1011
import java.io.FileOutputStream
1112
import java.net.HttpURLConnection
1213
import java.net.URL
14+
import javax.inject.Inject
15+
import javax.inject.Singleton
1316

14-
15-
object ApkDownloader {
17+
@Singleton
18+
class ApkDownloader @Inject constructor(
19+
@ApplicationContext private val context: Context
20+
) {
1621

1722
fun downloadApk(
18-
context: Context,
1923
url: String,
2024
onProgress: (Int) -> Unit,
21-
onDownloaded: (File) -> Unit
25+
onDownloaded: (File) -> Unit,
26+
onError: (Exception) -> Unit
2227
) {
2328
CoroutineScope(Dispatchers.IO).launch {
2429
try {
@@ -53,7 +58,10 @@ object ApkDownloader {
5358

5459
} catch (e: Exception) {
5560
e.printStackTrace()
61+
withContext(Dispatchers.Main) {
62+
onError(e)
63+
}
5664
}
5765
}
5866
}
59-
}
67+
}

0 commit comments

Comments
 (0)