Skip to content

Commit

Permalink
polished dev mode and tested with cli
Browse files Browse the repository at this point in the history
  • Loading branch information
carlpoole committed Mar 12, 2024
1 parent 6a2a34b commit d1eccf7
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 9 deletions.
62 changes: 62 additions & 0 deletions IonicPortals/src/main/kotlin/io/ionic/portals/DevConfiguration.kt
@@ -0,0 +1,62 @@
package io.ionic.portals

import android.annotation.SuppressLint
import android.content.Context
import com.getcapacitor.CapConfig
import com.getcapacitor.Logger

object DevConfiguration {

@SuppressLint("PrivateApi")
fun getServerUrl(context: Context, portalName: String): String? {
val portalDirName = "$portalName.debug"
val generalDirName = "portal.debug"
val urlFileName = "url"

val assetManager = context.assets
var serverUrl = try {
assetManager.open("$portalDirName/$urlFileName").bufferedReader().use {
it.readText()
}
} catch (e: Exception) {
null
}

if (serverUrl == null) {
serverUrl = try {
assetManager.open("$generalDirName/$urlFileName").bufferedReader().use {
it.readText()
}
} catch (e: Exception) {
null
}
}

return serverUrl
}

@SuppressLint("PrivateApi")
fun getCapacitorConfig(context: Context, portalName: String): CapConfig? {
val portalDirName = "$portalName.debug"
val generalDirName = "portal.debug"
val capConfigFileName = "capacitor.config.json"

var serverConfig = try {
val configFile = context.assets.open("$portalDirName/$capConfigFileName")
CapConfig.loadFromAssets(context, portalDirName)
} catch (e: Exception) {
null
}

if (serverConfig == null) {
serverConfig = try {
val configFile = context.assets.open("$generalDirName/$capConfigFileName")
CapConfig.loadFromAssets(context, generalDirName)
} catch (e: Exception) {
null
}
}

return serverConfig
}
}
6 changes: 5 additions & 1 deletion IonicPortals/src/main/kotlin/io/ionic/portals/Portal.kt
Expand Up @@ -301,7 +301,7 @@ class PortalBuilder(val name: String) {
private var portalFragmentType: Class<out PortalFragment?> = PortalFragment::class.java
private var onCreate: (portal: Portal) -> Unit = {}
private var liveUpdateConfig: LiveUpdate? = null
private var devMode: Boolean = false
private var devMode: Boolean = true

internal constructor(name: String, onCreate: (portal: Portal) -> Unit) : this(name) {
this.onCreate = onCreate
Expand Down Expand Up @@ -551,6 +551,10 @@ class PortalBuilder(val name: String) {

/**
* Set development mode on the Portal which will look for a server URL set by the Portals CLI.
* This is set to true by default but can be turned off manually if desired.
*
* @param devMode if the Portal should be loaded in development mode
* @return the instance of the PortalBuilder with the development mode set
*/
fun setDevMode(devMode: Boolean): PortalBuilder {
this.devMode = devMode
Expand Down
37 changes: 30 additions & 7 deletions IonicPortals/src/main/kotlin/io/ionic/portals/PortalFragment.kt
Expand Up @@ -2,6 +2,7 @@ package io.ionic.portals

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.content.pm.ApplicationInfo
import android.content.res.Configuration
import android.os.Bundle
import android.view.LayoutInflater
Expand All @@ -11,11 +12,11 @@ import android.webkit.JavascriptInterface
import androidx.annotation.NonNull
import androidx.fragment.app.Fragment
import com.getcapacitor.*
import io.ionic.liveupdates.BuildConfig
import io.ionic.liveupdates.LiveUpdateManager
import org.json.JSONException
import org.json.JSONObject
import java.io.File
import java.lang.reflect.Field
import kotlin.reflect.KVisibility

/**
Expand Down Expand Up @@ -49,9 +50,6 @@ open class PortalFragment : Fragment {
private var pubSub = PortalsPubSub.shared
private var initialContext: Any? = null

private val devURL: String = "${context?.packageName}.${portal?.name}.url"
private val devConfig: String = "${context?.packageName}.${portal?.name}.config"

constructor()

constructor(portal: Portal?) {
Expand Down Expand Up @@ -238,7 +236,7 @@ open class PortalFragment : Fragment {
* If Live Updates is used and the web content was updated, the new content will be loaded.
*/
fun reload() {
if(portal?.devMode == false && portal?.liveUpdateConfig != null) {
if(portal?.liveUpdateConfig != null) {
val latestLiveUpdateFiles = LiveUpdateManager.getLatestAppDirectory(requireContext(), portal?.liveUpdateConfig?.appId!!)
if (latestLiveUpdateFiles != null) {
if (liveUpdateFiles == null || liveUpdateFiles!!.path != latestLiveUpdateFiles.path) {
Expand Down Expand Up @@ -286,7 +284,7 @@ open class PortalFragment : Fragment {
.setInstanceState(savedInstanceState)
.setPlugins(initialPlugins)
.addPluginInstances(initialPluginInstances)
.addWebViewListeners(webViewListeners);
.addWebViewListeners(webViewListeners)

if (portal?.liveUpdateConfig != null) {
liveUpdateFiles = LiveUpdateManager.getLatestAppDirectory(requireContext(), portal?.liveUpdateConfig?.appId!!)
Expand Down Expand Up @@ -330,6 +328,31 @@ open class PortalFragment : Fragment {
configToUse = CapConfig.Builder(requireContext()).setInitialFocus(false).create()
}

// Dev mode
val isDebuggable = 0 != requireContext().applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE
if (isDebuggable && portal?.devMode == true) {
val devConfig = DevConfiguration.getCapacitorConfig(requireContext(), portal?.name!!)
if (devConfig != null) {
configToUse = devConfig
} else {
Logger.debug("No dev config set by Portals CLI for portal ${portal?.name}, loading the non-debug config")
}

val devUrl = DevConfiguration.getServerUrl(requireContext(), portal?.name!!)
if (devUrl != null && configToUse != null) {
val devModeField: Field = configToUse.javaClass.getDeclaredField("serverUrl")
devModeField.isAccessible = true
devModeField.set(configToUse, devUrl)
} else {
val noDevUrlMsg = "No dev URL set by Portals CLI for portal ${portal?.name}"
if (devConfig != null && devConfig.serverUrl != null) {
Logger.debug("$noDevUrlMsg, using URL from dev config")
} else {
Logger.debug("$noDevUrlMsg, loading Portal from assets")
}
}
}

bridgeBuilder = bridgeBuilder.setConfig(configToUse)
bridge = bridgeBuilder.create()

Expand Down Expand Up @@ -453,4 +476,4 @@ open class PortalFragment : Fragment {
}
}
}
}
}
Expand Up @@ -203,7 +203,7 @@ class PortalView : FrameLayout {
.add(id, portalFragment!!, "")
.commitNowAllowingStateLoss()
} else {
Log.w("PortalView", "Unable to find active PortalView with id: $id. Skipping Portal inflation.")
Log.e("PortalView", "A problem occurred. Unable to find loaded container with id: $id. Skipping Portal inflation.")
}
}

Expand Down

0 comments on commit d1eccf7

Please sign in to comment.