Skip to content

Commit

Permalink
Remove system and environment lookup properties (#620)
Browse files Browse the repository at this point in the history
* Remove system and environment lookup properties

* Remove reference from cloud-run
  • Loading branch information
ashdavies authored Nov 3, 2023
1 parent 8e5ef8a commit 951e1c5
Show file tree
Hide file tree
Showing 19 changed files with 68 additions and 115 deletions.
1 change: 1 addition & 0 deletions app-launcher/android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ android {
dependencies {
implementation(projects.appLauncher.common)
implementation(projects.firebaseCompose)
implementation(projects.httpClient)

with(libs.androidx) {
implementation(libs.androidx.core.splashscreen)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
package io.ashdavies.playground

import android.os.Build
import androidx.activity.compose.setContent
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.CompositionLocalProvider
import com.slack.circuit.backstack.rememberSaveableBackStack
import com.slack.circuit.foundation.CircuitCompositionLocals
import com.slack.circuit.foundation.NavigableCircuitContent
import com.slack.circuit.foundation.rememberCircuitNavigator
import com.slack.circuit.overlay.ContentWithOverlays
import io.ashdavies.compose.ProvideFirebaseApp
import io.ashdavies.http.HttpCredentials
import io.ashdavies.http.LocalHttpCredentials

internal class LauncherActivity : KotlinActivity(action = {
val credentials = HttpCredentials(getString(R.string.playground_api_key), Build.PRODUCT)
val initialBackStack = buildInitialBackStack(intent.getStringExtra("route"))
val circuitConfig = CircuitConfig(applicationContext)
val circuit = CircuitConfig(applicationContext)

setContent {
val backStack = rememberSaveableBackStack { initialBackStack.forEach(::push) }
val navigator = rememberCircuitNavigator(backStack)

ProvideFirebaseApp {
MaterialTheme(colorScheme = dynamicColorScheme()) {
CircuitCompositionLocals(circuitConfig) {
ContentWithOverlays { NavigableCircuitContent(navigator, backStack) }
CompositionLocalProvider(LocalHttpCredentials provides credentials) {
CircuitCompositionLocals(circuit) {
ContentWithOverlays {
MaterialTheme(dynamicColorScheme()) {
NavigableCircuitContent(
navigator = rememberCircuitNavigator(backStack),
backstack = backStack,
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import io.ashdavies.graphics.rememberAsyncImagePainter
ExperimentalFoundationApi::class,
ExperimentalMaterial3Api::class,
)
public fun LauncherScreen(
internal fun LauncherScreen(
state: LauncherScreen.State,
modifier: Modifier = Modifier,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.ashdavies.playground
import com.slack.circuit.runtime.ui.Ui
import com.slack.circuit.runtime.ui.ui

public fun LauncherUiFactory(): Ui.Factory = Ui.Factory { screen, _ ->
internal fun LauncherUiFactory(): Ui.Factory = Ui.Factory { screen, _ ->
when (screen is LauncherScreen) {
true -> ui<LauncherScreen.State> { state, modifier -> LauncherScreen(state, modifier) }
false -> null
Expand Down
1 change: 1 addition & 0 deletions app-launcher/desktop/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ kotlin {
}

jvmMain.dependencies {
implementation(projects.httpClient)
implementation(libs.jetbrains.kotlinx.cli)
implementation(libs.slack.circuit.foundation)
implementation(projects.appLauncher.common)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.ashdavies.playground

import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
Expand All @@ -11,6 +12,8 @@ import com.slack.circuit.foundation.CircuitCompositionLocals
import com.slack.circuit.foundation.NavigableCircuitContent
import com.slack.circuit.foundation.rememberCircuitNavigator
import io.ashdavies.content.PlatformContext
import io.ashdavies.http.HttpCredentials
import io.ashdavies.http.LocalHttpCredentials
import kotlinx.cli.ArgParser
import kotlinx.cli.ArgType

Expand All @@ -20,23 +23,27 @@ public fun main(args: Array<String>) {
val argResult = argParser.parse(args)

val initialBackStack = buildInitialBackStack(routerArgOption)
val circuitConfig = CircuitConfig(PlatformContext.Default)
val circuit = CircuitConfig(PlatformContext.Default)

application {
val windowState = rememberWindowState(size = DpSize(450.dp, 975.dp))
val credentials = HttpCredentials(
apiKey = System.getProperty("PLAYGROUND_API_KEY"),
userAgent = System.getProperty("os.name"),
)

application {
Window(
onCloseRequest = ::exitApplication,
state = rememberWindowState(size = DpSize(450.dp, 975.dp)),
title = argResult.commandName,
state = windowState,
) {
val backStack = rememberSaveableBackStack { initialBackStack.forEach(::push) }
val navigator = rememberCircuitNavigator(backStack, ::exitApplication)
val colorScheme = dynamicColorScheme()

MaterialTheme(colorScheme = colorScheme) {
CircuitCompositionLocals(circuitConfig) {
NavigableCircuitContent(navigator, backStack)
MaterialTheme(dynamicColorScheme()) {
CircuitCompositionLocals(circuit) {
CompositionLocalProvider(LocalHttpCredentials provides credentials) {
NavigableCircuitContent(navigator, backStack)
}
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions cloud-run/src/jvmMain/kotlin/io/ashdavies/cloud/Main.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.ashdavies.cloud

import io.ashdavies.http.DefaultHttpClient
import io.ashdavies.http.Software
import io.ktor.client.HttpClient
import io.ktor.http.HttpHeaders
import io.ktor.serialization.Configuration
Expand Down Expand Up @@ -45,5 +44,5 @@ internal fun Application.main(client: HttpClient = DefaultHttpClient()) {
}

private fun DefaultHeadersConfig.headers() {
header(HttpHeaders.Server, Software.clientName)
header(HttpHeaders.Server, System.getProperty("os.name"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import app.cash.paging.ExperimentalPagingApi
import app.cash.paging.Pager
import app.cash.paging.PagingConfig
import io.ashdavies.generated.apis.EventsApi
import io.ashdavies.http.buildApi
import io.ashdavies.http.LocalHttpCredentials
import io.ashdavies.playground.Event
import io.ashdavies.playground.EventsQueries
import io.ashdavies.playground.MultipleReferenceWarning
Expand All @@ -20,7 +20,7 @@ private const val DEFAULT_PAGE_SIZE = 10
@MultipleReferenceWarning
internal fun rememberEventPager(
eventsQueries: EventsQueries = rememberPlaygroundDatabase().eventsQueries,
eventsApi: EventsApi = remember { buildApi(::EventsApi) },
eventsApi: EventsApi = rememberEventsApi(),
initialKey: String = todayAsString(),
pageSize: Int = DEFAULT_PAGE_SIZE,
): Pager<String, Event> = remember(eventsQueries, eventsApi) {
Expand All @@ -31,3 +31,10 @@ internal fun rememberEventPager(
pagingSourceFactory = { EventsPagingSource(eventsQueries) },
)
}

@Composable
private fun rememberEventsApi(
apiKey: String = LocalHttpCredentials.current.apiKey,
): EventsApi = remember(apiKey) {
EventsApi().apply { setApiKey(apiKey) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,17 @@ public val LocalFirebaseAndroidApp: ProvidableCompositionLocal<FirebaseApp> = st
}

@Composable
public fun ProvideFirebaseApp(context: Context = LocalContext.current, content: @Composable () -> Unit) {
public fun FirebaseApp(context: Context = LocalContext.current, content: @Composable () -> Unit) {
CompositionLocalProvider(
LocalFirebaseAndroidApp provides requireFirebaseApp(context),
content = content,
)
}

private fun requireFirebaseApp(context: Context): FirebaseApp {
val firebaseApp = if (FirebaseApp.getApps(context).size > 0) {
FirebaseApp.getInstance()
} else {
FirebaseApp.initializeApp(context)
val firebaseApp = when (FirebaseApp.getApps(context).isEmpty()) {
true -> FirebaseApp.initializeApp(context)
else -> FirebaseApp.getInstance()
}

return requireNotNull(firebaseApp) {
Expand Down

This file was deleted.

32 changes: 0 additions & 32 deletions http-client/src/androidMain/kotlin/io/ashdavies/http/System.kt

This file was deleted.

This file was deleted.

11 changes: 0 additions & 11 deletions http-client/src/commonMain/kotlin/io/ashdavies/http/Environment.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ public val LocalHttpClient: ProvidableCompositionLocal<HttpClient> = staticCompo
DefaultHttpClient { install(HttpCache) }
}

private val defaultUserAgent: String
get() = "${Software.clientName} (${Software.productName}; ${Software.buildVersion})"
public fun DefaultHttpClient(
credentials: HttpCredentials,
configure: HttpClientConfig<CIOEngineConfig>.() -> Unit = { },
): HttpClient = DefaultHttpClient {
install(DefaultRequest) { userAgent(credentials.userAgent) }
configure()
}

public fun DefaultHttpClient(
logLevel: LogLevel = LogLevel.INFO,
configure: HttpClientConfig<CIOEngineConfig>.() -> Unit = { },
): HttpClient = HttpClient(CIO) {
install(ContentNegotiation) {
Expand All @@ -42,12 +46,11 @@ public fun DefaultHttpClient(
install(DefaultRequest) {
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent(defaultUserAgent)
}

install(Logging) {
logger = DefaultLogger()
level = logLevel
level = LogLevel.INFO
}

configure()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.ashdavies.http

import androidx.compose.runtime.staticCompositionLocalOf

public val LocalHttpCredentials = staticCompositionLocalOf<HttpCredentials> {
error("CompositionLocal LocalHttpCredentials not present")
}

public data class HttpCredentials(
val apiKey: String,
val userAgent: String,
)

This file was deleted.

3 changes: 0 additions & 3 deletions http-client/src/commonMain/kotlin/io/ashdavies/http/System.kt

This file was deleted.

7 changes: 0 additions & 7 deletions http-client/src/jvmMain/kotlin/io/ashdavies/http/Software.kt

This file was deleted.

8 changes: 0 additions & 8 deletions http-client/src/jvmMain/kotlin/io/ashdavies/http/System.kt

This file was deleted.

0 comments on commit 951e1c5

Please sign in to comment.