Skip to content

Commit 27a9a88

Browse files
committed
Updated Adapter protocol
1 parent bb9892c commit 27a9a88

File tree

1 file changed

+57
-112
lines changed

1 file changed

+57
-112
lines changed

Sources/WidgetAdapter.swift

Lines changed: 57 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -7,69 +7,22 @@
77

88
import Foundation
99
import java_swift
10+
import JNI
1011

1112
public extension Android.Widget {
1213

1314
public typealias Adapter = AndroidWidgetAdapter
1415
}
1516

16-
open class AndroidWidgetAdapter: JavaProtocol {
17+
public protocol AndroidWidgetAdapter: JavaProtocol {
1718

18-
// MARK: - Properties
19-
20-
fileprivate lazy var listener: AndroidWidgetAdapterListenerLocal = {
21-
22-
let proxy = AndroidWidgetAdapterListenerProxy()
23-
proxy.swiftObject = self
24-
25-
return AndroidWidgetAdapterListenerLocal(owned: proxy, proto: proxy)
26-
}()
27-
28-
// MARK: - Initialization
29-
30-
public func localJavaObject( _ locals: UnsafeMutablePointer<[jobject]> ) -> jobject? {
31-
32-
return listener.localJavaObject( locals )
33-
}
34-
35-
public final func notifyDataSetChanged() {
36-
37-
listener.notifyDataSetChanged()
38-
}
39-
40-
// MARK: - Listener
41-
42-
open func getCount() -> Int { return 0 }
19+
func getCount() -> Int
4320

44-
open func getView(position: Int, convertView: Android.View.View?, parent: Android.View.ViewGroup) -> Android.View.View {
45-
46-
fatalError("\(#function) must be implemented in subclass")
47-
}
21+
func getView(position: Int, convertView: Android.View.View?, parent: Android.View.ViewGroup) -> Android.View.View
4822
}
4923

5024
// MARK: - Private
5125

52-
fileprivate extension Android.Widget.Adapter {
53-
54-
/// JNI Cache
55-
struct JNICache {
56-
57-
static let classSignature = Android.SwiftSupport.className(["SwiftAdapter"])
58-
59-
/// JNI Java class name
60-
static let className = classSignature.rawValue
61-
62-
/// JNI Java class
63-
static var jniClass: jclass? = AndroidWidgetAdapterListenerLocal._proxyClass
64-
65-
/// JNI Method ID cache
66-
struct MethodID {
67-
68-
static var notifyDataSetChanged: jmethodID?
69-
}
70-
}
71-
}
72-
7326
private typealias AndroidWidgetAdapter_getCount_type = @convention(c) ( _: UnsafeMutablePointer<JNIEnv?>, _: jobject?, _: jlong) -> (jint)
7427

7528
private func AndroidWidgetAdapter_getCount( _ __env: UnsafeMutablePointer<JNIEnv?>,
@@ -102,61 +55,18 @@ private func AndroidWidgetAdapter_getView( _ __env: UnsafeMutablePointer<JNIEnv?
10255

10356
var __locals = [jobject]()
10457

105-
return result?.localJavaObject(&__locals)
58+
return result.localJavaObject(&__locals)
10659
}
10760

108-
fileprivate final class AndroidWidgetAdapterListenerProxy: AndroidWidgetAdapterListenerProtocol {
109-
110-
weak var swiftObject: Android.Widget.Adapter?
111-
112-
func getCount() -> Int {
113-
114-
return swiftObject?.getCount() ?? 0
115-
}
116-
117-
func getView(position: Int, convertView: Android.View.View?, parent: Android.View.ViewGroup) -> Android.View.View? {
118-
119-
return swiftObject?.getView(position: position, convertView: convertView, parent: parent)
120-
}
121-
}
122-
123-
fileprivate protocol AndroidWidgetAdapterListenerProtocol: JavaProtocol {
124-
125-
func getCount() -> Int
126-
127-
func getView(position: Int, convertView: Android.View.View?, parent: Android.View.ViewGroup) -> Android.View.View?
128-
}
129-
130-
extension AndroidWidgetAdapterListenerProtocol {
131-
132-
public func localJavaObject( _ locals: UnsafeMutablePointer<[jobject]> ) -> jobject? {
133-
134-
return AndroidWidgetAdapterListenerLocal( owned: self, proto: self ).localJavaObject( locals )
135-
}
136-
137-
}
138-
139-
fileprivate class AndroidWidgetAdapterListenerLocal: JNILocalProxy<AndroidWidgetAdapterListenerProtocol, Any> {
61+
internal class AndroidWidgetAdapterListenerLocal: JNILocalProxy<AndroidWidgetAdapter, Any> {
14062

14163
fileprivate static let _proxyClass: jclass = {
14264

143-
var natives = [JNINativeMethod]()
144-
145-
let getCountThunk: AndroidWidgetAdapter_getCount_type = AndroidWidgetAdapter_getCount
146-
147-
natives.append( JNINativeMethod(name: strdup("__getCount"),
148-
signature: strdup("(J)I"),
149-
fnPtr: unsafeBitCast( getCountThunk, to: UnsafeMutableRawPointer.self ) ))
150-
151-
let getViewThunk: AndroidWidgetAdapter_getView_type = AndroidWidgetAdapter_getView
152-
153-
natives.append( JNINativeMethod(name: strdup("__getView"),
154-
signature: strdup("(JILandroid/view/View;Landroid/view/ViewGroup;)Landroid/view/View;"),
155-
fnPtr: unsafeBitCast( getViewThunk, to: UnsafeMutableRawPointer.self ) ))
156-
157-
natives.append( JNINativeMethod( name: strdup("__finalize"),
158-
signature: strdup("(J)V"),
159-
fnPtr: unsafeBitCast( JNIReleasableProxy__finalize_thunk, to: UnsafeMutableRawPointer.self ) ) )
65+
var natives: [JNINativeMethod] = [
66+
JNICache.Method.getCount.method,
67+
JNICache.Method.getView.method,
68+
.finalize
69+
]
16070

16171
let clazz = JNI.FindClass( proxyClassName() )
16272

@@ -172,24 +82,59 @@ fileprivate class AndroidWidgetAdapterListenerLocal: JNILocalProxy<AndroidWidget
17282
return JNI.api.NewGlobalRef( JNI.env, clazz )!
17383
}()
17484

175-
override open class func proxyClassName() -> String { return Android.Widget.Adapter.JNICache.className }
85+
override open class func proxyClassName() -> String { return JNICache.className }
17686

17787
override open class func proxyClass() -> jclass? { return _proxyClass }
88+
}
89+
90+
extension AndroidWidgetAdapter {
17891

179-
func notifyDataSetChanged() {
92+
public func localJavaObject( _ locals: UnsafeMutablePointer<[jobject]> ) -> jobject? {
18093

181-
var __locals = [jobject]()
94+
return AndroidWidgetAdapterListenerLocal( owned: self, proto: self ).localJavaObject( locals )
95+
}
96+
}
97+
98+
internal extension AndroidWidgetAdapterListenerLocal {
99+
100+
/// JNI Cache
101+
struct JNICache {
102+
103+
static let classSignature = Android.SwiftSupport.className(["SwiftAdapter"])
182104

183-
var __args = [jvalue]( repeating: jvalue(), count: 1 )
105+
/// JNI Java class name
106+
static let className = classSignature.rawValue
184107

185-
withJavaObject {
108+
/// JNI Method cache
109+
fileprivate enum Method {
110+
111+
enum getCount: JNINativeMethodEntry {
112+
113+
static let name = "__getCount"
114+
115+
/// "(J)I"
116+
static let signature = JNIMethodSignature(argumentTypes: [.long], returnType: .int)
117+
118+
static let thunk: AndroidWidgetAdapter_getCount_type = AndroidWidgetAdapter_getCount
119+
}
120+
121+
enum getView: JNINativeMethodEntry {
122+
123+
static let name = "__getView"
124+
125+
/// "(JILandroid/view/View;Landroid/view/ViewGroup;)Landroid/view/View;"
126+
static let signature = JNIMethodSignature(
127+
argumentTypes: [
128+
.long,
129+
.int,
130+
.object(Android.View.ViewGroup.ViewGroupJNICache.classSignature)
131+
],
132+
returnType: .object(Android.View.View.JNICache.classSignature)
133+
)
134+
135+
static let thunk: AndroidWidgetAdapter_getView_type = AndroidWidgetAdapter_getView
136+
}
186137

187-
JNIMethod.CallVoidMethod(object: $0,
188-
methodName: "notifyDataSetChanged",
189-
methodSig: "()V",
190-
methodCache: &Android.Widget.Adapter.JNICache.MethodID.notifyDataSetChanged,
191-
args: &__args,
192-
locals: &__locals)
193138
}
194139
}
195140
}

0 commit comments

Comments
 (0)