Skip to content

Commit

Permalink
Refactor circuit component (#1151)
Browse files Browse the repository at this point in the history
* Refactor circuit component

* Remote extra new line

---------

Co-authored-by: Ashley Davies <[email protected]>
  • Loading branch information
ashdavies and ashdavies authored Sep 4, 2024
1 parent bfbe1f4 commit 27593bd
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.core.content.pm.PackageInfoCompat
import com.slack.circuit.backstack.rememberSaveableBackStack
import com.slack.circuit.foundation.Circuit
import com.slack.circuit.foundation.CircuitCompositionLocals
import com.slack.circuit.foundation.NavigableCircuitContent
import com.slack.circuit.foundation.rememberCircuitNavigator
Expand All @@ -24,7 +22,7 @@ import io.ashdavies.http.ProvideHttpClient
import io.ashdavies.http.publicStorage
import io.ashdavies.io.resolveCacheDir
import io.ashdavies.material.dynamicColorScheme
import io.ashdavies.party.config.Circuit
import io.ashdavies.party.config.rememberCircuit
import io.ashdavies.party.home.HomeScreen
import io.ashdavies.playground.BuildConfig
import io.ashdavies.playground.PlaygroundDatabase
Expand Down Expand Up @@ -67,7 +65,7 @@ private fun LauncherApp(context: Context = LocalContext.current) {
}
},
) {
CircuitCompositionLocals(remember<Circuit> { Circuit(context) }) {
CircuitCompositionLocals(rememberCircuit(context)) {
ContentWithOverlays {
ProvideAppCheckToken {
val transacter = rememberTransacter(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,82 @@
package io.ashdavies.party.config

import androidx.compose.runtime.Composable
import androidx.compose.runtime.RememberObserver
import androidx.compose.runtime.Stable
import androidx.compose.runtime.remember
import androidx.paging.Pager
import com.slack.circuit.foundation.Circuit
import com.slack.circuit.retained.rememberRetained
import com.slack.circuit.runtime.presenter.presenterOf
import io.ashdavies.content.PlatformContext
import io.ashdavies.party.events.eventsPresenterFactory
import io.ashdavies.party.events.eventsUiFactory
import io.ashdavies.party.gallery.galleryPresenterFactory
import io.ashdavies.party.gallery.galleryUiFactory
import io.ashdavies.party.home.homePresenterFactory
import io.ashdavies.party.home.homeUiFactory
import io.ashdavies.identity.IdentityManager
import io.ashdavies.party.events.EventsPresenter
import io.ashdavies.party.events.EventsScreen
import io.ashdavies.party.events.paging.rememberEventPager
import io.ashdavies.party.gallery.GalleryPresenter
import io.ashdavies.party.gallery.GalleryScreen
import io.ashdavies.party.gallery.PathProvider
import io.ashdavies.party.gallery.StorageManager
import io.ashdavies.party.home.HomePresenter
import io.ashdavies.party.home.HomeScreen
import io.ashdavies.playground.PlaygroundDatabase
import io.ashdavies.sql.LocalTransacter
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlin.coroutines.CoroutineContext
import io.ashdavies.party.events.Event as DatabaseEvent

public fun Circuit(context: PlatformContext): Circuit = Circuit.Builder()
.addPresenterFactories(getPresenterFactories(context))
.addUiFactories(getUiFactories(context))
.build()
private const val COROUTINE_SCOPE = "COROUTINE_SCOPE"

private fun getPresenterFactories(context: PlatformContext) = listOf(
homePresenterFactory(context),
eventsPresenterFactory(),
galleryPresenterFactory(context),
)
@Composable
public fun rememberCircuit(
platformContext: PlatformContext,
eventPager: Pager<String, DatabaseEvent> = rememberEventPager(),
playgroundDatabase: PlaygroundDatabase = LocalTransacter.current as PlaygroundDatabase,
coroutineScope: CoroutineScope = rememberRetainedCoroutineScope(),
): Circuit = remember(platformContext) {
val identityManager = IdentityManager(platformContext, playgroundDatabase.credentialQueries)
val storageManager = StorageManager(PathProvider(platformContext))

private fun getUiFactories(context: PlatformContext) = listOf(
homeUiFactory(context),
eventsUiFactory(),
galleryUiFactory(context),
)
Circuit.Builder()
.addPresenter<HomeScreen, HomeScreen.State> { _, navigator, _ ->
presenterOf { HomePresenter(identityManager, coroutineScope, navigator) }
}
.addPresenter<EventsScreen, EventsScreen.State> { _, _, _ ->
presenterOf { EventsPresenter(eventPager, coroutineScope) }
}
.addPresenter<GalleryScreen, GalleryScreen.State> { _, _, _ ->
presenterOf { GalleryPresenter(platformContext) }
}
.addUi<HomeScreen, HomeScreen.State> { state, modifier ->
HomeScreen(state, modifier)
}
.addUi<EventsScreen, EventsScreen.State> { state, modifier ->
EventsScreen(state, modifier)
}
.addUi<GalleryScreen, GalleryScreen.State> { state, modifier ->
GalleryScreen(state, storageManager, modifier)
}
.build()
}

@Stable
private class StableCoroutineScope(scope: CoroutineScope) : CoroutineScope by scope

@Composable
private fun rememberRetainedCoroutineScope(
key: String = COROUTINE_SCOPE,
context: CoroutineContext = Dispatchers.Main.immediate,
): StableCoroutineScope = rememberRetained(key) {
val coroutineScope = StableCoroutineScope(CoroutineScope(context + Job()))
rememberObserver(coroutineScope::cancel)
coroutineScope
}

private fun rememberObserver(onForgotten: () -> Unit) = object : RememberObserver {
override fun onAbandoned() = onForgotten()
override fun onForgotten() = onForgotten()
override fun onRemembered() = Unit
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,51 +1,22 @@
package io.ashdavies.party.events

import androidx.compose.runtime.Composable
import androidx.compose.runtime.RememberObserver
import androidx.compose.runtime.Stable
import androidx.paging.ExperimentalPagingApi
import androidx.paging.Pager
import androidx.paging.cachedIn
import com.slack.circuit.retained.rememberRetained
import io.ashdavies.paging.collectAsLazyPagingItems
import io.ashdavies.party.events.paging.rememberEventPager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlin.coroutines.CoroutineContext

private const val COROUTINE_SCOPE = "COROUTINE_SCOPE"

@Composable
@OptIn(ExperimentalPagingApi::class)
internal fun EventsPresenter(
coroutineScope: CoroutineScope = rememberRetainedCoroutineScope(),
eventPager: Pager<String, Event> = rememberEventPager(),
eventPager: Pager<String, Event>,
coroutineScope: CoroutineScope,
): EventsScreen.State {
val pagingData = rememberRetained(coroutineScope) {
val pagingItems = rememberRetained(coroutineScope) {
eventPager.flow.cachedIn(coroutineScope)
}
}.collectAsLazyPagingItems()

return EventsScreen.State(
pagingItems = pagingData.collectAsLazyPagingItems(),
pagingItems = pagingItems,
)
}

@Stable
private class StableCoroutineScope(scope: CoroutineScope) : CoroutineScope by scope

@Composable
private fun rememberRetainedCoroutineScope(
context: CoroutineContext = Dispatchers.Main.immediate,
): StableCoroutineScope = rememberRetained(COROUTINE_SCOPE) {
val coroutineScope = StableCoroutineScope(CoroutineScope(context + Job()))
rememberObserver(coroutineScope::cancel)
coroutineScope
}

private fun rememberObserver(onForgotten: () -> Unit) = object : RememberObserver {
override fun onAbandoned() = onForgotten()
override fun onForgotten() = onForgotten()
override fun onRemembered() = Unit
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private const val DEFAULT_PAGE_SIZE = 10
private suspend fun RemoteConfig.isPagingEnabled() = getBoolean("paging_enabled")

@Composable
@ExperimentalPagingApi
@OptIn(ExperimentalPagingApi::class)
internal fun rememberEventPager(
eventsQueries: EventsQueries = rememberLocalQueries { it.eventsQueries },
eventsCallable: PagedUpcomingEventsCallable = rememberUpcomingEventsCallable(),
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,22 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import com.slack.circuit.foundation.onNavEvent
import com.slack.circuit.retained.rememberRetained
import com.slack.circuit.runtime.Navigator
import com.slack.circuit.runtime.screen.Screen
import io.ashdavies.content.PlatformContext
import io.ashdavies.identity.CredentialQueries
import io.ashdavies.identity.IdentityManager
import io.ashdavies.identity.IdentityState
import io.ashdavies.party.config.booleanConfigAsState
import io.ashdavies.party.config.isHomeEnabled
import io.ashdavies.party.events.EventsScreen
import io.ashdavies.party.gallery.GalleryScreen
import io.ashdavies.party.sql.rememberLocalQueries
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

@Composable
internal fun HomePresenter(
platformContext: PlatformContext,
navigator: Navigator,
): HomeScreen.State = HomePresenter(
identityManager = rememberIdentityManager(
platformContext = platformContext,
credentialQueries = rememberLocalQueries {
it.credentialQueries
},
),
coroutineScope = rememberCoroutineScope(),
navigator = navigator,
)

@Composable
private fun HomePresenter(
identityManager: IdentityManager,
coroutineScope: CoroutineScope,
navigator: Navigator,
Expand All @@ -61,14 +41,3 @@ private fun HomePresenter(
}
}
}

@Composable
private fun rememberIdentityManager(
platformContext: PlatformContext,
credentialQueries: CredentialQueries,
): IdentityManager = remember(platformContext, credentialQueries) {
IdentityManager(
platformContext = platformContext,
credentialQueries = credentialQueries,
)
}

0 comments on commit 27593bd

Please sign in to comment.