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

Delegated propertyで Android開発をもっと便利に @ Gunosy Beer Bash #12

Delegated propertyで Android開発をもっと便利に @ Gunosy Beer Bash #12

chibatching

June 14, 2017
Tweet

More Decks by chibatching

Other Decks in Programming

Transcript

  1. %FMFHBUFEQSPQFSUZͰ
    "OESPJE։ൃΛ΋ͬͱศརʹ

    (VOPTZ#FFS#BTI
    גࣜձࣾτΫόΠ
    ઍ༿ོ༤

    View full-size slide

  2. w ઍ༿ོ༤ !DIJCBUDIJOH

    w גࣜձࣾτΫόΠ
    w "OESPJEΤϯδχΞ
    ࣗݾ঺հ

    View full-size slide

  3. w /VMM҆શ
    w ֦ுؔ਺
    w WBSWBM
    ,PUMJOͱ͍͑͹ʁ

    View full-size slide

  4. w /VMM҆શ
    w ֦ுؔ਺
    w WBSWBM
    w %FMFHBUFE1SPQFSUZ
    ,PUMJOͱ͍͑͹ʁ

    View full-size slide

  5. %FMFHBUFE1SPQFSUZ

    View full-size slide

  6. w ϓϩύςΟͷHFUUFSTFUUFSॲཧΛ
    ผͷΫϥεΦϒδΣΫτʹҕৡͰ͖Δ
    ࢓૊Έ
    w CZΩʔϫʔυͰҕৡઌΛࢦఆ͢Δ
    %FMFHBUFE1SPQFSUZ

    View full-size slide

  7. ҕৡ͢Δଆ
    var foo1: String by Foo()
    var foo2: String by getFoo()
    fun getFoo() = Foo()
    fun main(vararg args: String) {
    var foo2: String by getFoo()
    }
    CZͷޙΖʹҕৡઌͷ
    ΫϥεΠϯελϯε

    View full-size slide

  8. ҕৡ͢Δଆ
    var foo1: String by Foo()
    var foo2: String by getFoo()
    fun getFoo() = Foo()
    fun main(vararg args: String) {
    var foo2: String by getFoo()
    }
    CZͷޙΖʹҕৡઌͷ
    ΫϥεΠϯελϯε
    ҕৡઌΛฦ͢
    ؔ਺Ͱ΋Մ

    View full-size slide

  9. ҕৡ͞ΕΔଆ
    var foo1: String by Foo()
    var foo2: String by getFoo()
    fun getFoo() = Foo()
    fun main(vararg args: String) {
    var foo2: String by getFoo()
    }
    CZͷޙΖʹҕৡઌͷ
    ΫϥεΠϯελϯε
    ҕৡઌΛฦ͢
    ؔ਺Ͱ΋Մ
    ,PUMJO͔Β͸
    ϩʔΧϧม਺Ͱ΋ར༻Մೳʹʂ

    View full-size slide

  10. ҕৡ͞ΕΔଆ
    class Foo {
    operator fun getValue(thisRef: Any?,
    property: KProperty<*>): String {
    return "get value through delegation"
    }
    operator fun setValue(thisRef: Any?,
    property: KProperty<*>,
    value: String) {
    Log.d(TAG, "set value $value through delegation")
    }
    }

    View full-size slide

  11. ҕৡ͞ΕΔଆ
    class Foo {
    operator fun getValue(thisRef: Any?,
    property: KProperty<*>): String {
    return "get value through delegation"
    }
    operator fun setValue(thisRef: Any?,
    property: KProperty<*>,
    value: String) {
    Log.d(TAG, “set value $value through delegation")
    }
    } HFU7BMVF TFU7BMVF
    PQFSBUPSΛ࣮૷

    View full-size slide

  12. ҕৡ͞ΕΔଆ
    class Foo : ReadWriteProperty {
    override fun getValue(thisRef: Any?,
    property: KProperty<*>): String {
    return "get value through delegation"
    }
    override fun setValue(thisRef: Any?,
    property: KProperty<*>,
    value: String) {
    Log.d(TAG, "set value $value through delegation")
    }
    } ΠϯλʔϑΣʔεΛ
    ࣮૷͢Δͱগָ͠

    View full-size slide

  13. HFUUFSTFUUFSͷ
    ΧελϚΠζͱԿ͕ҧ͏ͷʁ

    View full-size slide

  14. %FMFHBUFEQSPQFSUZ
    class Foo {
    operator fun getValue(thisRef: Any?,
    property: KProperty<*>): String {
    return "get value through delegation"
    }
    operator fun setValue(thisRef: Any?,
    property: KProperty<*>,
    value: String) {
    Log.d(TAG, “set value $value through delegation")
    }
    }

    View full-size slide

  15. HFUUFSTFUUFS
    var foo: String
    get() = "get value through getter"
    set(value) {
    Log.d(TAG, "set value $value through delegation")
    }

    View full-size slide

  16. HFUUFSTFUUFS
    ࣮૷ͷ΄͏͕ѹ౗తʹ؆୯

    View full-size slide

  17. %FMFHBUFE1SPQFSUZΛ
    ࢖͏΂͖৔໘

    View full-size slide

  18. w ڞ௨ԽͰ͖Δॲཧ
    w ෳࡶͳঢ়ଶΛอ͍࣋ͨ͠
    w ෳࡶͳॲཧΛ͍ͨ͠
    w ΍Γա͗஫ҙ
    %FMFHBUF1SPQFSUZΛ࢖͏΂͖৔໘

    View full-size slide

  19. ڞ௨Խͯ͠੾Γग़ͯ͠
    ҕৡݩ΍ϓϩύςΟͷ৘ใ͕
    Θ͔Βͳ͘ͳΒͳ͍ʁ

    View full-size slide

  20. w ҕৡݩΫϥεͷऔಘɾ੍ݶ
    w ϓϩύςΟͷϝλσʔλࢀর
    %FMFHBUF1SPQFSUZͰͰ͖Δ͜ͱ

    View full-size slide

  21. ϓϩύςΟͷϝλσʔλ
    class Foo {
    operator fun getValue(thisRef: Any?,
    property: KProperty<*>): String {
    return "get value through delegation"
    }
    operator fun setValue(thisRef: Any?,
    property: KProperty<*>,
    value: String) {
    Log.d(TAG, "set value $value through delegation")
    }
    } ҕৡݩͷϓϩύςΟ͕ଐ͢ΔΫϥε
    ܕͰ੍ݶ͢Δ͜ͱ΋Մೳ

    View full-size slide

  22. ϓϩύςΟͷϝλσʔλ
    class Foo {
    operator fun getValue(thisRef: Any?,
    property: KProperty<*>): String {
    return "get value through delegation"
    }
    operator fun setValue(thisRef: Any?,
    property: KProperty<*>,
    value: String) {
    Log.d(TAG, "set value $value through delegation")
    }
    } ҕৡݩͷϓϩύςΟͷ
    ϝλσʔλΛࢀরՄೳ

    View full-size slide

  23. ϓϩύςΟͷϝλσʔλ
    class Bar {

    operator fun provideDelegate(thisRef: Any?,
    property: KProperty<*>): ReadOnlyProperty {
    return this
    }
    }
    ,PUMJO͔Β͸
    ҕৡΛఏڙͨ͠
    λΠϛϯάͰݺ͹ΕΔ
    PQFSBUPS͕௥Ճ͞Εͨ

    View full-size slide

  24. ϓϩύςΟͷϝλσʔλ
    class Sample {
    var bar: String by Bar()
    }
    ΫϥεͷΠϯελϯεΛ
    ੜ੒ͨ͠ஈ֊ͰϓϩύςΟͷ৘ใΛ
    ಘΒΕΔΑ͏ʹͳͬͨ

    View full-size slide

  25. w HFUUFSTFUUFSͷΧελϚΠζͰ

    े෼ͳ৔໘͕ଟ͍
    w ඪ४ϥΠϒϥϦ΍αʔυύʔςΟͷ

    ϥΠϒϥϦΛ༗ޮ׆༻͢Δ
    %FMFHBUFE1SPQFSUZΛ࣮૷͢Δ͔

    View full-size slide

  26. "OESPJEͰ
    %FMFHBUFE1SPQFSUZ

    View full-size slide

  27. ඪ४ϥΠϒϥϦ

    View full-size slide

  28. w ϓϩύςΟʹॳΊͯΞΫηεͨ࣌͠ʹ
    ϒϩοΫͷ಺༰Λ࣮ߦ
    w ࠷ऴߦͷ஋͕ճ໨Ҏ߱ͷ

    ΞΫηεͰ͸࢖ΘΕΔ
    MB[Z

    View full-size slide

  29. w "OESPJEʹ͸ίϯετϥΫλͰ

    ॳظԽͰ͖ͳ͍΋ͷ͕ଟ͍
    w FY
    "DUJWJUZͷPO$SFBUFҎ߱ʹΞ
    ΫηεՄೳͳ΋ͷ
    w *OUFOU
    w γεςϜαʔϏε
    w FUDʜ
    MB[Z

    View full-size slide

  30. MB[Z
    var param: String? = null
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    param = intent.getStringExtra("param")
    Log.d(TAG, param)
    } ,PUMJOͷϓϩύςΟ͸
    ॳظ஋͕ඞཁ

    View full-size slide

  31. lateinit var param: String
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    param = intent.getStringExtra("param")
    Log.d(TAG, param)
    }
    MB[Z
    MBUFJOJUͰॳظ஋ෆཁʹͰ͖Δ

    View full-size slide

  32. lateinit var param: String
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    param = intent.getStringExtra("param")
    Log.d(TAG, param)
    }
    MB[Z
    Ͱ΋Ұ౓୅ೖͨ͠Βมߋ͢Δඞཁ͕
    ͳ͍ͷʹWBSʹ͢Δͷ͸ؾ࣋ͪѱ͍ʜ

    View full-size slide

  33. val param: String by lazy { intent.getStringExtra("param") }
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    Log.d(TAG, param)
    }
    MB[Z
    Πϯελϯεੜ੒࣌ʹ͸࣮ߦ͞Εͳ͍
    WBMͰએݴ

    View full-size slide

  34. val param: String by lazy { intent.getStringExtra("param") }
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    Log.d(TAG, param)
    }
    MB[Z
    ࠷ॳʹΞΫηεͨ࣌͠ʹॳΊͯॳظԽ

    View full-size slide

  35. w ϥΠϑαΠΫϧʹ஫ҙ͕ඞཁ
    w ಛʹ'SBHNFOU
    MB[Z

    View full-size slide

  36. PCTFSWBCMFWFUPBCMF

    View full-size slide

  37. w ϓϩύςΟͷมߋΛݕ஌
    w WFUPBCMFͰ͸มߋΛड͚ೖΕΔ͔

    ൑ఆ͢Δ͜ͱ͕Ͱ͖Δ
    PCTFSWBCMFWFUPBCMF

    View full-size slide

  38. w XBZ%BUB#JOEJOHͱ

    ૬ੑ͕ྑͦ͞͏
    PCTFSWBCMFWFUPBCMF

    View full-size slide

  39. w &EJU5FYUʹԿ͔ೖྗ͞Ε͍ͯͨΒ

    ૹ৴ϘλϯΛ༗ޮʹ͢Δ
    PCTFSWBCMFWFUPBCMF

    View full-size slide

  40. PCTFSWBCMFWFUPBCMF
    class ViewModel {
    var inputText: String by Delegates.observable("")
    { property, oldValue, newValue ->
    buttonEnabled.set(newValue.isNotEmpty())
    }
    var buttonEnabled = ObservableBoolean(false)
    }
    ςΩετ͕ۭͰͳ͚Ε͹ϘλϯΛ༗ޮʹ͢Δ
    %FMFHBUFE1SPQFSUZͷػೳ

    View full-size slide

  41. PCTFSWBCMFWFUPBCMF
    class ViewModel {
    var inputText: String by Delegates.observable("")
    { property, oldValue, newValue ->
    buttonEnabled.set(newValue.isNotEmpty())
    }
    val buttonEnabled = ObservableBoolean(false)
    }
    Ϙλϯͷ༗ޮແޮΛ%BUB#JOEJOHʹ௨஌͢Δ
    %BUB#JOEJOHͷػೳ

    View full-size slide

  42. PCTFSWBCMFWFUPBCMF





    android:hint="Please input some text"
    android:text="@={viewModel.inputText}"
    />
    android:enabled="@{viewModel.buttonEnabled}"
    android:text="Send"
    />


    View full-size slide

  43. PCTFSWBCMFWFUPBCMF





    android:hint="Please input some text"
    android:text="@={viewModel.inputText}"
    />
    android:enabled="@{viewModel.buttonEnabled}"
    android:text="Send"
    />


    View full-size slide

  44. PCTFSWBCMFWFUPBCMF
    class MainActivity : AppCompatActivity() {
    val binding by lazy {
    DataBindingUtil.setContentView(
    this@MainActivity, R.layout.activity_main)
    }
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding.viewModel = ViewModel()
    }
    }

    View full-size slide

  45. PCTFSWBCMFWFUPBCMF

    View full-size slide

  46. αʔυύʔςΟ
    ϥΠϒϥϦ

    View full-size slide

  47. ,PUUFS,OJGF
    IUUQTHJUIVCDPN+BLF8IBSUPOLPUUFSLOJGF

    View full-size slide

  48. w #VUUFS,OJGFͷ+BLFۘ੡
    w #VUUFS,OJGFͷ,PUMJOόʔδϣϯ
    ,PUUFS,OJGF

    View full-size slide

  49. ,PUUFS,OJGF
    val firstName: TextView by bindView(R.id.first_name)
    val lastName: TextView by bindView(R.id.last_name)
    // Optional binding.
    val details: TextView? by bindOptionalView(R.id.details)

    View full-size slide

  50. w ࣮૷͸#VUUFS,OJGFLUͷ

    ϑΝΠϧͷΈ
    w ࣗ࡞%FMFHBUFE1SPQFSUZͷ

    ࠷ॳͷษڧʹͪΐ͏ͲΑ͍
    w 'SBHNFOUͰ࢖͏ͱ͖͸஫ҙ͕ඞཁ
    w %BUB#JOEJOH࢖͍ͬͯΕ͹

    ΋͏ඞཁ͸ͳ͍ʜ
    ,PUUFS,OJGF

    View full-size slide

  51. ,PUQSFG
    IUUQTHJUIVCDPNDIJCBUDIJOH,PUQSFG

    View full-size slide

  52. w 4IBSFE1SFGFSFODFͷѻ͍Λ؆୯ʹ
    ͢ΔͨΊͷϥΠϒϥϦ
    w ࢲ͕࡞Γ·ͨ͠#
    ,PUQSFG

    View full-size slide

  53. ,PUQSFG
    object UserInfo : KotprefModel() {
    var name by stringPref()
    var age by intPref(default = 14)
    }
    fun save(name: String, age: Int) {
    UserInfo.name = name
    UserInfo.age = age
    }
    fun show() {
    nameTextView.text = UserInfo.name
    ageTextView.text = UserInfo.age
    }
    1SFGFSFODFͷΩʔ͸
    ࢦఆෆཁ

    View full-size slide

  54. ,PUQSFG
    UserInfo.xml


    chibatching


    ϓϩύςΟ໊ΛΩʔʹͯ͠อଘ
    Ϋϥε໊ͷ9.-Λੜ੒

    View full-size slide

  55. w ϓϩύςΟ໊͕Ωʔ஋ʹͳΔ
    w Ωʔ஋ͷ؅ཧΛTUSJOHϦιʔε΍ఆ
    ਺ΫϥεͰ͠ͳ͍͍͘
    ,PUQSFG

    View full-size slide

  56. w ,PEFJO
    w %*ϥΠϒϥϦ
    w SFNPUFLPOpH
    w 'JSFCBTF3FNPUF$POpHͷσʔλΞΫηε
    ΄͔ʹ΋ʜ
    IUUQTHJUIVCDPN4BMPNPO#SZT,PEFJO
    IUUQTHJUIVCDPNDIJCBUDIJOHSFNPUFLPOpH

    View full-size slide

  57. w %FMFHBUFE1SPQFSUZ͸ෳࡶͳ

    ϓϩύςΟͷΞΫηεॲཧΛ

    ࣮ݱͰ͖Δ
    w ैདྷͳΒ"OOPUBUJPO1SPDFTTPSͰ΍ͬͯ
    ͍ͨΑ͏ͳ͜ͱ΋৔߹ʹΑͬͯ͸Մೳ
    w HFUUFSTFUUFSͷΧελϚΠζ΍

    ϥΠϒϥϦͷ׆༻ͱ߹Θͤͯݕ౼
    ·ͱΊ

    View full-size slide