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

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

App Lounge: App installation by workmanager

App Lounge: Synchronization issue in DownloadManagerBroadcastReceiver
parent 13c267f2
Loading
Loading
Loading
Loading
+20 −8
Original line number Diff line number Diff line
@@ -24,15 +24,18 @@ 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 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,14 +145,23 @@ 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
            }
            if (!shouldDownload && list.isNotEmpty()) {
                for (item in list) {
                    if (item.status == Status.QUEUED) {
                        viewModel.downloadApp(item)
                        break
//            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}")
                    }
                }
            }
+5 −1
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()
@@ -142,6 +142,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
@@ -122,7 +122,7 @@ class HomeChildRVAdapter(
                        }
                    }
                }
                Status.QUEUED, Status.DOWNLOADING -> {
                Status.QUEUED, Status.AWAITING, Status.DOWNLOADING -> {
                    installButton.apply {
                        text = context.getString(R.string.cancel)
                        setOnClickListener {
+4 −2
Original line number Diff line number Diff line
@@ -6,7 +6,9 @@ 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
) {
@@ -24,7 +26,7 @@ class DatabaseRepository @Inject constructor(
    }

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

    suspend fun deleteDownload(fusedDownload: FusedDownload) {
@@ -35,7 +37,7 @@ class DatabaseRepository @Inject constructor(
        return fusedDownloadDAO.getDownloadById(id)
    }

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