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

Commit 7205a85c authored by Hasib Prince's avatar Hasib Prince
Browse files

Merge branch 'diffutil_#4832' into epic_176-all-refactorAndGplay

parents ff391ee8 2e56fa63
Loading
Loading
Loading
Loading
+55 −0
Original line number Diff line number Diff line
/*
 * Apps  Quickly and easily install Android apps onto your device!
 * Copyright (C) 2022  E FOUNDATION
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package foundation.e.apps.applicationlist.model

import androidx.recyclerview.widget.DiffUtil
import foundation.e.apps.api.fused.data.FusedApp

class ApplicationDiffUtil : DiffUtil.ItemCallback<FusedApp>() {
    override fun areItemsTheSame(oldItem: FusedApp, newItem: FusedApp): Boolean {
        return oldItem._id == newItem._id
    }

    override fun areContentsTheSame(oldItem: FusedApp, newItem: FusedApp): Boolean {
        return oldItem._id == newItem._id &&
            oldItem.appSize.contentEquals(newItem.appSize) &&
            oldItem.author.contentEquals(newItem.author) &&
            oldItem.category.contentEquals(newItem.category) &&
            oldItem.description.contentEquals(newItem.description) &&
            oldItem.icon_image_path.contentEquals(newItem.icon_image_path) &&
            oldItem.last_modified.contentEquals(newItem.last_modified) &&
            oldItem.latest_version_code == newItem.latest_version_code &&
            oldItem.latest_version_number.contentEquals(newItem.latest_version_number) &&
            oldItem.licence.contentEquals(newItem.licence) &&
            oldItem.appSize.contentEquals(newItem.appSize) &&
            oldItem.name.contentEquals(newItem.name) &&
            oldItem.offer_type == newItem.offer_type &&
            oldItem.origin == newItem.origin &&
            oldItem.other_images_path == newItem.other_images_path &&
            oldItem.package_name.contentEquals(newItem.package_name) &&
            oldItem.perms == newItem.perms &&
            oldItem.ratings == newItem.ratings &&
            oldItem.shareUrl.contentEquals(newItem.shareUrl) &&
            oldItem.source.contentEquals(newItem.source) &&
            oldItem.status == newItem.status &&
            oldItem.trackers == newItem.trackers &&
            oldItem.url.contentEquals(newItem.url) &&
            oldItem.isFree == newItem.isFree &&
            oldItem.is_pwa == newItem.is_pwa
    }
}
+0 −68
Original line number Diff line number Diff line
/*
 * Apps  Quickly and easily install Android apps onto your device!
 * Copyright (C) 2021  E FOUNDATION
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package foundation.e.apps.applicationlist.model

import androidx.recyclerview.widget.DiffUtil
import foundation.e.apps.api.fused.data.FusedApp

class ApplicationListDiffUtil(
    private val oldList: List<FusedApp>,
    private val newList: List<FusedApp>
) : DiffUtil.Callback() {
    override fun getOldListSize(): Int {
        return oldList.size
    }

    override fun getNewListSize(): Int {
        return newList.size
    }

    override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        // Check id, package name and origin as we are fetching data from multiple sources to avoid issues
        return oldList[oldItemPosition]._id == newList[newItemPosition]._id &&
            oldList[oldItemPosition].package_name == newList[newItemPosition].package_name &&
            oldList[oldItemPosition].origin == newList[newItemPosition].origin
    }

    // TODO: FIX LOGIC HERE TO AVOID UPDATING ENTIRE ITEM
    override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return when {
            oldList[oldItemPosition]._id != newList[newItemPosition]._id -> false
            oldList[oldItemPosition].author != newList[newItemPosition].author -> false
            oldList[oldItemPosition].category != newList[newItemPosition].category -> false
            oldList[oldItemPosition].description != newList[newItemPosition].description -> false
            oldList[oldItemPosition].perms != newList[newItemPosition].perms -> false
            oldList[oldItemPosition].trackers != newList[newItemPosition].trackers -> false
            oldList[oldItemPosition].last_modified != newList[newItemPosition].last_modified -> false
            oldList[oldItemPosition].latest_version_code != newList[newItemPosition].latest_version_code -> false
            oldList[oldItemPosition].latest_version_number != newList[newItemPosition].latest_version_number -> false
            oldList[oldItemPosition].licence != newList[newItemPosition].licence -> false
            oldList[oldItemPosition].name != newList[newItemPosition].name -> false
            oldList[oldItemPosition].other_images_path != newList[newItemPosition].other_images_path -> false
            oldList[oldItemPosition].package_name != newList[newItemPosition].package_name -> false
            oldList[oldItemPosition].ratings != newList[newItemPosition].ratings -> false
            oldList[oldItemPosition].offer_type != newList[newItemPosition].offer_type -> false
            oldList[oldItemPosition].status != newList[newItemPosition].status -> false
            oldList[oldItemPosition].origin != newList[newItemPosition].origin -> false
            oldList[oldItemPosition].shareUrl != newList[newItemPosition].shareUrl -> false
            oldList[oldItemPosition].appSize != newList[newItemPosition].appSize -> false
            else -> false
        }
    }
}
+4 −13
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@ import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.content.ContextCompat
import androidx.navigation.findNavController
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.facebook.shimmer.Shimmer
@@ -53,10 +53,8 @@ class ApplicationListRVAdapter(
    private val currentDestinationId: Int,
    private val pkgManagerModule: PkgManagerModule,
    private val user: User
) :
    RecyclerView.Adapter<ApplicationListRVAdapter.ViewHolder>() {
) : ListAdapter<FusedApp, ApplicationListRVAdapter.ViewHolder>(ApplicationDiffUtil()) {

    private var oldList = emptyList<FusedApp>()
    private val TAG = ApplicationListRVAdapter::class.java.simpleName

    private val shimmer = Shimmer.ColorHighlightBuilder()
@@ -82,7 +80,7 @@ class ApplicationListRVAdapter(

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val view = holder.itemView
        val searchApp = oldList[position]
        val searchApp = getItem(position)
        val shimmerDrawable = ShimmerDrawable().apply { setShimmer(shimmer) }

        holder.binding.apply {
@@ -203,15 +201,8 @@ class ApplicationListRVAdapter(
        }
    }

    override fun getItemCount(): Int {
        return oldList.size
    }

    fun setData(newList: List<FusedApp>) {
        val diffUtil = ApplicationListDiffUtil(oldList, newList)
        val diffResult = DiffUtil.calculateDiff(diffUtil)
        oldList = newList
        diffResult.dispatchUpdatesTo(this)
        this.submitList(newList.map { it.copy() })
    }

    private fun installApplication(searchApp: FusedApp, appIcon: ImageView) {
+2 −15
Original line number Diff line number Diff line
@@ -59,7 +59,8 @@ class HomeFragment : Fragment(R.layout.fragment_home), FusedAPIInterface {
        val homeParentRVAdapter = HomeParentRVAdapter(
            this,
            pkgManagerModule,
            User.valueOf(mainActivityViewModel.userType.value ?: User.UNAVAILABLE.name)
            User.valueOf(mainActivityViewModel.userType.value ?: User.UNAVAILABLE.name),
            mainActivityViewModel, viewLifecycleOwner
        )

        binding.parentRV.apply {
@@ -67,20 +68,6 @@ class HomeFragment : Fragment(R.layout.fragment_home), FusedAPIInterface {
            layoutManager = LinearLayoutManager(view.context)
        }

        mainActivityViewModel.downloadList.observe(viewLifecycleOwner) { list ->
            val homeList = homeViewModel.homeScreenData.value?.toMutableList()
            if (!homeList.isNullOrEmpty()) {
                homeList.forEach { home ->
                    list.forEach {
                        home.list.find { app ->
                            app.origin == it.origin && (app.package_name == it.package_name || app._id == it.id)
                        }?.status = it.status
                    }
                }
                homeViewModel.homeScreenData.value = homeList
            }
        }

        homeViewModel.homeScreenData.observe(viewLifecycleOwner) {
            homeParentRVAdapter.setData(it)
            binding.shimmerLayout.visibility = View.GONE
+32 −0
Original line number Diff line number Diff line
/*
 * Apps  Quickly and easily install Android apps onto your device!
 * Copyright (C) 2021  E FOUNDATION
 * Copyright (C) 2022  E FOUNDATION
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
@@ -15,34 +15,18 @@
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package foundation.e.apps.home.model

import androidx.recyclerview.widget.DiffUtil
import foundation.e.apps.api.fused.data.FusedHome

class HomeParentDiffUtil(
    private val oldList: List<FusedHome>,
    private val newList: List<FusedHome>
) : DiffUtil.Callback() {
    override fun getOldListSize(): Int {
        return oldList.size
    }

    override fun getNewListSize(): Int {
        return newList.size
class FusedHomeDiffUtil : DiffUtil.ItemCallback<FusedHome>() {
    override fun areItemsTheSame(oldItem: FusedHome, newItem: FusedHome): Boolean {
        return oldItem.list == newItem.list
    }

    override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return oldList[oldItemPosition].list == newList[newItemPosition].list
    }

    // TODO: FIX LOGIC HERE TO AVOID UPDATING ENTIRE ITEM
    override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return when {
            oldList[oldItemPosition].title != newList[newItemPosition].title -> false
            oldList[oldItemPosition].list != newList[newItemPosition].list -> false
            else -> false
        }
    override fun areContentsTheSame(oldItem: FusedHome, newItem: FusedHome): Boolean {
        return oldItem.title.contentEquals(newItem.title) &&
            oldItem.list == newItem.list
    }
}
Loading