kopia lustrzana https://github.com/ryukoposting/Signal-Android
Load media in preview earlier than target attachment.
rodzic
bef83e4c0c
commit
b342ce6874
|
@ -28,21 +28,29 @@ class MediaPreviewRepository {
|
||||||
* @param sorting the ordering of the results
|
* @param sorting the ordering of the results
|
||||||
* @param limit the maximum quantity of the results
|
* @param limit the maximum quantity of the results
|
||||||
*/
|
*/
|
||||||
fun getAttachments(startingUri: Uri, threadId: Long, sorting: Sorting, limit: Int = 500): Flowable<List<MediaDatabase.MediaRecord>> {
|
fun getAttachments(startingUri: Uri, threadId: Long, sorting: Sorting, limit: Int = 500): Flowable<Result> {
|
||||||
return Single.fromCallable {
|
return Single.fromCallable {
|
||||||
val cursor = media.getGalleryMediaForThread(threadId, sorting)
|
val cursor = media.getGalleryMediaForThread(threadId, sorting)
|
||||||
|
|
||||||
val acc = mutableListOf<MediaDatabase.MediaRecord>()
|
val acc = mutableListOf<MediaDatabase.MediaRecord>()
|
||||||
|
var initialPosition = 0
|
||||||
var attachmentUri: Uri? = null
|
var attachmentUri: Uri? = null
|
||||||
while (cursor.moveToNext()) {
|
while (cursor.moveToNext()) {
|
||||||
val attachmentId = AttachmentId(cursor.requireLong(AttachmentDatabase.ROW_ID), cursor.requireLong(AttachmentDatabase.UNIQUE_ID))
|
val attachmentId = AttachmentId(cursor.requireLong(AttachmentDatabase.ROW_ID), cursor.requireLong(AttachmentDatabase.UNIQUE_ID))
|
||||||
attachmentUri = PartAuthority.getAttachmentDataUri(attachmentId)
|
attachmentUri = PartAuthority.getAttachmentDataUri(attachmentId)
|
||||||
if (attachmentUri == startingUri) {
|
if (attachmentUri == startingUri) {
|
||||||
|
initialPosition = cursor.position
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (attachmentUri == startingUri) {
|
if (attachmentUri == startingUri) {
|
||||||
|
val frontLimit: Int = limit / 2
|
||||||
|
if (initialPosition < frontLimit) {
|
||||||
|
cursor.moveToFirst()
|
||||||
|
} else {
|
||||||
|
cursor.move(-frontLimit)
|
||||||
|
}
|
||||||
for (i in 0..limit) {
|
for (i in 0..limit) {
|
||||||
val element = MediaDatabase.MediaRecord.from(cursor)
|
val element = MediaDatabase.MediaRecord.from(cursor)
|
||||||
if (element != null) {
|
if (element != null) {
|
||||||
|
@ -54,11 +62,11 @@ class MediaPreviewRepository {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
acc.toList()
|
|
||||||
} else {
|
} else {
|
||||||
Log.e(TAG, "Could not find $startingUri in thread $threadId")
|
Log.e(TAG, "Could not find $startingUri in thread $threadId")
|
||||||
emptyList()
|
|
||||||
}
|
}
|
||||||
|
Result(initialPosition, acc.toList())
|
||||||
}.subscribeOn(Schedulers.io()).toFlowable()
|
}.subscribeOn(Schedulers.io()).toFlowable()
|
||||||
}
|
}
|
||||||
|
data class Result(val initialPosition: Int, val records: List<MediaDatabase.MediaRecord>)
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,6 +151,9 @@ class MediaPreviewV2Fragment : Fragment(R.layout.fragment_media_preview_v2), Med
|
||||||
|
|
||||||
private fun bindReadyState(currentState: MediaPreviewV2State) {
|
private fun bindReadyState(currentState: MediaPreviewV2State) {
|
||||||
(binding.mediaPager.adapter as MediaPreviewV2Adapter).updateBackingItems(currentState.mediaRecords.mapNotNull { it.attachment })
|
(binding.mediaPager.adapter as MediaPreviewV2Adapter).updateBackingItems(currentState.mediaRecords.mapNotNull { it.attachment })
|
||||||
|
if (binding.mediaPager.currentItem != currentState.position) {
|
||||||
|
binding.mediaPager.currentItem = currentState.position
|
||||||
|
}
|
||||||
val currentItem: MediaDatabase.MediaRecord = currentState.mediaRecords[currentState.position]
|
val currentItem: MediaDatabase.MediaRecord = currentState.mediaRecords[currentState.position]
|
||||||
binding.toolbar.title = getTitleText(currentItem, currentState.showThread)
|
binding.toolbar.title = getTitleText(currentItem, currentState.showThread)
|
||||||
binding.toolbar.subtitle = getSubTitleText(currentItem)
|
binding.toolbar.subtitle = getSubTitleText(currentItem)
|
||||||
|
|
|
@ -26,10 +26,11 @@ class MediaPreviewV2ViewModel : ViewModel() {
|
||||||
|
|
||||||
fun fetchAttachments(startingUri: Uri, threadId: Long, sorting: MediaDatabase.Sorting) {
|
fun fetchAttachments(startingUri: Uri, threadId: Long, sorting: MediaDatabase.Sorting) {
|
||||||
disposables += store.update(repository.getAttachments(startingUri, threadId, sorting)) {
|
disposables += store.update(repository.getAttachments(startingUri, threadId, sorting)) {
|
||||||
mediaRecords: List<MediaDatabase.MediaRecord>, oldState: MediaPreviewV2State ->
|
result: MediaPreviewRepository.Result, oldState: MediaPreviewV2State ->
|
||||||
oldState.copy(
|
oldState.copy(
|
||||||
mediaRecords = mediaRecords,
|
position = result.initialPosition,
|
||||||
loadState = MediaPreviewV2State.LoadState.READY
|
mediaRecords = result.records,
|
||||||
|
loadState = MediaPreviewV2State.LoadState.READY,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue