2018-10-15 19:56:11 +02:00
|
|
|
package com.keylesspalace.tusky.pager
|
|
|
|
|
2018-12-17 15:25:35 +01:00
|
|
|
import androidx.fragment.app.Fragment
|
2019-11-06 20:17:53 +01:00
|
|
|
import androidx.fragment.app.FragmentActivity
|
|
|
|
import com.keylesspalace.tusky.ViewMediaAdapter
|
2018-10-15 19:56:11 +02:00
|
|
|
import com.keylesspalace.tusky.entity.Attachment
|
|
|
|
import com.keylesspalace.tusky.fragment.ViewMediaFragment
|
2019-11-06 20:17:53 +01:00
|
|
|
import java.lang.ref.WeakReference
|
2018-10-15 19:56:11 +02:00
|
|
|
|
|
|
|
class ImagePagerAdapter(
|
2019-11-06 20:17:53 +01:00
|
|
|
activity: FragmentActivity,
|
2018-12-17 15:25:35 +01:00
|
|
|
private val attachments: List<Attachment>,
|
|
|
|
private val initialPosition: Int
|
2019-11-06 20:17:53 +01:00
|
|
|
) : ViewMediaAdapter(activity) {
|
2019-08-04 20:22:57 +02:00
|
|
|
|
2019-08-17 20:05:24 +02:00
|
|
|
private var didTransition = false
|
2019-11-06 20:17:53 +01:00
|
|
|
private val fragments = MutableList<WeakReference<ViewMediaFragment>?>(attachments.size) { null }
|
2019-08-04 20:22:57 +02:00
|
|
|
|
2019-11-06 20:17:53 +01:00
|
|
|
override fun getItemCount() = attachments.size
|
2018-10-15 19:56:11 +02:00
|
|
|
|
2019-11-06 20:17:53 +01:00
|
|
|
override fun createFragment(position: Int): Fragment {
|
|
|
|
if (position >= 0 && position < attachments.size) {
|
2019-08-17 20:05:24 +02:00
|
|
|
// Fragment should not wait for or start transition if it already happened but we
|
|
|
|
// instantiate the same fragment again, e.g. open the first photo, scroll to the
|
2019-11-06 20:17:53 +01:00
|
|
|
// forth photo and then back to the first. The first fragment will try to start the
|
2019-08-17 20:05:24 +02:00
|
|
|
// transition and wait until it's over and it will never take place.
|
2019-11-06 20:17:53 +01:00
|
|
|
val fragment = ViewMediaFragment.newInstance(
|
2019-08-17 20:05:24 +02:00
|
|
|
attachment = attachments[position],
|
|
|
|
shouldStartPostponedTransition = !didTransition && position == initialPosition
|
|
|
|
)
|
2019-11-06 20:17:53 +01:00
|
|
|
fragments[position] = WeakReference(fragment)
|
|
|
|
return fragment
|
2018-10-15 19:56:11 +02:00
|
|
|
} else {
|
2018-12-17 15:25:35 +01:00
|
|
|
throw IllegalStateException()
|
2018-10-15 19:56:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 20:17:53 +01:00
|
|
|
override fun onTransitionEnd(position: Int) {
|
2019-08-17 20:05:24 +02:00
|
|
|
this.didTransition = true
|
2019-11-06 20:17:53 +01:00
|
|
|
fragments[position]?.get()?.onTransitionEnd()
|
2019-08-04 20:22:57 +02:00
|
|
|
}
|
2018-10-15 19:56:11 +02:00
|
|
|
}
|