tonyofrancis / Fetch

The best file downloader library for Android
https://www.meta.stackoverflow.com/tags/fetch2
Apache License 2.0
1.67k stars 344 forks source link

Cancel if download not started in particular time. #693

Open PratikLahagora opened 2 months ago

PratikLahagora commented 2 months ago

I want to implement a feature where, if a download doesn’t start within a specified time, it should be canceled, and an error should be thrown. After reviewing various suggestions, I found that it can be achieved using a custom HttpDownloader. I've implemented the code as shown below, but it still doesn’t trigger any listener or status change.

private fun initDownloader(context: Context) {
        val fetchConfiguration: FetchConfiguration =
            FetchConfiguration.Builder(context).setDownloadConcurrentLimit(1)
                .setHttpDownloader(getOkHttpDownloader())
                .build()
        fetchDownloader = Fetch.getInstance(fetchConfiguration)
        fetchDownloader?.addListener(fetchDownloaderListener)
    }

    private fun getOkHttpDownloader(): OkHttpDownloader {
        val okHttpClient: OkHttpClient = OkHttpClient.Builder()
            .readTimeout(20, TimeUnit.SECONDS)
            .connectTimeout(20, TimeUnit.SECONDS)
            .writeTimeout(20, TimeUnit.SECONDS)
            .build()
        return OkHttpDownloader(
            okHttpClient,
            Downloader.FileDownloaderType.SEQUENTIAL
        )
    }