From b8db594dae7938dea0156fe4cab162422ac08709 Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Tue, 2 Jan 2024 10:30:02 +0800 Subject: [PATCH] refactor: merge to BetterAndroid --- .../src/androidMain/kotlin/MainActivity.kt | 4 +- gradle.properties | 1 + .../sweet-dependency-config.yaml | 11 +++++ iosApp/iosApp.xcodeproj/project.pbxproj | 14 ++---- iosApp/iosApp/AppDelegate.swift | 15 ++++++ iosApp/iosApp/ContentView.swift | 19 -------- iosApp/iosApp/iOSApp.swift | 10 ---- shared/build.gradle.kts | 5 ++ shared/src/commonMain/kotlin/App.kt | 48 +++++++++++++++---- shared/src/iosMain/kotlin/App.ios.kt | 4 +- 10 files changed, 80 insertions(+), 51 deletions(-) create mode 100644 iosApp/iosApp/AppDelegate.swift delete mode 100644 iosApp/iosApp/ContentView.swift delete mode 100644 iosApp/iosApp/iOSApp.swift diff --git a/androidApp/src/androidMain/kotlin/MainActivity.kt b/androidApp/src/androidMain/kotlin/MainActivity.kt index 606b083..5204109 100644 --- a/androidApp/src/androidMain/kotlin/MainActivity.kt +++ b/androidApp/src/androidMain/kotlin/MainActivity.kt @@ -2,10 +2,10 @@ package __GROUP_NAME__ import MainView import android.os.Bundle -import androidx.activity.ComponentActivity import androidx.activity.compose.setContent +import com.highcapable.betterandroid.ui.component.activity.AppComponentActivity -class MainActivity : ComponentActivity() { +class MainActivity : AppComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) diff --git a/gradle.properties b/gradle.properties index 3f7f1c4..ac7e6f0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,6 +4,7 @@ android.useAndroidX=true android.nonTransitiveRClass=true kotlin.code.style=official kotlin.incremental.useClasspathSnapshot=true +kotlin.mpp.applyDefaultHierarchyTemplate=false org.jetbrains.compose.experimental.uikit.enabled=true # Project Configuration project.name=__PROJECT_NAME__ diff --git a/gradle/sweet-dependency/sweet-dependency-config.yaml b/gradle/sweet-dependency/sweet-dependency-config.yaml index 32495c9..318f07d 100644 --- a/gradle/sweet-dependency/sweet-dependency-config.yaml +++ b/gradle/sweet-dependency/sweet-dependency-config.yaml @@ -27,6 +27,17 @@ plugins: version-ref: android-application libraries: + com.highcapable.betterandroid: + ui-component: + version: + + ui-extension: + version: + + system-extension: + version: + + compose-extension: + version: + + compose-multiplatform: + version: + androidx.activity: activity: version: + diff --git a/iosApp/iosApp.xcodeproj/project.pbxproj b/iosApp/iosApp.xcodeproj/project.pbxproj index c852042..3cc7489 100644 --- a/iosApp/iosApp.xcodeproj/project.pbxproj +++ b/iosApp/iosApp.xcodeproj/project.pbxproj @@ -9,16 +9,14 @@ /* Begin PBXBuildFile section */ 058557BB273AAA24004C7B11 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 058557BA273AAA24004C7B11 /* Assets.xcassets */; }; 058557D9273AAEEB004C7B11 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 058557D8273AAEEB004C7B11 /* Preview Assets.xcassets */; }; - 2152FB042600AC8F00CF470E /* iOSApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2152FB032600AC8F00CF470E /* iOSApp.swift */; }; - 7555FF83242A565900829871 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7555FF82242A565900829871 /* ContentView.swift */; }; + 508B32322B43A12E0061B326 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 508B32312B43A12E0061B326 /* AppDelegate.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ 058557BA273AAA24004C7B11 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 058557D8273AAEEB004C7B11 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; - 2152FB032600AC8F00CF470E /* iOSApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = iOSApp.swift; sourceTree = ""; }; - 7555FF7B242A565900829871 /* My application.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "My application.app"; sourceTree = BUILT_PRODUCTS_DIR; }; - 7555FF82242A565900829871 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; + 508B32312B43A12E0061B326 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; + 7555FF7B242A565900829871 /* My application.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; name = "My application.app"; path = "compose-multiplatform-demo.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 7555FF8C242A565B00829871 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; AB3632DC29227652001CCB65 /* Config.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = Config.xcconfig; sourceTree = ""; }; /* End PBXFileReference section */ @@ -71,10 +69,9 @@ isa = PBXGroup; children = ( 058557BA273AAA24004C7B11 /* Assets.xcassets */, - 7555FF82242A565900829871 /* ContentView.swift */, 7555FF8C242A565B00829871 /* Info.plist */, - 2152FB032600AC8F00CF470E /* iOSApp.swift */, 058557D7273AAEEB004C7B11 /* Preview Content */, + 508B32312B43A12E0061B326 /* AppDelegate.swift */, ); path = iosApp; sourceTree = ""; @@ -179,8 +176,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 2152FB042600AC8F00CF470E /* iOSApp.swift in Sources */, - 7555FF83242A565900829871 /* ContentView.swift in Sources */, + 508B32322B43A12E0061B326 /* AppDelegate.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/iosApp/iosApp/AppDelegate.swift b/iosApp/iosApp/AppDelegate.swift new file mode 100644 index 0000000..092f4a8 --- /dev/null +++ b/iosApp/iosApp/AppDelegate.swift @@ -0,0 +1,15 @@ +import UIKit +import shared + +@UIApplicationMain +class AppDelegate: UIResponder, UIApplicationDelegate { + + var window: UIWindow? + + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { + window = UIWindow(frame: UIScreen.main.bounds) + window?.rootViewController = App_iosKt.createUIViewController() + window?.makeKeyAndVisible() + return true + } +} diff --git a/iosApp/iosApp/ContentView.swift b/iosApp/iosApp/ContentView.swift deleted file mode 100644 index 82a9037..0000000 --- a/iosApp/iosApp/ContentView.swift +++ /dev/null @@ -1,19 +0,0 @@ -import UIKit -import SwiftUI -import shared - -struct ComposeView: UIViewControllerRepresentable { - func makeUIViewController(context: Context) -> UIViewController { - let viewController = App_iosKt.createUIViewController() - return viewController - } - - func updateUIViewController(_ uiViewController: UIViewController, context: Context) {} -} - -struct ContentView: View { - var body: some View { - ComposeView() - .ignoresSafeArea(.all, edges: .bottom) // Compose has own keyboard handler - } -} diff --git a/iosApp/iosApp/iOSApp.swift b/iosApp/iosApp/iOSApp.swift deleted file mode 100644 index b7bf2f4..0000000 --- a/iosApp/iosApp/iOSApp.swift +++ /dev/null @@ -1,10 +0,0 @@ -import SwiftUI - -@main -struct iOSApp: App { - var body: some Scene { - WindowGroup { - ContentView() - } - } -} diff --git a/shared/build.gradle.kts b/shared/build.gradle.kts index 885a277..77b41a5 100644 --- a/shared/build.gradle.kts +++ b/shared/build.gradle.kts @@ -31,6 +31,8 @@ kotlin { implementation(compose.runtime) implementation(compose.foundation) implementation(compose.material3) + implementation(com.highcapable.betterandroid.compose.extension) + implementation(com.highcapable.betterandroid.compose.multiplatform) } } val androidMain by getting { @@ -39,6 +41,9 @@ kotlin { api(androidx.core.core.ktx) api(androidx.activity.activity) api(androidx.activity.activity.compose) + api(com.highcapable.betterandroid.ui.component) + api(com.highcapable.betterandroid.ui.extension) + api(com.highcapable.betterandroid.system.extension) } } val desktopMain by getting { diff --git a/shared/src/commonMain/kotlin/App.kt b/shared/src/commonMain/kotlin/App.kt index 7efac3e..2fb2c0e 100644 --- a/shared/src/commonMain/kotlin/App.kt +++ b/shared/src/commonMain/kotlin/App.kt @@ -1,34 +1,64 @@ import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.safeDrawingPadding import androidx.compose.material3.Button import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import com.highcapable.betterandroid.compose.multiplatform.systembar.PlatformSystemBars +import com.highcapable.betterandroid.compose.multiplatform.systembar.rememberSystemBarsController @Composable fun App() { MaterialTheme { + val systemBars = rememberSystemBarsController() + var hideOrShowBars by remember { mutableStateOf(false) } var greeting by remember { mutableStateOf("Hello world!") } - Column( - modifier = Modifier.fillMaxSize(), - verticalArrangement = Arrangement.Center, - horizontalAlignment = Alignment.CenterHorizontally - ) { - Text(text = greeting) - Spacer(Modifier.padding(15.dp)) - Button(onClick = { greeting = "Hello Jetpack Compose Multiplatform!" }) { - Text(text = "Greeting") + Column(modifier = Modifier.safeDrawingPadding()) { + Row( + modifier = Modifier.padding(15.dp), + verticalAlignment = Alignment.CenterVertically + ) { + Text( + text = "__APP_NAME__", + fontSize = 20.sp, + fontWeight = FontWeight.Bold + ) } + Column( + modifier = Modifier.fillMaxSize(), + verticalArrangement = Arrangement.Center, + horizontalAlignment = Alignment.CenterHorizontally + ) { + Text(text = greeting) + Spacer(Modifier.padding(15.dp)) + Button(onClick = { greeting = "Hello Jetpack Compose Multiplatform!" }) { + Text(text = "Greeting") + } + Spacer(Modifier.padding(15.dp)) + Button(onClick = { hideOrShowBars = !hideOrShowBars }) { + Text(text = "Trigger SystemBars") + } + } + } + LaunchedEffect(hideOrShowBars) { + if (hideOrShowBars) + systemBars.hide(PlatformSystemBars.All) + else systemBars.show(PlatformSystemBars.All) } } } \ No newline at end of file diff --git a/shared/src/iosMain/kotlin/App.ios.kt b/shared/src/iosMain/kotlin/App.ios.kt index f85cd45..164e66b 100644 --- a/shared/src/iosMain/kotlin/App.ios.kt +++ b/shared/src/iosMain/kotlin/App.ios.kt @@ -1,9 +1,9 @@ @file:Suppress("unused") import androidx.compose.runtime.Composable -import androidx.compose.ui.window.ComposeUIViewController +import com.highcapable.betterandroid.compose.multiplatform.platform.AppComponentUIViewController -fun createUIViewController() = ComposeUIViewController { MainView() } +fun createUIViewController() = AppComponentUIViewController { MainView() } @Composable fun MainView() = App() \ No newline at end of file