2022-09-30 13:42:06 +00:00
|
|
|
package org.thoughtcrime.securesms.mediapreview
|
|
|
|
|
|
|
|
import androidx.core.os.bundleOf
|
|
|
|
import androidx.fragment.app.Fragment
|
|
|
|
import androidx.viewpager2.adapter.FragmentStateAdapter
|
|
|
|
import org.thoughtcrime.securesms.attachments.Attachment
|
2022-11-14 22:28:36 +00:00
|
|
|
import org.thoughtcrime.securesms.mediasend.Media
|
2022-09-30 13:42:06 +00:00
|
|
|
import org.thoughtcrime.securesms.util.MediaUtil
|
2022-12-19 19:30:37 +00:00
|
|
|
import org.thoughtcrime.securesms.util.adapter.StableIdGenerator
|
2022-09-30 13:42:06 +00:00
|
|
|
|
2022-12-19 19:30:37 +00:00
|
|
|
class MediaPreviewV2Adapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
|
2022-10-03 19:10:30 +00:00
|
|
|
private var items: List<Attachment> = listOf()
|
2022-12-19 19:30:37 +00:00
|
|
|
private val stableIdGenerator = StableIdGenerator<Attachment>()
|
2022-09-30 13:42:06 +00:00
|
|
|
|
|
|
|
override fun getItemCount(): Int {
|
|
|
|
return items.count()
|
|
|
|
}
|
|
|
|
|
2022-12-19 19:30:37 +00:00
|
|
|
override fun getItemId(position: Int): Long {
|
|
|
|
return stableIdGenerator.getId(items[position])
|
|
|
|
}
|
|
|
|
|
2022-09-30 13:42:06 +00:00
|
|
|
override fun createFragment(position: Int): Fragment {
|
|
|
|
val attachment: Attachment = items[position]
|
|
|
|
|
|
|
|
val contentType = attachment.contentType
|
|
|
|
val args = bundleOf(
|
|
|
|
MediaPreviewFragment.DATA_URI to attachment.uri,
|
|
|
|
MediaPreviewFragment.DATA_CONTENT_TYPE to contentType,
|
|
|
|
MediaPreviewFragment.DATA_SIZE to attachment.size,
|
2022-12-21 17:40:13 +00:00
|
|
|
MediaPreviewFragment.AUTO_PLAY to attachment.isVideoGif,
|
2022-09-30 13:42:06 +00:00
|
|
|
MediaPreviewFragment.VIDEO_GIF to attachment.isVideoGif,
|
|
|
|
)
|
|
|
|
val fragment = if (MediaUtil.isVideo(contentType)) {
|
|
|
|
VideoMediaPreviewFragment()
|
|
|
|
} else if (MediaUtil.isImageType(contentType)) {
|
|
|
|
ImageMediaPreviewFragment()
|
|
|
|
} else {
|
|
|
|
throw AssertionError("Unexpected media type: $contentType")
|
|
|
|
}
|
|
|
|
|
|
|
|
fragment.arguments = args
|
|
|
|
|
|
|
|
return fragment
|
|
|
|
}
|
2022-10-03 19:10:30 +00:00
|
|
|
|
2022-12-19 19:30:37 +00:00
|
|
|
fun getFragmentTag(position: Int): String? {
|
2022-12-23 17:20:56 +00:00
|
|
|
if (items.isEmpty() || position < 0 || position > itemCount) {
|
2022-12-19 19:30:37 +00:00
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
return "f${getItemId(position)}"
|
|
|
|
}
|
|
|
|
|
2022-11-14 22:28:36 +00:00
|
|
|
fun findItemPosition(media: Media): Int {
|
|
|
|
return items.indexOfFirst { it.uri == media.uri }
|
|
|
|
}
|
|
|
|
|
2022-10-03 19:10:30 +00:00
|
|
|
fun updateBackingItems(newItems: Collection<Attachment>) {
|
|
|
|
if (newItems != items) {
|
|
|
|
items = newItems.toList()
|
|
|
|
notifyDataSetChanged()
|
|
|
|
}
|
|
|
|
}
|
2022-09-30 13:42:06 +00:00
|
|
|
}
|