Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6.3.2
1 change: 1 addition & 0 deletions Demo/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ dependencies {
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
implementation(libs.androidx.foundation)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.pureswift.swiftandroid

import android.content.Context
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.unit.dp
import com.pureswift.swiftandroid.ui.theme.SwiftAndroidTheme

// SwiftUI `List` backed by a Jetpack Compose `LazyColumn`, sourcing its rows from a Swift-implemented `ListViewAdapter`.
// `ComposeView` is final, so it's hosted as a child of this `FrameLayout` rather than subclassed.
class ComposeListView(context: Context, private val adapter: ListViewAdapter) : FrameLayout(context) {

private var version by mutableIntStateOf(0)

init {
val composeView = ComposeView(context)
composeView.setContent {
SwiftAndroidTheme {
key(version) {
LazyColumn {
items(adapter.getCount()) { index ->
Text(
text = adapter.getItem(index) as String,
modifier = Modifier.padding(16.dp)
)
}
}
}
}
}
addView(composeView, ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
}

fun getAdapter(): ListViewAdapter = adapter

fun refresh() {
version++
}
}
1 change: 1 addition & 0 deletions Demo/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-toolin
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
androidx-foundation = { group = "androidx.compose.foundation", name = "foundation" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
Expand Down
53 changes: 13 additions & 40 deletions Sources/AndroidSwiftUI/AndroidListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import AndroidKit

/// SwitUI View for Android `android.widget.ListView`
/// SwiftUI `List` for Android, backed by a Jetpack Compose `LazyColumn`.
public struct AndroidListView {

let items: [String]
Expand All @@ -18,47 +18,20 @@ public struct AndroidListView {
}

extension AndroidListView: AndroidViewRepresentable {

/// Creates the view object and configures its initial state.
public func makeAndroidView(context: Self.Context) -> ListView {
createView(context: context.androidContext)
}

/// Updates the state of the specified view with new information from SwiftUI.
public func updateAndroidView(_ view: ListView, context: Self.Context) {
updateView(view)
public func makeAndroidView(context: Self.Context) -> ComposeListView {
let adapter = ListViewAdapter(swiftObject: SwiftObject(ListViewAdapter.Context(items: items)))
return ComposeListView(context.androidContext, adapter)
}
}

extension AndroidListView {

func createView(context: AndroidContent.Context) -> AndroidWidget.ListView {
let view = AndroidWidget.ListView(context)
updateView(view)
return view
}

func updateView(_ view: AndroidWidget.ListView) {
let layout = try! JavaClass<R.layout>()
let resource = layout.simple_list_item_1
let objects: [JavaObject?] = items.map { JavaString($0) }
let adapter = ArrayAdapter<JavaObject>(
context: view.getContext(),
resource: resource,
objects: objects
)
view.setAdapter(adapter.as(Adapter.self))
/// Updates the state of the specified view with new information from SwiftUI.
public func updateAndroidView(_ view: ComposeListView, context: Self.Context) {
guard let adapter = view.getAdapter() else {
assertionFailure("Missing adapter")
return
}
adapter.context = ListViewAdapter.Context(items: items)
view.refresh()
}
}

extension JavaClass<R.layout> {

@JavaStaticField(isFinal: true)
public var list_view_row: Int32
}

extension JavaClass<R.id> {

@JavaStaticField(isFinal: true)
public var textView: Int32
}
8 changes: 7 additions & 1 deletion Sources/AndroidSwiftUI/AndroidRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Created by Alsey Coleman Miller on 6/8/25.
//

import JavaKit
import AndroidKit

final class AndroidRenderer: Renderer {
Expand Down Expand Up @@ -74,6 +73,10 @@ final class AndroidRenderer: Renderer {
viewGroup.addView(viewObject)
log("\(self).\(#function) \(#line): Add \(viewObject.getClass().getName()) to \(viewGroup.getClass().getName())")
return AndroidTarget(host.view, viewObject)
case .fragment:
// TODO: Mount into a fragment via FragmentManager/FragmentTransaction
log("\(self).\(#function) \(#line) Fragment mounting not yet implemented")
return nil
}
} else {

Expand Down Expand Up @@ -106,6 +109,9 @@ final class AndroidRenderer: Renderer {
case .view(let view):
log("\(self).\(#function) Update \(view.getClass().getName())")
widget.updateAndroidView(view)
case .fragment:
// TODO: Update fragment
break
}
}

Expand Down
1 change: 0 additions & 1 deletion Sources/AndroidSwiftUI/AndroidTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Created by Alsey Coleman Miller on 6/8/25.
//

import JavaKit
import AndroidKit

final class AndroidTarget: Target {
Expand Down
22 changes: 22 additions & 0 deletions Sources/AndroidSwiftUI/ComposeListView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// ComposeListView.swift
// AndroidSwiftUI
//
// Created by Alsey Coleman Miller on 7/16/26.
//

import AndroidKit

/// SwiftUI `List` backed by a Jetpack Compose `LazyColumn`.
@JavaClass("com.pureswift.swiftandroid.ComposeListView")
open class ComposeListView: AndroidView.View {

@JavaMethod
@_nonoverride public convenience init(_ context: AndroidContent.Context?, _ adapter: ListViewAdapter?, environment: JNIEnvironment? = nil)

@JavaMethod
open func getAdapter() -> ListViewAdapter?

@JavaMethod
open func refresh()
}
25 changes: 12 additions & 13 deletions Sources/AndroidSwiftUI/Fragment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,27 @@ public extension Fragment {
}

extension Fragment {

static var logTag: LogTag { "Fragment" }


static var logTag: String { "Fragment" }

static let log = try! JavaClass<AndroidUtil.Log>()

static func log(_ string: String) {
try? AndroidLogger(tag: logTag, priority: .debug)
.log(string)
_ = Self.log.d(Self.logTag, string)
}

static func logInfo(_ string: String) {
try? AndroidLogger(tag: logTag, priority: .info)
.log(string)
_ = Self.log.i(Self.logTag, string)
}

static func logError(_ string: String) {
try? AndroidLogger(tag: logTag, priority: .error)
.log(string)
_ = Self.log.e(Self.logTag, string)
}

func log(_ string: String) {
Self.log(string)
}

func logError(_ string: String) {
Self.logError(string)
}
Expand Down
1 change: 1 addition & 0 deletions Sources/AndroidSwiftUI/ImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created by Alsey Coleman Miller on 6/9/25.
//

import Foundation
import AndroidKit

extension Image: AnyAndroidView {
Expand Down
3 changes: 1 addition & 2 deletions Sources/AndroidSwiftUI/JavaRetainedValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
// Created by Alsey Coleman Miller on 6/9/25.
//

import JavaKit
import JavaRuntime
import SwiftJava

/// Java class that retains a Swift value for the duration of its lifetime.
@JavaClass("com.pureswift.swiftandroid.SwiftObject")
Expand Down
2 changes: 1 addition & 1 deletion Sources/AndroidSwiftUI/ListViewAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation
import AndroidKit

@JavaClass("com.pureswift.swiftandroid.ListViewAdapter", extends: ListAdapter.self)
@JavaClass("com.pureswift.swiftandroid.ListViewAdapter", extends: AndroidWidget.ListAdapter.self)
open class ListViewAdapter: JavaObject {

@JavaMethod
Expand Down
2 changes: 0 additions & 2 deletions Sources/AndroidSwiftUI/Runnable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// Created by Alsey Coleman Miller on 6/9/25.
//

import JavaKit
import JavaRuntime
import AndroidKit
import JavaLang
/*
Expand Down
Loading