Skip to content

Commit cd2014a

Browse files
authored
Fix bug: not setting current file position when closing others tabs (#543)
1 parent 93f22d7 commit cd2014a

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

app/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ dependencies {
196196
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.0-alpha03")
197197
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.0-alpha03")
198198
implementation("androidx.swiperefreshlayout:swiperefreshlayout:1.2.0-alpha01")
199-
implementation("androidx.viewpager2:viewpager2:1.1.0-beta02")
199+
implementation("androidx.viewpager2:viewpager2:1.1.0-rc01")
200200
implementation("androidx.activity:activity-ktx:1.9.0-beta01")
201201
implementation("androidx.startup:startup-runtime:1.2.0-alpha02")
202202

app/src/main/kotlin/org/cosmicide/fragment/EditorFragment.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class EditorFragment : BaseBindingFragment<FragmentEditorBinding>() {
145145
}
146146
})
147147

148-
TabLayoutMediator(binding.tabLayout, binding.pager) { tab, position ->
148+
TabLayoutMediator(binding.tabLayout, binding.pager, true, false) { tab, position ->
149149
tab.text = fileViewModel.files.value!![position].name
150150
tab.view.setOnLongClickListener {
151151
Log.d("EditorFragment", "onLongClick: $position")
@@ -617,7 +617,7 @@ class EditorFragment : BaseBindingFragment<FragmentEditorBinding>() {
617617
R.id.close_all_tab -> fileViewModel.removeAll()
618618
R.id.close_left_tab -> fileViewModel.removeLeft(pos - 1)
619619
R.id.close_right_tab -> fileViewModel.removeRight(pos + 1)
620-
R.id.close_other_tab -> fileViewModel.removeOthers(fileViewModel.files.value!![pos])
620+
R.id.close_other_tab -> fileViewModel.removeOthers(fileViewModel.files.value!![position])
621621
}
622622
true
623623
}

app/src/main/kotlin/org/cosmicide/model/FileViewModel.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,12 @@ class FileViewModel : ViewModel() {
7070
* Removes all files from the list except for the given file, and sets the current position to 0.
7171
*/
7272
fun removeOthers(file: File) {
73-
files.value = mutableListOf(file)
74-
setCurrentPosition(0)
73+
if (files.value!!.size > 1) {
74+
files.value = mutableListOf(file)
75+
setCurrentPosition(0)
76+
}
7577
}
76-
78+
7779
/**
7880
* Removes all files from the list.
7981
* Sets the current position to -1 to indicate that there is no current file.
@@ -113,4 +115,4 @@ class FileViewModel : ViewModel() {
113115
files.value =
114116
(files.value.orEmpty() + newFiles).distinctBy { it.absolutePath }.toMutableList()
115117
}
116-
}
118+
}

0 commit comments

Comments
 (0)