Skip to content

Commit

Permalink
Further DI improvements (#1161)
Browse files Browse the repository at this point in the history
* Further DI improvements

* Roll back sync manager and fix source set issue

* Apply same fixes to app launcher

---------

Co-authored-by: Ashley Davies <[email protected]>
  • Loading branch information
ashdavies and ashdavies authored Sep 10, 2024
1 parent bcdaaed commit 05d844c
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 34 deletions.
5 changes: 4 additions & 1 deletion app-launcher/android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ kotlin {
}

val androidInstrumentedTest by getting {
dependsOn(androidMain.get())
dependencies {
implementation(libs.androidx.core.splashscreen)
implementation(libs.google.android.material)
}
}
}
}
5 changes: 4 additions & 1 deletion conferences-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ kotlin {
}

val androidInstrumentedTest by getting {
dependsOn(androidMain.get())
dependencies {
implementation(libs.androidx.core.splashscreen)
implementation(libs.google.android.material)
}
}

jvmMain.dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,26 @@ import com.slack.circuit.foundation.Circuit
import com.slack.circuit.runtime.presenter.presenterOf
import io.ashdavies.content.PlatformContext
import io.ashdavies.content.reportFullyDrawn
import io.ashdavies.http.DefaultHttpConfiguration
import io.ashdavies.identity.IdentityManager
import io.ashdavies.party.coroutines.rememberRetainedCoroutineScope
import io.ashdavies.party.events.EventsPresenter
import io.ashdavies.party.events.EventsScreen
import io.ashdavies.party.events.paging.rememberEventPager
import io.ashdavies.party.gallery.File
import io.ashdavies.party.gallery.GalleryPresenter
import io.ashdavies.party.gallery.GalleryScreen
import io.ashdavies.party.gallery.ImageManager
import io.ashdavies.party.gallery.PathProvider
import io.ashdavies.party.gallery.StorageManager
import io.ashdavies.party.gallery.SyncManager
import io.ashdavies.party.gallery.inMemoryHttpClientEngine
import io.ashdavies.party.gallery.readChannel
import io.ashdavies.party.home.HomePresenter
import io.ashdavies.party.home.HomeScreen
import io.ashdavies.playground.PlaygroundDatabase
import io.ashdavies.sql.LocalTransacter
import io.ktor.client.HttpClient
import kotlinx.coroutines.CoroutineScope
import io.ashdavies.party.events.Event as DatabaseEvent

Expand All @@ -31,6 +38,9 @@ public fun rememberCircuit(
coroutineScope: CoroutineScope = rememberRetainedCoroutineScope(),
): Circuit = remember(platformContext) {
val identityManager = IdentityManager(platformContext, playgroundDatabase.credentialQueries)
val imageManager = ImageManager(platformContext, playgroundDatabase.imageQueries)
val inMemoryHttpClient = HttpClient(inMemoryHttpClientEngine(), DefaultHttpConfiguration)
val syncManager = SyncManager(inMemoryHttpClient, File::readChannel)
val storageManager = StorageManager(PathProvider(platformContext))

Circuit.Builder()
Expand All @@ -41,7 +51,7 @@ public fun rememberCircuit(
presenterOf { EventsPresenter(eventPager, coroutineScope) }
}
.addPresenter<GalleryScreen, GalleryScreen.State> { _, _, _ ->
presenterOf { GalleryPresenter(platformContext) }
presenterOf { GalleryPresenter(imageManager, syncManager) }
}
.addUi<HomeScreen, HomeScreen.State> { state, modifier ->
HomeScreen(state, modifier, platformContext::reportFullyDrawn)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.paging.ExperimentalPagingApi
import androidx.paging.InvalidatingPagingSourceFactory
import androidx.paging.Pager
import androidx.paging.PagingConfig
import app.cash.sqldelight.Transacter
import io.ashdavies.aggregator.AsgConference
import io.ashdavies.aggregator.UpcomingConferencesCallable
import io.ashdavies.config.RemoteConfig
Expand All @@ -15,7 +16,8 @@ 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.ashdavies.playground.PlaygroundDatabase
import io.ashdavies.sql.LocalTransacter
import io.ktor.client.HttpClient
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
Expand Down Expand Up @@ -70,6 +72,12 @@ private fun rememberUpcomingEventsCallable(
}
}

@Composable
internal fun <T : Transacter> rememberLocalQueries(
database: PlaygroundDatabase = LocalTransacter.current as PlaygroundDatabase,
transform: (PlaygroundDatabase) -> T,
): T = remember { transform(database) }

private fun AsgConference.toEvent(): ApiEvent = ApiEvent(
id = hash(), name = name, website = website, location = location, dateStart = dateStart,
dateEnd = dateEnd, imageUrl = imageUrl, status = status, online = online,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,16 @@ package io.ashdavies.party.gallery
import androidx.compose.runtime.Composable
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.retained.produceRetainedState
import com.slack.circuit.retained.rememberRetained
import io.ashdavies.content.PlatformContext
import io.ashdavies.party.sql.rememberLocalQueries
import kotlinx.coroutines.launch

@Composable
internal fun GalleryPresenter(
platformContext: PlatformContext,
imageQueries: ImageQueries = rememberLocalQueries { it.imageQueries },
): GalleryScreen.State = GalleryPresenter(
imageManager = remember(imageQueries) {
ImageManager(platformContext, imageQueries)
},
)

@Composable
internal fun GalleryPresenter(
imageManager: ImageManager,
syncManager: SyncManager = remember { SyncManager() },
syncManager: SyncManager,
): GalleryScreen.State {
val itemList by produceRetainedState(emptyList<Image>()) {
imageManager.list.collect { value = it }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.ashdavies.party.gallery

import io.ashdavies.http.DefaultHttpConfiguration
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.get
Expand Down Expand Up @@ -28,8 +27,8 @@ internal enum class SyncState {
}

internal fun SyncManager(
client: HttpClient = HttpClient(inMemoryHttpClientEngine(), DefaultHttpConfiguration),
reader: File.() -> ByteReadChannel = File::readChannel,
client: HttpClient,
reader: File.() -> ByteReadChannel,
): SyncManager = object : SyncManager {

private val _state = MutableStateFlow<Map<String, SyncState>>(emptyMap())
Expand Down

This file was deleted.

0 comments on commit 05d844c

Please sign in to comment.