Skip to content

Commit

Permalink
Initial pass at separating out a navigation-fragments module for all …
Browse files Browse the repository at this point in the history
…navigation-related code
  • Loading branch information
jayohms committed May 23, 2024
1 parent 1fa7f18 commit 10e84f2
Show file tree
Hide file tree
Showing 84 changed files with 475 additions and 247 deletions.
16 changes: 7 additions & 9 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ val developerEmail by extra("[email protected]")
android {
namespace = "dev.hotwire.core"
compileSdk = 34

testOptions.unitTests.isIncludeAndroidResources = true
testOptions.unitTests.isReturnDefaultValues = true
testOptions.targetSdk = 34

defaultConfig {
minSdk = 28
targetSdk = 34
}

buildTypes {
Expand Down Expand Up @@ -71,10 +72,10 @@ android {

dependencies {
// Kotlin
implementation("org.jetbrains.kotlin:kotlin-reflect:1.9.22")
implementation("org.jetbrains.kotlin:kotlin-reflect:1.9.23")

// Material
implementation("com.google.android.material:material:1.11.0")
implementation("com.google.android.material:material:1.12.0")

// AndroidX
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
Expand All @@ -86,19 +87,16 @@ dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")

// Networking/API
implementation("com.squareup.okhttp3:okhttp:4.11.0")
implementation("com.squareup.okhttp3:logging-interceptor:4.11.0")
implementation("com.squareup.okhttp3:okhttp:4.12.0")
implementation("com.squareup.okhttp3:logging-interceptor:4.12.0")

// Coroutines
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")

// Browser
implementation("androidx.browser:browser:1.7.0")

// Exported AndroidX dependencies
api("androidx.appcompat:appcompat:1.6.1")
api("androidx.core:core-ktx:1.12.0")
api("androidx.core:core-ktx:1.13.1")
api("androidx.webkit:webkit:1.8.0")
api("androidx.activity:activity-ktx:1.8.1")
api("androidx.fragment:fragment-ktx:1.6.2")
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/kotlin/dev/hotwire/core/bridge/Bridge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dev.hotwire.core.bridge
import android.webkit.JavascriptInterface
import android.webkit.WebView
import androidx.annotation.VisibleForTesting
import dev.hotwire.core.lib.logging.logEvent
import dev.hotwire.core.logging.logEvent
import kotlinx.serialization.json.JsonElement
import java.lang.ref.WeakReference

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.hotwire.core.bridge

import dev.hotwire.core.lib.logging.logWarning
import dev.hotwire.core.logging.logWarning

abstract class BridgeComponent(
val name: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import android.webkit.WebView
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import dev.hotwire.core.config.Hotwire
import dev.hotwire.core.lib.logging.logEvent
import dev.hotwire.core.lib.logging.logWarning
import dev.hotwire.core.turbo.nav.HotwireNavDestination
import dev.hotwire.core.logging.logEvent
import dev.hotwire.core.logging.logWarning
import dev.hotwire.navigation.destinations.HotwireNavDestination

@Suppress("unused")
class BridgeDelegate(
val location: String,
val destination: HotwireNavDestination
val destination: dev.hotwire.navigation.destinations.HotwireNavDestination
) : DefaultLifecycleObserver {
internal var bridge: Bridge? = null
private var destinationIsActive: Boolean = false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.hotwire.core.bridge

import dev.hotwire.core.lib.logging.logError
import dev.hotwire.core.logging.logError
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.hotwire.core.bridge

import dev.hotwire.core.config.Hotwire
import dev.hotwire.core.lib.logging.logError
import dev.hotwire.core.logging.logError
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/kotlin/dev/hotwire/core/config/Hotwire.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import dev.hotwire.core.turbo.config.PathConfiguration
import kotlin.reflect.KClass

object Hotwire {
internal var registeredBridgeComponentFactories:
var registeredBridgeComponentFactories:
List<BridgeComponentFactory<BridgeComponent>> = emptyList()
private set

Expand Down
6 changes: 6 additions & 0 deletions core/src/main/kotlin/dev/hotwire/core/config/HotwireConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.webkit.WebView
import dev.hotwire.core.bridge.StradaJsonConverter
import dev.hotwire.core.turbo.http.TurboHttpClient
import dev.hotwire.core.turbo.http.TurboOfflineRequestHandler
import dev.hotwire.core.turbo.views.TurboWebView

class HotwireConfig internal constructor() {
Expand All @@ -14,6 +15,11 @@ class HotwireConfig internal constructor() {
*/
var jsonConverter: StradaJsonConverter? = null

/**
* Experimental: API may change, not ready for production use.
*/
var offlineRequestHandler: TurboOfflineRequestHandler? = null

/**
* Enables/disables debug logging. This should be disabled in production environments.
* Disabled by default.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package dev.hotwire.core.lib.logging
package dev.hotwire.core.logging

import android.util.Log
import dev.hotwire.core.config.Hotwire

internal object HotwireLog {
private const val DEFAULT_TAG = "Hotwire"
internal object CoreLog {
private const val DEFAULT_TAG = "Hotwire-Core"

private val debugEnabled get() = Hotwire.config.debugLoggingEnabled

Expand All @@ -26,20 +26,20 @@ internal object HotwireLog {
private const val PAD_END_LENGTH = 35

internal fun logEvent(event: String, details: String = "") {
HotwireLog.d("$event ".padEnd(PAD_END_LENGTH, '.') + " [$details]")
CoreLog.d("$event ".padEnd(PAD_END_LENGTH, '.') + " [$details]")
}

internal fun logEvent(event: String, attributes: List<Pair<String, Any>>) {
val description = attributes.joinToString(prefix = "[", postfix = "]", separator = ", ") {
"${it.first}: ${it.second}"
}
HotwireLog.d("$event ".padEnd(PAD_END_LENGTH, '.') + " $description")
CoreLog.d("$event ".padEnd(PAD_END_LENGTH, '.') + " $description")
}

internal fun logWarning(event: String, details: String) {
HotwireLog.w("$event ".padEnd(PAD_END_LENGTH, '.') + " [$details]")
CoreLog.w("$event ".padEnd(PAD_END_LENGTH, '.') + " [$details]")
}

internal fun logError(event: String, error: Exception) {
HotwireLog.e("$event: ${error.stackTraceToString()}")
CoreLog.e("$event: ${error.stackTraceToString()}")
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.net.Uri
import androidx.core.net.toUri
import com.google.gson.annotations.SerializedName
import dev.hotwire.core.config.Hotwire
import dev.hotwire.core.turbo.nav.HotwireDestination
import dev.hotwire.navigation.destinations.HotwireDestination
import dev.hotwire.core.turbo.nav.TurboNavPresentation
import dev.hotwire.core.turbo.nav.TurboNavPresentationContext
import dev.hotwire.core.turbo.nav.TurboNavQueryStringPresentation
Expand Down Expand Up @@ -60,7 +60,7 @@ class PathConfiguration {
* Loads and parses the specified configuration file(s) from their local
* and/or remote locations.
*/
internal fun load(context: Context, location: Location) {
fun load(context: Context, location: Location) {
if (loader == null) {
loader = PathConfigurationLoader(context.applicationContext)
}
Expand Down Expand Up @@ -135,7 +135,7 @@ val PathConfigurationProperties.context: TurboNavPresentationContext

val PathConfigurationProperties.uri: Uri
get() = get("uri")?.toUri() ?:
HotwireDestination.from(Hotwire.defaultFragmentDestination).uri.toUri()
dev.hotwire.navigation.destinations.HotwireDestination.from(Hotwire.defaultFragmentDestination).uri.toUri()

val PathConfigurationProperties.fallbackUri: Uri?
get() = get("fallback_uri")?.toUri()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package dev.hotwire.core.turbo.config

import android.content.Context
import com.google.gson.reflect.TypeToken
import dev.hotwire.core.lib.logging.logError
import dev.hotwire.core.lib.logging.logEvent
import dev.hotwire.core.logging.logError
import dev.hotwire.core.logging.logEvent
import dev.hotwire.core.turbo.util.dispatcherProvider
import dev.hotwire.core.turbo.util.toObject
import kotlinx.coroutines.CoroutineScope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dev.hotwire.core.turbo.config
import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import dev.hotwire.core.lib.logging.logError
import dev.hotwire.core.logging.logError
import dev.hotwire.core.turbo.http.TurboHttpClient
import dev.hotwire.core.turbo.util.dispatcherProvider
import dev.hotwire.core.turbo.util.toJson
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package dev.hotwire.core.turbo.config

import com.google.gson.annotations.SerializedName
import dev.hotwire.core.BuildConfig
import dev.hotwire.core.lib.logging.logError
import dev.hotwire.core.logging.logError
import java.util.regex.PatternSyntaxException

internal data class PathConfigurationRule(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.content.Intent
import android.net.Uri
import android.provider.MediaStore
import android.webkit.WebChromeClient.FileChooserParams
import dev.hotwire.core.lib.logging.logError
import dev.hotwire.core.logging.logError
import dev.hotwire.core.turbo.util.TurboFileProvider
import java.io.File
import java.io.IOException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import android.webkit.ValueCallback
import android.webkit.WebChromeClient.FileChooserParams
import androidx.activity.result.ActivityResult
import dev.hotwire.core.R
import dev.hotwire.core.lib.logging.logError
import dev.hotwire.core.logging.logError
import dev.hotwire.core.turbo.session.Session
import dev.hotwire.core.turbo.util.TURBO_REQUEST_CODE_FILES
import dev.hotwire.core.turbo.util.TurboFileProvider
Expand All @@ -18,7 +18,7 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlin.coroutines.CoroutineContext

internal class TurboFileChooserDelegate(val session: Session) : CoroutineScope {
class TurboFileChooserDelegate(val session: Session) : CoroutineScope {
private val context: Context = session.context
private var uploadCallback: ValueCallback<Array<Uri>>? = null
private val browseFilesDelegate = TurboBrowseFilesDelegate(context)
Expand Down Expand Up @@ -66,7 +66,7 @@ internal class TurboFileChooserDelegate(val session: Session) : CoroutineScope {
}

private fun startIntent(intent: Intent): Boolean {
val destination = session.currentVisitNavDestination ?: return false
val destination = session.currentVisit?.callback?.visitDestination() ?: return false

return try {
destination.activityResultLauncher(TURBO_REQUEST_CODE_FILES)?.launch(intent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package dev.hotwire.core.turbo.http

import android.content.Context
import dev.hotwire.core.config.Hotwire
import dev.hotwire.core.lib.logging.logError
import dev.hotwire.core.logging.logError
import okhttp3.Cache
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dev.hotwire.core.turbo.http
import android.webkit.CookieManager
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import dev.hotwire.core.lib.logging.logError
import dev.hotwire.core.logging.logError
import dev.hotwire.core.turbo.util.dispatcherProvider
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package dev.hotwire.core.turbo.http

import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import dev.hotwire.core.lib.logging.logEvent
import dev.hotwire.core.config.Hotwire
import dev.hotwire.core.logging.logEvent
import dev.hotwire.core.turbo.session.Session
import dev.hotwire.core.turbo.util.isHttpGetRequest

internal class TurboWebViewRequestInterceptor(val session: Session) {
private val offlineRequestHandler get() = session.offlineRequestHandler
private val offlineRequestHandler get() = Hotwire.config.offlineRequestHandler
private val httpRepository get() = session.httpRepository
private val currentVisit get() = session.currentVisit

Expand Down
Loading

0 comments on commit 10e84f2

Please sign in to comment.