Skip to content

Instantly share code, notes, and snippets.

@raystatic
Created March 2, 2022 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raystatic/00e1a1657067551661cd32b2c0c42170 to your computer and use it in GitHub Desktop.
Save raystatic/00e1a1657067551661cd32b2c0c42170 to your computer and use it in GitHub Desktop.
@Composable
fun Home() {
Column(
modifier = Modifier
.fillMaxSize()
.padding(32.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
val data = remember {
mutableStateOf(
File(
id = "10",
name = "Pdf File 10 MB",
type = "PDF",
url = "https://www.learningcontainer.com/wp-content/uploads/2019/09/sample-pdf-download-10-mb.pdf",
downloadedUri = null
)
)
}
ItemFile(
file = data.value,
startDownload = {
startDownloadingFile(
file = data.value,
success = {
data.value = data.value.copy().apply {
isDownloading = false
downloadedUri = it
}
},
failed = {
data.value = data.value.copy().apply {
isDownloading = false
downloadedUri = null
}
},
running = {
data.value = data.value.copy().apply {
isDownloading = true
}
}
)
},
openFile = {
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(it.downloadedUri?.toUri(),"application/pdf")
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
try {
startActivity(intent)
}catch (e:ActivityNotFoundException){
Toast.makeText(
this@MainActivity,
"Can't open Pdf",
Toast.LENGTH_SHORT
).show()
}
}
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment