Kotlin's Delegated Properties × Android

Kotlin's Delegated Properties × Android

shibuya.apk #17での発表資料

4195da03f4e1b649ec7266fe84e4a3e5?s=128

Ippei Nawate

August 04, 2017
Tweet

Transcript

  1. Kotlin's Delegated Properties × Android @shibuya.apk #17

  2. About me ࡧख Ұฏ / Nawate Ippei @sakuna63

  3. Property

  4. Property class User { private long id = 1; public

    long getId() { return this.name; } public void setId(long id) { this.id = id; } } class User { var id: Long = 1 } • JavaͰ͍͏FieldΈ͍ͨͳ΋ͷ • ཪͰField(Backing Field), Getter, SetterΛ࣋ͭ
  5. Delegated Properties

  6. Delegated Properties • Kotlinͷݴޠػೳͷ̍ͭ • PropertyͷGetter, SetterͷॲཧΛଞΫϥεʹ ҕৡ͢Δ࢓૊Έ

  7. Delegated Properties class User { var id: Long get() {

    return 100 } set(id) { println(id) } }
  8. Delegated Properties class User { var id: Long get() {

    return 100 } set(id) { println(id) } } class User { var id by IdProp() } class IdPref: ReadWriteProperty<Any, Long> { override fun getValue(thisRef: Any, property: KProperty<*>): Long { return 100 } override fun setValue(thisRef: Any, property: KProperty<*>, value: Long) { println(value) } }
  9. AndroidͰͲ͏׆͔͔͢

  10. AndroidͰͲ͏׆͔͔͢ • Activity Extra • Fragment Argument • Saved/RestoreInstanceState1 •

    SharedPreferences2 1. https://github.com/yamamotoj/Pikkel 2. https://github.com/chibatching/Kotpref
  11. AndroidͰͲ͏׆͔͔͢ • Activity Extra • Fragment Argument • Saved/RestoreInstanceState1 •

    SharedPreferences2 1. https://github.com/yamamotoj/Pikkel 2. https://github.com/chibatching/Kotpref
  12. Delegated Porperties × Activity Extra UserActivity.kt (Delegated Properties࢖༻લ) class UserActivity:

    Activity() { private lateinit var userId: Long override protected fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) userId = intent.getLongExtra(EXTRA_USER_ID, -1) // ... } companion object { private const val EXTRA_USER_ID = "userId" fun createIntent(context: Context, userId: Long): Intent { return Intent(context, UserActivity::class.java) .putExtra(EXTRA_USER_ID, userId) } } }
  13. Delegated Porperties × Activity Extra UserActivity.kt (Delegated Properties࢖༻લ) class UserActivity:

    Activity() { private lateinit var userId: Long // or 「var userId: Long?」 override protected fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) userId = intent.getLongExtra(EXTRA_USER_ID, -1) // ... } companion object { private const val EXTRA_USER_ID = "userId" fun createIntent(context: Context, userId: Long): Intent { return Intent(context, UserActivity::class.java) .putExtra(EXTRA_USER_ID, userId) } } }
  14. Delegated Porperties × Activity Extra Extra.kt private object UNINITIALIZED_VALUE class

    Extra<T>: ReadWriteProperty<Activity, T> { var field: Any? = UNINITIALIZED_VALUE override fun getValue(thisRef: Activity, property: KProperty<*>): T { if (field == UNINITIALIZED_VALUE) { field = thisRef.intent.extras?.get(property.name) } return field as T } override fun setValue(thisRef: Activity, property: KProperty<*>, value: T) { field = value } }
  15. Delegated Porperties × Activity Extra Extra.kt private object UNINITIALIZED_VALUE class

    Extra<T>: ReadWriteProperty<Activity, T> { var field: Any? = UNINITIALIZED_VALUE override fun getValue(thisRef: Activity, property: KProperty<*>): T { if (field == UNINITIALIZED_VALUE) { field = thisRef.intent.extras?.get(property.name) } return field as T } override fun setValue(thisRef: Activity, property: KProperty<*>, value: T) { field = value } }
  16. Delegated Porperties × Activity Extra UserActivity.kt (Delegated Properties࢖༻લ) class UserActivity:

    Activity() { private lateinit var userId: Long override protected fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) userId = intent.getLongExtra(EXTRA_USER_ID, -1) // ... } companion object { private const val EXTRA_USER_ID = "userId" fun createIntent(context: Context, userId: Long): Intent { return Intent(context, UserActivity::class.java) .putExtra(EXTRA_USER_ID, userId) } } }
  17. Delegated Porperties × Activity Extra UserActivity.kt (Delegated Properties࢖༻ޙ) class UserActivity:

    Activity() { private lateinit var userId: Long override protected fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) userId = intent.getLongExtra(EXTRA_USER_ID, -1) // ... } companion object { private const val EXTRA_USER_ID = "userId" fun createIntent(context: Context, userId: Long): Intent { return Intent(context, UserActivity::class.java) .putExtra(EXTRA_USER_ID, userId) } } }
  18. Delegated Porperties × Activity Extra UserActivity.kt (Delegated Properties࢖༻ޙ) class UserActivity:

    Activity() { private lateinit var userId: Long by Extra() private val userId: Long by Extra() override protected fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) userId = intent.getLongExtra(EXTRA_USER_ID, -1) // ... } companion object { private const val EXTRA_USER_ID = "userId" fun createIntent(context: Context, userId: Long): Intent { return Intent(context, UserActivity::class.java) .putExtra(EXTRA_USER_ID, userId) } } }
  19. Delegated Porperties × Activity Extra UserActivity.kt (Delegated Properties࢖༻ޙ) class UserActivity:

    Activity() { private val userId: Long by Extra() override protected fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // ... } companion object { private const val EXTRA_USER_ID = "userId" fun createIntent(context: Context, userId: Long): Intent { return Intent(context, UserActivity::class.java) .putExtra(EXTRA_USER_ID, userId) } } }
  20. Delegated Porperties × Activity Extra UserActivity.kt (Delegated Properties࢖༻ޙ) class UserActivity:

    Activity() { private val userId: Long by Extra() override protected fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // ... } companion object { private const val EXTRA_USER_ID = "userId" fun createIntent(context: Context, userId: Long): Intent { return Intent(context, UserActivity::class.java) .putExtra(EXTRA_USER_ID, userId) } } }
  21. Delegated Porperties × Activity Extra UserActivity.kt (Delegated Properties࢖༻ޙ) class UserActivity:

    Activity() { private val userId: Long by Extra() override protected fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // ... } companion object { private const val EXTRA_USER_ID = "userId" fun createIntent(context: Context, userId: Long): Intent { return Intent(context, UserActivity::class.java) .putExtra(EXTRA_USER_ID, userId) .putExtra(UserActivity::userId.name, userId) } } }
  22. Delegated Porperties × Activity Extra UserActivity.kt (Delegated Properties࢖༻ޙ) class UserActivity:

    Activity() { private val userId: Long by Extra() override protected fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // ... } companion object { fun createIntent(context: Context, userId: Long): Intent { return Intent(context, UserActivity::class.java) .putExtra(UserActivity::userId.name, userId) } } }
  23. ·ͱΊ Property • = (Backing)Field + Getter + Setter Delegated

    Properties • Getter, SetterͷॲཧΛ؆୯ʹҕৡՄೳ • Propertyͷϝλ৘ใʹΞΫηεͰ͖ΔͷͰɺ༷ʑͳԠ༻͕Մೳ • Androidಛ༗ͷ࢖͍ॴ΋ͨ͘͞Μ
  24. Fin.