Skip to content

Commit

Permalink
Fix bug: not setting current file position when closing others tabs (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Deenu488 authored May 12, 2024
1 parent 93f22d7 commit cd2014a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/kotlin/org/cosmicide/fragment/EditorFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class EditorFragment : BaseBindingFragment<FragmentEditorBinding>() {
}
})

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")
Expand Down Expand Up @@ -617,7 +617,7 @@ class EditorFragment : BaseBindingFragment<FragmentEditorBinding>() {
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
}
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/kotlin/org/cosmicide/model/FileViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -113,4 +115,4 @@ class FileViewModel : ViewModel() {
files.value =
(files.value.orEmpty() + newFiles).distinctBy { it.absolutePath }.toMutableList()
}
}
}

0 comments on commit cd2014a

Please sign in to comment.