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