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

はてなブックマークAndroidアプリでのKotlin導入

 はてなブックマークAndroidアプリでのKotlin導入

Takuji Nishibayashi

June 26, 2017
Tweet

More Decks by Takuji Nishibayashi

Other Decks in Technology

Transcript

  1. ࣗݾ঺հ • ੢ྛ ୓ࢤ (ʹ͠͹΍͠ ͨ͘͡) • (hatena|twitter|github) takuji31 •

    גࣜձࣾ͸ͯͳ ΞϓϦέʔγϣϯΤϯδχΞ • ͸ͯͳϒοΫϚʔΫΞϓϦνʔϜॴଐ 
  2. AGENDA • Kotlinʹ͍ͭͯ • ͸ͯͳϒοΫϚʔΫʹ͍ͭͯ • ͸ͯͳϒοΫϚʔΫΞϓϦνʔϜͷߏ੒ • Kotlinಋೖ·ͰͷಓͷΓ •

    Kotlinͷಋೖͱͦͷޙ • KotlinԽͰى͖Δ໰୊ • KotlinͷݴޠػೳΛ׆༻͢Δ 
  3. Kotlinͷಛ௃ • ֦ுؔ਺ / ֦ுϓϩύςΟʔ • data class / sealed

    class • delegation • Coroutine (ex. async/await) 
  4. 

  5. KotlinԽʹ౰ͨͬͯಋೖͨ͠ϥΠϒ ϥϦʔୈ1஄ • Koreference • ੿࡞ • SharedPreferencesΛ͍͍ײ͡ʹModelͬΆ͘ѻ͏ϥΠϒϥϦʔ • ϓϩύςΟʔΛఆٛ͢Δ͚ͩͰSharedPreferencesͷ஋Λૢ࡞Ͱ͖Δ

    • RxJava2ʹΑΔมߋ஋؂ࢹαϙʔτ • ͱʹ͔͘खͰSharedPreferencesͷॲཧॻ͖ͨ͘ͳ͍ͷͰಋೖ • υοάϑʔσΟϯά΋݉Ͷͯ 
  6. 

  7. 

  8. طଘͷRepositoryʹϝιουΛ ௥Ճ͢Δ • ΫϥεϑΝΠϧΛCode Convert Java file to Kotlin fileͰKotlinʹ

    ม׵ • KotlinΒ͘͠ॻ͚Δͱ͜Ζ͸ॻ͖׵͑Δ • ֦ுؔ਺ • ϓϩύςΟʔ • ϥϜμࣜ • Repository͔Βฦ͢model͸Kotlinͷdata class 
  9. 

  10. 

  11. Property class User(id: Long, name: String) {
 val id: Long


    var name: String
 var registered: Boolean = false
 
 init {
 this.id = id
 this.name = name
 }
 
 }
 
  12. Property class User(id: Long, name: String) {
 val id: Long


    var name: String
 var registered: Boolean = false
 
 init {
 this.id = id
 this.name = name
 }
 
 }
  ಡΈऔΓઐ༻
  13. Property class User(id: Long, name: String) {
 val id: Long


    var name: String
 var registered: Boolean = false
 
 init {
 this.id = id
 this.name = name
 }
 
 }
  ಡΈऔΓઐ༻ ಡΈॻ͖Մ
  14. Property class User(id: Long, name: String) {
 val id: Long


    var name: String
 var registered: Boolean = false
 
 init {
 this.id = id
 this.name = name
 }
 
 }
  ಡΈऔΓઐ༻ ಡΈॻ͖Մ ܕ
  15. Property class User(id: Long, name: String) {
 val id: Long


    var name: String
 var registered: Boolean = false
 
 init {
 this.id = id
 this.name = name
 }
 
 }
  ಡΈऔΓઐ༻ ಡΈॻ͖Մ ܕ ॳظ஋
  16. Property class User(id: Long, name: String) {
 val id: Long


    var name: String
 var registered: Boolean = false
 
 init {
 this.id = id
 this.name = name
 }
 
 }
  ಡΈऔΓઐ༻ ಡΈॻ͖Մ ܕ ॳظ஋ JOJUϒϩοΫͰ ॳظԽ
  17. ͜͏ஔ͖׵͑ΒΕ·͢ class User(val id: Long, var name: String) {
 var

    registered: Boolean = false
 }
  ίϯετϥΫλʔ಺Ͱ ϓϩύςΟʔఆٛ͢Δͱɺࣗಈతʹ୅ೖ͞ΕΔ
  18. registered΋஋Λड͚औΕΔΑ͏ʹ class User(
 val id: Long,
 var name: String,
 var

    registered: Boolean = false
 )
  SFHJTUFSFEΛ౉͞ͳ͔ͬͨΒGBMTF͕୅ೖ͞ΕΔ
  19. ࢖͏ fun main(args: Array<String>) {
 val unregisteredUser = User( id

    = 114514L, name = "takuji31", registered = false )
 val user = register(unregisteredUser)
 print(user)
 }
 
 fun register(user: User): User {
 // do something
 return User(user.id, user.name, true)
 } 
  20. એݴ data class User(
 val id: Long,
 val name: String,


    val registered: Boolean = false
 ) 
  21. એݴ data class User(
 val id: Long,
 val name: String,


    val registered: Boolean = false
 )  ಄ʹEBUB Λ͚ͭΔ
  22. એݴ data class User(
 val id: Long,
 val name: String,


    val registered: Boolean = false
 )  ಄ʹEBUB Λ͚ͭΔ શͯͷϓϩύςΟʔΛ DPOTUSVDUPSͰఆٛ
  23. ࢖͏ fun register(user: User): User {
 // do something
 return

    user.copy(registered = true)
 }  DPQZϝιουͷҾ਺ʹ มߋ͍ͨ͠஋͚ͩΛ౉͢ͱ ίϐʔ͞ΕΔ
  24. FragmentΛ֦ுϓϩύςΟʔͰ ֦ு͢Δ val Fragment.baseActivity: BaseActivity?
 get() = activity as BaseActivity?


    val Fragment.actionBar: ActionBar?
 get() = baseActivity?.supportActionBar 
  25. FragmentΛ֦ுϓϩύςΟʔͰ ֦ு͢Δ val Fragment.baseActivity: BaseActivity?
 get() = activity as BaseActivity?


    val Fragment.actionBar: ActionBar?
 get() = baseActivity?.supportActionBar  ֦ு͍ͨ͠ܕ Λॻ͘
  26. FragmentΛ֦ுϓϩύςΟʔͰ ֦ு͢Δ val Fragment.baseActivity: BaseActivity?
 get() = activity as BaseActivity?


    val Fragment.actionBar: ActionBar?
 get() = baseActivity?.supportActionBar  ֦ு͍ͨ͠ܕ Λॻ͘ ͦͷଞ͸௨ৗͷ ϓϩύςΟʔͱಉ͡
  27. RealmͷProviderΛ֦ுؔ਺Ͱ ֦ு͢Δ fun Provider<Realm>.getAutoCloseObservable(): Observable<Realm> {
 val realm = get()


    realm.use {
 return Observable.create { emitter ->
 val instance = get()
 emitter.setCancellable {
 instance.close()
 }
 emitter.onNext(instance)
 }
 }
 } 
  28. RealmͷProviderΛ֦ுؔ਺Ͱ ֦ு͢Δ fun Provider<Realm>.getAutoCloseObservable(): Observable<Realm> {
 val realm = get()


    realm.use {
 return Observable.create { emitter ->
 val instance = get()
 emitter.setCancellable {
 instance.close()
 }
 emitter.onNext(instance)
 }
 }
 }  ֦ு͍ͨ͠ܕ Λॻ͘
  29. RealmͷProviderΛ֦ுؔ਺Ͱ ֦ு͢Δ fun Provider<Realm>.getAutoCloseObservable(): Observable<Realm> {
 val realm = get()


    realm.use {
 return Observable.create { emitter ->
 val instance = get()
 emitter.setCancellable {
 instance.close()
 }
 emitter.onNext(instance)
 }
 }
 }  ֦ு͍ͨ͠ܕ Λॻ͘ (FOFSJDTͷ ܕύϥϝʔλʔ΋ࢦఆՄ
 ϓϩύςΟʔ΋ಉ༷
  30. RealmͷProviderΛ֦ுؔ਺Ͱ ֦ு͢Δ fun Provider<Realm>.getAutoCloseObservable(): Observable<Realm> {
 val realm = get()


    realm.use {
 return Observable.create { emitter ->
 val instance = get()
 emitter.setCancellable {
 instance.close()
 }
 emitter.onNext(instance)
 }
 }
 }  ֦ு͍ͨ͠ܕ Λॻ͘ ͦͷଞ͸௨ৗͷ ؔ਺ͱಉ͡ (FOFSJDTͷ ܕύϥϝʔλʔ΋ࢦఆՄ
 ϓϩύςΟʔ΋ಉ༷
  31. σϑΥϧτͷ Delegated property(Ұ෦) • Delegates.notNull • not-null͕ͩॳظԽ࣌ʹॳظԽͰ͖ͳ͍ϓϩύςΟʔ • nullͷ··ΞΫηεͨ͠Βྫ֎ •

    lazy • ஗ԆॳظԽ • ࠷ॳͷΞΫηε࣌ʹҾ਺Ͱ౉ͨ͠ϒϩοΫͷ໭Γ஋Λฦͯ͠ Ωϟογϡ͢Δ