Created
June 3, 2023 10:30
Revisions
-
omkar-tenkale created this gist
Jun 3, 2023 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ fun launch(block: suspend () -> Unit) { val callback = object : Continuation<Unit> { override val context: CoroutineContext = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>) {} } block.createCoroutineUnintercepted(callback).resumeWith(Result.success(Unit)) } ... downloadButton.setOnClickListener{ launch { textView.text = "Downloading file" download("example.com/file.txt", "/sdcard/file.txt") textView.text = "Download Complete" } } ... suspend fun download(url: String, filePath: String) { suspendCoroutineUninterceptedOrReturn<Unit> { cont -> //Download in a background thread thread { URL(url).readText().writeTo(File(filePath)) cont.resumeWith(Result.success(Unit)) } COROUTINE_SUSPENDED } }