Fix racecondition when button spam

This commit is contained in:
soraefir 2024-04-08 23:46:58 +02:00
parent 6123048426
commit 728994a8f3
Signed by: sora
GPG Key ID: A362EA0491E2EEA0

View File

@ -6,6 +6,7 @@ import androidx.lifecycle.Lifecycle
import androidx.viewpager2.adapter.FragmentStateAdapter import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2 import androidx.viewpager2.widget.ViewPager2
import net.helcel.beans.activity.fragment.EditPlaceFragment import net.helcel.beans.activity.fragment.EditPlaceFragment
import kotlin.math.max
class ViewPagerAdapter( class ViewPagerAdapter(
fragmentManager: FragmentManager, fragmentManager: FragmentManager,
@ -17,15 +18,13 @@ class ViewPagerAdapter(
private val fragmentList: MutableList<EditPlaceFragment> = ArrayList() private val fragmentList: MutableList<EditPlaceFragment> = ArrayList()
fun addFragment(src: EditPlaceFragment?, target: EditPlaceFragment) { fun addFragment(src: EditPlaceFragment?, target: EditPlaceFragment) {
if (src != null) { val idx = fragmentList.indexOf(src)
while (fragmentList.last() != src) { viewPager.currentItem = max(0, idx)
fragmentList.removeLast() if (src != null && idx >= 0) {
notifyItemRemoved(fragmentList.size) fragmentList.subList(idx + 1, fragmentList.size).clear()
} }
}
println(src.toString() + " - " + target.toString())
fragmentList.add(target) fragmentList.add(target)
notifyItemInserted(fragmentList.size) notifyItemRangeChanged(max(0, idx), fragmentList.size)
viewPager.currentItem = fragmentList.size - 1 viewPager.currentItem = fragmentList.size - 1
} }