andriydruk / swift-weather-app

Cross-platform Swift application for iOS/Mac/Android/Windows
MIT License
49 stars 10 forks source link

Generated files #13

Closed AndreiVidrasco closed 4 years ago

AndreiVidrasco commented 4 years ago

Hi,

Amazing example. While browsing through files, I have found several with this remark at the top:

/* This file was generated with Readdle SwiftJava Codegen */
/* Don't change it manually! */

But I couldn't find neither blueprints nor documentation on how to rebuild(regenerate) them.

Can you help me out, on finding some info about it?

andriydruk commented 4 years ago

Hi,

First of all, thank you for such nice words

These lines look like a header from a Swift file generated by an annotation processor. Source code available here: https://github.com/readdle/swift-java-codegen

There is no much documentation yet. But general idea: analyze all Kotlin files with appropriate annotations to generate JNI code for access Swift code from native libraries.

AndreiVidrasco commented 4 years ago

Thank you for fast reply.

So basically, if I understood correctly, I need to write all public apis, that I want to access from swift library, in some kotlin(or Java) form and put annotations on them, and swift-java-codegen will generate such files?

andriydruk commented 4 years ago

Yes. It's correct

AndreiVidrasco commented 4 years ago

Ok, thank you very much.

Having a tool for auto-generating kotlin files would be amazing in the future, but for now it's already impressive.

Thank you once more

AndreiVidrasco commented 4 years ago

@andriydruk Hi again,

I was wondering, what would be the simplest way of defining and using SwiftBlock from Kotlin?

Right now, based on example from swift-java-codegen, I ended with

@SwiftBlock("(String?) -> Void")
interface AsBlockString {
    fun call(update: String?)
}

class AsBlockStringImp(var block: (String?) -> Void) : AsBlockString {
    override fun call(update: String?) {
        this.block(update)
    }
}

with creating and using AsBlockStringImp { println("$it") } in other parts of the app which is quite verbose. Do you know maybe of a simpler way?

andriydruk commented 4 years ago

I believe you can use fun interface from Kotlin 1.4 https://kotlinlang.org/docs/reference/fun-interfaces.html

        @SwiftBlock("(String?)->Void")
        fun interface AsBlockString {
            fun invoke(string: String?)
        }

        external fun call(@SwiftBlock block: AsBlockString)

        fun check() {
            call {
                print(it)
            }
        }