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

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

6b7fa48f32e52512924ff44597d9881f?s=128

chibatching

June 14, 2017
Tweet

Transcript

  1. 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ͷޙΖʹҕৡઌͷ ΫϥεΠϯελϯε
  2. 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ͷޙΖʹҕৡઌͷ ΫϥεΠϯελϯε ҕৡઌΛฦ͢ ؔ਺Ͱ΋Մ
  3. 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͔Β͸ ϩʔΧϧม਺Ͱ΋ར༻Մೳʹʂ
  4. 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") } }
  5. 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Λ࣮૷
  6. 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") } } ΠϯλʔϑΣʔεΛ ࣮૷͢Δͱগָ͠
  7. 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") } }
  8. 15.

    HFUUFSTFUUFS var foo: String get() = "get value through getter"

    set(value) { Log.d(TAG, "set value $value through delegation") }
  9. 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") } } ҕৡݩͷϓϩύςΟ͕ଐ͢ΔΫϥε ܕͰ੍ݶ͢Δ͜ͱ΋Մೳ
  10. 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") } } ҕৡݩͷϓϩύςΟͷ ϝλσʔλΛࢀরՄೳ
  11. 23.

    ϓϩύςΟͷϝλσʔλ class Bar { … operator fun provideDelegate(thisRef: Any?, property:

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

    ϓϩύςΟͷϝλσʔλ class Sample { var bar: String by Bar() }

    ΫϥεͷΠϯελϯεΛ ੜ੒ͨ͠ஈ֊ͰϓϩύςΟͷ৘ใΛ ಘΒΕΔΑ͏ʹͳͬͨ
  13. 28.
  14. 31.

    MB[Z var param: String? = null override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) param = intent.getStringExtra("param") Log.d(TAG, param) } ,PUMJOͷϓϩύςΟ͸ ॳظ஋͕ඞཁ
  15. 32.

    lateinit var param: String override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)

    param = intent.getStringExtra("param") Log.d(TAG, param) } MB[Z MBUFJOJUͰॳظ஋ෆཁʹͰ͖Δ
  16. 33.

    lateinit var param: String override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)

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

    val param: String by lazy { intent.getStringExtra("param") } override fun

    onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) Log.d(TAG, param) } MB[Z Πϯελϯεੜ੒࣌ʹ͸࣮ߦ͞Εͳ͍ WBMͰએݴ
  18. 35.

    val param: String by lazy { intent.getStringExtra("param") } override fun

    onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) Log.d(TAG, param) } MB[Z ࠷ॳʹΞΫηεͨ࣌͠ʹॳΊͯॳظԽ
  19. 41.

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

    property, oldValue, newValue -> buttonEnabled.set(newValue.isNotEmpty()) } var buttonEnabled = ObservableBoolean(false) } ςΩετ͕ۭͰͳ͚Ε͹ϘλϯΛ༗ޮʹ͢Δ %FMFHBUFE1SPQFSUZͷػೳ
  20. 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ͷػೳ
  21. 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>
  22. 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>
  23. 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() } }
  24. 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)
  25. 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ͷΩʔ͸ ࢦఆෆཁ
  26. 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.-Λੜ੒