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

Delegated propertyで Android開発をもっと便利に @ Gunosy B...

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

chibatching

June 14, 2017
Tweet

More Decks by chibatching

Other Decks in Programming

Transcript

  1. ҕৡ͢Δଆ 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. ҕৡ͢Δଆ 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. ҕৡ͞ΕΔଆ 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. ҕৡ͞ΕΔଆ 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. ҕৡ͞ΕΔଆ 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. ҕৡ͞ΕΔଆ 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. %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. HFUUFSTFUUFS var foo: String get() = "get value through getter"

    set(value) { Log.d(TAG, "set value $value through delegation") }
  9. ϓϩύςΟͷϝλσʔλ 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. ϓϩύςΟͷϝλσʔλ 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 Bar { … operator fun provideDelegate(thisRef: Any?, property:

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

    ΫϥεͷΠϯελϯεΛ ੜ੒ͨ͠ஈ֊ͰϓϩύςΟͷ৘ใΛ ಘΒΕΔΑ͏ʹͳͬͨ
  13. MB[Z var param: String? = null override fun onCreate(savedInstanceState: Bundle?)

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

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

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

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

    onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) Log.d(TAG, param) } MB[Z ࠷ॳʹΞΫηεͨ࣌͠ʹॳΊͯॳظԽ
  18. PCTFSWBCMFWFUPBCMF class ViewModel { var inputText: String by Delegates.observable("") {

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

    property, oldValue, newValue -> buttonEnabled.set(newValue.isNotEmpty()) } val buttonEnabled = ObservableBoolean(false) } Ϙλϯͷ༗ޮແޮΛ%BUB#JOEJOHʹ௨஌͢Δ %BUB#JOEJOHͷػೳ
  20. 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>
  21. 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. 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() } }
  23. ,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)
  24. ,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ͷΩʔ͸ ࢦఆෆཁ
  25. ,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.-Λੜ੒