Skip to content

Commit

Permalink
use a viewmodel or allow one to be set
Browse files Browse the repository at this point in the history
  • Loading branch information
carlpoole committed Apr 5, 2024
1 parent 23c886a commit 869bf11
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 11 deletions.
11 changes: 6 additions & 5 deletions IonicPortals/build.gradle.kts
Expand Up @@ -46,13 +46,14 @@ dependencies {
implementation(kotlin("reflect"))

api("com.capacitorjs:core:[5.5.0,5.8.0)")
compileOnly("io.ionic:liveupdates:0.5.0")
compileOnly("io.ionic:liveupdates:0.5.1")

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3")
implementation("androidx.core:core-ktx:1.10.0")
implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.fragment:fragment-ktx:1.6.2")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.8.0")
implementation("com.google.android.material:material:1.11.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.4")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.0")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
47 changes: 47 additions & 0 deletions IonicPortals/src/main/kotlin/io/ionic/portals/PortalFragment.kt
Expand Up @@ -11,6 +11,7 @@ import android.view.ViewGroup
import android.webkit.JavascriptInterface
import androidx.annotation.NonNull
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.getcapacitor.*
import io.ionic.liveupdates.LiveUpdateManager
import org.json.JSONException
Expand Down Expand Up @@ -50,17 +51,42 @@ open class PortalFragment : Fragment {
private var pubSub = PortalsPubSub.shared
private var initialContext: Any? = null

private var viewModel: PortalViewModel? = null

constructor()

constructor(portal: Portal?) {
this.portal = portal
}

constructor(portal: Portal?, viewModel: PortalViewModel) {
this.portal = portal
this.viewModel = viewModel
}

constructor(portal: Portal?, onBridgeAvailable: ((bridge: Bridge) -> Unit)?) {
this.portal = portal
this.onBridgeAvailable = onBridgeAvailable
}

constructor(portal: Portal?, viewModel: PortalViewModel, onBridgeAvailable: ((bridge: Bridge) -> Unit)?) : super() {
this.portal = portal
this.viewModel = viewModel
this.onBridgeAvailable = onBridgeAvailable
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
portal?.let { portal ->
viewModel?.let { viewModel ->
viewModel.state.value = portal
} ?: run {
viewModel = viewModels<PortalViewModel>().value
viewModel?.state?.value = portal
}
}
}

/**
* Extends the Android Fragment `onCreateView` lifecycle event.
*/
Expand All @@ -80,6 +106,9 @@ open class PortalFragment : Fragment {
*/
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel?.state?.value?.let {
portal = it
}
load(savedInstanceState)
}

Expand All @@ -99,6 +128,15 @@ open class PortalFragment : Fragment {
}
}

/**
* Extends the Android Fragment 'onStart' lifecycle event.
*/
override fun onStart() {
super.onStart()
bridge?.onStart()
Logger.debug("App started")
}

/**
* Extends the Android Fragment 'onResume' lifecycle event.
*/
Expand All @@ -118,6 +156,15 @@ open class PortalFragment : Fragment {
Logger.debug("App paused")
}

/**
* Extends the Android Fragment 'onStop' lifecycle event.
*/
override fun onStop() {
super.onStop()
bridge?.onStop()
Logger.debug("App stopped")
}

/**
* Extends the Android Fragment 'onSaveInstanceState' event.
*/
Expand Down
@@ -0,0 +1,8 @@
package io.ionic.portals

import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow

open class PortalViewModel: ViewModel() {
val state = MutableStateFlow<Portal?>(null)
}
9 changes: 5 additions & 4 deletions TestApp/build.gradle.kts
Expand Up @@ -9,7 +9,7 @@ plugins {

android {
namespace = "io.ionic.portals.testapp"
compileSdk = 33
compileSdk = 34

buildFeatures {
buildConfig = true
Expand All @@ -18,7 +18,7 @@ android {
defaultConfig {
applicationId = "io.ionic.portals.testapp"
minSdk = 24
targetSdk = 33
targetSdk = 34
versionCode = 1
versionName = "1.0"

Expand Down Expand Up @@ -50,9 +50,10 @@ androidComponents {

dependencies {
implementation(project(":IonicPortals"))
implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.fragment:fragment-ktx:1.6.2")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.9.0")
implementation("com.google.android.material:material:1.11.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
Expand Down
9 changes: 8 additions & 1 deletion TestApp/src/main/AndroidManifest.xml
Expand Up @@ -14,10 +14,17 @@
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ViewModelActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>

</manifest>
@@ -0,0 +1,7 @@
package io.ionic.portals.testapp

import io.ionic.portals.PortalViewModel

class MyPortalViewModel: PortalViewModel() {

}
@@ -0,0 +1,32 @@
package io.ionic.portals.testapp

import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.commit
import io.ionic.portals.PortalBuilder
import io.ionic.portals.PortalFragment

class ViewModelActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val viewModel: MyPortalViewModel by viewModels()

setContentView(R.layout.activity_viewmodel)
if (savedInstanceState == null) {
supportFragmentManager.commit {
setReorderingAllowed(true)
add(
R.id.fragmentContainerView,
PortalFragment(
portal = PortalBuilder("testportal").create()
),
"test"
)
}
}
}

}
12 changes: 12 additions & 0 deletions TestApp/src/main/res/layout/activity_viewmodel.xml
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
2 changes: 1 addition & 1 deletion build.gradle.kts
Expand Up @@ -27,7 +27,7 @@ buildscript {
}

classpath("org.jetbrains.dokka:dokka-base:1.7.20")
classpath("com.android.tools.build:gradle:8.1.1")
classpath("com.android.tools.build:gradle:8.1.4")
classpath("org.jetbrains.kotlin:kotlin-serialization:$kotlinVersion")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
}
Expand Down

0 comments on commit 869bf11

Please sign in to comment.