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

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

fixed: redundant livedata observers for applist page

parent 7a416b75
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -73,7 +73,7 @@ class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        Timber.d("### oncreate 125")
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

+26 −27
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ import foundation.e.apps.utils.exceptions.GPlayLoginException
import foundation.e.apps.utils.modules.PWAManagerModule
import foundation.e.apps.utils.parentFragment.TimeoutFragment
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject

@AndroidEntryPoint
@@ -116,12 +117,20 @@ class ApplicationListFragment :
        }
    }

    private fun observeDownloadList(adapter: ApplicationListRVAdapter) {
    private fun observeDownloadList(
        adapter: ApplicationListRVAdapter,
        fusedAppResult: ResultSupreme<List<FusedApp>>
    ) {
        mainActivityViewModel.downloadList.removeObservers(viewLifecycleOwner)
        mainActivityViewModel.downloadList.observe(viewLifecycleOwner) { list ->
            Timber.d("### downloadlist observer responded: ${list.firstOrNull()?.name + ": " + list.firstOrNull()?.status}")
            val appList = viewModel.appListLiveData.value?.data?.toMutableList() ?: emptyList()

            appList.let {
                mainActivityViewModel.updateStatusOfFusedApps(it, list)
                adapter.setData(it)
                if (isFusedAppsUpdated(fusedAppResult, listAdapter.currentList)) {
                    adapter.setData(it, args.translation)
                }
            }
        }
    }
@@ -134,26 +143,31 @@ class ApplicationListFragment :

    override fun onResume() {
        super.onResume()

        if (listAdapter.currentList.isNotEmpty() && viewModel.hasAnyAppInstallStatusChanged(listAdapter.currentList)) {
        addDownloadProgressObserver()
        if (listAdapter.currentList.isNotEmpty() && viewModel.hasAnyAppInstallStatusChanged(
                listAdapter.currentList
            )
        ) {
            /*mainActivityViewModel.authData.value?.let {
                refreshData(it)
            }*/
            repostAuthObjects()
        }

    }

    private fun addDownloadProgressObserver() {
        appProgressViewModel.downloadProgress.removeObservers(viewLifecycleOwner)
        appProgressViewModel.downloadProgress.observe(viewLifecycleOwner) {
            updateProgressOfDownloadingItems(binding.recyclerView, it)
        }
    }

    private fun observeAppListLiveData() {
        viewModel.appListLiveData.observe(viewLifecycleOwner) {
            stopLoadingUI()
            if (it.isSuccess()) {
                if (!isFusedAppsUpdated(it)) {
                    return@observe
                }
                updateAppListRecyclerView(listAdapter, it)
                appProgressViewModel.downloadProgress.observe(viewLifecycleOwner) {
                    updateProgressOfDownloadingItems(binding.recyclerView, it)
                }
                observeDownloadList(listAdapter, it)
            }
        }
    }
@@ -203,21 +217,6 @@ class ApplicationListFragment :
        return recyclerView
    }

    private fun updateAppListRecyclerView(
        listAdapter: ApplicationListRVAdapter?,
        fusedAppResult: ResultSupreme<List<FusedApp>>
    ) {
        val currentList = listAdapter?.currentList
        if (!isFusedAppsUpdated(fusedAppResult, currentList)
        ) {
            return
        }
        listAdapter?.setData(fusedAppResult.data!!, args.translation)
        listAdapter?.let { adapter ->
            observeDownloadList(adapter)
        }
    }

    private fun isFusedAppsUpdated(
        fusedAppResult: ResultSupreme<List<FusedApp>>,
        currentList: MutableList<FusedApp>?
@@ -340,7 +339,7 @@ class ApplicationListFragment :

    override fun onPause() {
        binding.shimmerLayout.stopShimmer()
        mainActivityViewModel.downloadList.removeObservers(viewLifecycleOwner)
//        mainActivityViewModel.downloadList.removeObservers(viewLifecycleOwner)
        super.onPause()
    }

+2 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ import foundation.e.apps.OpenForTesting
import foundation.e.apps.manager.database.fusedDownload.FusedDownload
import foundation.e.apps.manager.database.fusedDownload.FusedDownloadDAO
import kotlinx.coroutines.flow.Flow
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton

@@ -28,6 +29,7 @@ class DatabaseRepository @Inject constructor(
    }

    suspend fun updateDownload(fusedDownload: FusedDownload) {
        Timber.d("### fusedownload updated: ${fusedDownload.name + ": " + fusedDownload.status}")
        fusedDownloadDAO.updateDownload(fusedDownload)
    }

+10 −4
Original line number Diff line number Diff line
@@ -122,7 +122,8 @@ class SearchFragment :
        setupListening()

        authObjects.observe(viewLifecycleOwner) {
            if (it == null) return@observe
            val currentQuery = searchView?.query?.toString() ?: ""
            if (it == null || (currentQuery.isNotEmpty() && lastSearch == currentQuery)) return@observe
            loadData(it)
        }

@@ -326,9 +327,7 @@ class SearchFragment :
    override fun onResume() {
        super.onResume()
        binding.shimmerLayout.startShimmer()
        appProgressViewModel.downloadProgress.observe(viewLifecycleOwner) {
            updateProgressOfInstallingApps(it)
        }
        addDownloadProgressObservers()

        if (shouldRefreshData()) {
            repostAuthObjects()
@@ -340,6 +339,13 @@ class SearchFragment :
        }
    }

    private fun addDownloadProgressObservers() {
        appProgressViewModel.downloadProgress.removeObservers(viewLifecycleOwner)
        appProgressViewModel.downloadProgress.observe(viewLifecycleOwner) {
            updateProgressOfInstallingApps(it)
        }
    }

    private fun shouldRefreshData() =
        searchText.isNotEmpty() && recyclerView?.adapter != null && searchViewModel.hasAnyAppInstallStatusChanged(
            (recyclerView?.adapter as ApplicationListRVAdapter).currentList