Fetch: Any plan to support observables instead of callbacks?

Created on 28 May 2020  路  1Comment  路  Source: tonyofrancis/Fetch

Hi @tonyofrancis, thanks for the amazing library really appreciate the work that you've put in! I was just wondering that since the androidx variant of the project uses kotlin it would be nice if you ship minor artifacts with either lifecycle livedata or coroutines flow to replace/complement the follwoing:

  • com.tonyodev.fetch2core.Func<R>
  • com.tonyodev.fetch2core.DownloadBlock.AbstractFetchListener

The idea was to take something like: fun addListener(listener: FetchListener): Fetch and turn it into something like fun addListener(): Flow<DownloadItem>, where I DownloadItem could be a sealed class that other download classes extend from for each event class type. I don't know what you've got planned for the future, so let me know what you think.

Right now I am currently doing the following:

internal sealed class Downloadable(
    val download: Download
)

internal class DownloadCompleted(
    download: Download
) : Downloadable(download)

internal class DownloadAdded(
    download: Download
) : Downloadable(download)

internal class DownloadResumed(
    download: Download
) : Downloadable(download)

internal class DownloadPaused(
    download: Download
) : Downloadable(download)

internal class DownloadCanceled(
    download: Download
) : Downloadable(download)

internal class DownloadRemoved(
    download: Download
) : Downloadable(download)

internal class DownloadDeleted(
    download: Download
) : Downloadable(download)

internal class DownloadQueued(
    download: Download,
    val waitingOnNetwork: Boolean
) : Downloadable(download)

internal class DownloadError(
    download: Download,
    val error: Error,
    val throwable: Throwable?
) : Downloadable(download)

internal class DownloadProgress(
    download: Download,
    val etaInMilliSeconds: Long,
    val downloadedBytesPerSecond: Long
) : Downloadable(download)
internal class DownloadSourceImpl(
    private val fetch: Fetch,
    supportDispatchers: SupportDispatchers
) : DownloadSource(supportDispatchers) {

    @ExperimentalCoroutinesApi
    override val downloadableListenerFlow =
        callbackFlow<Downloadable> {
            val fetchListener = object : AbstractFetchListener() {
                override fun onAdded(download: Download) {
                    super.onAdded(download)
                    offer(DownloadAdded(download))
                }

                override fun onCancelled(download: Download) {
                    super.onCancelled(download)
                    offer(DownloadCanceled(download))
                }

                override fun onCompleted(download: Download) {
                    super.onCompleted(download)
                    offer(DownloadCompleted(download))
                }

                override fun onDeleted(download: Download) {
                    super.onDeleted(download)
                    offer(DownloadDeleted(download))
                }

                override fun onError(download: Download, error: Error, throwable: Throwable?) {
                    super.onError(download, error, throwable)
                    offer(DownloadError(download, error, throwable))
                }

                override fun onPaused(download: Download) {
                    super.onPaused(download)
                    offer(DownloadPaused(download))
                }

                override fun onProgress(download: Download, etaInMilliSeconds: Long, downloadedBytesPerSecond: Long) {
                    super.onProgress(download, etaInMilliSeconds, downloadedBytesPerSecond)
                    offer(DownloadProgress(download, etaInMilliSeconds, downloadedBytesPerSecond))
                }

                override fun onQueued(download: Download, waitingOnNetwork: Boolean) {
                    super.onQueued(download, waitingOnNetwork)
                    offer(DownloadQueued(download, waitingOnNetwork))
                }

                override fun onRemoved(download: Download) {
                    super.onRemoved(download)
                    offer(DownloadRemoved(download))
                }

                override fun onResumed(download: Download) {
                    super.onResumed(download)
                    offer(DownloadResumed(download))
                }
            }
            fetch.addListener(fetchListener)
            awaitClose {
                fetch.removeListener(fetchListener)
            }
        }
}
enhancement

Most helpful comment

@wax911 I plan on bringing live data and flow to fetch in version 4. Please stay tune!

>All comments

@wax911 I plan on bringing live data and flow to fetch in version 4. Please stay tune!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rs-georg picture rs-georg  路  4Comments

DeevD picture DeevD  路  3Comments

jpvs0101 picture jpvs0101  路  5Comments

Moeinh77 picture Moeinh77  路  4Comments

maulik9898 picture maulik9898  路  4Comments