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

Kotlin's Delegated Properties × Android

Kotlin's Delegated Properties × Android

shibuya.apk #17での発表資料

Ippei Nawate

August 04, 2017
Tweet

Other Decks in Technology

Transcript

  1. 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Λ࣋ͭ
  2. Delegated Properties class User { var id: Long get() {

    return 100 } set(id) { println(id) } }
  3. 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) } }
  4. AndroidͰͲ͏׆͔͔͢ • Activity Extra • Fragment Argument • Saved/RestoreInstanceState1 •

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

    SharedPreferences2 1. https://github.com/yamamotoj/Pikkel 2. https://github.com/chibatching/Kotpref
  6. 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) } } }
  7. 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) } } }
  8. 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 } }
  9. 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 } }
  10. 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) } } }
  11. 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) } } }
  12. 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) } } }
  13. 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) } } }
  14. 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) } } }
  15. 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) } } }
  16. 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) } } }
  17. ·ͱΊ Property • = (Backing)Field + Getter + Setter Delegated

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