Merge pull request #2881 from kiwix/Issue#2845

Increase read, connect and auto retry attempts
This commit is contained in:
Kelson 2022-07-01 19:19:04 +02:00 committed by GitHub
commit e25df2b10e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 46 additions and 7 deletions

View File

@ -46,6 +46,7 @@ import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.observe
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.tonyodev.fetch2.Status
import eu.mhutti1.utils.storage.StorageDevice
import eu.mhutti1.utils.storage.StorageSelectDialog
import kotlinx.android.synthetic.main.fragment_destination_download.allowInternetPermissionButton
@ -106,10 +107,18 @@ class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions {
LibraryAdapter(
LibraryDelegate.BookDelegate(bookUtils, ::onBookItemClick),
LibraryDelegate.DownloadDelegate {
dialogShower.show(
KiwixDialog.YesNoDialog.StopDownload,
{ downloader.cancelDownload(it.downloadId) }
)
if (it.currentDownloadState == Status.FAILED) {
if (isNotConnected) {
noInternetSnackbar()
} else {
downloader.retryDownload(it.downloadId)
}
} else {
dialogShower.show(
KiwixDialog.YesNoDialog.StopDownload,
{ downloader.cancelDownload(it.downloadId) }
)
}
},
LibraryDelegate.DividerDelegate
)

View File

@ -19,6 +19,7 @@
package org.kiwix.kiwixmobile.zim_manager.library_view.adapter
import androidx.annotation.StringRes
import com.tonyodev.fetch2.Status
import org.kiwix.kiwixmobile.core.downloader.model.Base64String
import org.kiwix.kiwixmobile.core.downloader.model.DownloadModel
import org.kiwix.kiwixmobile.core.downloader.model.DownloadState
@ -69,7 +70,8 @@ sealed class LibraryListItem {
val progress: Int,
val eta: Seconds,
val downloadState: DownloadState,
override val id: Long
override val id: Long,
val currentDownloadState: Status
) : LibraryListItem() {
val readableEta: CharSequence = eta.takeIf { it.seconds > 0L }?.toHumanReadableTime() ?: ""
@ -84,7 +86,8 @@ sealed class LibraryListItem {
downloadModel.progress,
Seconds(downloadModel.etaInMilliSeconds / 1000L),
DownloadState.from(downloadModel.state, downloadModel.error),
downloadModel.book.id.hashCode().toLong()
downloadModel.book.id.hashCode().toLong(),
downloadModel.state
)
}
}

View File

@ -23,6 +23,7 @@ import android.view.View
import android.view.View.MeasureSpec
import android.widget.Toast
import androidx.annotation.StringRes
import com.tonyodev.fetch2.Status
import kotlinx.android.synthetic.main.item_download.downloadProgress
import kotlinx.android.synthetic.main.item_download.downloadState
import kotlinx.android.synthetic.main.item_download.eta
@ -99,6 +100,9 @@ sealed class LibraryViewHolder<in T : LibraryListItem>(containerView: View) :
downloadProgress.progress = item.progress
stop.setOnClickListener { clickAction.invoke(item) }
downloadState.text = item.downloadState.toReadableState(containerView.context)
if (item.currentDownloadState == Status.FAILED) {
clickAction.invoke(item)
}
eta.text = item.readableEta
}
}

View File

@ -34,7 +34,10 @@ import org.kiwix.kiwixmobile.core.downloader.Downloader
import org.kiwix.kiwixmobile.core.downloader.DownloaderImpl
import org.kiwix.kiwixmobile.core.downloader.fetch.FetchDownloadNotificationManager
import org.kiwix.kiwixmobile.core.downloader.fetch.FetchDownloadRequester
import org.kiwix.kiwixmobile.core.utils.CONNECT_TIME_OUT
import org.kiwix.kiwixmobile.core.utils.READ_TIME_OUT
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import java.util.concurrent.TimeUnit
import javax.inject.Singleton
@Module
@ -77,6 +80,8 @@ object DownloaderModule {
@Singleton
fun provideOkHttpDownloader() = OkHttpDownloader(
OkHttpClient.Builder()
.connectTimeout(CONNECT_TIME_OUT, TimeUnit.MINUTES)
.readTimeout(READ_TIME_OUT, TimeUnit.MINUTES)
.followRedirects(true)
.followSslRedirects(true)
.build()

View File

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

View File

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

View File

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

View File

@ -23,6 +23,7 @@ import com.tonyodev.fetch2.NetworkType.WIFI_ONLY
import com.tonyodev.fetch2.Request
import org.kiwix.kiwixmobile.core.downloader.DownloadRequester
import org.kiwix.kiwixmobile.core.downloader.model.DownloadRequest
import org.kiwix.kiwixmobile.core.utils.AUTO_RETRY_MAX_ATTEMPTS
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import javax.inject.Inject
@ -40,10 +41,14 @@ class FetchDownloadRequester @Inject constructor(
override fun cancel(downloadId: Long) {
fetch.delete(downloadId.toInt())
}
override fun retryDownload(downloadId: Long) {
fetch.retry(downloadId.toInt())
}
}
private fun DownloadRequest.toFetchRequest(sharedPreferenceUtil: SharedPreferenceUtil) =
Request("$uri", getDestination(sharedPreferenceUtil)).apply {
networkType = if (sharedPreferenceUtil.prefWifiOnly) WIFI_ONLY else ALL
autoRetryMaxAttempts = 10
autoRetryMaxAttempts = AUTO_RETRY_MAX_ATTEMPTS
}

View File

@ -44,3 +44,10 @@ const val INTERNAL_SELECT_POSITION = 0
const val EXTERNAL_SELECT_POSITION = 1
const val FILE_SELECT_CODE = 5
// For Read and Connect timeout on download OkHttpClient both are in minutes
const val READ_TIME_OUT = 1L
const val CONNECT_TIME_OUT = 1L
// For autoRetryMaxAttempts in download zim file
const val AUTO_RETRY_MAX_ATTEMPTS = 20