-
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.
* Refactor circuit component * Remote extra new line --------- Co-authored-by: Ashley Davies <[email protected]>
- Loading branch information
Showing
8 changed files
with
83 additions
and
154 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
95 changes: 75 additions & 20 deletions
95
conferences-app/src/commonMain/kotlin/io/ashdavies/party/config/CircuitConfig.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,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 | ||
} |
23 changes: 0 additions & 23 deletions
23
conferences-app/src/commonMain/kotlin/io/ashdavies/party/events/EventsFactory.kt
This file was deleted.
Oops, something went wrong.
39 changes: 5 additions & 34 deletions
39
conferences-app/src/commonMain/kotlin/io/ashdavies/party/events/EventsPresenter.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,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 | ||
} |
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
20 changes: 0 additions & 20 deletions
20
conferences-app/src/commonMain/kotlin/io/ashdavies/party/gallery/GalleryFactory.kt
This file was deleted.
Oops, something went wrong.
21 changes: 0 additions & 21 deletions
21
conferences-app/src/commonMain/kotlin/io/ashdavies/party/home/HomeFactory.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