Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin 더 잘 활용하기

TaeHwan
August 12, 2018

Kotlin 더 잘 활용하기

GDG Korea Android

TaeHwan

August 12, 2018
Tweet

More Decks by TaeHwan

Other Decks in Education

Transcript

  1. ࣗѐ • GDG Seoul ਍৔૓ • RGP Korea ਃӝਃ উ٘۽੉٘

    ѐߊ • Է ݆਷ ѐߊ੗о غ੗ ࠶۽Ӓ ਍ ৔ - http://thdev.tech • Udemy ௏ౣܽ ъ੄
  2. Kotlin ೟ण ߑߨ જ਷ ଼җ ݆਷ ޙࢲ ࠁӝ ӝࠄ ௏٘

    ٮۄ ೞҊ, Convert ೧ࠁӝ թٜ੉ જ׮ח ۄ੉࠳۞ܻ ഝਊ ௏٘ ઴੉ӝ(let, apply, with, run, also ١)
  3. ೞ૑݅ Kotlin݅ ೟णೞӝূ ই૒ Javaܳ ݽܳ ࣽ হ׮ ۞׬ ழ࠳о

    ծ૑݅ ੉೧ೡࣻ۾ য۵׮ Kotlinਸ ખ ؊ ੜ ঌ۰ݶ Effective Java ੉೧
  4. Ӓېب ੉Ѥ ੉೧ೞݶ જ׮ Lambdas Kotlin Extension Higher-Order function ੉

    3 о૑ ੉೧ೞݶ Kotlin ഝਊࢿ੉ ֫ইઉਃ
  5. Kotlin Extension public inline fun <T> T.apply(block: T.() -> Unit):

    T { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } block() return this } // use val button = Button(this).apply { text = "Button" textSize = resources.getDimension(R.id.button_text_size) }
  6. Kotlin Extension public inline fun <T> T.apply(block: T.() -> Unit):

    T { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } block() return this } // use val button = Button(this).apply { text = "Button" textSize = resources.getDimension(R.id.button_text_size) } Extension Higher-Order function Lambdas
  7. ੉޷ ੜ ഝਊ ઺੉׮. let, apply, run ١ ݅ ੜ

    ঌইب Kotlin য۵૑ ঋ׮ Ө਷ ղਊ਷ ա઺ী ଺ইоݶࢲ ੊൦׮.
  8. Kotlin Extension ૒੽ ٜ݅যࠁ੗ 1 ױ҅ - String੉ Emptyо ইפݶ

    callback ߉ӝ interface Callback { fun notEmptyString(param: String) } fun notEmptyString(param: String?, callback: Callback) { if (!param.isNullOrEmpty()) { callback.notEmptyString(param!!) } }
  9. Kotlin Extension ٜ݅যࠁ੗ 1 ױ҅ - String੉ Emptyо ইפݶ callback

    ߉ӝ पઁ ࢎਊ਷ notEmptyString(null, object : Callback { override fun notEmptyString(param: String) { // Use NotNull param } })
  10. Kotlin Extension੉ۆ? Java୊ۢ UtilClassܳ ૒੽ ૑੿ೞ૑ ঋҊ, ё୓੄ ഛ੢ਸ ઁҕ

    StringUtil.notEmptyString() -> text.notEmptyString() functionҗ ೐۽ಌ౭(val/var) ݽف ഛ੢ਸ ઁҕ Generic ੸ਊ оמ
  11. Kotlin Extension ٜ݅যࠁ੗ 2 ױ҅ - Extension ੸ਊ Receiver type

    fun String?.notEmptyString(callback: Callback) fun notEmptyString(param: String?, callback: Callback) fun notEmptyString( String? . callback: Callback)
  12. Kotlin Extension ٜ݅যࠁ੗ 2 ױ҅ - Extension ੸ਊ fun String?.notEmptyString(callback:

    Callback) { if (!this.isNullOrEmpty()) { callback.notEmptyString(this!!) } } Receiver object Receiver type
  13. Kotlin Extension ٜ݅যࠁ੗ 2 ױ҅ - Extension ੸ਊ notEmptyString(null, object

    : Callback { override fun notEmptyString(param: String) { // Use Message } }) null , notEmptyString( .
  14. Kotlin Extension ٜ݅যࠁ੗ 2 ױ҅ - Extension ੸ਊ val name:

    String? = "Taehwan" name.notEmptyString(object : Callback { override fun notEmptyString(param: String) { // Use Message } })
  15. Higher-Order function ୡӝച val higherOrder: () -> Boolean = {

    false } fun higherOrder(block: () -> String) { block() }
  16. Higher-Order function ୡӝച val higherOrder: () -> Boolean = {

    false } Method return type Function initialization return value
  17. Higher-Order function ୡӝച val higherOrder: (param: String) -> Boolean =

    { false } Method Parameters param -> Kotlin lambdasח 1ѐ੄ parameters ࢤۚ оמ > ؀न it ఃਕ٘ ࢎਊ
  18. Higher-Order function ੸ਊೞӝ ੹ী val higherOrder: String?.() -> Boolean =

    { false } Method Receiver type val name: String? = "Taehwan" name.higherOrder()
  19. Higher-Order function ੸ਊೞӝ 3 ױ҅ - Higher-Order function ੸ਊ fun

    String?.notEmptyString(callback: (String) -> Unit) { if (!this.isNullOrEmpty()) { callback(this!!) } } fun String?.notEmptyString(callback: Callback) { if (!this.isNullOrEmpty()) { callback.notEmptyString(this!!) } }
  20. Higher-Order function ੸ਊೞӝ 3 ױ҅ - Higher-Order function ੸ਊ val

    name: String? = "Taehwan" name.notEmptyString({ println("notEmptyString $it") }) ( ) function ݃૑݄ Higher-Order functionীࢲ ࣗҚഐ()ח ࢤۚೡ ࣻ ੓׮.
  21. Higher-Order function ੸ਊೞӝ fun String?.notEmptyString( callback: (String) -> Unit, callback2:

    () -> Unit) { if (!this.isNullOrEmpty()) { callback(this!!) } } "Taehwan".notEmptyString({ println("notEmptyString $it") }, { // something } , )
  22. Kotlin extension ખ ؊ ࠁ੗ public final void notEmptyString(@Nullable String

    $receiver, @NotNull Function1 callback) { Intrinsics.checkParameterIsNotNull(callback, "callback"); CharSequence var3 = (CharSequence)$receiver; if (var3 != null && var3.length() != 0) { if ($receiver == null) { Intrinsics.throwNpe(); } callback.invoke($receiver); } } public final void notEmptyTest() { this.notEmptyString("Taehwan", (Function1)null.INSTANCE); }
  23. Kotlin extension ખ ؊ ࠁ੗ public final void notEmptyString(@Nullable String

    $receiver, @NotNull Function1 callback) { Intrinsics.checkParameterIsNotNull(callback, "callback"); CharSequence var3 = (CharSequence)$receiver; if (var3 != null && var3.length() != 0) { if ($receiver == null) { Intrinsics.throwNpe(); } callback.invoke($receiver); } } public final void notEmptyTest() { this.notEmptyString("Taehwan", (Function1)null.INSTANCE); } ೣࣻ ഐ୹੉ ੌযաঠ ೡө?
  24. inline ഝਊ inlineਸ ୶о inlineਸ ୶оೣਵ۽ॄ kotlin extension ௏٘ܳ inline

    ೧ળ׮ Ѿҗ੸ਵ۽ function ഐ୹ ௏٘о ࢎۄ૓׮
  25. inline ഝਊ - ࢎਊೞח ௏٘ীࢲ this.notEmptyString("Taehwan", (Function1)null.INSTANCE); -> public final

    void notEmptyTest() { String $receiver$iv = "Taehwan"; CharSequence var3 = (CharSequence)$receiver$iv; if (var3.length() != 0) { String var5 = "notEmptyString " + $receiver$iv; System.out.println(var5); } }
  26. DSLs (Domain-specific language) SQLীࢲ ൔೞѱ ࠅ ࣻ ੓׮. Select …

    / Delete … ѾҴ খীࢲ ੉ঠӝೠ 3о૑ܳ ੜ ഝਊೞݶ DSLਸ औѱ ੸ਊೡ ࣻ ੓׮.
  27. inline if (!this.isNullOrEmpty()) { callback(this!!) } } fun String?.notEmptyString(callback: String.()

    -> Unit) { DSLs (Domain-specific language) “Taehwan" notEmptyString { println("notEmptyString $this") } .
  28. Kotlin Android KTX Jetpack family Android - Kotlin ࢎਊ ಞ੄ܳ

    ذח extensions AndroidX ಁః૑۽ ઁҕ
 implementation ‘androidx.core:core-ktx:1.0.0-alpha1’ Android : https://developer.android.com/kotlin/ktx API : https://github.com/android/android-ktx API reference documentation : https://android.github.io/android-ktx/core-ktx/ Android Jetpack: sweetening Kotlin development with Android KTX (Google I/O '18)
  29. /** * Creates and shows a [Toast] with the given

    [text] * * @param duration Toast duration, defaults to [Toast.LENGTH_SHORT] */ inline fun Context.toast(text: CharSequence, duration: Int = Toast.LENGTH_SHORT): Toast { return Toast.makeText(this, text, duration).apply { show() } } Use -> toast(“Message”) Kotlin Android KTX
  30. inline fun Context.toast(text: CharSequence, duration: Int = Toast.LENGTH_SHORT): Toast {

    return Toast.makeText(this, text, duration).apply { show() } } infix ୶оೞݶ? messageо ઺੼ੋ ҃਋ ੉৬ э੉ ೡ ࣻ ੓׮ ೞ૑݅ durationب ೙ਃೞ׮ݶ? inline fun Context.toast(message: () -> String) { toast(message()) } toast { "Message" }
  31. data class ToastData(val message: String, val duration: Int) class ToastDataBuilder

    { var message: String = "" var duration: Int = Toast.LENGTH_SHORT fun build(): ToastData = ToastData(message, duration) } message/duration ݽف ࢸ੿ೞ੗
  32. inline fun Context.toast(message: () -> String) { toast(message()) } message/duration

    ݽف ࢸ੿ೞ੗ inline fun Context.toast(body: ToastDataBuilder.() -> Unit) { val builder = ToastDataBuilder() builder.body() val item = builder.build() toast(item.message, item.duration) }
  33. toast { "Message" } message/duration ݽف ࢸ੿ೞ੗ toast { message

    = "Message" } toast { message = "Message" duration = Toast.LENGTH_SHORT }
  34. inline fun Context.toast(body: () -> Pair<String, Int>) { val (message,

    duration) = body() toast(message, duration) } toast { “Message" to Toast.LENGTH_SHORT } Pair ੉ਊ೧ࢲ ୊ܻ?
  35. private val yourViewModel: YourViewModel by lazyInject // create Your ViewModel

    YourViewModel(..., ..., ...) } ੉۠Ѫب ٜ݅ ࣻ ੓׮. ੉Ѧ ೞӝਤೠ ௏٘ח ড 30઴ ॳחҔীࢶ ױ 3઴ https://thdev.tech/LifecycleExtensions/