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

Commit e2a8a221 authored by dev-12's avatar dev-12
Browse files

Use `ConciseAppDiffUtils` DiffUtils for search and update screen

 `ApplicationDiffUtil` doesn't fulfill our requirements/use-case in search and update fragment. mostly because it depends on object and data being same.
parent 79a456a2
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -51,6 +51,7 @@ import foundation.e.apps.install.pkg.InstallerService
import foundation.e.apps.ui.AppInfoFetchViewModel
import foundation.e.apps.ui.MainActivityViewModel
import foundation.e.apps.ui.PrivacyInfoViewModel
import foundation.e.apps.ui.applicationlist.diffUtils.ConciseAppDiffUtils
import foundation.e.apps.ui.search.SearchFragmentDirections
import foundation.e.apps.ui.updates.UpdatesFragmentDirections
import foundation.e.apps.utils.disableInstallButton
@@ -67,7 +68,7 @@ class ApplicationListRVAdapter(
    private val currentDestinationId: Int,
    private var lifecycleOwner: LifecycleOwner?,
    private var paidAppHandler: ((Application) -> Unit)? = null
) : ListAdapter<Application, ApplicationListRVAdapter.ViewHolder>(ApplicationDiffUtil()) {
) : ListAdapter<Application, ApplicationListRVAdapter.ViewHolder>(ConciseAppDiffUtils()) {

    private var optionalCategory = ""

+93 −0
Original line number Diff line number Diff line
package foundation.e.apps.ui.applicationlist.diffUtils

import androidx.recyclerview.widget.DiffUtil
import foundation.e.apps.BuildConfig
import foundation.e.apps.data.application.data.Application
import timber.log.Timber

private const val TAG = "ConciseAppDiffUtils"
private val PERFORMANCE_DEBUGGING = BuildConfig.DEBUG

class ConciseAppDiffUtils : DiffUtil.ItemCallback<Application>() {

    override fun areItemsTheSame(oldItem: Application, newItem: Application): Boolean {
        /**
         * #Application._id is based on App.id which is (unimplemented/default) hashCode of the App
         * class we care about package name and it's source to define if the item is same.
         */
        val isSame = oldItem.package_name == newItem.package_name &&
                oldItem.source.ordinal == newItem.source.ordinal

        if (PERFORMANCE_DEBUGGING && !isSame) {
            val oldInfo = "Old(pkg=${oldItem.package_name}, src=${oldItem.source})"
            val newInfo = "New(pkg=${newItem.package_name}, src=${newItem.source})"
            Timber.tag(TAG).d("item aren't same : $oldInfo vs $newInfo")
        }
        return isSame
    }

    override fun areContentsTheSame(oldItem: Application, newItem: Application): Boolean {
        /**
         * most of the properties in the Application is val
         * so they won't change the same way var would
         * and in search screen we don't care about most fields
         * like a version changes should not be considered as a
         * significant changes to re render the item
         */

        val areAllPropsSame = oldItem.name == newItem.name &&
                oldItem.author == newItem.author && oldItem.package_name == newItem.package_name &&
                oldItem.category == newItem.category && oldItem.licence == newItem.licence &&
                oldItem.icon_image_path == newItem.icon_image_path &&
                oldItem.description == newItem.description &&
                oldItem.isPurchased == newItem.isPurchased &&
                oldItem.isPlaceHolder == newItem.isPlaceHolder &&
                oldItem.ratings.usageQualityScore == newItem.ratings.usageQualityScore

        if (PERFORMANCE_DEBUGGING && !areAllPropsSame) {
            logContentChanged(oldItem, newItem)
        }
        return areAllPropsSame
    }

    private fun logContentChanged(oldItem: Application, newItem: Application) {
        val diff = StringBuilder("contents are not the same for ${oldItem.package_name}:\n")

        if (oldItem.name != newItem.name) {
            diff.append("  name: '${oldItem.name}' vs '${newItem.name}'\n")
        }
        if (oldItem.author != newItem.author) {
            diff.append("  author: '${oldItem.author}' vs '${newItem.author}'\n")
        }
        if (oldItem.category != newItem.category) {
            diff.append("  category: '${oldItem.category}' vs '${newItem.category}'\n")
        }
        if (oldItem.description != newItem.description) {
            diff.append("  description: (changed)\n") // descriptions might be very long
        }
        if (oldItem.icon_image_path != newItem.icon_image_path) {
            val oldIcon = oldItem.icon_image_path
            val newIcon = newItem.icon_image_path
            diff.append("  icon_image_path: '$oldIcon' vs '$newIcon'\n")
        }
        if (oldItem.licence != newItem.licence) {
            diff.append("  licence: '${oldItem.licence}' vs '${newItem.licence}'\n")
        }
        if (oldItem.isPurchased != newItem.isPurchased) {
            diff.append("  isPurchased: ${oldItem.isPurchased} vs ${newItem.isPurchased}\n")
        }
        if (oldItem.isPlaceHolder != newItem.isPlaceHolder) {
            diff.append("  isPlaceHolder: ${oldItem.isPlaceHolder} vs ${newItem.isPlaceHolder}\n")
        }
        if (oldItem.ratings.usageQualityScore != newItem.ratings.usageQualityScore) {
            val oldRating = oldItem.ratings.usageQualityScore
            val newRating = newItem.ratings.usageQualityScore
            diff.append("  usageQualityScore: $oldRating vs $newRating\n")
        }
        if (oldItem.package_name != newItem.package_name) {
            diff.append("  package_name: '${oldItem.package_name}' vs '${newItem.package_name}'\n")
        }

        Timber.tag(TAG).d(diff.toString())
    }
}