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

Commit 2c7d3db1 authored by Hasib Prince's avatar Hasib Prince
Browse files

Merge branch 'background_install_#4986' into epic_176-all-refactorAndGplay

parents 9e9e990e 12bdb894
Loading
Loading
Loading
Loading
+33 −8
Original line number Diff line number Diff line
@@ -24,15 +24,20 @@ import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavOptions
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.aurora.gplayapi.exceptions.ApiException
import com.google.android.material.snackbar.Snackbar
import dagger.hilt.android.AndroidEntryPoint
import foundation.e.apps.databinding.ActivityMainBinding
import foundation.e.apps.manager.workmanager.InstallWorkManager
import foundation.e.apps.updates.UpdatesNotifier
import foundation.e.apps.utils.enums.Status
import foundation.e.apps.utils.enums.User
import foundation.e.apps.utils.modules.CommonUtilsModule
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@@ -142,17 +147,33 @@ class MainActivity : AppCompatActivity() {

        // Observe and handle downloads
        viewModel.downloadList.observe(this) { list ->
            val shouldDownload = list.any {
                it.status == Status.INSTALLING || it.status == Status.DOWNLOADING || it.status == Status.INSTALLED
//            val shouldDownload = list.any {
//                it.status == Status.INSTALLING || it.status == Status.DOWNLOADING || it.status == Status.INSTALLED
//            }
//            if (!shouldDownload && list.isNotEmpty()) {
//                for (item in list) {
//                    if (item.status == Status.QUEUED) {
//                        viewModel.downloadApp(item)
//                        break
//                    }
//                }
//            }
            list.forEach {
                if (it.status == Status.QUEUED) {
                    lifecycleScope.launch {
                        viewModel.updateAwaiting(it)
                        InstallWorkManager.enqueueWork(applicationContext, it)
                        Log.d(TAG, "===> onCreate: AWAITING ${it.name}")
                    }
            if (!shouldDownload && list.isNotEmpty()) {
                for (item in list) {
                    if (item.status == Status.QUEUED) {
                        viewModel.downloadApp(item)
                        break
                }
            }
        }

        viewModel.errorMessage.observe(this) {
            when (it) {
                is ApiException.AppNotPurchased -> showSnackbarMessage(getString(R.string.message_app_available_later))
                else -> showSnackbarMessage(it.localizedMessage ?: getString(R.string.unknown_error))
            }
        }

        if (!CommonUtilsModule.isNetworkAvailable(this)) {
@@ -160,6 +181,10 @@ class MainActivity : AppCompatActivity() {
        }
    }

    private fun showSnackbarMessage(message: String) {
        Snackbar.make(binding.root, message, Snackbar.LENGTH_SHORT).show()
    }

    private fun showNoInternet() {
        binding.noInternet.visibility = View.VISIBLE
        binding.fragment.visibility = View.GONE
+30 −17
Original line number Diff line number Diff line
@@ -51,7 +51,7 @@ class MainActivityViewModel @Inject constructor(
    private val gson: Gson,
    private val dataStoreModule: DataStoreModule,
    private val fusedAPIRepository: FusedAPIRepository,
    private val fusedManagerRepository: FusedManagerRepository
    private val fusedManagerRepository: FusedManagerRepository,
) : ViewModel() {

    val authDataJson: LiveData<String> = dataStoreModule.authData.asLiveData()
@@ -66,7 +66,8 @@ class MainActivityViewModel @Inject constructor(
    // Downloads
    val downloadList = fusedManagerRepository.getDownloadLiveList()
    var installInProgress = false

    private val _errorMessage = MutableLiveData<Exception>()
    val errorMessage: LiveData<Exception> = _errorMessage
    /*
     * Authentication related functions
     */
@@ -120,10 +121,13 @@ class MainActivityViewModel @Inject constructor(

    fun getApplication(app: FusedApp, imageView: ImageView?) {
        viewModelScope.launch {
            val fusedDownload: FusedDownload
            try {
                val appIcon = imageView?.let { getImageBase64(it) } ?: ""
            val downloadList = getAppDownloadLink(app).toMutableList()
                val downloadList: List<String>
                downloadList = getAppDownloadLink(app).toMutableList()

            val fusedDownload = FusedDownload(
                fusedDownload = FusedDownload(
                    app._id,
                    app.origin,
                    app.status,
@@ -135,6 +139,11 @@ class MainActivityViewModel @Inject constructor(
                    app.type,
                    appIcon
                )
            } catch (e: Exception) {
                _errorMessage.value = e
                return@launch
            }

            if (fusedDownload.status == Status.INSTALLATION_ISSUE) {
                fusedManagerRepository.clearInstallationIssue(fusedDownload)
            }
@@ -142,6 +151,10 @@ class MainActivityViewModel @Inject constructor(
        }
    }

    suspend fun updateAwaiting(fusedDownload: FusedDownload) {
        fusedManagerRepository.updateAwaiting(fusedDownload)
    }

    fun cancelDownload(app: FusedApp) {
        viewModelScope.launch {
            val fusedDownload =
+1 −1
Original line number Diff line number Diff line
@@ -174,7 +174,7 @@ class ApplicationListRVAdapter(
                        }
                    }
                }
                Status.QUEUED, Status.DOWNLOADING -> {
                Status.QUEUED, Status.AWAITING, Status.DOWNLOADING -> {
                    installButton.apply {
                        text = context.getString(R.string.cancel)
                        setOnClickListener {
+1 −1
Original line number Diff line number Diff line
@@ -127,7 +127,7 @@ class HomeChildRVAdapter(
                        }
                    }
                }
                Status.QUEUED, Status.DOWNLOADING -> {
                Status.QUEUED, Status.AWAITING, Status.DOWNLOADING -> {
                    installButton.apply {
                        text = context.getString(R.string.cancel)
                        setTextColor(context.getColor(R.color.colorAccent))
+13 −1
Original line number Diff line number Diff line
package foundation.e.apps.manager.database

import androidx.lifecycle.LiveData
import androidx.lifecycle.asFlow
import foundation.e.apps.manager.database.fusedDownload.FusedDownload
import foundation.e.apps.manager.database.fusedDownload.FusedDownloadDAO
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class DatabaseRepository @Inject constructor(
    private val fusedDownloadDAO: FusedDownloadDAO
) {
@@ -22,10 +26,18 @@ class DatabaseRepository @Inject constructor(
    }

    suspend fun updateDownload(fusedDownload: FusedDownload) {
        return fusedDownloadDAO.updateDownload(fusedDownload)
        fusedDownloadDAO.updateDownload(fusedDownload)
    }

    suspend fun deleteDownload(fusedDownload: FusedDownload) {
        return fusedDownloadDAO.deleteDownload(fusedDownload)
    }

    suspend fun getDownloadById(id: String): FusedDownload? {
        return fusedDownloadDAO.getDownloadById(id)
    }

    fun getDownloadFlowById(id: String): Flow<FusedDownload> {
        return fusedDownloadDAO.getDownloadFlowById(id).asFlow()
    }
}
Loading