Last active
October 16, 2024 16:13
-
-
Save nasserkhosravi/f947efa52651d9683a192dc9c5712e16 to your computer and use it in GitHub Desktop.
Modular lazy function pattern in kotlin
This file contains hidden or 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
import android.net.Uri | |
import androidx.core.net.toUri | |
//This is a object, something that has uninterable identity, here the id is deeplink. | |
class DeeplinkObject( | |
val deeplink: String | |
) { | |
private val asUri = deeplink.toUri() | |
private val subjectsAskedMemo = HashMap<String, Any>() | |
fun getLazy(key: String, computer: (deeplink: Uri) -> Any): Any { | |
return subjectsAskedMemo.getOrPut(key) { | |
computer.invoke(asUri) | |
} | |
} | |
} | |
//isUniversal is a subject | |
fun DeeplinkObject.isUniversal(): Boolean { | |
val key = "isHttps" | |
return getLazy(key) { | |
it.scheme == "https" | |
} as Boolean | |
} | |
//isLocal is a subject | |
fun DeeplinkObject.isLocal(): Boolean { | |
val key = "isLocal" | |
return getLazy(key) { | |
it.scheme == "myapp" | |
} as Boolean | |
} | |
//isProfile is a subject | |
fun DeeplinkObject.isProfile(): Boolean { | |
val key = "isProfile" | |
return getLazy(key) { | |
it.path?.startsWith("profile") ?: false | |
} as Boolean | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment