Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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