Skip to content

Instantly share code, notes, and snippets.

@raystatic
Last active November 1, 2022 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save raystatic/fe44762858e302f3d6ee8e3bef4f15d8 to your computer and use it in GitHub Desktop.
Save raystatic/fe44762858e302f3d6ee8e3bef4f15d8 to your computer and use it in GitHub Desktop.
override suspend fun doWork(): Result {
val fileUrl = inputData.getString(FileParams.KEY_FILE_URL) ?: ""
val fileName = inputData.getString(FileParams.KEY_FILE_NAME) ?: ""
val fileType = inputData.getString(FileParams.KEY_FILE_TYPE) ?: ""
Log.d("TAG", "doWork: $fileUrl | $fileName | $fileType")
if (fileName.isEmpty()
|| fileType.isEmpty()
|| fileUrl.isEmpty()
){
Result.failure()
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
val name = NotificationConstants.CHANNEL_NAME
val description = NotificationConstants.CHANNEL_DESCRIPTION
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(NotificationConstants.CHANNEL_ID,name,importance)
channel.description = description
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?
notificationManager?.createNotificationChannel(channel)
}
val builder = NotificationCompat.Builder(context,NotificationConstants.CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Downloading your file...")
.setOngoing(true)
.setProgress(0,0,true)
NotificationManagerCompat.from(context).notify(NotificationConstants.NOTIFICATION_ID,builder.build())
val uri = getSavedFileUri(
fileName = fileName,
fileType = fileType,
fileUrl = fileUrl,
context = context
)
NotificationManagerCompat.from(context).cancel(NotificationConstants.NOTIFICATION_ID)
return if (uri != null){
Result.success(workDataOf(FileParams.KEY_FILE_URI to uri.toString()))
}else{
Result.failure()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment