-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- In this commit, we added the option to download exam resources such as images and fonts. - Once all questions are downloaded, we extract the resource URLs from the response, download the resources, and store them locally. After allresources are downloaded, we replace the network URLs with local URLs in the response and save it in the database. - This will allow us to display images without an internet connection.
- Loading branch information
1 parent
19f754c
commit 42a8e12
Showing
9 changed files
with
214 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
package `in`.testpress.util.extension | ||
|
||
fun String?.isNotNullAndNotEmpty() = this != null && this.isNotEmpty() | ||
fun String?.isNotNullAndNotEmpty() = this != null && this.isNotEmpty() | ||
|
||
fun List<String>.validateHttpAndHttpsUrls(): List<String> { | ||
return this.filter { it.startsWith("http://") || it.startsWith("https://") } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
course/src/main/java/in/testpress/course/util/ResourceDownloader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package `in`.testpress.course.util | ||
|
||
import android.content.Context | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.sync.Semaphore | ||
import okhttp3.OkHttpClient | ||
import java.io.File | ||
import java.io.FileOutputStream | ||
import okhttp3.Request | ||
import java.util.concurrent.ConcurrentHashMap | ||
|
||
|
||
class ResourceDownloader(val context: Context) { | ||
private val client = OkHttpClient() | ||
private val semaphore = Semaphore(10) | ||
|
||
suspend fun downloadResources( | ||
urls: List<String>, | ||
onComplete: suspend (HashMap<String, String>) -> Unit | ||
) { | ||
val urlToLocalPathMap = ConcurrentHashMap<String, String>() | ||
coroutineScope { | ||
val deferredDownloads = urls.map { url -> | ||
async { | ||
semaphore.withPermit { | ||
downloadResource(url)?.let { localPath -> | ||
urlToLocalPathMap[url] = localPath | ||
} | ||
} | ||
} | ||
} | ||
deferredDownloads.awaitAll() | ||
onComplete(HashMap(urlToLocalPathMap)) | ||
} | ||
} | ||
|
||
private fun downloadResource(url: String): String? { | ||
return try { | ||
val request = Request.Builder().url(url).build() | ||
val response = client.newCall(request).execute() | ||
|
||
if (response.isSuccessful) { | ||
response.body?.let { body -> | ||
val fileName = url.substringAfterLast('/') | ||
val file = File(context.filesDir, fileName) | ||
val fos = FileOutputStream(file) | ||
fos.use { | ||
it.write(body.bytes()) | ||
} | ||
body.close() | ||
return "file://${file.absolutePath}" | ||
} | ||
} | ||
response.close() | ||
null | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
null | ||
} | ||
} | ||
|
||
private suspend fun <T> Semaphore.withPermit(block: suspend () -> T): T { | ||
acquire() | ||
try { | ||
return block() | ||
} finally { | ||
release() | ||
} | ||
} | ||
} | ||
|
||
|