-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make paging an opt in feature (#1148)
* Make paging an opt in feature * Change to when comparison --------- Co-authored-by: Ashley Davies <[email protected]>
- Loading branch information
Showing
11 changed files
with
126 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 7 additions & 15 deletions
22
cloud-run/src/commonMain/kotlin/io/ashdavies/cloud/Identifier.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,19 @@ | ||
package io.ashdavies.cloud | ||
|
||
import kotlinx.serialization.SerializationStrategy | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.serializer | ||
import okio.ByteString.Companion.encode | ||
|
||
internal interface Identifier<T : Any> : (T) -> String | ||
internal fun interface Identifier<T : Any> : (T) -> String | ||
|
||
internal class HashIdentifier<T : Any>( | ||
private val serializer: SerializationStrategy<T>, | ||
) : Identifier<T> { | ||
|
||
private val cache = mutableMapOf<T, String>() | ||
|
||
override fun invoke(value: T): String = cache.getOrPut(value) { | ||
internal inline fun <reified T : Any> Identifier( | ||
cache: MutableMap<T, String> = mutableMapOf(), | ||
) = Identifier<T> { value -> | ||
cache.getOrPut(value) { | ||
Json | ||
.encodeToString(serializer, value) | ||
.encodeToString(value) | ||
.encode() | ||
.md5() | ||
.hex() | ||
} | ||
} | ||
|
||
internal inline fun <reified T : Any> Identifier(): Identifier<T> { | ||
return HashIdentifier(serializer()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 0 additions & 43 deletions
43
conferences-app/src/commonMain/kotlin/io/ashdavies/party/events/EventPager.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
conferences-app/src/commonMain/kotlin/io/ashdavies/party/events/paging/EventPager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package io.ashdavies.party.events.paging | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.paging.ExperimentalPagingApi | ||
import androidx.paging.InvalidatingPagingSourceFactory | ||
import androidx.paging.Pager | ||
import androidx.paging.PagingConfig | ||
import io.ashdavies.aggregator.AsgConference | ||
import io.ashdavies.aggregator.UpcomingConferencesCallable | ||
import io.ashdavies.config.RemoteConfig | ||
import io.ashdavies.config.getBoolean | ||
import io.ashdavies.http.LocalHttpClient | ||
import io.ashdavies.http.common.models.EventCfp | ||
import io.ashdavies.party.events.EventsQueries | ||
import io.ashdavies.party.events.callable.PagedUpcomingEventsCallable | ||
import io.ashdavies.party.network.todayAsString | ||
import io.ashdavies.party.sql.rememberLocalQueries | ||
import io.ktor.client.HttpClient | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import okio.ByteString.Companion.encode | ||
import io.ashdavies.http.common.models.Event as ApiEvent | ||
import io.ashdavies.party.events.Event as DatabaseEvent | ||
|
||
private const val PLAYGROUND_BASE_URL = "playground.ashdavies.dev" | ||
private const val DEFAULT_PAGE_SIZE = 10 | ||
|
||
private suspend fun RemoteConfig.isPagingEnabled() = getBoolean("paging_enabled") | ||
|
||
@Composable | ||
@ExperimentalPagingApi | ||
internal fun rememberEventPager( | ||
eventsQueries: EventsQueries = rememberLocalQueries { it.eventsQueries }, | ||
eventsCallable: PagedUpcomingEventsCallable = rememberUpcomingEventsCallable(), | ||
initialKey: String = todayAsString(), | ||
pageSize: Int = DEFAULT_PAGE_SIZE, | ||
): Pager<String, DatabaseEvent> = remember(eventsQueries, eventsCallable) { | ||
val pagingSourceFactory = InvalidatingPagingSourceFactory { | ||
EventsPagingSource(eventsQueries) | ||
} | ||
|
||
val remoteMediator = EventsRemoteMediator( | ||
eventsQueries = eventsQueries, | ||
eventsCallable = eventsCallable, | ||
onInvalidate = pagingSourceFactory::invalidate, | ||
) | ||
|
||
Pager( | ||
config = PagingConfig(pageSize), | ||
initialKey = initialKey, | ||
remoteMediator = remoteMediator, | ||
pagingSourceFactory = pagingSourceFactory, | ||
) | ||
} | ||
|
||
@Composable | ||
private fun rememberUpcomingEventsCallable( | ||
httpClient: HttpClient = LocalHttpClient.current, | ||
remoteConfig: RemoteConfig = RemoteConfig, | ||
): PagedUpcomingEventsCallable { | ||
val pagedCallable by lazy { PagedUpcomingEventsCallable(httpClient, PLAYGROUND_BASE_URL) } | ||
val asgCallable by lazy { UpcomingConferencesCallable(httpClient) } | ||
|
||
return PagedUpcomingEventsCallable { request -> | ||
when { | ||
remoteConfig.isPagingEnabled() -> pagedCallable(request) | ||
else -> asgCallable(Unit).map(AsgConference::toEvent) | ||
} | ||
} | ||
} | ||
|
||
private fun AsgConference.toEvent(): ApiEvent = ApiEvent( | ||
id = hash(), name = name, website = website, location = location, dateStart = dateStart, | ||
dateEnd = dateEnd, imageUrl = imageUrl, status = status, online = online, | ||
cfp = cfp?.let { EventCfp(start = it.start, end = it.end, site = it.site) }, | ||
) | ||
|
||
private inline fun <reified T : Any> T.hash() = Json | ||
.encodeToString(this) | ||
.encode().md5().hex() |
4 changes: 3 additions & 1 deletion
4
...davies/party/events/EventsPagingSource.kt → ...party/events/paging/EventsPagingSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters