Skip to content

Commit

Permalink
new download fragment now monitors for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
LivingWithHippos committed Oct 8, 2020
1 parent da657e1 commit ad25da8
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
package com.github.livingwithhippos.unchained.data.repositoy

import arrow.core.Either
import com.github.livingwithhippos.unchained.data.model.CompleteNetworkResponse
import com.github.livingwithhippos.unchained.data.model.DownloadItem
import com.github.livingwithhippos.unchained.data.model.UnchainedNetworkException
import com.github.livingwithhippos.unchained.data.remote.UnrestrictApiHelper
import kotlinx.coroutines.delay
import javax.inject.Inject

class UnrestrictRepository @Inject constructor(private val unrestrictApiHelper: UnrestrictApiHelper) :
BaseRepository() {

suspend fun getEitherUnrestrictedLink(
token: String,
link: String,
password: String? = null,
remote: Int? = null
): Either<UnchainedNetworkException, DownloadItem> {

val linkResponse = eitherApiResult(
call = {
unrestrictApiHelper.getUnrestrictedLink(
token = "Bearer $token",
link = link,
password = password,
remote = remote
)
},
errorMessage = "Error Fetching Unrestricted Link Info"
)

return linkResponse
}

suspend fun getUnrestrictedLink(
token: String,
link: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ import com.github.livingwithhippos.unchained.R
import com.github.livingwithhippos.unchained.base.UnchainedFragment
import com.github.livingwithhippos.unchained.data.model.APIError
import com.github.livingwithhippos.unchained.data.model.AuthenticationState
import com.github.livingwithhippos.unchained.data.model.EmptyBodyError
import com.github.livingwithhippos.unchained.data.model.NetworkError
import com.github.livingwithhippos.unchained.databinding.NewDownloadFragmentBinding
import com.github.livingwithhippos.unchained.lists.view.ListsTabFragment
import com.github.livingwithhippos.unchained.newdownload.viewmodel.NewDownloadViewModel
import com.github.livingwithhippos.unchained.utilities.REMOTE_TRAFFIC_ON
import com.github.livingwithhippos.unchained.utilities.SCHEME_HTTP
import com.github.livingwithhippos.unchained.utilities.SCHEME_HTTPS
import com.github.livingwithhippos.unchained.utilities.SCHEME_MAGNET
import com.github.livingwithhippos.unchained.utilities.extension.getApiErrorMessage
import com.github.livingwithhippos.unchained.utilities.extension.getClipboardText
import com.github.livingwithhippos.unchained.utilities.extension.isMagnet
import com.github.livingwithhippos.unchained.utilities.extension.isWebUrl
Expand Down Expand Up @@ -195,59 +198,54 @@ class NewDownloadFragment : UnchainedFragment(), NewDownloadListener {
}
})

return downloadBinding.root
}
viewModel.networkExceptionLiveData.observe(viewLifecycleOwner, { e ->
val exception = e.getContentIfNotHandled()

private fun showErrorMessage(error: APIError) {
when (error.errorCode) {
-1 -> context?.showToast(R.string.internal_error)
1 -> context?.showToast(R.string.missing_parameter)
2 -> context?.showToast(R.string.bad_parameter_value)
3 -> context?.showToast(R.string.unknown_method)
4 -> context?.showToast(R.string.method_not_allowed)
// what is this error for?
5 -> context?.showToast(R.string.slow_down)
6 -> context?.showToast(R.string.resource_unreachable)
7 -> context?.showToast(R.string.resource_not_found)
//todo: check these
8 -> {
context?.showToast(R.string.bad_token)
activityViewModel.setUnauthenticated()
}
9 -> context?.showToast(R.string.permission_denied)
10 -> context?.showToast(R.string.tfa_needed)
11 -> context?.showToast(R.string.tfa_pending)
12 -> {
context?.showToast(R.string.invalid_login)
activityViewModel.setUnauthenticated()
}
13 -> context?.showToast(R.string.invalid_password)
14 -> {
context?.showToast(R.string.account_locked)
activityViewModel.setUnauthenticated()
// re-enable the buttons to allow the user to take new actions
downloadBinding.bUnrestrict.isEnabled = true
downloadBinding.bLoadTorrent.isEnabled = true

when (exception) {
is APIError -> {
// error codes outside the known range will return unknown error
val errorCode = exception.errorCode ?: -2
// manage the api error result
when (exception.errorCode) {
-1,1 -> context?.let {
it.showToast(it.getApiErrorMessage(errorCode))
}
// since here we monitor new downloads, use a less generic, custom message
2 -> context?.showToast(R.string.unsupported_hoster)
in 3..7 -> context?.let {
it.showToast(it.getApiErrorMessage(errorCode))
}
in 8..15 -> {
context?.let {
it.showToast(it.getApiErrorMessage(errorCode))
}
activityViewModel.setUnauthenticated()
}
else -> {
context?.let {
it.showToast(it.getApiErrorMessage(errorCode))
}
}
}
}
is EmptyBodyError -> {
// call successful, fit to singular api case
}
is NetworkError -> {
// todo: alert the user according to the different network error
context?.showToast(R.string.network_error)
}
// already handled
null -> {
}
}
15 -> context?.showToast(R.string.account_not_activated)
16 -> context?.showToast(R.string.unsupported_hoster)
17 -> context?.showToast(R.string.hoster_in_maintenance)
18 -> context?.showToast(R.string.hoster_limit_reached)
19 -> context?.showToast(R.string.hoster_temporarily_unavailable)
20 -> context?.showToast(R.string.hoster_not_available_for_free_users)
21 -> context?.showToast(R.string.too_many_active_downloads)
22 -> context?.showToast(R.string.ip_Address_not_allowed)
23 -> context?.showToast(R.string.traffic_exhausted)
24 -> context?.showToast(R.string.file_unavailable)
25 -> context?.showToast(R.string.service_unavailable)
26 -> context?.showToast(R.string.upload_too_big)
27 -> context?.showToast(R.string.upload_error)
28 -> context?.showToast(R.string.file_not_allowed)
29 -> context?.showToast(R.string.torrent_too_big)
30 -> context?.showToast(R.string.torrent_file_invalid)
31 -> context?.showToast(R.string.action_already_done)
32 -> context?.showToast(R.string.image_resolution_error)
33 -> context?.showToast(R.string.torrent_already_active)
else -> context?.showToast(R.string.error_unrestricting_download)
}
})

return downloadBinding.root
}

private val getTorrent: ActivityResultLauncher<String> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import androidx.hilt.lifecycle.ViewModelInject
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import arrow.core.Either
import com.github.livingwithhippos.unchained.BuildConfig
import com.github.livingwithhippos.unchained.data.model.APIError
import com.github.livingwithhippos.unchained.data.model.DownloadItem
import com.github.livingwithhippos.unchained.data.model.UnchainedNetworkException
import com.github.livingwithhippos.unchained.data.model.UploadedTorrent
import com.github.livingwithhippos.unchained.data.repositoy.CredentialsRepository
import com.github.livingwithhippos.unchained.data.repositoy.TorrentsRepository
Expand All @@ -33,13 +35,17 @@ class NewDownloadViewModel @ViewModelInject constructor(
*/
val linkLiveData = MutableLiveData<Event<DownloadItem?>>()
val torrentLiveData = MutableLiveData<Event<UploadedTorrent?>>()
val networkExceptionLiveData = MutableLiveData<Event<UnchainedNetworkException>>()

fun fetchUnrestrictedLink(link: String, password: String?, remote: Int? = null) {
viewModelScope.launch {
val token = getToken()
val unrestrictedData =
unrestrictRepository.getUnrestrictedLink(token, link, password, remote)
linkLiveData.postValue(Event(unrestrictedData))
val response =
unrestrictRepository.getEitherUnrestrictedLink(token, link, password, remote)
when(response) {
is Either.Left -> networkExceptionLiveData.postValue(Event(response.a))
is Either.Right -> linkLiveData.postValue(Event(response.b))
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,45 @@ fun AppCompatActivity.setCustomTheme(theme: String) {
"original" -> setTheme(R.style.Theme_Unchained)
"tropical_sunset" -> setTheme(R.style.Theme_Unchained_TropicalSunset)
}
}

fun Context.getApiErrorMessage(errorCode: Int): String {
return when (errorCode) {
-1 -> getString(R.string.internal_error)
1 -> getString(R.string.missing_parameter)
2 -> getString(R.string.bad_parameter_value)
3 -> getString(R.string.unknown_method)
4 -> getString(R.string.method_not_allowed)
// note: what is this error for?
5 -> getString(R.string.slow_down)
6 -> getString(R.string.resource_unreachable)
7 -> getString(R.string.resource_not_found)
8 -> getString(R.string.bad_token)
9 -> getString(R.string.permission_denied)
10 -> getString(R.string.tfa_needed)
11 -> getString(R.string.tfa_pending)
12 -> getString(R.string.invalid_login)
13 -> getString(R.string.invalid_password)
14 -> getString(R.string.account_locked)
15 -> getString(R.string.account_not_activated)
16 -> getString(R.string.unsupported_hoster)
17 -> getString(R.string.hoster_in_maintenance)
18 -> getString(R.string.hoster_limit_reached)
19 -> getString(R.string.hoster_temporarily_unavailable)
20 -> getString(R.string.hoster_not_available_for_free_users)
21 -> getString(R.string.too_many_active_downloads)
22 -> getString(R.string.ip_Address_not_allowed)
23 -> getString(R.string.traffic_exhausted)
24 -> getString(R.string.file_unavailable)
25 -> getString(R.string.service_unavailable)
26 -> getString(R.string.upload_too_big)
27 -> getString(R.string.upload_error)
28 -> getString(R.string.file_not_allowed)
29 -> getString(R.string.torrent_too_big)
30 -> getString(R.string.torrent_file_invalid)
31 -> getString(R.string.action_already_done)
32 -> getString(R.string.image_resolution_error)
33 -> getString(R.string.torrent_already_active)
else -> getString(R.string.unknown_error)
}
}
2 changes: 2 additions & 0 deletions app/app/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,6 @@
<string name="title_debugging">Debug</string>
<string name="update_regexps">Aggiorna riconoscimento link hosts</string>
<string name="updating_link_matcher">Aggiornamento dei link…</string>
<string name="unknown_error">Errore sconosciuto</string>
<string name="network_error">Errore di rete</string>
</resources>
2 changes: 2 additions & 0 deletions app/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -405,4 +405,6 @@
<string name="title_debugging">Debug</string>
<string name="update_regexps">Update hosts link matcher</string>
<string name="updating_link_matcher">Updating link matcher…</string>
<string name="unknown_error">Unknown error</string>
<string name="network_error">Network error</string>
</resources>

0 comments on commit ad25da8

Please sign in to comment.