Last active
November 1, 2022 10:47
-
-
Save raystatic/fe44762858e302f3d6ee8e3bef4f15d8 to your computer and use it in GitHub Desktop.
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
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