Kotlin 더 잘 활용하기

7589a5a8fec022e8af4e46525150a291?s=47 TaeHwan
August 12, 2018

Kotlin 더 잘 활용하기

GDG Korea Android

7589a5a8fec022e8af4e46525150a291?s=128

TaeHwan

August 12, 2018
Tweet

Transcript

  1. ؊ ա਷ Kotlin ഝਊߨ਷? I love kotlin Taehwan

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

    ѐߊ • Է ݆਷ ѐߊ੗о غ੗ ࠶۽Ӓ ਍ ৔ - http://thdev.tech • Udemy ௏ౣܽ ъ੄
  3. Kotlin ؊ ੜ ࢎਊೞ۰ݶ?

  4. Kotlin ೟ण ߑߨ જ਷ ଼җ ݆਷ ޙࢲ ࠁӝ ӝࠄ ௏٘

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

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

    3 о૑ ੉೧ೞݶ Kotlin ഝਊࢿ੉ ֫ইઉਃ
  7. Kotlin Extension Kotlinਸ ೟णೞח ׼न਷ ੉޷ ঌҊ ੓׮. let, run,

    apply, with, also and more.
  8. 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) }
  9. 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
  10. ੉޷ ੜ ഝਊ ઺੉׮. let, apply, run ١ ݅ ੜ

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

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

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

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

  15. Kotlin Extension ٜ݅যࠁ੗ 2 ױ҅ - Extension ੸ਊ Receiver type

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

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

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

    String? = "Taehwan" name.notEmptyString(object : Callback { override fun notEmptyString(param: String) { // Use Message } })
  19. 3 ױ҅ Kotlin Extension ੸ਊ೮૑݅ ӭՔೞ૑ ঋ׮. Listener??? Listener ؀न

    Higher-Order Functionਸ ࢎਊ೧ࠁ੗
  20. Higher-Order function੉ۆ? functionਸ parameter۽ ੹׳ೞѢա, return ೡ ࣻ ੓׮ functionਸ

    ೐۽ಌ౭ب(val/var) о૕ ࣻ ੓׮
  21. Higher-Order function ୡӝച val higherOrder: () -> Boolean = {

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

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

    { false } Method Parameters param -> Kotlin lambdasח 1ѐ੄ parameters ࢤۚ оמ > ؀न it ఃਕ٘ ࢎਊ
  24. ୶о۽ Higher-Order function Kotlin Extension ੸ਊب оמ

  25. Higher-Order function ੸ਊೞӝ ੹ী val higherOrder: String?.() -> Boolean =

    { false } Method Receiver type val name: String? = "Taehwan" name.higherOrder()
  26. 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!!) } }
  27. Higher-Order function ੸ਊೞӝ 3 ױ҅ - Higher-Order function ੸ਊ val

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

    () -> Unit) { if (!this.isNullOrEmpty()) { callback(this!!) } } "Taehwan".notEmptyString({ println("notEmptyString $it") }, { // something } , )
  29. 3ѐח ঌও׮ Lambdas Kotlin Extension Higher-Order function Kotlin ؀ࠗ࠙੄ ۄ੉࠳۞ܻ

    णٙ оמ
  30. 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); }
  31. 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); } ೣࣻ ഐ୹੉ ੌযաঠ ೡө?
  32. inline ഝਊ inlineਸ ୶о inlineਸ ୶оೣਵ۽ॄ kotlin extension ௏٘ܳ inline

    ೧ળ׮ Ѿҗ੸ਵ۽ function ഐ୹ ௏٘о ࢎۄ૓׮
  33. inline ഝਊ if (!this.isNullOrEmpty()) { callback(this!!) } } fun String?.notEmptyString(callback:

    (String) -> Unit) {
  34. 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); } }
  35. Kotlin DSLs (Domain-specific language)

  36. DSLs (Domain-specific language) SQLীࢲ ൔೞѱ ࠅ ࣻ ੓׮. Select …

    / Delete … ѾҴ খীࢲ ੉ঠӝೠ 3о૑ܳ ੜ ഝਊೞݶ DSLਸ औѱ ੸ਊೡ ࣻ ੓׮.
  37. inline fun Int.plus(param: Int) = this + param DSLs (Domain-specific

    language) 1 plus 10 . (10)
  38. inline if (!this.isNullOrEmpty()) { callback(this!!) } } fun String?.notEmptyString(callback: String.()

    -> Unit) { DSLs (Domain-specific language) “Taehwan" notEmptyString { println("notEmptyString $this") } .
  39. String.() -> Unit DSLs (Domain-specific language) Parameters Receiver type Return

    type
  40. KTX Android KTX

  41. 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)
  42. /** * 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
  43. 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" }
  44. 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 ݽف ࢸ੿ೞ੗
  45. 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) }
  46. toast { "Message" } message/duration ݽف ࢸ੿ೞ੗ toast { message

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

    duration) = body() toast(message, duration) } toast { “Message" to Toast.LENGTH_SHORT } Pair ੉ਊ೧ࢲ ୊ܻ?
  48. ݃ஜ I Love Kotlin

  49. private val yourViewModel: YourViewModel by lazyInject // create Your ViewModel

    YourViewModel(..., ..., ...) } ੉۠Ѫب ٜ݅ ࣻ ੓׮. ੉Ѧ ೞӝਤೠ ௏٘ח ড 30઴ ॳחҔীࢶ ױ 3઴ https://thdev.tech/LifecycleExtensions/
  50. ѾҴ Lambdas Kotlin Extension Higher-Order function 3ѐ݅ ঌݶ KTXܳ ށۋযب

    ؊ औѱ ѐߊೡ ࣻ ੓׮.
  51. хࢎ೤פ׮ taehwan@thdev.net GDG Seoul ਍৔૓ Taehwan ইې ੉ݫੌ۽...