Retry strategy if download failed due to network fluctuation

This commit is contained in:
MohitMaliFtechiz 2022-06-28 12:32:49 +05:30 committed by Kelson
parent 0472b2c5ff
commit 90cb76839c
6 changed files with 25 additions and 4 deletions

View File

@ -106,10 +106,18 @@ class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions {
LibraryAdapter( LibraryAdapter(
LibraryDelegate.BookDelegate(bookUtils, ::onBookItemClick), LibraryDelegate.BookDelegate(bookUtils, ::onBookItemClick),
LibraryDelegate.DownloadDelegate { LibraryDelegate.DownloadDelegate {
dialogShower.show( if (it.downloadState.toReadableState(requireActivity()).contains("Failed")) {
KiwixDialog.YesNoDialog.StopDownload, if (isNotConnected) {
{ downloader.cancelDownload(it.downloadId) } noInternetSnackbar()
) } else {
downloader.retryDownload(it.downloadId)
}
} else {
dialogShower.show(
KiwixDialog.YesNoDialog.StopDownload,
{ downloader.cancelDownload(it.downloadId) }
)
}
}, },
LibraryDelegate.DividerDelegate LibraryDelegate.DividerDelegate
) )

View File

@ -99,6 +99,9 @@ sealed class LibraryViewHolder<in T : LibraryListItem>(containerView: View) :
downloadProgress.progress = item.progress downloadProgress.progress = item.progress
stop.setOnClickListener { clickAction.invoke(item) } stop.setOnClickListener { clickAction.invoke(item) }
downloadState.text = item.downloadState.toReadableState(containerView.context) downloadState.text = item.downloadState.toReadableState(containerView.context)
if (item.downloadState.toReadableState(containerView.context).contains("Failed")) {
clickAction.invoke(item)
}
eta.text = item.readableEta eta.text = item.readableEta
} }
} }

View File

@ -22,4 +22,5 @@ import org.kiwix.kiwixmobile.core.downloader.model.DownloadRequest
interface DownloadRequester { interface DownloadRequester {
fun enqueue(downloadRequest: DownloadRequest): Long fun enqueue(downloadRequest: DownloadRequest): Long
fun cancel(downloadId: Long) fun cancel(downloadId: Long)
fun retryDownload(downloadId: Long)
} }

View File

@ -22,4 +22,5 @@ import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity
interface Downloader { interface Downloader {
fun download(book: LibraryNetworkEntity.Book) fun download(book: LibraryNetworkEntity.Book)
fun cancelDownload(downloadId: Long) fun cancelDownload(downloadId: Long)
fun retryDownload(downloadId: Long)
} }

View File

@ -49,4 +49,8 @@ class DownloaderImpl @Inject constructor(
override fun cancelDownload(downloadId: Long) { override fun cancelDownload(downloadId: Long) {
downloadRequester.cancel(downloadId) downloadRequester.cancel(downloadId)
} }
override fun retryDownload(downloadId: Long) {
downloadRequester.retryDownload(downloadId)
}
} }

View File

@ -41,6 +41,10 @@ class FetchDownloadRequester @Inject constructor(
override fun cancel(downloadId: Long) { override fun cancel(downloadId: Long) {
fetch.delete(downloadId.toInt()) fetch.delete(downloadId.toInt())
} }
override fun retryDownload(downloadId: Long) {
fetch.retry(downloadId.toInt())
}
} }
private fun DownloadRequest.toFetchRequest(sharedPreferenceUtil: SharedPreferenceUtil) = private fun DownloadRequest.toFetchRequest(sharedPreferenceUtil: SharedPreferenceUtil) =