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

Commit 6677f20b authored by Hasib Prince's avatar Hasib Prince
Browse files

App lounge: progress data calculated for downloading item

parent 235be956
Loading
Loading
Loading
Loading
+36 −0
Original line number Diff line number Diff line
package foundation.e.apps

import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import foundation.e.apps.api.fused.FusedAPIRepository
import foundation.e.apps.api.fused.data.FusedApp
import foundation.e.apps.manager.download.data.DownloadProgress
import foundation.e.apps.manager.download.data.DownloadProgressLD
import foundation.e.apps.manager.fused.FusedManagerRepository
import javax.inject.Inject

@HiltViewModel
class AppProgressViewModel @Inject constructor(
    downloadProgressLD: DownloadProgressLD,
    private val fusedManagerRepository: FusedManagerRepository
) : ViewModel() {

    val downloadProgress = downloadProgressLD

    suspend fun calculateProgress(fusedApp: FusedApp?, progress: DownloadProgress): Pair<Long, Long> {
        fusedApp?.let { app ->
            val appDownload = fusedManagerRepository.getDownloadList()
                .singleOrNull { it.id.contentEquals(app._id) }
            val downloadingMap = progress.totalSizeBytes.filter { item ->
                appDownload?.downloadIdMap?.keys?.contains(item.key) == true
            }
            val totalSizeBytes = downloadingMap.values.sum()
            val downloadedSoFar = progress.bytesDownloadedSoFar.filter { item ->
                appDownload?.downloadIdMap?.keys?.contains(item.key) == true
            }.values.sum()

            return Pair(totalSizeBytes, downloadedSoFar)
        }
        return Pair(1, 0)
    }
}
 No newline at end of file
+15 −7
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.LinearLayoutManager
import dagger.hilt.android.AndroidEntryPoint
import foundation.e.apps.AppProgressViewModel
import foundation.e.apps.MainActivityViewModel
import foundation.e.apps.PrivacyInfoViewModel
import foundation.e.apps.R
@@ -51,6 +52,7 @@ class ApplicationListFragment : Fragment(R.layout.fragment_application_list), Fu
    private val viewModel: ApplicationListViewModel by viewModels()
    private val privacyInfoViewModel: PrivacyInfoViewModel by viewModels()
    private val mainActivityViewModel: MainActivityViewModel by activityViewModels()
    private val appProgressViewModel: AppProgressViewModel by viewModels()

    private var _binding: FragmentApplicationListBinding? = null
    private val binding get() = _binding!!
@@ -91,7 +93,8 @@ class ApplicationListFragment : Fragment(R.layout.fragment_application_list), Fu
                    it,
                    pkgManagerModule,
                    User.valueOf(mainActivityViewModel.userType.value ?: User.UNAVAILABLE.name),
                    viewLifecycleOwner
                    viewLifecycleOwner,
                    appProgressViewModel
                )
            }
        recyclerView.apply {
@@ -99,6 +102,17 @@ class ApplicationListFragment : Fragment(R.layout.fragment_application_list), Fu
            layoutManager = LinearLayoutManager(view.context)
        }

        observeDownloadList()

        viewModel.appListLiveData.observe(viewLifecycleOwner) {
            listAdapter?.setData(it)

            binding.shimmerLayout.visibility = View.GONE
            recyclerView.visibility = View.VISIBLE
        }
    }

    private fun observeDownloadList() {
        mainActivityViewModel.downloadList.observe(viewLifecycleOwner) { list ->
            val categoryList = viewModel.appListLiveData.value?.toMutableList()
            if (!categoryList.isNullOrEmpty()) {
@@ -110,12 +124,6 @@ class ApplicationListFragment : Fragment(R.layout.fragment_application_list), Fu
                viewModel.appListLiveData.value = categoryList
            }
        }

        viewModel.appListLiveData.observe(viewLifecycleOwner) {
            listAdapter?.setData(it)
            binding.shimmerLayout.visibility = View.GONE
            recyclerView.visibility = View.VISIBLE
        }
    }

    override fun onDestroyView() {
+16 −1
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.content.ContextCompat
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.viewModelScope
import androidx.navigation.findNavController
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
@@ -34,11 +35,13 @@ import com.facebook.shimmer.Shimmer
import com.facebook.shimmer.Shimmer.Direction.LEFT_TO_RIGHT
import com.facebook.shimmer.ShimmerDrawable
import com.google.android.material.snackbar.Snackbar
import foundation.e.apps.AppProgressViewModel
import foundation.e.apps.PrivacyInfoViewModel
import foundation.e.apps.R
import foundation.e.apps.api.cleanapk.CleanAPKInterface
import foundation.e.apps.api.fused.FusedAPIInterface
import foundation.e.apps.api.fused.data.FusedApp
import foundation.e.apps.application.ApplicationViewModel
import foundation.e.apps.applicationlist.ApplicationListFragmentDirections
import foundation.e.apps.databinding.ApplicationListItemBinding
import foundation.e.apps.manager.pkg.PkgManagerModule
@@ -47,6 +50,7 @@ import foundation.e.apps.updates.UpdatesFragmentDirections
import foundation.e.apps.utils.enums.Origin
import foundation.e.apps.utils.enums.Status
import foundation.e.apps.utils.enums.User
import kotlinx.coroutines.launch
import javax.inject.Singleton

@Singleton
@@ -56,7 +60,8 @@ class ApplicationListRVAdapter(
    private val currentDestinationId: Int,
    private val pkgManagerModule: PkgManagerModule,
    private val user: User,
    private val lifecycleOwner: LifecycleOwner
    private val lifecycleOwner: LifecycleOwner,
    private val appProgressViewModel: AppProgressViewModel
) : ListAdapter<FusedApp, ApplicationListRVAdapter.ViewHolder>(ApplicationDiffUtil()) {

    private val TAG = ApplicationListRVAdapter::class.java.simpleName
@@ -151,6 +156,7 @@ class ApplicationListRVAdapter(
                }
                else -> Log.wtf(TAG, "${searchApp.package_name} is from an unknown origin")
            }
            Log.d(TAG, "onBindViewHolder: ${searchApp.name} : ${searchApp.status}")
            when (searchApp.status) {
                Status.INSTALLED -> {
                    handleInstalled(view, searchApp)
@@ -280,6 +286,15 @@ class ApplicationListRVAdapter(
            backgroundTintList =
                ContextCompat.getColorStateList(view.context, android.R.color.transparent)
            strokeColor = ContextCompat.getColorStateList(view.context, R.color.colorAccent)
            appProgressViewModel.downloadProgress.observe(lifecycleOwner) {
                appProgressViewModel.viewModelScope.launch {
                    val progress = appProgressViewModel.calculateProgress(searchApp, it)
                    Log.d(TAG, "app Progress: $progress")
                    if(progress.second > 0) {
                        text = "${((progress.second / progress.first.toDouble()) * 100).toInt()}%"
                    }
                }
            }
            setOnClickListener {
                cancelDownload(searchApp)
            }
+4 −1
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.aurora.gplayapi.SearchSuggestEntry
import com.facebook.shimmer.ShimmerFrameLayout
import dagger.hilt.android.AndroidEntryPoint
import foundation.e.apps.AppProgressViewModel
import foundation.e.apps.MainActivityViewModel
import foundation.e.apps.PrivacyInfoViewModel
import foundation.e.apps.R
@@ -65,6 +66,7 @@ class SearchFragment :
    private val searchViewModel: SearchViewModel by viewModels()
    private val privacyInfoViewModel: PrivacyInfoViewModel by viewModels()
    private val mainActivityViewModel: MainActivityViewModel by activityViewModels()
    private val appProgressViewModel: AppProgressViewModel by viewModels()

    private val SUGGESTION_KEY = "suggestion"

@@ -111,7 +113,8 @@ class SearchFragment :
                it,
                pkgManagerModule,
                User.valueOf(mainActivityViewModel.userType.value ?: User.UNAVAILABLE.name),
                viewLifecycleOwner
                viewLifecycleOwner,
                appProgressViewModel
            )
        }

+4 −1
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import dagger.hilt.android.AndroidEntryPoint
import foundation.e.apps.AppProgressViewModel
import foundation.e.apps.MainActivityViewModel
import foundation.e.apps.PrivacyInfoViewModel
import foundation.e.apps.R
@@ -50,6 +51,7 @@ class UpdatesFragment : Fragment(R.layout.fragment_updates), FusedAPIInterface {
    private val updatesViewModel: UpdatesViewModel by viewModels()
    private val privacyInfoViewModel: PrivacyInfoViewModel by viewModels()
    private val mainActivityViewModel: MainActivityViewModel by activityViewModels()
    private val appProgressViewModel: AppProgressViewModel by viewModels()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
@@ -78,7 +80,8 @@ class UpdatesFragment : Fragment(R.layout.fragment_updates), FusedAPIInterface {
                it,
                pkgManagerModule,
                User.valueOf(mainActivityViewModel.userType.value ?: User.UNAVAILABLE.name),
                viewLifecycleOwner
                viewLifecycleOwner,
                appProgressViewModel
            )
        }