2018-10-15 19:56:11 +02:00
|
|
|
package com.keylesspalace.tusky.pager
|
|
|
|
|
2019-08-04 20:22:57 +02:00
|
|
|
import android.view.ViewGroup
|
2018-12-17 15:25:35 +01:00
|
|
|
import androidx.fragment.app.Fragment
|
|
|
|
import androidx.fragment.app.FragmentManager
|
|
|
|
import androidx.fragment.app.FragmentStatePagerAdapter
|
2019-08-04 20:22:57 +02:00
|
|
|
import com.keylesspalace.tusky.SharedElementTransitionListener
|
2018-10-15 19:56:11 +02:00
|
|
|
import com.keylesspalace.tusky.entity.Attachment
|
|
|
|
import com.keylesspalace.tusky.fragment.ViewMediaFragment
|
2019-08-04 20:22:57 +02:00
|
|
|
import java.util.*
|
2018-10-15 19:56:11 +02:00
|
|
|
|
|
|
|
class ImagePagerAdapter(
|
2018-12-17 15:25:35 +01:00
|
|
|
fragmentManager: FragmentManager,
|
|
|
|
private val attachments: List<Attachment>,
|
|
|
|
private val initialPosition: Int
|
2019-08-04 20:22:57 +02:00
|
|
|
) : FragmentStatePagerAdapter(fragmentManager), SharedElementTransitionListener {
|
|
|
|
|
|
|
|
private var primaryItem: ViewMediaFragment? = null
|
2019-08-17 20:05:24 +02:00
|
|
|
private var didTransition = false
|
2019-08-04 20:22:57 +02:00
|
|
|
|
|
|
|
override fun setPrimaryItem(container: ViewGroup, position: Int, item: Any) {
|
|
|
|
super.setPrimaryItem(container, position, item)
|
|
|
|
this.primaryItem = item as ViewMediaFragment
|
|
|
|
}
|
2018-10-15 19:56:11 +02:00
|
|
|
|
2018-12-17 15:25:35 +01:00
|
|
|
override fun getItem(position: Int): Fragment {
|
2018-10-15 19:56:11 +02:00
|
|
|
return 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
|
|
|
|
// forth photo and then back to the first. The first fragment will trz to start the
|
|
|
|
// transition and wait until it's over and it will never take place.
|
|
|
|
ViewMediaFragment.newInstance(
|
|
|
|
attachment = attachments[position],
|
|
|
|
shouldStartPostponedTransition = !didTransition && position == initialPosition
|
|
|
|
)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getCount(): Int {
|
|
|
|
return attachments.size
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getPageTitle(position: Int): CharSequence {
|
|
|
|
return String.format(Locale.getDefault(), "%d/%d", position + 1, attachments.size)
|
|
|
|
}
|
2019-08-04 20:22:57 +02:00
|
|
|
|
|
|
|
override fun onTransitionEnd() {
|
2019-08-17 20:05:24 +02:00
|
|
|
this.didTransition = true
|
2019-08-04 20:22:57 +02:00
|
|
|
primaryItem?.onTransitionEnd()
|
|
|
|
}
|
2018-10-15 19:56:11 +02:00
|
|
|
}
|