Skip to content

Commit

Permalink
Add onDestroy to essenty lifecycle on IOS
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedRejeb committed Mar 12, 2023
1 parent 95b440b commit a079144
Show file tree
Hide file tree
Showing 26 changed files with 48 additions and 3,651 deletions.
13 changes: 3 additions & 10 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
plugins {
//trick: for the same plugin versions in all sub-modules
kotlin("android") version "1.8.0" apply false
kotlin("multiplatform") version "1.8.0" apply false
kotlin("plugin.serialization") version "1.8.0" apply false
kotlin("android") version "1.8.10" apply false
kotlin("multiplatform") version "1.8.10" apply false
kotlin("plugin.serialization") version "1.8.10" apply false
id("app.cash.sqldelight") version "2.0.0-alpha05" apply false
id("com.android.application") version "7.3.0" apply false
id("com.android.library") version "7.3.0" apply false
Expand All @@ -15,11 +15,4 @@ allprojects {
mavenCentral()
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
}

afterEvaluate {
// Workaround for https://youtrack.jetbrains.com/issue/KT-52776
rootProject.extensions.findByType<org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension>()?.apply {
versions.webpackCli.version = "4.10.0"
}
}
}
4 changes: 2 additions & 2 deletions buildSrc/src/main/java/com/mocoding/pokedex/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package com.mocoding.pokedex

object Versions {
const val gradlePlugin = "7.3.0"
const val kotlin = "1.8.0"
const val kotlin = "1.8.10"
const val compose = "1.3.1"
const val composeCompoiler = "1.4.1"
const val composeCompoiler = "1.4.2"
const val coroutines = "1.6.4"
const val serialization = "1.4.1"
const val mviKotlin = "3.1.0"
Expand Down
7 changes: 1 addition & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ kotlin.mpp.androidSourceSetLayoutVersion=2
xcodeproj=./ios
org.jetbrains.compose.experimental.uikit.enabled=true

#Web
org.jetbrains.compose.experimental.jscanvas.enabled=true

#Versions
kotlin.version=1.8.10
agp.version=7.3.0
Expand All @@ -30,6 +27,4 @@ kotlin.native.binary.memoryModel=experimental

kotlin.native.cocoapods.generate.wrapper=true
kotlin.native.cacheKind=none
kotlin.native.useEmbeddableCompilerJar=true
#kotlin.native.enableDependencyPropagation=false
#kotlin.mpp.enableGranularSourceSetsMetadata=true
kotlin.native.useEmbeddableCompilerJar=true
4 changes: 4 additions & 0 deletions ios/Pokedex.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
EA2CADBC29BC9EC20007AE62 /* PokedexUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA2CADBB29BC9EC20007AE62 /* PokedexUITests.swift */; };
EA2CADBE29BC9EC20007AE62 /* PokedexUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA2CADBD29BC9EC20007AE62 /* PokedexUITestsLaunchTests.swift */; };
EA2CADCC29BCA7DB0007AE62 /* ComposeViewControllerToSwiftUi.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA2CADCB29BCA7DB0007AE62 /* ComposeViewControllerToSwiftUi.swift */; };
EA2CADD029BDF5470007AE62 /* LifecycleHolder.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA2CADCF29BDF5470007AE62 /* LifecycleHolder.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -49,6 +50,7 @@
EA2CADBD29BC9EC20007AE62 /* PokedexUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PokedexUITestsLaunchTests.swift; sourceTree = "<group>"; };
EA2CADCA29BCA63F0007AE62 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
EA2CADCB29BCA7DB0007AE62 /* ComposeViewControllerToSwiftUi.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComposeViewControllerToSwiftUi.swift; sourceTree = "<group>"; };
EA2CADCF29BDF5470007AE62 /* LifecycleHolder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LifecycleHolder.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -124,6 +126,7 @@
EA2CADA429BC9EC20007AE62 /* Assets.xcassets */,
EA2CADA629BC9EC20007AE62 /* Preview Content */,
EA2CADCB29BCA7DB0007AE62 /* ComposeViewControllerToSwiftUi.swift */,
EA2CADCF29BDF5470007AE62 /* LifecycleHolder.swift */,
);
path = Pokedex;
sourceTree = "<group>";
Expand Down Expand Up @@ -309,6 +312,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
EA2CADD029BDF5470007AE62 /* LifecycleHolder.swift in Sources */,
EA2CADCC29BCA7DB0007AE62 /* ComposeViewControllerToSwiftUi.swift in Sources */,
EA2CADA129BC9EC10007AE62 /* PokedexApp.swift in Sources */,
);
Expand Down
5 changes: 4 additions & 1 deletion ios/Pokedex/ComposeViewControllerToSwiftUi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ import SwiftUI
import shared

struct ComposeViewControllerToSwiftUI: UIViewControllerRepresentable {
private let lifecycle: LifecycleRegistry
private let topSafeArea: Float
private let bottomSafeArea: Float

init(topSafeArea: Float, bottomSafeArea: Float) {
init(lifecycle: LifecycleRegistry, topSafeArea: Float, bottomSafeArea: Float) {
self.lifecycle = lifecycle
self.topSafeArea = topSafeArea
self.bottomSafeArea = bottomSafeArea
}

func makeUIViewController(context: Context) -> UIViewController {
return Main_iosKt.MainViewController(
lifecycle: lifecycle,
topSafeArea: topSafeArea,
bottomSafeArea: bottomSafeArea
)
Expand Down
22 changes: 22 additions & 0 deletions ios/Pokedex/LifecycleHolder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// LifecycleHolder.swift
// Pokedex
//
// Created by Mohamed Ben Rejeb on 12/3/2023.
//

import shared

class LifecycleHolder : ObservableObject {
let lifecycle: LifecycleRegistry

init() {
lifecycle = LifecycleRegistryKt.LifecycleRegistry()

lifecycle.onCreate()
}

deinit {
lifecycle.onDestroy()
}
}
18 changes: 3 additions & 15 deletions ios/Pokedex/PokedexApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
// Created by Mohamed Ben Rejeb on 11/3/2023.
//

import SwiftUI
import SwiftUI
import shared


@main
struct PokedexApp: App {
@Environment(\.scenePhase) private var scenePhase

private let lifecycle = LifecycleRegistry()
private var lifecycleHolder: LifecycleHolder { LifecycleHolder() }

var body: some Scene {
WindowGroup {
GeometryReader { geo in
ComposeViewControllerToSwiftUI(
lifecycle: lifecycleHolder.lifecycle,
topSafeArea: Float(geo.safeAreaInsets.top),
bottomSafeArea: Float(geo.safeAreaInsets.bottom)
)
Expand All @@ -27,18 +27,6 @@ struct PokedexApp: App {
// Hide keyboard on tap outside of TextField
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
.onChange(of: scenePhase) { phase in
switch phase {
case .active:
print(">> your code is here on scene become active")
case .inactive:
print(">> your code is here on scene become inactive")
case .background:
print(">> your code is here on scene go background")
default:
print(">> do something else in future")
}
}
}
}
}
Expand Down
Loading

0 comments on commit a079144

Please sign in to comment.