From cd2014a74cce937eedff9eb26d049074fa620a31 Mon Sep 17 00:00:00 2001 From: Deenbandhu Netam <86369518+Deenu488@users.noreply.github.com> Date: Sun, 12 May 2024 15:19:43 +0530 Subject: [PATCH] Fix bug: not setting current file position when closing others tabs (#543) --- app/build.gradle.kts | 2 +- .../kotlin/org/cosmicide/fragment/EditorFragment.kt | 4 ++-- .../main/kotlin/org/cosmicide/model/FileViewModel.kt | 10 ++++++---- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 30076c52c..9139f514f 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -196,7 +196,7 @@ dependencies { implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.0-alpha03") implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.0-alpha03") implementation("androidx.swiperefreshlayout:swiperefreshlayout:1.2.0-alpha01") - implementation("androidx.viewpager2:viewpager2:1.1.0-beta02") + implementation("androidx.viewpager2:viewpager2:1.1.0-rc01") implementation("androidx.activity:activity-ktx:1.9.0-beta01") implementation("androidx.startup:startup-runtime:1.2.0-alpha02") diff --git a/app/src/main/kotlin/org/cosmicide/fragment/EditorFragment.kt b/app/src/main/kotlin/org/cosmicide/fragment/EditorFragment.kt index 3ecc70308..504158558 100644 --- a/app/src/main/kotlin/org/cosmicide/fragment/EditorFragment.kt +++ b/app/src/main/kotlin/org/cosmicide/fragment/EditorFragment.kt @@ -145,7 +145,7 @@ class EditorFragment : BaseBindingFragment() { } }) - TabLayoutMediator(binding.tabLayout, binding.pager) { tab, position -> + TabLayoutMediator(binding.tabLayout, binding.pager, true, false) { tab, position -> tab.text = fileViewModel.files.value!![position].name tab.view.setOnLongClickListener { Log.d("EditorFragment", "onLongClick: $position") @@ -617,7 +617,7 @@ class EditorFragment : BaseBindingFragment() { R.id.close_all_tab -> fileViewModel.removeAll() R.id.close_left_tab -> fileViewModel.removeLeft(pos - 1) R.id.close_right_tab -> fileViewModel.removeRight(pos + 1) - R.id.close_other_tab -> fileViewModel.removeOthers(fileViewModel.files.value!![pos]) + R.id.close_other_tab -> fileViewModel.removeOthers(fileViewModel.files.value!![position]) } true } diff --git a/app/src/main/kotlin/org/cosmicide/model/FileViewModel.kt b/app/src/main/kotlin/org/cosmicide/model/FileViewModel.kt index 0838a60c8..cbc655f75 100644 --- a/app/src/main/kotlin/org/cosmicide/model/FileViewModel.kt +++ b/app/src/main/kotlin/org/cosmicide/model/FileViewModel.kt @@ -70,10 +70,12 @@ class FileViewModel : ViewModel() { * Removes all files from the list except for the given file, and sets the current position to 0. */ fun removeOthers(file: File) { - files.value = mutableListOf(file) - setCurrentPosition(0) + if (files.value!!.size > 1) { + files.value = mutableListOf(file) + setCurrentPosition(0) + } } - + /** * Removes all files from the list. * Sets the current position to -1 to indicate that there is no current file. @@ -113,4 +115,4 @@ class FileViewModel : ViewModel() { files.value = (files.value.orEmpty() + newFiles).distinctBy { it.absolutePath }.toMutableList() } -} \ No newline at end of file +}