Skip to content

Commit

Permalink
Update the Hotwire config namespace so the core module has access to …
Browse files Browse the repository at this point in the history
…the global configuration
  • Loading branch information
jayohms committed May 23, 2024
1 parent 10e84f2 commit 63d9e2b
Show file tree
Hide file tree
Showing 18 changed files with 68 additions and 68 deletions.
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 @@ -120,7 +120,7 @@ class Bridge internal constructor(webView: WebView) {
companion object {
private val instances = mutableListOf<Bridge>()

internal fun initialize(webView: WebView) {
fun initialize(webView: WebView) {
if (getBridgeFor(webView) == null) {
initialize(Bridge(webView))
}
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.config.Hotwire
import dev.hotwire.core.config.HotwireCore
import dev.hotwire.core.logging.logError
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
Expand All @@ -15,7 +15,7 @@ abstract class StradaJsonConverter {
"or use the provided KotlinXJsonConverter."

inline fun <reified T> toObject(jsonData: String): T? {
val converter = requireNotNull(Hotwire.config.jsonConverter) { NO_CONVERTER }
val converter = requireNotNull(HotwireCore.config.jsonConverter) { NO_CONVERTER }

return when (converter) {
is KotlinXJsonConverter -> converter.toObject<T>(jsonData)
Expand All @@ -25,7 +25,7 @@ abstract class StradaJsonConverter {
}

inline fun <reified T> toJson(data: T): String {
val converter = requireNotNull(Hotwire.config.jsonConverter) { NO_CONVERTER }
val converter = requireNotNull(HotwireCore.config.jsonConverter) { NO_CONVERTER }

return when (converter) {
is KotlinXJsonConverter -> converter.toJson(data)
Expand Down
14 changes: 14 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 @@ -3,11 +3,25 @@ package dev.hotwire.core.config
import android.content.Context
import android.webkit.WebView
import dev.hotwire.core.bridge.StradaJsonConverter
import dev.hotwire.core.turbo.config.PathConfiguration
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() {
/**
* The path configuration that defines your navigation rules.
*/
val pathConfiguration = PathConfiguration()

/**
* Loads the [PathConfiguration] JSON file(s) from the provided location to
* configure navigation rules.
*/
fun loadPathConfiguration(context: Context, location: PathConfiguration.Location) {
pathConfiguration.load(context, location)
}

/**
* Set a custom JSON converter to easily decode Message.dataJson to a data
* object in received messages and to encode a data object back to json to
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/kotlin/dev/hotwire/core/config/HotwireCore.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.hotwire.core.config

object HotwireCore {
val config: HotwireConfig = HotwireConfig()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import android.content.Context
import android.net.Uri
import androidx.core.net.toUri
import com.google.gson.annotations.SerializedName
import dev.hotwire.core.config.Hotwire
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 @@ -133,9 +131,8 @@ val PathConfigurationProperties.context: TurboNavPresentationContext
TurboNavPresentationContext.DEFAULT
}

val PathConfigurationProperties.uri: Uri
get() = get("uri")?.toUri() ?:
dev.hotwire.navigation.destinations.HotwireDestination.from(Hotwire.defaultFragmentDestination).uri.toUri()
val PathConfigurationProperties.uri: Uri?
get() = get("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,13 +2,13 @@ package dev.hotwire.core.turbo.http

import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import dev.hotwire.core.config.Hotwire
import dev.hotwire.core.config.HotwireCore
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() = Hotwire.config.offlineRequestHandler
private val offlineRequestHandler get() = HotwireCore.config.offlineRequestHandler
private val httpRepository get() = session.httpRepository
private val currentVisit get() = session.currentVisit

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.webkit.WebViewCompat
import androidx.webkit.WebViewFeature.VISUAL_STATE_CALLBACK
import androidx.webkit.WebViewFeature.isFeatureSupported
import dev.hotwire.core.config.Hotwire
import dev.hotwire.core.config.HotwireCore
import dev.hotwire.core.logging.logEvent
import dev.hotwire.core.turbo.delegates.TurboFileChooserDelegate
import dev.hotwire.core.turbo.errors.HttpError
Expand Down Expand Up @@ -92,7 +93,7 @@ class Session(
* @param location Location to cache.
*/
fun preCacheLocation(location: String) {
val requestHandler = checkNotNull(Hotwire.config.offlineRequestHandler) {
val requestHandler = checkNotNull(HotwireCore.config.offlineRequestHandler) {
"An offline request handler must be provided to pre-cache $location"
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package dev.hotwire.core.turbo.util

import android.animation.ArgbEvaluator
import android.animation.ValueAnimator
import android.content.Context
import android.os.Handler
import android.util.TypedValue
import android.webkit.WebResourceRequest
import androidx.annotation.AttrRes
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import androidx.navigation.NavBackStackEntry
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import dev.hotwire.core.R
import dev.hotwire.core.turbo.visit.VisitAction
import dev.hotwire.core.turbo.visit.VisitActionAdapter
import java.io.File

internal fun WebResourceRequest.isHttpGetRequest(): Boolean {
return method.equals("GET", ignoreCase = true) &&
url.scheme?.startsWith("HTTP", ignoreCase = true) == true
}

internal fun Context.runOnUiThread(func: () -> Unit) {
when (mainLooper.isCurrentThread) {
true -> func()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import androidx.webkit.WebSettingsCompat
import androidx.webkit.WebViewCompat
import androidx.webkit.WebViewFeature
import com.google.gson.GsonBuilder
import dev.hotwire.core.config.Hotwire
import dev.hotwire.core.config.HotwireCore
import dev.hotwire.core.turbo.util.contentFromAsset
import dev.hotwire.core.turbo.util.runOnUiThread
import dev.hotwire.core.turbo.util.toJson
Expand All @@ -39,7 +39,7 @@ open class TurboWebView @JvmOverloads constructor(
id = View.generateViewId()
settings.javaScriptEnabled = true
settings.domStorageEnabled = true
settings.userAgentString = "${Hotwire.config.userAgent} ${settings.userAgentString}"
settings.userAgentString = "${HotwireCore.config.userAgent} ${settings.userAgentString}"
settings.setSupportMultipleWindows(true)
layoutParams = FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)
initDayNightTheming()
Expand Down
8 changes: 4 additions & 4 deletions demo/src/main/kotlin/dev/hotwire/demo/DemoApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import android.app.Application
import dev.hotwire.core.BuildConfig
import dev.hotwire.core.bridge.BridgeComponentFactory
import dev.hotwire.core.bridge.KotlinXJsonConverter
import dev.hotwire.core.config.Hotwire
import dev.hotwire.core.navigation.routing.AppNavigationRouteDecisionHandler
import dev.hotwire.core.navigation.routing.BrowserTabRouteDecisionHandler
import dev.hotwire.core.turbo.config.PathConfiguration
import dev.hotwire.demo.bridge.FormComponent
import dev.hotwire.demo.bridge.MenuComponent
Expand All @@ -18,6 +15,9 @@ import dev.hotwire.demo.features.web.WebBottomSheetFragment
import dev.hotwire.demo.features.web.WebFragment
import dev.hotwire.demo.features.web.WebHomeFragment
import dev.hotwire.demo.features.web.WebModalFragment
import dev.hotwire.navigation.config.Hotwire
import dev.hotwire.navigation.routing.AppNavigationRouteDecisionHandler
import dev.hotwire.navigation.routing.BrowserTabRouteDecisionHandler

class DemoApplication : Application() {
override fun onCreate() {
Expand All @@ -31,7 +31,7 @@ class DemoApplication : Application() {
Hotwire.config.webViewDebuggingEnabled = BuildConfig.DEBUG

// Loads the path configuration
Hotwire.loadPathConfiguration(
Hotwire.config.loadPathConfiguration(
context = this,
location = PathConfiguration.Location(
assetFilePath = "json/configuration.json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package dev.hotwire.core.config
package dev.hotwire.navigation.config

import android.content.Context
import androidx.fragment.app.Fragment
import dev.hotwire.core.bridge.BridgeComponent
import dev.hotwire.core.bridge.BridgeComponentFactory
import dev.hotwire.core.navigation.fragments.HotwireWebBottomSheetFragment
import dev.hotwire.core.navigation.fragments.HotwireWebFragment
import dev.hotwire.core.navigation.routing.AppNavigationRouteDecisionHandler
import dev.hotwire.core.navigation.routing.BrowserRouteDecisionHandler
import dev.hotwire.core.navigation.routing.Router
import dev.hotwire.core.turbo.config.PathConfiguration
import dev.hotwire.core.config.HotwireConfig
import dev.hotwire.core.config.HotwireCore
import dev.hotwire.navigation.fragments.HotwireWebBottomSheetFragment
import dev.hotwire.navigation.fragments.HotwireWebFragment
import dev.hotwire.navigation.routing.AppNavigationRouteDecisionHandler
import dev.hotwire.navigation.routing.BrowserRouteDecisionHandler
import dev.hotwire.navigation.routing.Router
import kotlin.reflect.KClass

object Hotwire {
var registeredBridgeComponentFactories:
val config: HotwireConfig = HotwireCore.config

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

Expand All @@ -29,21 +31,6 @@ object Hotwire {
BrowserRouteDecisionHandler()
))

val config: HotwireConfig = HotwireConfig()

/**
* The path configuration that defines your navigation rules.
*/
val pathConfiguration = PathConfiguration()

/**
* Loads the [PathConfiguration] JSON file(s) from the provided location to
* configure navigation rules.
*/
fun loadPathConfiguration(context: Context, location: PathConfiguration.Location) {
pathConfiguration.load(context, location)
}

/**
* Registers the [Router.RouteDecisionHandler] instances that determine whether to route location
* urls within in-app navigation or with alternative custom behaviors.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import androidx.fragment.app.Fragment
import androidx.navigation.NavOptions
import androidx.navigation.navOptions
import dev.hotwire.core.R
import dev.hotwire.core.config.Hotwire
import dev.hotwire.core.config.Hotwire.pathConfiguration
import dev.hotwire.core.turbo.config.PathConfigurationProperties
import dev.hotwire.core.turbo.config.context
import dev.hotwire.core.turbo.nav.TurboNavPresentationContext
import dev.hotwire.core.turbo.visit.VisitAction
import dev.hotwire.navigation.config.Hotwire
import dev.hotwire.navigation.fragments.HotwireFragmentDelegate
import dev.hotwire.navigation.fragments.HotwireFragmentViewModel
import dev.hotwire.navigation.navigator.Navigator
Expand Down Expand Up @@ -46,7 +45,7 @@ interface HotwireNavDestination {
* destination.
*/
val pathProperties: PathConfigurationProperties
get() = pathConfiguration.properties(location)
get() = Hotwire.config.pathConfiguration.properties(location)

/**
* Gets the [HotwireFragmentViewModel] associated with this destination.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import androidx.navigation.NavController
import androidx.navigation.NavOptions
import androidx.navigation.fragment.FragmentNavigator
import dev.hotwire.core.bridge.Bridge
import dev.hotwire.core.config.Hotwire
import dev.hotwire.core.config.Hotwire.pathConfiguration
import dev.hotwire.core.turbo.nav.TurboNavPresentation
import dev.hotwire.core.turbo.nav.TurboNavPresentationContext
import dev.hotwire.core.turbo.session.Session
import dev.hotwire.core.turbo.visit.VisitAction
import dev.hotwire.core.turbo.visit.VisitOptions
import dev.hotwire.navigation.config.Hotwire
import dev.hotwire.navigation.destinations.HotwireNavDestination
import dev.hotwire.navigation.destinations.HotwireNavDialogDestination
import dev.hotwire.navigation.logging.logEvent
Expand Down Expand Up @@ -108,7 +107,7 @@ class Navigator(
bundle = bundle,
navOptions = navOptions(location, options.action),
extras = extras,
pathConfiguration = pathConfiguration,
pathConfiguration = Hotwire.config.pathConfiguration,
controller = currentControllerForLocation(location)
)

Expand Down Expand Up @@ -365,7 +364,7 @@ class Navigator(
}

private fun navOptions(location: String, action: VisitAction): NavOptions {
val properties = pathConfiguration.properties(location)
val properties = Hotwire.config.pathConfiguration.properties(location)

return currentDestination.getNavigationOptions(
newLocation = location,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.navigation.fragment.FragmentNavigator
import androidx.navigation.fragment.FragmentNavigatorDestinationBuilder
import dev.hotwire.core.turbo.config.PathConfiguration
import dev.hotwire.core.turbo.config.uri
import dev.hotwire.navigation.config.Hotwire
import dev.hotwire.navigation.destinations.HotwireDestination
import java.util.UUID
import kotlin.reflect.KClass
Expand Down Expand Up @@ -88,7 +89,9 @@ internal class NavigatorGraphBuilder(
}

private fun List<FragmentDestination>.startDestination(): FragmentDestination {
val startDestinationUri = pathConfiguration.properties(startLocation).uri
val startDestinationUri = pathConfiguration.properties(startLocation).uri ?:
HotwireDestination.from(Hotwire.defaultFragmentDestination).uri.toUri()

return requireNotNull(firstOrNull { it.uri == startDestinationUri }) {
"A start Fragment destination was not found for uri: $startDestinationUri"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package dev.hotwire.navigation.navigator
import android.os.Bundle
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.fragment.findNavController
import dev.hotwire.core.config.Hotwire
import dev.hotwire.core.config.Hotwire.pathConfiguration
import dev.hotwire.navigation.activities.HotwireActivity
import dev.hotwire.navigation.config.Hotwire

open class NavigatorHost : NavHostFragment() {
internal lateinit var activity: HotwireActivity
Expand All @@ -31,7 +30,7 @@ open class NavigatorHost : NavHostFragment() {
navController.apply {
graph = NavigatorGraphBuilder(
startLocation = configuration.startLocation,
pathConfiguration = pathConfiguration,
pathConfiguration = Hotwire.config.pathConfiguration,
navController = findNavController()
).build(
registeredFragments = Hotwire.registeredFragmentDestinations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.hotwire.navigation.navigator

import android.net.Uri
import android.os.Bundle
import androidx.core.net.toUri
import androidx.core.os.bundleOf
import androidx.navigation.NavController
import androidx.navigation.NavDestination
Expand All @@ -14,6 +15,8 @@ import dev.hotwire.core.turbo.nav.TurboNavPresentationContext
import dev.hotwire.core.turbo.nav.TurboNavQueryStringPresentation
import dev.hotwire.core.turbo.visit.VisitAction
import dev.hotwire.core.turbo.visit.VisitOptions
import dev.hotwire.navigation.config.Hotwire
import dev.hotwire.navigation.destinations.HotwireDestination
import dev.hotwire.navigation.session.SessionModalResult
import dev.hotwire.navigation.util.location

Expand All @@ -27,6 +30,8 @@ internal class NavigatorRule(
pathConfiguration: PathConfiguration,
val controller: NavController
) {
val defaultUri = HotwireDestination.from(Hotwire.defaultFragmentDestination).uri.toUri()

// Current destination
val previousLocation = controller.previousBackStackEntry.location
val currentLocation = checkNotNull(controller.currentBackStackEntry.location)
Expand All @@ -45,7 +50,7 @@ internal class NavigatorRule(
val newPresentation = newPresentation()
val newNavigationMode = newNavigationMode()
val newModalResult = newModalResult()
val newDestinationUri = newProperties.uri
val newDestinationUri = newProperties.uri ?: defaultUri
val newFallbackUri = newProperties.fallbackUri
val newDestination = controller.destinationFor(newDestinationUri)
val newFallbackDestination = controller.destinationFor(newFallbackUri)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.animation.ArgbEvaluator
import android.animation.ValueAnimator
import android.content.Context
import android.util.TypedValue
import android.webkit.WebResourceRequest
import androidx.annotation.AttrRes
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
Expand All @@ -22,11 +21,6 @@ fun Toolbar.displayBackButtonAsCloseIcon() {
internal val NavBackStackEntry?.location: String?
get() = this?.arguments?.getString("location")

internal fun WebResourceRequest.isHttpGetRequest(): Boolean {
return method.equals("GET", ignoreCase = true) &&
url.scheme?.startsWith("HTTP", ignoreCase = true) == true
}

internal fun Context.colorFromThemeAttr(
@AttrRes attrColor: Int,
typedValue: TypedValue = TypedValue(),
Expand Down

0 comments on commit 63d9e2b

Please sign in to comment.