mirror of
https://github.com/anyproto/anytype-kotlin.git
synced 2025-06-08 05:47:05 +09:00
DROID-2897 All content | Update grouping by date (#1650)
This commit is contained in:
parent
a56132a979
commit
8078b7a3e4
9 changed files with 97 additions and 57 deletions
|
@ -73,9 +73,9 @@ import kotlinx.coroutines.flow.distinctUntilChanged
|
|||
import kotlinx.coroutines.flow.drop
|
||||
import kotlinx.coroutines.flow.emitAll
|
||||
import kotlinx.coroutines.flow.flatMapLatest
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.onCompletion
|
||||
import kotlinx.coroutines.flow.onStart
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.flow.take
|
||||
import kotlinx.coroutines.launch
|
||||
|
@ -221,14 +221,7 @@ class AllContentViewModel(
|
|||
}
|
||||
}
|
||||
|
||||
//todo discuss with someone
|
||||
private fun loadData(): Flow<List<UiContentItem>> = flow {
|
||||
|
||||
uiContentState.value = if (itemsLimit == DEFAULT_SEARCH_LIMIT) {
|
||||
UiContentState.InitLoading
|
||||
} else {
|
||||
UiContentState.Paging
|
||||
}
|
||||
private fun loadData(): Flow<List<UiContentItem>> {
|
||||
|
||||
val activeTab = uiTabsState.value.selectedTab
|
||||
val activeSort = sortState.value
|
||||
|
@ -243,31 +236,50 @@ class AllContentViewModel(
|
|||
activeMode = uiTitleState.value
|
||||
)
|
||||
|
||||
Timber.d("Restart subscription: with params: $searchParams")
|
||||
|
||||
val dataFlow = storelessSubscriptionContainer.subscribe(searchParams)
|
||||
emitAll(
|
||||
dataFlow.map { objWrappers ->
|
||||
canPaginate.value = objWrappers.size == itemsLimit
|
||||
val items = mapToUiContentItems(
|
||||
objectWrappers = objWrappers,
|
||||
activeSort = activeSort,
|
||||
activeTab = activeTab
|
||||
)
|
||||
uiContentState.value = if (items.isEmpty()) {
|
||||
UiContentState.Empty
|
||||
return storelessSubscriptionContainer.subscribe(searchParams)
|
||||
.onStart {
|
||||
uiContentState.value = if (itemsLimit == DEFAULT_SEARCH_LIMIT) {
|
||||
UiContentState.InitLoading
|
||||
} else {
|
||||
UiContentState.Idle(scrollToTop = shouldScrollToTopItems).also {
|
||||
shouldScrollToTopItems = false
|
||||
}
|
||||
UiContentState.Paging
|
||||
}
|
||||
items
|
||||
}.catch { e ->
|
||||
uiContentState.value = UiContentState.Error(
|
||||
message = e.message ?: "An error occurred while loading data."
|
||||
)
|
||||
emit(emptyList())
|
||||
Timber.d("Restart subscription: with params: $searchParams")
|
||||
}
|
||||
.map { objWrappers ->
|
||||
handleData(objWrappers, activeSort, activeTab)
|
||||
}.catch { e ->
|
||||
handleError(e)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun handleData(
|
||||
objWrappers: List<ObjectWrapper.Basic>,
|
||||
activeSort: AllContentSort,
|
||||
activeTab: AllContentTab
|
||||
): List<UiContentItem> {
|
||||
|
||||
canPaginate.value = objWrappers.size == itemsLimit
|
||||
|
||||
val items = mapToUiContentItems(
|
||||
objectWrappers = objWrappers,
|
||||
activeSort = activeSort,
|
||||
activeTab = activeTab
|
||||
)
|
||||
|
||||
uiContentState.value = if (items.isEmpty()) {
|
||||
UiContentState.Empty
|
||||
} else {
|
||||
UiContentState.Idle(scrollToTop = shouldScrollToTopItems).also {
|
||||
shouldScrollToTopItems = false
|
||||
}
|
||||
}
|
||||
|
||||
return items
|
||||
}
|
||||
|
||||
private fun handleError(e: Throwable) {
|
||||
uiContentState.value = UiContentState.Error(
|
||||
message = e.message ?: "An error occurred while loading data."
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -301,11 +313,11 @@ class AllContentViewModel(
|
|||
)
|
||||
val result = when (activeSort) {
|
||||
is AllContentSort.ByDateCreated -> {
|
||||
groupItemsByDate(items = items, isSortByDateCreated = true)
|
||||
groupItemsByDate(items = items, isSortByDateCreated = true, activeSort = activeSort)
|
||||
}
|
||||
|
||||
is AllContentSort.ByDateUpdated -> {
|
||||
groupItemsByDate(items = items, isSortByDateCreated = false)
|
||||
groupItemsByDate(items = items, isSortByDateCreated = false, activeSort = activeSort)
|
||||
}
|
||||
|
||||
is AllContentSort.ByName -> {
|
||||
|
@ -323,12 +335,28 @@ class AllContentViewModel(
|
|||
|
||||
private fun groupItemsByDate(
|
||||
items: List<UiContentItem.Item>,
|
||||
isSortByDateCreated: Boolean
|
||||
isSortByDateCreated: Boolean,
|
||||
activeSort: AllContentSort
|
||||
): List<UiContentItem> {
|
||||
|
||||
val groupedItems = mutableListOf<UiContentItem>()
|
||||
var currentGroupKey: String? = null
|
||||
|
||||
for (item in items) {
|
||||
val sortedItems = if (isSortByDateCreated) {
|
||||
if (activeSort.sortType == DVSortType.ASC) {
|
||||
items.sortedBy { it.createdDate }
|
||||
} else {
|
||||
items.sortedByDescending { it.createdDate }
|
||||
}
|
||||
} else {
|
||||
if (activeSort.sortType == DVSortType.ASC) {
|
||||
items.sortedBy { it.lastModifiedDate }
|
||||
} else {
|
||||
items.sortedByDescending { it.lastModifiedDate }
|
||||
}
|
||||
}
|
||||
|
||||
for (item in sortedItems) {
|
||||
val timestamp = if (isSortByDateCreated) {
|
||||
item.createdDate
|
||||
} else {
|
||||
|
@ -562,7 +590,8 @@ class AllContentViewModel(
|
|||
viewModelScope.launch {
|
||||
val params = UpdateAllContentState.Params(
|
||||
spaceId = vmParams.spaceId,
|
||||
sort = sort.relationKey.key
|
||||
sort = sort.relationKey.key,
|
||||
isAsc = sort.sortType == DVSortType.ASC
|
||||
)
|
||||
updateAllContentState.async(params).fold(
|
||||
onSuccess = {
|
||||
|
@ -732,6 +761,12 @@ class AllContentViewModel(
|
|||
viewModelScope.launch {
|
||||
storelessSubscriptionContainer.unsubscribe(listOf(subscriptionId()))
|
||||
}
|
||||
viewModelScope.launch {
|
||||
userInput.value = DEFAULT_QUERY
|
||||
searchResultIds.value = emptyList()
|
||||
uiItemsState.value = emptyList()
|
||||
uiContentState.value = UiContentState.Empty
|
||||
}
|
||||
}
|
||||
|
||||
fun proceedWithMoveToBin(item: UiContentItem.Item) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue