Search code examples
androidkotlinandroid-workmanager

Success can only be called from within the same library group (referenced groupId=androidx.work from groupId=My Composable)


I have been trying to use work manager and downloading image using retrofit and facing this issue.

the problem is here in this this code

Result.Success( // This is creating problem
                    workDataOf(
                        WorkerConstants.IMAGE_URI to file.toUri().toString()
                    )
                )

Here is the full function code...

import android.content.Context
import androidx.core.app.NotificationCompat
import androidx.core.net.toUri
import androidx.work.CoroutineWorker
import androidx.work.ForegroundInfo
import androidx.work.WorkerParameters
import androidx.work.workDataOf
import com.gk.mycomposable.R
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import kotlin.random.Random

class DownloadWorker(
    private val context: Context,
    private val workerParam: WorkerParameters
) : CoroutineWorker(context, workerParam) {
    override suspend fun doWork(): Result {
        startForegroundService()
        delay(5000L)

        val response = FileAPI.instance.downloadImage()
        response.body()?.let { body ->
            return withContext(Dispatchers.IO) {
                val file = File(context.cacheDir, "image.jpg")
                val outputStream = FileOutputStream(file)
                outputStream.use { stream ->
                    try {
                        stream.write(body.bytes())
                    } catch (e: IOException) {
                        return@withContext Result.failure(
                            workDataOf(
                                WorkerConstants.ERROR_MSG to e.localizedMessage
                            )
                        )
                    }
                }
                Result.Success( // This is creating problem
                    workDataOf(
                        WorkerConstants.IMAGE_URI to file.toUri().toString()
                    )
                )
            }
        }
        if (!response.isSuccessful){
            if (response.code().toString().startsWith("5")){ 
                return Result.retry()
            }
            return Result.failure(
                workDataOf(
                    WorkerConstants.ERROR_MSG to "Network error"
                )
            )
        }
        return Result.failure(
            workDataOf(
                WorkerConstants.ERROR_MSG to "Unknown error"
            )
        )
    }

    private suspend fun startForegroundService() {
        setForeground(
            ForegroundInfo(
                Random.nextInt(),
                NotificationCompat.Builder(context, WorkerConstants.channelId)
                    .setSmallIcon(R.drawable.skull)
                    .setContentText("Downloading...")
                    .setContentTitle("Download in progress")
                    .build()
            )
        )
    }

Can anyone tell me why Success() is showing that warning?


Solution

  • To fix this issue, you can use the Result.success method instead, which is part of the androidx.work library and can be called from your code.