start from Convert to Kotlin

6eecc52120fba9d87d982684211182ec?s=47 mochico
August 25, 2018

start from Convert to Kotlin

2018.8.25 Kotlin Fest 2018
初学者向けにJavaでつくられたAndroidアプリでConvertしたKotlinコードからよりよいKotlinコードの書き方を考えてみるセッションです。

サンプルコード: https://github.com/mochico/StartFromConvertToKotlinSample/

6eecc52120fba9d87d982684211182ec?s=128

mochico

August 25, 2018
Tweet

Transcript

  1. 7.
  2. 18.
  3. 19.

    MainActivity.java public class JavaMainActivity extends AppCompatActivity { RecyclerView recyclerView; ItemAdapter

    adapter = new ItemAdapter(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "tapped!", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); recyclerView = findViewById(R.id.recycler_view); recyclerView.setAdapter(adapter); } }
  4. 20.

    public class JavaItem implements Parcelable { public JavaItem(int id, String

    name) { this.id = id; this.name = name; } private int id; private String name; public int getId() { return id; } public String getName() { return name; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } private JavaItem(Parcel source) { id = source.readInt(); name = source.readString(); } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int atags) { dest.writeInt(id); dest.writeString(name); } public static final Creator<JavaItem> CREATOR = new Creator<JavaItem>() { @Override public JavaItem createFromParcel(Parcel source) { return new JavaItem(source); } @Override public JavaItem[] newArray(int size) { return new JavaItem[size]; } }; public boolean isSameItem(Object obj) { return obj instanceof JavaItem && ((JavaItem) obj).getId() == this.id; } } Item.java
  5. 24.

    public class JavaItem implements Parcelable { public JavaItem(int id, String

    name) { this.id = id; this.name = name; } private int id; private String name; public int getId() { return id; } public String getName() { return name; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } private JavaItem(Parcel source) { id = source.readInt(); name = source.readString(); } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int atags) { dest.writeInt(id); dest.writeString(name); } public static final Creator<JavaItem> CREATOR = new Creator<JavaItem>() { @Override public JavaItem createFromParcel(Parcel source) { return new JavaItem(source); } @Override public JavaItem[] newArray(int size) { return new JavaItem[size]; } }; public boolean isSameItem(Object obj) { return obj instanceof JavaItem && ((JavaItem) obj).getId() == this.id; } } Item.java
  6. 26.

    class KotlinItem : Parcelable { var id: Int = 0

    var name: String? = null constructor(id: Int, name: String) { this.id = id this.name = name } private constructor(source: Parcel) { id = source.readInt() name = source.readString() } public override fun describeContents(): Int { return 0 } public override fun writeToParcel(dest: Parcel, atags: Int) { dest.writeInt(id) dest.writeString(name) } fun isSameItem(obj: Any): Boolean { return (obj is KotlinItem && (obj as KotlinItem).id == this.id) } companion object { val CREATOR: Parcelable.Creator<KotlinItem> = object : Parcelable.Creator<KotlinItem> { public override fun createFromParcel(source: Parcel): KotlinItem { return KotlinItem(source) } public override fun newArray(size: Int): Array<KotlinItem> { return arrayOfNulls<KotlinItem>(size) } } } } Item.kt
  7. 29.
  8. 30.
  9. 31.
  10. 32.
  11. 33.

    class KotlinItem : Parcelable { var id: Int = 0

    var name: String? = null constructor(id: Int, name: String) { this.id = id this.name = name } private constructor(source: Parcel) { id = source.readInt() name = source.readString() } public override fun describeContents(): Int { return 0 } public override fun writeToParcel(dest: Parcel, atags: Int) { dest.writeInt(id) dest.writeString(name) } fun isSameItem(obj: Any): Boolean { return (obj is KotlinItem && (obj as KotlinItem).id == this.id) } companion object { val CREATOR: Parcelable.Creator<KotlinItem> = object : Parcelable.Creator<KotlinItem> { public override fun createFromParcel(source: Parcel): KotlinItem { return KotlinItem(source) } public override fun newArray(size: Int): Array<KotlinItem> { return arrayOfNulls<KotlinItem>(size) } } } } Item.kt
  12. 36.

    デフォルト引数 class KotlinItem( var id: Int = 0, var name:

    String? = null ) val item = KotlinItem(1)
  13. 37.

    class KotlinItem( var id: Int = 0, var name: String?

    = null ) : Parcelable { private constructor(source: Parcel) : this(source.readInt(),source.readString()) public override fun describeContents(): Int { return 0 } public override fun writeToParcel(dest: Parcel, atags: Int) { dest.writeInt(id) dest.writeString(name) } fun isSameItem(obj: Any): Boolean { return (obj is KotlinItem && (obj as KotlinItem).id == this.id) } companion object { val CREATOR: Parcelable.Creator<KotlinItem> = object : Parcelable.Creator<KotlinItem> { public override fun createFromParcel(source: Parcel): KotlinItem { return KotlinItem(source) } public override fun newArray(size: Int): Array<KotlinItem> { return arrayOfNulls<KotlinItem>(size) } } } } Item.kt
  14. 43.

    Item.kt data class KotlinItem( var id: Int = 0, var

    name: String? = null ) : Parcelable { private constructor(source: Parcel) : this(source.readInt(), source.readString()) public override fun describeContents(): Int { return 0 } public override fun writeToParcel(dest: Parcel, atags: Int) { dest.writeInt(id) dest.writeString(name) } fun isSameItem(obj: Any): Boolean { return (obj is KotlinItem && (obj as KotlinItem).id == this.id) } companion object { val CREATOR: Parcelable.Creator<KotlinItem> = object : Parcelable.Creator<KotlinItem> { public override fun createFromParcel(source: Parcel): KotlinItem { return KotlinItem(source) } public override fun newArray(size: Int): Array<KotlinItem> { return arrayOfNulls<KotlinItem>(size) } } } }
  15. 46.

    @Parcelize @Parcelize data class Item( var id: Int = 0,

    var name: String? = null ) : Parcelable
  16. 47.

    Decompile @Parcelize public static final android.os.Parcelable.Creator CREATOR = new Item.Creator();

    public final int describeContents() { return 0; } public final void writeToParcel(@NotNull Parcel parcel, int flags) { Intrinsics.checkParameterIsNotNull(parcel, "parcel"); parcel.writeInt(this.id); parcel.writeString(this.name); } @Metadata( mv = {1, 1, 10}, bv = {1, 0, 2}, k = 3 ) public static class Creator implements android.os.Parcelable.Creator { @NotNull public final Object[] newArray(int size) { return new Item[size]; } @NotNull public final Object createFromParcel(@NotNull Parcel in) { Intrinsics.checkParameterIsNotNull(in, "in"); return new Item(in.readInt(), in.readString()); } }
  17. 48.

    Item.kt @Parcelize data class Item( var id: Int = 0,

    var name: String? = null ) : Parcelable { fun isSameItem(obj: Any): Boolean { return (obj is KotlinItem && (obj as KotlinItem).id == this.id) } }
  18. 50.

    Item.kt @Parcelize data class KotlinItem( var id: Int = 0,

    var name: String? = null ) : Parcelable { fun isSameItem(obj: Any): Boolean { return ((obj as? KotlinItem)?.id == this.id) } }
  19. 52.
  20. 53.
  21. 61.

    Smart cast val item: Item? = null if (item ==

    null) { return } item.id item.name
  22. 66.

    named argument val item1 = Item(id = 1, name =

    "name") val item2 = Item(name = "name", id = 1)
  23. 68.

    MainActivity.java public class JavaMainActivity extends AppCompatActivity { RecyclerView recyclerView; ItemAdapter

    adapter = new ItemAdapter(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "tapped!", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); recyclerView = findViewById(R.id.recycler_view); recyclerView.setAdapter(adapter); } }
  24. 73.

    可視修飾⼦ • public:どこからも参照可能 • internal :モジュール内から のみ参照可能 • private:クラス内からのみ 参照可能

    • なし:public • public:どこからも参照可能 • protected:現在のクラスとサ ブクラスからアクセスできる • private:クラス内からのみ参 照可能 • なし:同じパッケージのクラ スから参照可能 Java Kotlin
  25. 76.

    var & val var variableNumber = 1 variableNumber = 2

    // OK val valueNumber = 1 valueNumber = 2 // ίϯύΠϧΤϥʔ
  26. 79.

    lateinit class KotlinMainActivity : AppCompatActivity() { private lateinit var recyclerView:

    RecyclerView private var adapter = ItemAdapter() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
  27. 83.

    MainActivity.kt class MainActivity : AppCompatActivity() { private lateinit var recyclerView:

    RecyclerView private var adapter = ItemAdapter() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val toolbar = findViewById<Toolbar>(R.id.toolbar) setSupportActionBar(toolbar) findViewById<View>(R.id.fab).setOnClickListener { view -> Snackbar.make(view, "tapped!", Snackbar.LENGTH_LONG) .setAction("Action", null).show() } recyclerView = findViewById(R.id.recycler_view) recyclerView.adapter = adapter } }
  28. 85.

    Java entity class public class JavaItem implements Parcelable { public

    JavaItem(int id, String name) { this.id = id; this.name = name; } private int id; private String name; public int getId() { return id; } public String getName() { return name; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } private JavaItem(Parcel source) { id = source.readInt(); name = source.readString(); } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int atags) { dest.writeInt(id); dest.writeString(name); } public static final Parcelable.Creator<JavaItem> CREATOR = new Parcelable.Creator<JavaItem>() { @Override public JavaItem createFromParcel(Parcel source) { return new JavaItem(source); } @Override public JavaItem[] newArray(int size) { return new JavaItem[size]; } }; }
  29. 86.

    Kotlin data class @Parcelize data class Item( var id: Int

    = 0, var name: String? = null ) : Parcelable { fun isSameItem(obj: Any): Boolean { return (obj as KotlinItem).id == this.id } }
  30. 87.

    MainActivity.java public class JavaMainActivity extends AppCompatActivity { RecyclerView recyclerView; ItemAdapter

    adapter = new ItemAdapter(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "tapped!", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); recyclerView = findViewById(R.id.recycler_view); recyclerView.setAdapter(adapter); } }
  31. 89.

    MainActivity.kt class MainActivity : AppCompatActivity() { private lateinit var recyclerView:

    RecyclerView private var adapter = ItemAdapter() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val toolbar = findViewById<Toolbar>(R.id.toolbar) setSupportActionBar(toolbar) findViewById<View>(R.id.fab).setOnClickListener { view -> Snackbar.make(view, "tapped!", Snackbar.LENGTH_LONG) .setAction("Action", null).show() } recyclerView = findViewById(R.id.recycler_view) recyclerView.adapter = adapter } }
  32. 94.

    ref. • Getting started with Android and Kotlin - Kotlin

    Programming Language : https://kotlinlang.org/ docs/tutorials/kotlin-android.html • Get Started with Kotlin on Android | Android Developers : https://developer.android.com/kotlin/ get-started • [Kotlinイン・アクション | マイナビブックス](https:// book.mynavi.jp/ec/products/detail/id=78137)