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

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

6b7fa48f32e52512924ff44597d9881f?s=128

chibatching

June 14, 2017
Tweet

Transcript

  1. %FMFHBUFEQSPQFSUZͰ "OESPJE։ൃΛ΋ͬͱศརʹ  (VOPTZ#FFS#BTI גࣜձࣾτΫόΠ ઍ༿ོ༤

  2. w ઍ༿ོ༤ !DIJCBUDIJOH  w גࣜձࣾτΫόΠ w "OESPJEΤϯδχΞ ࣗݾ঺հ

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

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

  5. %FMFHBUFE1SPQFSUZ

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

  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ͷޙΖʹҕৡઌͷ ΫϥεΠϯελϯε
  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ͷޙΖʹҕৡઌͷ ΫϥεΠϯελϯε ҕৡઌΛฦ͢ ؔ਺Ͱ΋Մ
  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͔Β͸ ϩʔΧϧม਺Ͱ΋ར༻Մೳʹʂ
  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") } }
  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Λ࣮૷
  12. ҕৡ͞ΕΔଆ class Foo : ReadWriteProperty<Any?, String> { 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") } } ΠϯλʔϑΣʔεΛ ࣮૷͢Δͱগָ͠
  13. HFUUFSTFUUFSͷ ΧελϚΠζͱԿ͕ҧ͏ͷʁ

  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") } }
  15. HFUUFSTFUUFS var foo: String get() = "get value through getter"

    set(value) { Log.d(TAG, "set value $value through delegation") }
  16. HFUUFSTFUUFS ࣮૷ͷ΄͏͕ѹ౗తʹ؆୯

  17. %FMFHBUFE1SPQFSUZΛ ࢖͏΂͖৔໘

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

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

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

  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") } } ҕৡݩͷϓϩύςΟ͕ଐ͢ΔΫϥε ܕͰ੍ݶ͢Δ͜ͱ΋Մೳ
  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") } } ҕৡݩͷϓϩύςΟͷ ϝλσʔλΛࢀরՄೳ
  23. ϓϩύςΟͷϝλσʔλ class Bar { … operator fun provideDelegate(thisRef: Any?, property:

    KProperty<*>): ReadOnlyProperty<Any?, T> { return this } } ,PUMJO͔Β͸ ҕৡΛఏڙͨ͠ λΠϛϯάͰݺ͹ΕΔ PQFSBUPS͕௥Ճ͞Εͨ
  24. ϓϩύςΟͷϝλσʔλ class Sample { var bar: String by Bar() }

    ΫϥεͷΠϯελϯεΛ ੜ੒ͨ͠ஈ֊ͰϓϩύςΟͷ৘ใΛ ಘΒΕΔΑ͏ʹͳͬͨ
  25. w HFUUFSTFUUFSͷΧελϚΠζͰ
 े෼ͳ৔໘͕ଟ͍ w ඪ४ϥΠϒϥϦ΍αʔυύʔςΟͷ
 ϥΠϒϥϦΛ༗ޮ׆༻͢Δ %FMFHBUFE1SPQFSUZΛ࣮૷͢Δ͔

  26. "OESPJEͰ %FMFHBUFE1SPQFSUZ

  27. ඪ४ϥΠϒϥϦ

  28. MB[Z

  29. w ϓϩύςΟʹॳΊͯΞΫηεͨ࣌͠ʹ ϒϩοΫͷ಺༰Λ࣮ߦ w ࠷ऴߦͷ஋͕ճ໨Ҏ߱ͷ
 ΞΫηεͰ͸࢖ΘΕΔ MB[Z

  30. w "OESPJEʹ͸ίϯετϥΫλͰ
 ॳظԽͰ͖ͳ͍΋ͷ͕ଟ͍ w FY "DUJWJUZͷPO$SFBUFҎ߱ʹΞ ΫηεՄೳͳ΋ͷ w *OUFOU w

    γεςϜαʔϏε w FUDʜ MB[Z
  31. MB[Z var param: String? = null override fun onCreate(savedInstanceState: Bundle?)

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

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

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

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

    onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) Log.d(TAG, param) } MB[Z ࠷ॳʹΞΫηεͨ࣌͠ʹॳΊͯॳظԽ
  36. w ϥΠϑαΠΫϧʹ஫ҙ͕ඞཁ w ಛʹ'SBHNFOU MB[Z

  37. PCTFSWBCMFWFUPBCMF

  38. w ϓϩύςΟͷมߋΛݕ஌ w WFUPBCMFͰ͸มߋΛड͚ೖΕΔ͔
 ൑ఆ͢Δ͜ͱ͕Ͱ͖Δ PCTFSWBCMFWFUPBCMF

  39. w XBZ%BUB#JOEJOHͱ
 ૬ੑ͕ྑͦ͞͏ PCTFSWBCMFWFUPBCMF

  40. w &EJU5FYUʹԿ͔ೖྗ͞Ε͍ͯͨΒ
 ૹ৴ϘλϯΛ༗ޮʹ͢Δ PCTFSWBCMFWFUPBCMF

  41. PCTFSWBCMFWFUPBCMF class ViewModel { var inputText: String by Delegates.observable("") {

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

    property, oldValue, newValue -> buttonEnabled.set(newValue.isNotEmpty()) } val buttonEnabled = ObservableBoolean(false) } Ϙλϯͷ༗ޮແޮΛ%BUB#JOEJOHʹ௨஌͢Δ %BUB#JOEJOHͷػೳ
  43. PCTFSWBCMFWFUPBCMF <layout> <data> <variable name="viewModel" type="com.example.ViewModel"/> </data> <LinearLayout android:orientation="vertical"> <EditText

    android:hint="Please input some text" android:text="@={viewModel.inputText}" /> <Button android:enabled="@{viewModel.buttonEnabled}" android:text="Send" /> </LinearLayout> </layout>
  44. PCTFSWBCMFWFUPBCMF <layout> <data> <variable name="viewModel" type="com.example.ViewModel"/> </data> <LinearLayout android:orientation="vertical"> <EditText

    android:hint="Please input some text" android:text="@={viewModel.inputText}" /> <Button android:enabled="@{viewModel.buttonEnabled}" android:text="Send" /> </LinearLayout> </layout>
  45. PCTFSWBCMFWFUPBCMF class MainActivity : AppCompatActivity() { val binding by lazy

    { DataBindingUtil.setContentView<ActivityMainBinding>( this@MainActivity, R.layout.activity_main) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding.viewModel = ViewModel() } }
  46. PCTFSWBCMFWFUPBCMF

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

  48. ,PUUFS,OJGF IUUQTHJUIVCDPN+BLF8IBSUPOLPUUFSLOJGF

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

  50. ,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)
  51. w ࣮૷͸#VUUFS,OJGFLUͷ
 ϑΝΠϧͷΈ w ࣗ࡞%FMFHBUFE1SPQFSUZͷ
 ࠷ॳͷษڧʹͪΐ͏ͲΑ͍ w 'SBHNFOUͰ࢖͏ͱ͖͸஫ҙ͕ඞཁ w %BUB#JOEJOH࢖͍ͬͯΕ͹


    ΋͏ඞཁ͸ͳ͍ʜ ,PUUFS,OJGF
  52. ,PUQSFG IUUQTHJUIVCDPNDIJCBUDIJOH,PUQSFG

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

  54. ,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ͷΩʔ͸ ࢦఆෆཁ
  55. ,PUQSFG UserInfo.xml <?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map> <string name="name">chibatching</string>

    <int name="age" value="31" /> </map> ϓϩύςΟ໊ΛΩʔʹͯ͠อଘ Ϋϥε໊ͷ9.-Λੜ੒
  56. w ϓϩύςΟ໊͕Ωʔ஋ʹͳΔ w Ωʔ஋ͷ؅ཧΛTUSJOHϦιʔε΍ఆ ਺ΫϥεͰ͠ͳ͍͍͘ ,PUQSFG

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

    IUUQTHJUIVCDPNDIJCBUDIJOHSFNPUFLPOpH
  58. w %FMFHBUFE1SPQFSUZ͸ෳࡶͳ
 ϓϩύςΟͷΞΫηεॲཧΛ
 ࣮ݱͰ͖Δ w ैདྷͳΒ"OOPUBUJPO1SPDFTTPSͰ΍ͬͯ ͍ͨΑ͏ͳ͜ͱ΋৔߹ʹΑͬͯ͸Մೳ w HFUUFSTFUUFSͷΧελϚΠζ΍
 ϥΠϒϥϦͷ׆༻ͱ߹Θͤͯݕ౼

    ·ͱΊ