Skip to content

Commit 6f651da

Browse files
committed
Add Swift bridge for BackHandlerView
1 parent 7f975eb commit 6f651da

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// BackHandlerView.swift
3+
// AndroidSwiftUI
4+
//
5+
// Created by Alsey Coleman Miller on 7/16/26.
6+
//
7+
8+
import AndroidKit
9+
10+
/// Hosts a Jetpack Compose `BackHandler` to intercept the system back button.
11+
@JavaClass("com.pureswift.swiftandroid.BackHandlerView")
12+
open class BackHandlerView: AndroidView.View {
13+
14+
@JavaMethod
15+
@_nonoverride public convenience init(_ context: AndroidContent.Context?, _ callback: SwiftObject?, environment: JNIEnvironment? = nil)
16+
17+
@JavaMethod
18+
public func getCallback() -> SwiftObject?
19+
20+
@JavaMethod
21+
open func setBackHandlerEnabled(_ enabled: Bool)
22+
}
23+
24+
@JavaImplementation("com.pureswift.swiftandroid.BackHandlerView")
25+
extension BackHandlerView {
26+
27+
@JavaMethod
28+
func onBack() {
29+
callback()
30+
}
31+
}
32+
33+
public extension BackHandlerView {
34+
35+
convenience init(_ context: AndroidContent.Context, environment: JNIEnvironment? = nil, action: @escaping () -> ()) {
36+
let object = SwiftObject(action, environment: environment)
37+
self.init(context, object, environment: environment)
38+
}
39+
40+
var callback: (() -> ()) {
41+
getCallback()!.valueObject().value as! (() -> ())
42+
}
43+
}

0 commit comments

Comments
 (0)