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

start from Convert to Kotlin

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/

mochico

August 25, 2018
Tweet

More Decks by mochico

Other Decks in Programming

Transcript

  1. 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); } }
  2. 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
  3. 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
  4. 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
  5. 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
  6. デフォルト引数 class KotlinItem( var id: Int = 0, var name:

    String? = null ) val item = KotlinItem(1)
  7. 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
  8. 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) } } } }
  9. @Parcelize @Parcelize data class Item( var id: Int = 0,

    var name: String? = null ) : Parcelable
  10. 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()); } }
  11. 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) } }
  12. 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) } }
  13. Smart cast val item: Item? = null if (item ==

    null) { return } item.id item.name
  14. named argument val item1 = Item(id = 1, name =

    "name") val item2 = Item(name = "name", id = 1)
  15. 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); } }
  16. 可視修飾⼦ • public:どこからも参照可能 • internal :モジュール内から のみ参照可能 • private:クラス内からのみ 参照可能

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

    // OK val valueNumber = 1 valueNumber = 2 // ίϯύΠϧΤϥʔ
  18. 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)
  19. 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 } }
  20. 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]; } }; }
  21. 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 } }
  22. 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); } }
  23. 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 } }
  24. 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)