Slide 1

Slide 1 text

KotlinͷݴޠػೳΛ ϑϧ׆༻ͨ͠ AndroidΞϓϦͷ։ൃ id:takuji31 Hatena Engineer Seminar #9

Slide 2

Slide 2 text

஫ҙࣄ߲ • AndroidΞϓϦ։ൃͷલఏ஌ࣝͱͳΔΑ͏ͳ ෦෼ͷղઆ͸͠·ͤΜ • ͜ͷεϥΠυʹॻ͔Ε͍ͯΔKotlinͷίʔυ΍ ࢓༷͸όʔδϣϯ1.1.51࣌఺ͷ΋ͷͰ͢

Slide 3

Slide 3 text

ࣗݾ঺հ • ੢ྛ ୓ࢤ (ʹ͠͹΍͠ ͨ͘͡) • (hatena | twitter | github) takuji31 • גࣜձࣾ͸ͯͳ ΞϓϦέʔγϣϯΤϯδχΞ • ϒοΫϚʔΫΞϓϦνʔϜॴଐ

Slide 4

Slide 4 text

ࣗݾ঺հ • Android։ൃ 2010೥2݄ʙ • Kotlin 2014೥12݄ʙ • iOS΍Webͷ։ൃ΋ᅂΉ

Slide 5

Slide 5 text

׆ಈ • Koreference • KotlinͰSharedPreferencesͷϞσϧΛ࡞Δ ϥΠϒϥϦʔ • github.com/takuji31/Koreference • Kansai.kt ӡӦ

Slide 6

Slide 6 text

ϓϥΠϕʔτ • Χϝϥ(not Ϩϯζ)পॅਓ • ϚΠϘ΢ϥʔ (AVG175) • ͏͗͞ • ΞΠίϯ͸͏ͪͷࢠͰ͢

Slide 7

Slide 7 text

ͱ͜ΖͰ

Slide 8

Slide 8 text

Έͳ͞ΜAndroidΞϓϦ ։ൃͯ͠·͔͢ʁ

Slide 9

Slide 9 text

Kotlinॻ͍ͯ·͔͢ʁ

Slide 10

Slide 10 text

Kotlinͷݴޠػೳ׆༻ ͍ͯ͠·͔͢ʁ

Slide 11

Slide 11 text

Null҆શ͚ͩͰຬ଍͍ͯ͠· ͤΜ͔ʁ

Slide 12

Slide 12 text

ࠓ೔͸͸ͯͳϒοΫϚʔΫ AndroidΞϓϦͰͷKotlin ׆༻ࣄྫΛ঺հ͠·͢

Slide 13

Slide 13 text

AGENDA • Kotlinಋೖ·ͰͷಓͷΓ • KotlinͷݴޠػೳΛ׆༻͢Δ • data class • Extension method / property • Delegated property

Slide 14

Slide 14 text

Kotlin ಋೖ·ͰͷಓͷΓ

Slide 15

Slide 15 text

͸ͯͳͰͷ৽ٕज़ (ݴޠ΍ϛυϧ΢ΣΞ) ಋೖ

Slide 16

Slide 16 text

ಋೖ·Ͱ • ΤϯδχΞ͕ىҊ • ΤϯδχΞνʔϑձͰಋೖͷੋඇΛٞ࿦ • CTO͕ঝೝ

Slide 17

Slide 17 text

Kotlinͷ৔߹ • I/O Keynote͕ऴΘͬͨ௚ޙͷேɺKotlinಋೖ ىҊ (5/18) • ΤϯδχΞνʔϑձ • ঝೝ (5/19) • ͜͜·Ͱ͓Αͦ1೔൒

Slide 18

Slide 18 text

Kotlin+͸ͯͳϒοΫϚʔΫͷ৔߹ • (ि໌͚ͯ) 22೔͸νʔϜͷσΟϨΫλʔ͕
 ෆࡏͩͬͨͷͰ1ճٳΈ • 23೔ேʹσΟϨΫλʔͱձ࿩ͯ͠ಋೖܾఆ • ଈ೔ಋೖ

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

Kotlinͷ ݴޠػೳΛ ׆༻͢Δ

Slide 22

Slide 22 text

data class

Slide 23

Slide 23 text

ԿΒ͔ͷσʔλΛอ࣋͢Δͨ ΊͷΫϥε

Slide 24

Slide 24 text

Model΍DTOΛ࡞ΔͨΊͷ ศརػೳຬࡌ

Slide 25

Slide 25 text

ྫ) طଘͷmodelΛKotlinʹ ม׵͍ͨ͠

Slide 26

Slide 26 text

ม׵͸Convert Java file to Kotlin fileͰ

Slide 27

Slide 27 text

୯७ͳม׵͚ͩͰΑ͍ͷ͔ʁ

Slide 28

Slide 28 text

୯७ͳม׵͚ͩͰΑ͍ͷ͔ʁ • Nullableͳͷ͔NonNullͳͷ͔ࢦఆ͍ͨ͠ • equals/hashCode/toStringΛ࣮૷͍ͨ͠

Slide 29

Slide 29 text

ྫʣϢʔβʔͷmodelΛఆٛ • id • ໊લ • ੏ • ໊ • ϓϩϑΟʔϧ • ొ࿥ঢ়ଶ

Slide 30

Slide 30 text

Javaͷmodel(Ұ෦) public class JavaUser { private final long id; private String firstName; private String lastName; private String profile; private boolean registered; public JavaUser(long id, String firstName, String lastName, String profile, boolean registered) { this.id = id; this.firstName = firstName; this.lastName = lastName; this.profile = profile; this.registered = registered; } public long getId() { return id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getProfile() { return profile; } public void setProfile(String profile) { this.profile = profile; } public boolean isRegistered() { return registered; } public void setRegistered(boolean registered) { this.registered = registered; } public String getFullName() { return firstName + " " + lastName; } }

Slide 31

Slide 31 text

ಡΊͳ͍

Slide 32

Slide 32 text

Kotlinͷmodel data class JavaUser( val id: Long, var firstName: String, var lastName: String, var profile: String?, var isRegistered: Boolean = false ) { val fullName: String get() = firstName + " " + lastName } ಄ʹEBUB Λ͚ͭΔ શͯͷ CBDLJOHpFME ϓϩύςΟʔΛ DPOTUSVDUPSͰఆٛ

Slide 33

Slide 33 text

equals/hashCode/toString →ࣗಈੜ੒

Slide 34

Slide 34 text

boilerplate codeΛ ࡟ݮ͖ͯͬ͢͠Γ

Slide 35

Slide 35 text

Extension

Slide 36

Slide 36 text

֦ுؔ਺/֦ுϓϩύςΟʔ

Slide 37

Slide 37 text

طଘͷΫϥεʹ ؔ਺΍ϓϩύςΟʔΛੜ΍͢ (Α͏ʹݟͤΔ)࢓૊Έ

Slide 38

Slide 38 text

FragmentΛ֦ுϓϩύςΟʔͰ ֦ு͢Δ val Fragment.baseActivity: BaseActivity?
 get() = activity as BaseActivity?
 val Fragment.actionBar: ActionBar?
 get() = baseActivity?.supportActionBar ֦ு͍ͨ͠ܕ Λॻ͘ ͦͷଞ͸௨ৗͷ ϓϩύςΟʔͱಉ͡

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

طଘͷJavaΫϥε΋ ֦ுͰ͖Δ

Slide 41

Slide 41 text

(Javaଆ͔Β͸) ୈ1Ҿ਺ΛϨγʔόʔʹͨ͠ staticϝιουʹݟ͑Δ

Slide 42

Slide 42 text

→ طଘΫϥεͷҰ෦Λ KotlinԽͰ͖Δ

Slide 43

Slide 43 text

ػೳͷଟ͍ը໘Λ KotlinԽ

Slide 44

Slide 44 text

KotlinԽͷखॱ • ֦ுؔ਺Λఆٛ͢ΔϑΝΠϧΛ࡞Δ • JavaϑΝΠϧͷϝιουΛ֦ுؔ਺ʹͯ͠Kotlinଆʹఆ ٛ • Javaͷݩϝιου͔Β֦ுؔ਺Λݺͼग़͢ • ͋Δఔ౓ݩϑΝΠϧ͕૫ͤͨΒKotlinʹม׵ • ֦ுؔ਺ͷத਎Λݩϝιουʹ໭͍ͯ͘͠

Slide 45

Slide 45 text

pros • 1ͭͣͭdiff͕খ͘͞ͳΔͷͰϨϏϡʔ͠΍͢ ͍ • Ұ෦͚ͩKotlinԽ͢Δ͜ͱ͕Ͱ͖ͯؾܰʹ΍Ε Δ

Slide 46

Slide 46 text

cons • gitͷྺ࢙͕ফ໓ • ϑΝΠϧ͕෼͔ΕͯίʔυΛ௥͏ͷ͕গ͠େ มʹ

Slide 47

Slide 47 text

֦ு͗͢͠Δͱ Θ͚͕Θ͔Βͳ͘ͳΔ

Slide 48

Slide 48 text

͝ར༻͸ܭըతʹ

Slide 49

Slide 49 text

Delegated property

Slide 50

Slide 50 text

ҕৡϓϩύςΟʔ

Slide 51

Slide 51 text

propertyͷॲཧΛ ผͷΦϒδΣΫτʹ ҕৡͰ͖Δ

Slide 52

Slide 52 text

Data Binding Libraryͷ BaseObservableͷ มߋ௨஌ΛࣗಈԽ

Slide 53

Slide 53 text

Delegated propertyͷఆٛ open class BaseObservableProperty( vararg val propertyId: Int, var value: T ) : ReadWriteProperty { override fun getValue( thisRef: BaseObservable, property: KProperty<*>): T { return value } override fun setValue( thisRef: BaseObservable, property: KProperty<*>, value: T) { this.value = value propertyId.forEach(thisRef::notifyPropertyChanged) } } 3FBE8SJUF1SPQFSUZ ΠϯλʔϑΣΠεΛ࣮૷ HFU7BMVFTFU7BMVFΛ ఆٛ UIJTͷࢀরͱQSPQFSUZͷ ৘ใ͕౉͞ΕΔ

Slide 54

Slide 54 text

Delegated propertyͷఆٛ class NonNullBaseObservableProperty(vararg propertyId: Int, value: T) : BaseObservableProperty(propertyId = *propertyId, value = value) fun BaseObservable.property(vararg propertyId: Int, value: T? = null) = BaseObservableProperty(propertyId = *propertyId, value = value) fun BaseObservable.nonNullProperty(vararg propertyId: Int, value: T) = NonNullBaseObservableProperty(propertyId = *propertyId, value = value) ܕ͕/PO/VMMͳ΋ͷ༻ͷఆٛ ֦ுؔ਺ʹ͓ͯ͘͠ͱ ࢖͏࣌ʹศར

Slide 55

Slide 55 text

ར༻͢Δ class ViewModel: BaseObservable() { @get:Bindable var users: List by nonNullProperty(BR.users, value = emptyList()) fun requestUsers(): Disposable { return getUser .execute() .subscribeBy( onSuccess = { user -> this.users = users }, ) } } CZͷޙʹΦϒδΣΫτΛ౉͢ TFU͢Δ͜ͱͰࣗಈతʹ OPUJGZ1SPQFSUZ$IBOHFE͕ ݺ͹ΕΔ

Slide 56

Slide 56 text

ศརͦ͏͚ͩͲ Ͳ͏͍͏༻్Ͱ࢖͑Δ͔ Θ͔Βͳ͍ʁ

Slide 57

Slide 57 text

SharedPreferencesͷૢ࡞

Slide 58

Slide 58 text

͜ΕΛ࢖͍·͠ΐ͏ˠ https://github.com/ takuji31/Koreference

Slide 59

Slide 59 text

͸ͯͳϒοΫϚʔΫͰ΋ ࢖͍ͬͯ·͢

Slide 60

Slide 60 text

σϑΥϧτͷ΋ͷ΋ ༻ҙ͞Ε͍ͯ·͢

Slide 61

Slide 61 text

σϑΥϧτͷ Delegated property(Ұ෦) • Delegates.notNull • not-null͕ͩॳظԽ࣌ʹॳظԽͰ͖ͳ͍ϓϩύςΟʔ • nullͷ··ΞΫηεͨ͠Βྫ֎ • lazy • ஗ԆॳظԽ • ࠷ॳͷΞΫηε࣌ʹҾ਺Ͱ౉ͨ͠ϒϩοΫͷ໭Γ஋Λฦͯ͠ Ωϟογϡ͢Δ

Slide 62

Slide 62 text

Kotlin͸ϞμϯͳݴޠͳͷͰ ॻ͘ίʔυ΋Ϟμϯʹ ͍͖ͯ͠·͠ΐ͏ :kotlin:

Slide 63

Slide 63 text

Enjoy Kotlin Life