Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update filelist UI #401

Merged
merged 3 commits into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
73 changes: 51 additions & 22 deletions app/src/main/java/com/infomaniak/drive/ui/fileList/FileAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ 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
Expand Down Expand Up @@ -66,6 +67,55 @@ open class FileAdapter(

var isComplete = false
private var showLoading = false
private var fileAdapterObserver: RecyclerView.AdapterDataObserver? = null

private fun createFileAdapterObserver(recyclerView: RecyclerView): RecyclerView.AdapterDataObserver {
return 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)

if (fileAdapterObserver == null) {
createFileAdapterObserver(recyclerView).also {
fileAdapterObserver = it
registerAdapterDataObserver(it)
}
}
}

override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
super.onDetachedFromRecyclerView(recyclerView)
kotlin.runCatching {
fileAdapterObserver?.let(::unregisterAdapterDataObserver)
fileAdapterObserver = null
}
}

private fun getFile(position: Int) = fileList[position]

Expand Down Expand Up @@ -115,30 +165,9 @@ open class FileAdapter(
notifyItemRangeInserted(beforeItemCount, newItemList.count())
}

fun addAt(position: Int, newFile: File) {
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)
}
}
}

fun deleteAt(position: Int) {
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)
}
}
}

fun getFileObjectsList(realm: Realm?): ArrayList<File> {
Expand All @@ -157,7 +186,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) {
Expand Down