Skip to content

Commit

Permalink
Merge branch 'main' into capacitor-v6
Browse files Browse the repository at this point in the history
  • Loading branch information
carlpoole committed Apr 23, 2024
2 parents 655fdda + 23c886a commit 9462e9e
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 12 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/publish-android-old.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Publish Native Android Library

on: workflow_dispatch

jobs:
publish-android:
runs-on: ubuntu-latest
permissions: write-all
steps:
- uses: actions/checkout@v2
- name: create local.properties file for app creds
env:
PORTALS_KEY: ${{ secrets.portals_key }}
run: echo portals_key=\"$PORTALS_KEY\" > ./local.properties
- name: set up JDK 17
uses: actions/setup-java@v2
with:
java-version: '17'
distribution: 'adopt'
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
- name: Grant execute permission for publishing script
run: chmod +x ./scripts/publish-android.sh
- name: Run publish script
working-directory: ./scripts
env:
ANDROID_OSSRH_USERNAME: ${{ secrets.ANDROID_OSSRH_USERNAME }}
ANDROID_OSSRH_PASSWORD: ${{ secrets.ANDROID_OSSRH_PASSWORD }}
ANDROID_SIGNING_KEY_ID: ${{ secrets.ANDROID_SIGNING_KEY_ID }}
ANDROID_SIGNING_PASSWORD: ${{ secrets.ANDROID_SIGNING_PASSWORD }}
ANDROID_SIGNING_KEY: ${{ secrets.ANDROID_SIGNING_KEY }}
ANDROID_SONATYPE_STAGING_PROFILE_ID: ${{ secrets.ANDROID_SONATYPE_STAGING_PROFILE_ID }}
run: ./publish-android.sh
4 changes: 1 addition & 3 deletions .github/workflows/publish-android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ on:
jobs:
publish-android:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
permissions: write-all
steps:
- uses: actions/checkout@v2
- name: create local.properties file for app creds
Expand Down
2 changes: 1 addition & 1 deletion IonicPortals/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ android {
dependencies {
implementation(kotlin("reflect"))

api("com.capacitorjs:core:6.0.0")
api("com.capacitorjs:core:[6.0.0,6.1.0)")
compileOnly("io.ionic:liveupdates:0.5.1")

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3")
Expand Down
68 changes: 68 additions & 0 deletions IonicPortals/src/main/kotlin/io/ionic/portals/DevConfiguration.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package io.ionic.portals

import android.content.Context
import com.getcapacitor.CapConfig

/**
* This class is used to load the server URL and Capacitor configuration from the assets folder when the app
* is being run in developer mode with the Portals CLI.
*/
object DevConfiguration {

/**
* Get the server URL for the given portal name from the developer mode assets folder.
*/
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
}

/**
* Get the Capacitor configuration for the given portal name from the developer mode assets folder.
*/
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
}
}
33 changes: 32 additions & 1 deletion IonicPortals/src/main/kotlin/io/ionic/portals/Portal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,23 @@ class Portal(val name: String) {
var startDir: String = ""
get() = if (field.isEmpty()) name else field

/**
* If the Portal should be loaded in development mode and look for a server URL.
*/
var devMode: Boolean = true

/**
* A LiveUpdate config, if live updates is being used.
*/
var liveUpdateConfig: LiveUpdate? = null
set(value) {
field = value
if (value != null) {
if(value.assetPath == null) {
value.assetPath = this.startDir
}
}
}

/**
* Whether to run a live update sync when the portal is added to the manager.
Expand Down Expand Up @@ -296,9 +309,10 @@ 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 = true

internal constructor(name: String, onCreate: (portal: Portal) -> Unit) : this(name) {
this.onCreate = onCreate;
this.onCreate = onCreate
}

/**
Expand Down Expand Up @@ -534,6 +548,10 @@ class PortalBuilder(val name: String) {
@JvmOverloads
fun setLiveUpdateConfig(context: Context, liveUpdateConfig: LiveUpdate, updateOnAppLoad: Boolean = true): PortalBuilder {
this.liveUpdateConfig = liveUpdateConfig
if(liveUpdateConfig.assetPath == null) {
liveUpdateConfig.assetPath = this._startDir ?: this.name
}

LiveUpdateManager.initialize(context)
LiveUpdateManager.cleanVersions(context, liveUpdateConfig.appId)
LiveUpdateManager.addLiveUpdateInstance(context, liveUpdateConfig)
Expand All @@ -543,6 +561,18 @@ class PortalBuilder(val name: String) {
return this
}

/**
* 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
return this
}

/**
* Creates the [Portal] instance from the current state of the [PortalBuilder] provided.
* This finishes building the Portal.
Expand All @@ -568,6 +598,7 @@ class PortalBuilder(val name: String) {
portal.initialContext = this.initialContext
portal.portalFragmentType = this.portalFragmentType
portal.liveUpdateConfig = this.liveUpdateConfig
portal.devMode = this.devMode
onCreate(portal)
return portal
}
Expand Down
31 changes: 29 additions & 2 deletions IonicPortals/src/main/kotlin/io/ionic/portals/PortalFragment.kt
Original file line number Diff line number Diff line change
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 @@ -15,6 +16,7 @@ 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 @@ -282,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 @@ -326,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 @@ -449,4 +476,4 @@ open class PortalFragment : Fragment {
}
}
}
}
}
15 changes: 11 additions & 4 deletions IonicPortals/src/main/kotlin/io/ionic/portals/PortalView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.getcapacitor.Bridge
import com.getcapacitor.Logger
import java.util.ArrayList

/**
Expand Down Expand Up @@ -191,12 +192,18 @@ class PortalView : FrameLayout {
attrs?.let { attributeSet ->
portalFragment?.onInflate(context, attributeSet, null)
}

val handler = Handler()
val runnable = Runnable {
fmTransaction
.setReorderingAllowed(true)
.add(id, portalFragment!!, "")
.commitNowAllowingStateLoss()
val thisView = findViewById<PortalView>(id)
if(thisView != null) {
fmTransaction
.setReorderingAllowed(true)
.add(id, portalFragment!!, "")
.commitNowAllowingStateLoss()
} else {
Logger.warn("PortalView", "Unable to find active PortalView with id: $id. Skipping Portal inflation.")
}
}

handler.post(runnable)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ionic-portals-android",
"version": "0.8.3",
"version": "0.9.0",
"description": "Ionic Portals",
"homepage": "https://ionic.io/portals",
"author": "Ionic Team <hi@ionic.io> (https://ionic.io)",
Expand Down

0 comments on commit 9462e9e

Please sign in to comment.