Skip to content

Commit

Permalink
Paging and Filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
stigi committed Oct 9, 2024
1 parent 57135d0 commit 750ab90
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 125 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package com.magicbell.sdk.feature.store

import androidx.annotation.VisibleForTesting
import com.magicbell.sdk.common.error.MagicBellError
import com.magicbell.sdk.common.network.graphql.CursorPredicate
import com.magicbell.sdk.common.network.graphql.CursorPredicate.Cursor.Next
import com.magicbell.sdk.common.query.UserQuery
import com.magicbell.sdk.common.threading.MainThread
import com.magicbell.sdk.feature.notification.Notification
Expand Down Expand Up @@ -115,7 +113,7 @@ class NotificationStore internal constructor(
var hasNextPage: Boolean = true
private set

private var nextPageCursor: String? = null
private var nextPage: Int = 1

private val mutableContentFlow = MutableSharedFlow<NotificationStoreContentEvent>()

Expand Down Expand Up @@ -320,8 +318,8 @@ class NotificationStore internal constructor(
suspend fun refresh(): Result<List<Notification>> {
return runCatching {
withContext(coroutineContext) {
val cursorPredicate = CursorPredicate(size = pageSize)
val storePage = fetchStorePageInteractor(predicate, cursorPredicate, userQuery)
val storePagePredicate = StorePagePredicate(1, pageSize)
val storePage = fetchStorePageInteractor(predicate, storePagePredicate, userQuery)
clear(false)
configurePagination(storePage)
configureCount(storePage)
Expand All @@ -345,13 +343,8 @@ class NotificationStore internal constructor(
if (!hasNextPage) {
return@withContext listOf<Notification>()
}
val cursorPredicate: CursorPredicate = nextPageCursor?.let { after ->
CursorPredicate(Next(after), pageSize)
} ?: run {
CursorPredicate(size = pageSize)
}

val storePage = fetchStorePageInteractor(predicate, cursorPredicate, userQuery)
val storePagePredicate = StorePagePredicate(nextPage, pageSize)
val storePage = fetchStorePageInteractor(predicate, storePagePredicate, userQuery)
configurePagination(storePage)
configureCount(storePage)

Expand Down Expand Up @@ -482,7 +475,7 @@ class NotificationStore internal constructor(
setTotalCount(0, notifyChanges)
setUnreadCount(0, notifyChanges)
setUnseenCount(0, notifyChanges)
nextPageCursor = null
nextPage = 1
setHasNextPage(true)
if (notifyChanges) {
val indexes = 0 until notificationCount
Expand Down Expand Up @@ -524,9 +517,7 @@ class NotificationStore internal constructor(
}

private fun configurePagination(storePage: StorePage) {
// TODO: pagination
// val pageInfo = storePage.pageInfo
// nextPageCursor = pageInfo.endCursor
nextPage = storePage.currentPage + 1
setHasNextPage(storePage.currentPage < storePage.totalPages)
}

Expand Down
15 changes: 2 additions & 13 deletions sdk/src/main/java/com/magicbell/sdk/feature/store/StoreContext.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
package com.magicbell.sdk.feature.store

import com.magicbell.sdk.common.network.graphql.CursorPredicate
import com.magicbell.sdk.common.network.graphql.GraphQLRepresentable

internal class StoreContext(
val storePredicate: StorePredicate,
val cursorPredicate: CursorPredicate,
) : GraphQLRepresentable {
override val graphQLValue: String
get() {
val storePredicateString = storePredicate.graphQLValue
val cursorPredicateString = cursorPredicate.graphQLValue

return " data: notifications ($storePredicateString, $cursorPredicateString) { ...notification }"
}
}
val storePagePredicate: StorePagePredicate,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.magicbell.sdk.feature.store

internal data class StorePagePredicate(
val page: Int,
val size: Int,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.magicbell.sdk.feature.store

import com.magicbell.sdk.common.network.graphql.GraphQLRepresentable
import com.magicbell.sdk.feature.notification.Notification

/**
Expand All @@ -17,44 +16,7 @@ data class StorePredicate(
val archived: Boolean = false,
val category: String? = null,
val topic: String? = null,
) : GraphQLRepresentable {
override val graphQLValue: String
get() {
val storePredicateParams = mutableListOf<String>()

read?.also {
if (it) {
storePredicateParams.add("read: true")
} else {
storePredicateParams.add("read: false")
}
}

seen?.also {
if (it) {
storePredicateParams.add("seen: true")
} else {
storePredicateParams.add("seen: false")
}
}

if (archived) {
storePredicateParams.add("archived: true")
} else {
storePredicateParams.add("archived: false")
}

category?.let {
storePredicateParams.add("categories:[$it]")
}

topic?.let {
storePredicateParams.add("topics:[$it]")
}

return storePredicateParams.joinToString(", ")
}
}
)

internal fun StorePredicate.match(notification: Notification): Boolean {
val validator = NotificationValidator(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ internal class StoreNetworkDataSource(
"notifications",
query.userQuery.externalId,
query.userQuery.email,
query.userQuery.hmac
query.userQuery.hmac,
HttpClient.HttpMethod.Get(query.context.asQueryParameters())
)

httpClient.performRequest(request)?.let {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
package com.magicbell.sdk.feature.store.data

import com.mobilejazz.harmony.data.query.Query
import com.magicbell.sdk.common.network.graphql.CursorPredicate
import com.magicbell.sdk.common.network.graphql.GraphQLRepresentable
import com.magicbell.sdk.common.query.UserQuery
import com.magicbell.sdk.feature.store.StoreContext
import com.magicbell.sdk.feature.store.StorePagePredicate
import com.magicbell.sdk.feature.store.StorePredicate

internal class StoreQuery(
val context: StoreContext,
val userQuery: UserQuery,
) : Query(), GraphQLRepresentable {
) : Query() {

constructor(
storePredicate: StorePredicate,
cursorPredicate: CursorPredicate,
storePagePredicate: StorePagePredicate,
userQuery: UserQuery,
) : this(StoreContext(storePredicate, cursorPredicate), userQuery)

override val graphQLValue: String
get() {
return context.graphQLValue
}
) : this(StoreContext(storePredicate, storePagePredicate), userQuery)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.magicbell.sdk.feature.store.data

import com.magicbell.sdk.feature.store.StoreContext
import com.magicbell.sdk.feature.store.StorePagePredicate
import com.magicbell.sdk.feature.store.StorePredicate

internal fun StoreContext.asQueryParameters(): Map<String, String> {
return storePredicate.asQueryParameters() + storePagePredicate.asQueryParameters()
}

private fun StorePredicate.asQueryParameters(): Map<String, String> {
val result: MutableMap<String, String> = mutableMapOf()
read?.let {
result["read"] = "$read"
}
seen?.let {
result["seen"] = "$seen"
}
result["archived"] = "$archived"


return result
}

private fun StorePagePredicate.asQueryParameters(): Map<String, String> {
return mapOf(
"page" to "$page",
"per_page" to "$size",
)
}

Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.magicbell.sdk.feature.store.interactor

import com.magicbell.sdk.common.network.graphql.CursorPredicate
import com.magicbell.sdk.common.query.UserQuery
import com.magicbell.sdk.feature.store.StorePage
import com.magicbell.sdk.feature.store.StorePagePredicate
import com.magicbell.sdk.feature.store.StorePredicate
import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext

internal interface FetchStorePageInteractor {
suspend operator fun invoke(
storePredicate: StorePredicate,
cursorPredicate: CursorPredicate,
storePagePredicate: StorePagePredicate,
userQuery: UserQuery,
): StorePage
}
Expand All @@ -22,11 +22,11 @@ internal class FetchStorePageDefaultInteractor(

override suspend operator fun invoke(
storePredicate: StorePredicate,
cursorPredicate: CursorPredicate,
storePagePredicate: StorePagePredicate,
userQuery: UserQuery,
): StorePage {
return withContext(interactorCoroutineContext) {
getStorePagesInteractor(storePredicate, cursorPredicate, userQuery)
getStorePagesInteractor(storePredicate, storePagePredicate, userQuery)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.magicbell.sdk.feature.store.interactor

import com.mobilejazz.harmony.domain.interactor.GetInteractor
import com.magicbell.sdk.common.error.MagicBellError
import com.magicbell.sdk.common.network.graphql.CursorPredicate
import com.magicbell.sdk.common.query.UserQuery
import com.magicbell.sdk.feature.store.StoreContext
import com.magicbell.sdk.feature.store.StorePage
import com.magicbell.sdk.feature.store.StorePagePredicate
import com.magicbell.sdk.feature.store.StorePredicate
import com.magicbell.sdk.feature.store.data.StoreQuery
import com.mobilejazz.harmony.domain.interactor.GetInteractor
import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext

Expand All @@ -18,10 +17,10 @@ internal class GetStorePagesInteractor(

suspend operator fun invoke(
storePredicate: StorePredicate,
cursorPredicate: CursorPredicate,
storePagePredicate: StorePagePredicate,
userQuery: UserQuery,
): StorePage {
val context = StoreContext(storePredicate, cursorPredicate)
val context = StoreContext(storePredicate, storePagePredicate)
return withContext(coroutineContext) {
getStoreNotificationInteractor(StoreQuery(context, userQuery))
}
Expand Down

0 comments on commit 750ab90

Please sign in to comment.