Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit df8251e7 authored by Hasib Prince's avatar Hasib Prince
Browse files

Refactoring classes related to update

parent ae90f7a1
Loading
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -39,7 +39,6 @@ import foundation.e.apps.api.gplay.utils.GPlayHttpClient
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.supervisorScope
import kotlinx.coroutines.withContext
import timber.log.Timber
import javax.inject.Inject

class GPlayAPIImpl @Inject constructor(private val gPlayHttpClient: GPlayHttpClient) {
+0 −1
Original line number Diff line number Diff line
@@ -33,7 +33,6 @@ import androidx.work.WorkerParameters
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import foundation.e.apps.R
import foundation.e.apps.api.fused.UpdatesDao
import foundation.e.apps.manager.database.DatabaseRepository
import foundation.e.apps.manager.database.fusedDownload.FusedDownload
import foundation.e.apps.manager.fused.FusedManagerRepository
+36 −25
Original line number Diff line number Diff line
@@ -176,6 +176,12 @@ class UpdatesFragment : TimeoutFragment(R.layout.fragment_updates), FusedAPIInte
        viewLifecycleOwner.lifecycleScope.launch {
            EventBus.events.flowWithLifecycle(viewLifecycleOwner.lifecycle)
                .filter { appEvent -> appEvent is AppEvent.UpdateEvent }.collectLatest {
                    handleUpdateEvent(it)
                }
        }
    }

    private fun handleUpdateEvent(it: AppEvent) {
        val event = it.data as ResultSupreme.WorkError<*>
        when (event.data) {
            ResultStatus.USER_NOT_AVAILABLE -> {
@@ -185,6 +191,17 @@ class UpdatesFragment : TimeoutFragment(R.layout.fragment_updates), FusedAPIInte
                requireContext().toast(getString(R.string.message_retry))
            }
            else -> {
                handleUnknownErrorEvent(event)
            }
        }
    }

    private fun handleUnknownErrorEvent(event: ResultSupreme.WorkError<*>) {
        if (event.otherPayload == null) {
            requireContext().toast(getString(R.string.message_update_failed))
            return
        }

        if (event.otherPayload is FusedDownload) {
            requireContext().toast(
                getString(
@@ -192,12 +209,6 @@ class UpdatesFragment : TimeoutFragment(R.layout.fragment_updates), FusedAPIInte
                    (event.otherPayload as FusedDownload).name
                )
            )
                            } else {
                                requireContext().toast(getString(R.string.message_update_failed))
                            }
                        }
                    }
                }
        }
    }

+0 −2
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@ import com.aurora.gplayapi.data.models.AuthData
import foundation.e.apps.api.fused.UpdatesDao
import foundation.e.apps.api.fused.data.FusedApp
import foundation.e.apps.utils.enums.ResultStatus
import timber.log.Timber
import javax.inject.Inject

class UpdatesManagerRepository @Inject constructor(
@@ -30,7 +29,6 @@ class UpdatesManagerRepository @Inject constructor(
) {

    suspend fun getUpdates(authData: AuthData): Pair<List<FusedApp>, ResultStatus> {
        Timber.d("===> getUpdates: ${UpdatesDao.hasAnyAppsForUpdate()}")
        if (UpdatesDao.hasAnyAppsForUpdate()) {
            return Pair(UpdatesDao.appsAwaitingForUpdate, ResultStatus.OK)
        }
+13 −9
Original line number Diff line number Diff line
@@ -47,9 +47,10 @@ class UpdatesWorker @AssistedInject constructor(
    private val dataStoreModule: DataStoreModule,
    private val gson: Gson,
) : CoroutineWorker(context, params) {

    companion object {
        const val IS_AUTO_UPDATE = "IS_AUTO_UPDATE"
        private const val MAX_RETRY_COUNT = 10
        private const val DELAY_FOR_RETRY = 3000L
    }

    val TAG = UpdatesWorker::class.simpleName
@@ -84,22 +85,24 @@ class UpdatesWorker @AssistedInject constructor(
        val user = getUser()
        val authData = getAuthData()
        var resultStatus = ResultStatus.OK

        fun fetchUpdate(updateData: Pair<List<FusedApp>, ResultStatus>) {
            appsNeededToUpdate.addAll(updateData.first)
            resultStatus = updateData.second
        }

        if (user in listOf(User.ANONYMOUS, User.GOOGLE) && authData != null) {
            /*
             * Signifies valid Google user and valid auth data to update
             * apps from Google Play store.
             * The user check will be more useful in No Google mode.
             */
            val updateData = updatesManagerRepository.getUpdates(authData)
            appsNeededToUpdate.addAll(updateData.first)
            resultStatus = updateData.second
            fetchUpdate(updatesManagerRepository.getUpdates(authData))
        } else if (user != User.UNAVAILABLE) {
            /*
             * If authData is null, update apps from cleanapk only.
             */
            val updateData = updatesManagerRepository.getUpdatesOSS()
            appsNeededToUpdate.addAll(updateData.first)
            resultStatus = updateData.second
            fetchUpdate(updatesManagerRepository.getUpdatesOSS())
        } else {
            /*
             * If user in UNAVAILABLE, don't do anything.
@@ -138,8 +141,9 @@ class UpdatesWorker @AssistedInject constructor(
        if (retryCount == 1) {
            EventBus.invokeEvent(AppEvent.UpdateEvent(ResultSupreme.WorkError(ResultStatus.RETRY)))
        }
        if (retryCount <= 10) {
            delay(3000)

        if (retryCount <= MAX_RETRY_COUNT) {
            delay(DELAY_FOR_RETRY)
            checkForUpdates()
        } else {
            EventBus.invokeEvent(AppEvent.UpdateEvent(ResultSupreme.WorkError(ResultStatus.UNKNOWN)))