Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions app/src/main/java/com/nextcloud/talk/api/NcApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import com.nextcloud.talk.models.json.capabilities.CapabilitiesOverall;
import com.nextcloud.talk.models.json.capabilities.RoomCapabilitiesOverall;
import com.nextcloud.talk.models.json.chat.ChatOverallSingleMessage;
import com.nextcloud.talk.models.json.chat.ChatShareOverall;
import com.nextcloud.talk.models.json.chat.ChatShareOverviewOverall;
import com.nextcloud.talk.models.json.conversations.RoomOverall;
import com.nextcloud.talk.models.json.conversations.RoomsOverall;
import com.nextcloud.talk.models.json.generic.GenericOverall;
Expand Down Expand Up @@ -375,19 +373,6 @@ Observable<ChatOverallSingleMessage> sendChatMessage(@Header("Authorization") St
@Field("referenceId") String referenceId
);

@GET
Observable<Response<ChatShareOverall>> getSharedItems(
@Header("Authorization") String authorization,
@Url String url,
@Query("objectType") String objectType,
@Nullable @Query("lastKnownMessageId") Integer lastKnownMessageId,
@Nullable @Query("limit") Integer limit);

@GET
Observable<Response<ChatShareOverviewOverall>> getSharedItemsOverview(@Header("Authorization") String authorization,
@Url String url,
@Nullable @Query("limit") Integer limit);


@GET
Observable<MentionOverall> getMentionAutocompleteSuggestions(@Header("Authorization") String authorization,
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import com.nextcloud.talk.models.json.capabilities.CapabilitiesOverall
import com.nextcloud.talk.models.json.chat.ChatOverall
import com.nextcloud.talk.models.json.wipe.WipeCheckResponseDto
import com.nextcloud.talk.models.json.chat.ChatOverallSingleMessage
import com.nextcloud.talk.models.json.chat.ChatShareOverall
import com.nextcloud.talk.models.json.chat.ChatShareOverviewOverall
import com.nextcloud.talk.models.json.chatpostattachment.ChatPostAttachmentOverall
import com.nextcloud.talk.models.json.chatpostattachment.PostConversationAttachmentRequestDto
import com.nextcloud.talk.models.json.chatprobeattachmentfolder.ChatProbeAttachmentFolderOverall
Expand Down Expand Up @@ -652,4 +654,20 @@ interface NcApiCoroutines {
@Field("metaData") metaData: String,
@Field("referenceId") referenceId: String
): GenericOverall

@GET
suspend fun getSharedItems(
@Header("Authorization") authorization: String?,
@Url url: String,
@Query("objectType") objectType: String,
@Query("lastKnownMessageId") lastKnownMessageId: Int?,
@Query("limit") limit: Int?
): Response<ChatShareOverall>

@GET
suspend fun getSharedItemsOverview(
@Header("Authorization") authorization: String?,
@Url url: String,
@Query("limit") limit: Int?
): Response<ChatShareOverviewOverall>
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ class RepositoryModule {
ConversationsRepositoryImpl(ncApi, ncApiCoroutines)

@Provides
fun provideSharedItemsRepository(ncApi: NcApi, dateUtils: DateUtils): SharedItemsRepository =
SharedItemsRepositoryImpl(ncApi, dateUtils)
fun provideSharedItemsRepository(ncApiCoroutines: NcApiCoroutines, dateUtils: DateUtils): SharedItemsRepository =
SharedItemsRepositoryImpl(ncApiCoroutines, dateUtils)

@Provides
fun provideUnifiedSearchRepository(ncApiCoroutines: NcApiCoroutines): UnifiedSearchRepository =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,16 @@ import com.nextcloud.talk.mediaviewer.model.toMediaViewerGroups
import com.nextcloud.talk.mediaviewer.model.toMediaViewerItem
import com.nextcloud.talk.shareditems.model.SharedFileItem
import com.nextcloud.talk.shareditems.model.SharedItemType
import com.nextcloud.talk.shareditems.model.SharedItems
import com.nextcloud.talk.shareditems.repositories.SharedItemsRepository
import com.nextcloud.talk.utils.CapabilitiesUtil
import com.nextcloud.talk.utils.FileUtils
import io.reactivex.Observable
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
import javax.inject.Inject
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException

/**
* Backs the swipeable media viewer: a flattened, chronologically ordered (oldest first) sequence
Expand Down Expand Up @@ -78,7 +74,7 @@ class MediaViewerViewModel @Inject constructor(private val sharedItemsRepository

private lateinit var user: User
private lateinit var repositoryParameters: SharedItemsRepository.Parameters
private var oldestKnownMessageId: Long? = null
private var olderPagesCursor: Long? = null

fun initialize(user: User, roomToken: String, seedItems: List<MediaViewerItem>, startMessageId: Long) {
this.user = user
Expand All @@ -91,7 +87,7 @@ class MediaViewerViewModel @Inject constructor(private val sharedItemsRepository
)
val groups = seedItems.toMediaViewerGroups()
val startIndex = seedItems.indexOfFirst { it.messageId == startMessageId }.coerceAtLeast(0)
oldestKnownMessageId = seedItems.minOfOrNull { it.messageId }
olderPagesCursor = seedItems.minOfOrNull { it.messageId }

_uiState.value = UiState(groups = groups, currentGlobalIndex = startIndex)
ensureCachedAround(startIndex)
Expand Down Expand Up @@ -185,57 +181,68 @@ class MediaViewerViewModel @Inject constructor(private val sharedItemsRepository
}
}

@Suppress("TooGenericExceptionCaught")
private fun loadOlderGroups() {
val state = _uiState.value
if (state.loadingOlder || !state.canLoadOlder) return
val cursor = oldestKnownMessageId ?: run {
val cursor = olderPagesCursor ?: run {
_uiState.update { it.copy(canLoadOlder = false) }
return
}

_uiState.update { it.copy(loadingOlder = true) }
viewModelScope.launch {
val sharedItems = try {
sharedItemsRepository.media(repositoryParameters, SharedItemType.MEDIA, cursor.toInt())?.awaitFirst()
} catch (e: Exception) {
Log.e(TAG, "Failed to load older shared items", e)
null
}

val olderItems = sharedItems?.items.orEmpty()
.filterIsInstance<SharedFileItem>()
.filter { it.isImageOrVideo() }
.map { it.toMediaViewerItem() }
.sortedBy { it.messageId }
val olderPage = loadOlderPage(cursor)
val olderItems = olderPage?.items.orEmpty()

if (olderItems.isEmpty()) {
_uiState.update { it.copy(loadingOlder = false, canLoadOlder = false) }
return@launch
}

oldestKnownMessageId = olderItems.first().messageId
olderPagesCursor = olderPage?.cursor
_uiState.update { current ->
val combined = (olderItems + current.flattenedItems).toMediaViewerGroups()
current.copy(
groups = combined,
currentGlobalIndex = current.currentGlobalIndex + olderItems.size,
loadingOlder = false,
canLoadOlder = sharedItems?.moreItemsExisting == true,
canLoadOlder = olderPage?.moreItemsExisting == true && olderPage.cursor != null,
pendingShift = PendingShift(id = nextShiftId++, amount = olderItems.size)
)
}
}
}

private suspend fun Observable<SharedItems>.awaitFirst(): SharedItems =
suspendCancellableCoroutine { continuation ->
val disposable = subscribe(
{ continuation.resume(it) },
{ continuation.resumeWithException(it) }
)
continuation.invokeOnCancellation { disposable.dispose() }
private class OlderPage(val items: List<MediaViewerItem>, val cursor: Long?, val moreItemsExisting: Boolean)

/**
* Loads the images and videos shared before [startCursor], oldest first. Pages without any are skipped while
* the server reports more. Returns null if a request fails.
*/
@Suppress("TooGenericExceptionCaught")
private suspend fun loadOlderPage(startCursor: Long): OlderPage? {
var cursor = startCursor
while (true) {
val sharedItems = try {
sharedItemsRepository.media(repositoryParameters, SharedItemType.MEDIA, cursor.toInt())
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
Log.e(TAG, "Failed to load older shared items", e)
return null
}
val items = sharedItems.items
.filterIsInstance<SharedFileItem>()
.filter { it.isImageOrVideo() }
.map { it.toMediaViewerItem() }
.sortedBy { it.messageId }
val nextCursor = sharedItems.lastSeenId?.toLong()
if (items.isNotEmpty() || !sharedItems.moreItemsExisting || nextCursor == null || nextCursor == cursor) {
return OlderPage(items, nextCursor, sharedItems.moreItemsExisting)
}
cursor = nextCursor
}
}

companion object {
private val TAG = MediaViewerViewModel::class.simpleName
Expand Down
Loading
Loading