From 559876df8cedec4e55fbdc1423ae907cba74dd01 Mon Sep 17 00:00:00 2001 From: Abdourahamane BOINAIDI Date: Fri, 17 Dec 2021 09:47:15 +0100 Subject: [PATCH 1/3] update ui after realm live update Signed-off-by: Abdourahamane BOINAIDI --- .../drive/data/models/AppSettings.kt | 2 +- .../drive/ui/fileList/FileAdapter.kt | 69 ++++++++++++++++--- 2 files changed, 59 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/infomaniak/drive/data/models/AppSettings.kt b/app/src/main/java/com/infomaniak/drive/data/models/AppSettings.kt index 782c655ffe..5bbe6bdd07 100644 --- a/app/src/main/java/com/infomaniak/drive/data/models/AppSettings.kt +++ b/app/src/main/java/com/infomaniak/drive/data/models/AppSettings.kt @@ -36,7 +36,7 @@ open class AppSettings( companion object { private const val DB_NAME = "AppSettings.realm" - private var realmConfiguration: RealmConfiguration = RealmConfiguration.Builder().name(DB_NAME) + private val realmConfiguration: RealmConfiguration = RealmConfiguration.Builder().name(DB_NAME) .modules(RealmModules.AppSettingsModule()) .build() diff --git a/app/src/main/java/com/infomaniak/drive/ui/fileList/FileAdapter.kt b/app/src/main/java/com/infomaniak/drive/ui/fileList/FileAdapter.kt index 1198df23b5..2b75d0b28c 100644 --- a/app/src/main/java/com/infomaniak/drive/ui/fileList/FileAdapter.kt +++ b/app/src/main/java/com/infomaniak/drive/ui/fileList/FileAdapter.kt @@ -18,11 +18,13 @@ package com.infomaniak.drive.ui.fileList import android.content.Context +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.core.view.isGone import androidx.core.view.isVisible +import androidx.recyclerview.widget.RecyclerView import com.google.android.material.shape.CornerFamily import com.infomaniak.drive.R import com.infomaniak.drive.data.models.AppSettings @@ -67,6 +69,49 @@ open class FileAdapter( var isComplete = false private var showLoading = false + private var hasFileAdapterObserver = false + private var recyclerView: RecyclerView? = null + + private val fileAdapterObserver = object : RecyclerView.AdapterDataObserver() { + + private fun notifyChanged(position: Int) { + recyclerView?.post { + if (fileList.isNotEmpty() && position < fileList.count()) notifyItemChanged(position) + } + } + + override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) { + if (viewHolderType == DisplayType.LIST && fileList.isNotEmpty()) { + when { + positionStart == 0 -> notifyChanged(0) + positionStart >= fileList.count() -> notifyChanged(fileList.lastIndex) + } + } + } + + override fun onItemRangeInserted(positionStart: Int, itemCount: Int) { + if (viewHolderType == DisplayType.LIST && fileList.count() > 1) { + when { + positionStart == 0 -> notifyChanged(itemCount) + positionStart + itemCount == fileList.count() -> notifyChanged(fileList.lastIndex - itemCount) + } + } + } + } + + override fun onAttachedToRecyclerView(recyclerView: RecyclerView) { + super.onAttachedToRecyclerView(recyclerView) + this.recyclerView = recyclerView + } + + override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) { + super.onDetachedFromRecyclerView(recyclerView) + if (hasFileAdapterObserver) kotlin.runCatching { + unregisterAdapterDataObserver(fileAdapterObserver) + hasFileAdapterObserver = false + } + } + private fun getFile(position: Int) = fileList[position] fun getFiles() = fileList @@ -90,6 +135,10 @@ open class FileAdapter( fun updateFileList(newFileList: OrderedRealmCollection) { fileList = newFileList + if (!hasFileAdapterObserver) { + registerAdapterDataObserver(fileAdapterObserver) + hasFileAdapterObserver = true + } super.updateData(newFileList) } @@ -119,11 +168,10 @@ open class FileAdapter( fileList.add(position, newFile) notifyItemInserted(position) - if (viewHolderType == DisplayType.LIST) { - if (position == 0 && fileList.size > 1) { - notifyItemChanged(1) - } else if (position == fileList.size - 1 && fileList.size > 1) { - notifyItemChanged(fileList.size - 2) + if (viewHolderType == DisplayType.LIST && fileList.count() > 1) { + when (position) { + 0 -> notifyItemChanged(1) + fileList.lastIndex -> notifyItemChanged(fileList.size - 2) } } } @@ -132,11 +180,10 @@ open class FileAdapter( fileList.removeAt(position) notifyItemRemoved(position) - if (viewHolderType == DisplayType.LIST) { - if (position == 0 && fileList.size > 0) { - notifyItemChanged(0) - } else if (position == fileList.size && fileList.size > 0) { - notifyItemChanged(fileList.size - 1) + if (viewHolderType == DisplayType.LIST && fileList.isNotEmpty()) { + when (position) { + 0 -> notifyItemChanged(0) + fileList.size -> notifyItemChanged(fileList.lastIndex) } } } @@ -157,7 +204,7 @@ open class FileAdapter( } } - fun indexOf(fileId: Int) = fileList.indexOfFirst { it.id == fileId } + private fun indexOf(fileId: Int) = fileList.indexOfFirst { it.id == fileId } fun indexOf(fileName: String) = fileList.indexOfFirst { it.name == fileName } fun notifyFileChanged(fileId: Int, onChange: ((file: File) -> Unit)? = null) { From 659fb5662e2e83cf515799726a75bea814102318 Mon Sep 17 00:00:00 2001 From: Abdourahamane BOINAIDI Date: Fri, 17 Dec 2021 10:18:48 +0100 Subject: [PATCH 2/3] update any FileList and children automatically when new elements are added or deleted Signed-off-by: Abdourahamane BOINAIDI --- .../drive/ui/fileList/FileAdapter.kt | 29 ++++--------------- 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/infomaniak/drive/ui/fileList/FileAdapter.kt b/app/src/main/java/com/infomaniak/drive/ui/fileList/FileAdapter.kt index 2b75d0b28c..59b1c11573 100644 --- a/app/src/main/java/com/infomaniak/drive/ui/fileList/FileAdapter.kt +++ b/app/src/main/java/com/infomaniak/drive/ui/fileList/FileAdapter.kt @@ -18,7 +18,6 @@ package com.infomaniak.drive.ui.fileList import android.content.Context -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -102,6 +101,11 @@ open class FileAdapter( override fun onAttachedToRecyclerView(recyclerView: RecyclerView) { super.onAttachedToRecyclerView(recyclerView) this.recyclerView = recyclerView + + if (!hasFileAdapterObserver) { + registerAdapterDataObserver(fileAdapterObserver) + hasFileAdapterObserver = true + } } override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) { @@ -135,10 +139,6 @@ open class FileAdapter( fun updateFileList(newFileList: OrderedRealmCollection) { fileList = newFileList - if (!hasFileAdapterObserver) { - registerAdapterDataObserver(fileAdapterObserver) - hasFileAdapterObserver = true - } super.updateData(newFileList) } @@ -164,28 +164,9 @@ open class FileAdapter( notifyItemRangeInserted(beforeItemCount, newItemList.count()) } - fun addAt(position: Int, newFile: File) { - fileList.add(position, newFile) - notifyItemInserted(position) - - if (viewHolderType == DisplayType.LIST && fileList.count() > 1) { - when (position) { - 0 -> notifyItemChanged(1) - fileList.lastIndex -> notifyItemChanged(fileList.size - 2) - } - } - } - fun deleteAt(position: Int) { fileList.removeAt(position) notifyItemRemoved(position) - - if (viewHolderType == DisplayType.LIST && fileList.isNotEmpty()) { - when (position) { - 0 -> notifyItemChanged(0) - fileList.size -> notifyItemChanged(fileList.lastIndex) - } - } } fun getFileObjectsList(realm: Realm?): ArrayList { From 2f2657b544293732d0f3df4afd49b30000c1ea32 Mon Sep 17 00:00:00 2001 From: Abdourahamane BOINAIDI Date: Mon, 20 Dec 2021 17:26:52 +0100 Subject: [PATCH 3/3] refactor Signed-off-by: Abdourahamane BOINAIDI --- .../drive/ui/fileList/FileAdapter.kt | 53 ++++++++++--------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/com/infomaniak/drive/ui/fileList/FileAdapter.kt b/app/src/main/java/com/infomaniak/drive/ui/fileList/FileAdapter.kt index 59b1c11573..6915e7e3cc 100644 --- a/app/src/main/java/com/infomaniak/drive/ui/fileList/FileAdapter.kt +++ b/app/src/main/java/com/infomaniak/drive/ui/fileList/FileAdapter.kt @@ -67,32 +67,32 @@ open class FileAdapter( var isComplete = false private var showLoading = false + private var fileAdapterObserver: RecyclerView.AdapterDataObserver? = null - private var hasFileAdapterObserver = false - private var recyclerView: RecyclerView? = null + private fun createFileAdapterObserver(recyclerView: RecyclerView): RecyclerView.AdapterDataObserver { + return object : RecyclerView.AdapterDataObserver() { - private val fileAdapterObserver = object : RecyclerView.AdapterDataObserver() { - - private fun notifyChanged(position: Int) { - recyclerView?.post { - if (fileList.isNotEmpty() && position < fileList.count()) notifyItemChanged(position) + private fun notifyChanged(position: Int) { + recyclerView.post { + if (fileList.isNotEmpty() && position < fileList.count()) notifyItemChanged(position) + } } - } - override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) { - if (viewHolderType == DisplayType.LIST && fileList.isNotEmpty()) { - when { - positionStart == 0 -> notifyChanged(0) - positionStart >= fileList.count() -> notifyChanged(fileList.lastIndex) + override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) { + if (viewHolderType == DisplayType.LIST && fileList.isNotEmpty()) { + when { + positionStart == 0 -> notifyChanged(0) + positionStart >= fileList.count() -> notifyChanged(fileList.lastIndex) + } } } - } - override fun onItemRangeInserted(positionStart: Int, itemCount: Int) { - if (viewHolderType == DisplayType.LIST && fileList.count() > 1) { - when { - positionStart == 0 -> notifyChanged(itemCount) - positionStart + itemCount == fileList.count() -> notifyChanged(fileList.lastIndex - itemCount) + override fun onItemRangeInserted(positionStart: Int, itemCount: Int) { + if (viewHolderType == DisplayType.LIST && fileList.count() > 1) { + when { + positionStart == 0 -> notifyChanged(itemCount) + positionStart + itemCount == fileList.count() -> notifyChanged(fileList.lastIndex - itemCount) + } } } } @@ -100,19 +100,20 @@ open class FileAdapter( override fun onAttachedToRecyclerView(recyclerView: RecyclerView) { super.onAttachedToRecyclerView(recyclerView) - this.recyclerView = recyclerView - if (!hasFileAdapterObserver) { - registerAdapterDataObserver(fileAdapterObserver) - hasFileAdapterObserver = true + if (fileAdapterObserver == null) { + createFileAdapterObserver(recyclerView).also { + fileAdapterObserver = it + registerAdapterDataObserver(it) + } } } override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) { super.onDetachedFromRecyclerView(recyclerView) - if (hasFileAdapterObserver) kotlin.runCatching { - unregisterAdapterDataObserver(fileAdapterObserver) - hasFileAdapterObserver = false + kotlin.runCatching { + fileAdapterObserver?.let(::unregisterAdapterDataObserver) + fileAdapterObserver = null } }