Created
May 4, 2019 00:38
-
-
Save ermik/62ef3b837f71fff567ac3f4c123b89c3 to your computer and use it in GitHub Desktop.
Accessing existing RIBs libraries from pure-Kotlin code via Kotlin Wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I did not separate this gist into multiple "files" because all this | |
// code is drafted in Kotlin and explains a single thought. | |
// | |
// | |
// -------------------------------------------------- | |
// Wrapper package, commonMain (can't import Java RIBs) | |
// It's a KMP project but is meant to be a library for other KMPs | |
// -------------------------------------------------- | |
package com.uber.rib.native | |
expect interface NativeInteractable { /* | |
Defines Interactor functionality for _other_ Kotlin packages (for example | |
a ChangePassword RIB for Story written in Kotlin / KMP) that will use Wrapper | |
package to gain access to RIBs API same way native code imports existing RIBs | |
libraries. | |
*/ } | |
expect open class NativeInteractor<TypeConstraints> : NativeInteractable | |
// -------------------------------------------------- | |
// Wrapper package, androidMain; same can be done for iosMain | |
// -------------------------------------------------- | |
package com.uber.rib.native | |
import com.uber.rib.core.Interactable // now we can import | |
import com.uber.rib.core.Interactor // can do same for Swift library in iosMain | |
actual interface NativeInteractable: Interactable { /* extras */ } | |
actual open class NativeInteractor<TypeStuff>: Interactor<MoreTypeStuff>, NativeInteractable { | |
/* we don't need to re-implement RIBs — the functionality | |
will be inherited and will now become availabe to the commonMain | |
above via expect/actual; only extra methods needed to service | |
this pattern have to be written */ | |
} | |
// -------------------------------------------------- | |
// We pull above code as another dependency for Story | |
// -------------------------------------------------- | |
// build.gradle.kts for Story KMP | |
dependencies { | |
//.... | |
implementation("com.uber.rib.native") version "0.9.5" | |
} | |
// -------------------------------------------------- | |
// commonMain in Story KMP | |
// Now we can write the RIB code "as expected" in Story KMP | |
// by using the new library | |
// -------------------------------------------------- | |
package org.makegoodstuff.story.root.main.logged_in.change_password | |
import com.uber.rib.native // this can be imported because it is Pure-Kotlin | |
class ChangePasswordInteractor<TypesManyTypes>: NativeInteractor<MoreTypes> { | |
/* here we write our "shared code" and can use all the expected functions | |
from RIBs Interacotor API because com.uber.rib.native exposes them to | |
Kotlin. What actually happens is that calls to RIBs API from here get | |
executed in the **native** libarary and, vice versa: | |
*/ | |
fun didStart() { | |
/* This function will actually be called by the respective Swift or Java | |
library via the Wrapper — but whoever is developing the Story KMP does | |
not need to care about that. | |
*/ | |
} | |
} | |
// -------------------------------------------------- | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment