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

KotlinでAndroid開発を気持ちよくしよう

 KotlinでAndroid開発を気持ちよくしよう

Cybozu Meetup #10 モバイル で発表した内容
-------------------------------
Kotlin はまだ使っていませんか?使っているけれど Java と比べて何が良いかがはっきりしませんか?
実は Java で当たり前な面倒くさい事いっぱいあるけれど Kotlin では当たり前な便利な事がいっぱいあります!
日々の開発が楽になり、「もっと早く Kotlin の事を知っとけばよかった!」と皆様が思うように説明させて頂きます。
Kotlin で Android の開発を気持ちよくしましょう。

Benoît Quenaudon

January 30, 2018
Tweet

More Decks by Benoît Quenaudon

Other Decks in Programming

Transcript

  1. public final class Task { @NonNulla private final String id;

    @Nullableb private final String title; @Nullablec private final String description; private final boolean completed; public Task(@Nullable String title, @Nullable String description, @NonNull String id, boolean completed) { this.id = id; this.title = title; this.description = description; this.completed = completed; }a }l
  2. this.description = description; this.completed = completed; }a @NonNulld public String

    getId() { return id; }b @Nullablee public String getTitle() { return title; }c @Nullablef public String getTitleForList() { if (!Strings.isNullOrEmpty(title)) { return title; } else { return description; }z }d @Nullableg public String getDescription() { return description; }e public boolean isCompleted() { return completed; }f public boolean isActive() { return !completed; }g public boolean isEmpty() { return Strings.isNullOrEmpty(title) && Strings.isNullOrEmpty(description); }h }l
  3. }e public boolean isCompleted() { return completed; }f public boolean

    isActive() { return !completed; }g public boolean isEmpty() { return Strings.isNullOrEmpty(title) && Strings.isNullOrEmpty(description); }h @Overrideh public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Task task = (Task) o; return Objects.equal(id, task.id) && Objects.equal(title, task.title) && Objects.equal(description, task.description); }i @Overridei public int hashCode() { return Objects.hashCode(id, title, description); }j @Overridej public String toString() { return "Task with title " + title; }k }l
  4. public final class Task { @NonNulla private final String id;

    @Nullableb private final String title; @Nullablec private final String description; private final boolean completed; public Task(@Nullable String title, @Nullable String description, @NonNull String id, boolean completed) { this.id = id; this.title = title; this.description = description; this.completed = completed; }a @NonNulld public String getId() { return id; }b @Nullablee public String getTitle() { return title; }c @Nullablef public String getTitleForList() { if (!Strings.isNullOrEmpty(title)) { return title; } else { return description; } }d @Nullableg public String getDescription() { return description; }e public boolean isCompleted() { return completed; }f public boolean isActive() { return !completed; }g public boolean isEmpty() { return Strings.isNullOrEmpty(title) && Strings.isNullOrEmpty(description); }h @Overrideh public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Task task = (Task) o; return Objects.equal(id, task.id) && Objects.equal(title, task.title) && Objects.equal(description, task.description); }i @Overridei public int hashCode() { return Objects.hashCode(id, title, description); }j @Overridej public String toString() { return "Task with title " + title; }k }l
  5. @Getter @AllArgsConstructor @EqualsAndHashCode @ToString public final class Task { @NonNull

    private final String id; @Nullable private final String title; @Nullable private final String description; private final boolean completed; @Nullable public String getTitleForList() { if (!Strings.isNullOrEmpty(title)) { return title; } else { return description; } } public boolean isActive() { return !completed; } public boolean isEmpty() { return Strings.isNullOrEmpty(title) && Strings.isNullOrEmpty(description); } } Lombok
  6. @AutoValue public abstract class Task { @NonNull abstract String getId();

    @Nullable abstract String getTitle(); @Nullable abstract String getDescription(); abstract boolean isCompleted(); public static Task create(@Nullable String title, @Nullable String description, @NonNull String id, boolean completed) { return new AutoValue_Task(id, title, description, completed); } public boolean isActive() { return !isCompleted(); } @Nullable public String getTitleForList() { if (!Strings.isNullOrEmpty(getTitle())) { return getTitle(); } else { return getDescription(); } } public boolean isEmpty() { return Strings.isNullOrEmpty(getTitle()) && Strings.isNullOrEmpty(getDescription()); } } AutoValue
  7. data class Task( val id: String, val title: String?, val

    description: String?, val completed: Boolean ) { val titleForList = if (title.isNotNullNorEmpty()) { title } else { description } val active = !completed val empty = title.isNullOrEmpty() && description.isNullOrEmpty() } Kotlin
  8. data class Task( val id: String, val title: String?, val

    description: String?, var completed: Boolean )
  9. data class Task( val id: String, val title: String?, val

    description: String?, var completed: Boolean ) • constructor • getters • setters (var only) • toString • equals & hashCode • copy • componentN
  10. data class Task( val id: String, val title: String?, val

    description: String?, var completed: Boolean ) • constructor • getters • setters (var only) • toString • equals & hashCode • copy • componentN
  11. data class Task( val id: String, val title: String?, val

    description: String?, var completed: Boolean ) • constructor • getters • setters (var only) • toString • equals & hashCode • copy • componentN
  12. data class Task( val id: String, val title: String?, val

    description: String?, var completed: Boolean ) • constructor • getters • setters (var only) • toString • equals & hashCode • copy • componentN
  13. data class Task( val id: String, val title: String?, val

    description: String?, var completed: Boolean ) • constructor • getters • setters (var only) • toString • equals & hashCode • copy • componentN
  14. data class Task( val id: String, val title: String?, val

    description: String?, var completed: Boolean ) • constructor • getters • setters (var only) • toString • equals & hashCode • copy • componentN
  15. data class Task( val id: String, val title: String?, val

    description: String?, var completed: Boolean ) • constructor • getters • setters (var only) • toString • equals & hashCode • copy • componentN
  16. sealed class ViewHolder { data class HeaderViewHolder(val text: String) :

    ViewHolder() class ItemViewHolder(val data: String) : ViewHolder() object LoadingViewHolder : ViewHolder() }z
  17. sealed class ViewHolder { data class HeaderViewHolder(val text: String) :

    ViewHolder() class ItemViewHolder(val data: String) : ViewHolder() object LoadingViewHolder : ViewHolder() }z
  18. sealed class ViewHolder { data class HeaderViewHolder(val text: String) :

    ViewHolder() class ItemViewHolder(val data: String) : ViewHolder() object LoadingViewHolder : ViewHolder() }z
  19. sealed class ViewHolder { data class HeaderViewHolder(val text: String) :

    ViewHolder() class ItemViewHolder(val data: String) : ViewHolder() object LoadingViewHolder : ViewHolder() }z
  20. sealed class ViewHolder { data class HeaderViewHolder(val text: String) :

    ViewHolder() class ItemViewHolder(val data: String) : ViewHolder() object LoadingViewHolder : ViewHolder() }z
  21. sealed class ViewHolder(val root: View) : RecyclerView.ViewHolder(view) { class HeaderViewHolder(root:

    View) : ViewHolder(root) class ItemViewHolder(root: View) : ViewHolder(root) class LoadingViewHolder(root: View) : ViewHolder(root) }z
  22. sealed class ViewHolder(val root: View) : RecyclerView.ViewHolder(view) { class HeaderViewHolder(root:

    View) : ViewHolder(root) class ItemViewHolder(root: View) : ViewHolder(root) class LoadingViewHolder(root: View) : ViewHolder(root) }z
  23. sealed class ViewHolder(val root: View) : RecyclerView.ViewHolder(view) { class HeaderViewHolder(root:

    View) : ViewHolder(root) class ItemViewHolder(root: View) : ViewHolder(root) class LoadingViewHolder(root: View) : ViewHolder(root) }z
  24. override fun onBindViewHolder(holder: ViewHolder, position: Int) { return when (holder)

    { is HeaderViewHolder -> { /*****/ } is ItemViewHolder -> { /*****/ } is LoadingViewHolder -> { /*****/ } } }
  25. sealed class ViewHolder(val root: View) : RecyclerView.ViewHolder(view) { class HeaderViewHolder(root:

    View) : ViewHolder(root) class ItemViewHolder(root: View) : ViewHolder(root) class LoadingViewHolder(root: View) : ViewHolder(root) }d
  26. sealed class ViewHolder(val root: View) : RecyclerView.ViewHolder(view) { class HeaderViewHolder(root:

    View) : ViewHolder(root) class ItemViewHolder(root: View) : ViewHolder(root) class LoadingViewHolder(root: View) : ViewHolder(root) class FooterViewHolder(root: View) : ViewHolder(root) }d
  27. override fun onBindViewHolder(holder: ViewHolder, position: Int) { return when (holder)

    { is HeaderViewHolder -> { /*****/ } is ItemViewHolder -> { /*****/ } is LoadingViewHolder -> { /*****/ } } }
  28. override fun onBindViewHolder(holder: ViewHolder, position: Int) { return when (holder)

    { is HeaderViewHolder -> { /*****/ } is ItemViewHolder -> { /*****/ } is LoadingViewHolder -> { /*****/ } }z }d
  29. override fun onBindViewHolder(holder: ViewHolder, position: Int) { return when (holder)

    { is HeaderViewHolder -> { /*****/ } is ItemViewHolder -> { /*****/ } is LoadingViewHolder -> { /*****/ } is FooterViewHolder -> { /*****/ }f }z }d
  30. override fun onBindViewHolder(holder: ViewHolder, position: Int) { return when (holder)

    { is HeaderViewHolder -> { /*****/ } is ItemViewHolder -> { /*****/ } is LoadingViewHolder -> { /*****/ } is FooterViewHolder -> { /*****/ }f }z }d
  31. override fun onBindViewHolder(holder: ViewHolder, position: Int) { return when (holder)

    { is HeaderViewHolder -> { /*****/ } is ItemViewHolder -> { /*****/ } is LoadingViewHolder -> { /*****/ } is FooterViewHolder -> { val footerVH = holder as FooterViewHolder footerVH.stuff() }f }z }d
  32. override fun onBindViewHolder(holder: ViewHolder, position: Int) { return when (holder)

    { is HeaderViewHolder -> { /*****/ } is ItemViewHolder -> { /*****/ } is LoadingViewHolder -> { /*****/ } is FooterViewHolder -> { holder.stuff() }f }z }d
  33. override fun onBindViewHolder(holder: ViewHolder, position: Int) { return when (holder)

    { is HeaderViewHolder -> { /*****/ } is ItemViewHolder -> { /*****/ } is LoadingViewHolder -> { /*****/ } is FooterViewHolder -> { /*****/ }f }z }d
  34. List<Action> actions; List<Task> tasks = new ArrayList<>(actions.size()); for (Action action

    : actions) { Task task = new Task(action.title(), action.description()); tasks.add(task); }
  35. List<Action> actions; List<Task> tasks = new ArrayList<>(actions.size()); for (Action action

    : actions) { Task task = new Task(action.title(), action.description()); tasks.add(task); }
  36. List<Action> actions; List<Task> tasks = new ArrayList<>(actions.size()); for (Action action

    : actions) { Task task = new Task(action.title(), action.description()); tasks.add(task); }
  37. List<Action> actions; List<Task> tasks = new ArrayList<>(actions.size()); for (Action action

    : actions) { Task task = new Task(action.title(), action.description()); tasks.add(task); }
  38. List<Action> actions; List<Task> tasks = new ArrayList<>(actions.size()); for (Action action

    : actions) { Task task = new Task(action.title(), action.description()); tasks.add(task); }
  39. List<Action> actions; List<Task> tasks = new ArrayList<>(actions.size()); for (Action action

    : actions) { Task task = new Task(action.title(), action.description()); tasks.add(task); }
  40. • map() • filter() • find() • distinct() • any()

    • groupBy() • zip() • … val list: List<Int> = listOf(1, 2, 3) val map: Map<String, Int> = mapOf("a" to 1, "b" to 2) val set: Set<String> = setOf("a", "b", “c")
  41. • map() • filter() • find() • distinct() • any()

    • groupBy() • zip() • … val list: List<Int> = listOf(1, 2, 3) val map: Map<String, Int> = mapOf("a" to 1, "b" to 2) val set: Set<String> = setOf("a", "b", “c")
  42. private lateinit var viewModel: TaskViewModel private lateinit var taskId: String

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) viewModel = ViewModelProviders .of(this, ViewModelFactory()) .get(TaskViewModel::class.java) taskId = arguments?.getString(ARGUMENT_EDIT_TASK_ID) }c
  43. private lateinit var viewModel: TaskViewModel private lateinit var taskId: String

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) viewModel = ViewModelProviders .of(this, ViewModelFactory()) .get(TaskViewModel::class.java) taskId = arguments?.getString(ARGUMENT_EDIT_TASK_ID) }c
  44. private lateinit var viewModel: TaskViewModel private lateinit var taskId: String

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) viewModel = ViewModelProviders .of(this, ViewModelFactory()) .get(TaskViewModel::class.java) taskId = arguments?.getString(ARGUMENT_EDIT_TASK_ID) }c
  45. private lateinit var viewModel: TaskViewModel private lateinit var taskId: String

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) viewModel = ViewModelProviders .of(this, ViewModelFactory()) .get(TaskViewModel::class.java) taskId = arguments?.getString(ARGUMENT_EDIT_TASK_ID) }c
  46. private val viewModel: TaskViewModel by lazy { ViewModelProviders .of(this, ViewModelFactory())

    .get(TaskViewModel::class.java) }x private val taskId: String by lazy { arguments?.getString(ARGUMENT_EDIT_TASK_ID) }y override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) }c
  47. private val viewModel: TaskViewModel by lazy { ViewModelProviders .of(this, ViewModelFactory())

    .get(TaskViewModel::class.java) }x private val taskId: String by lazy { arguments?.getString(ARGUMENT_EDIT_TASK_ID) }y override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) }c
  48. private val viewModel: TaskViewModel by lazy { ViewModelProviders .of(this, ViewModelFactory())

    .get(TaskViewModel::class.java) }x private val taskId: String by lazy { arguments?.getString(ARGUMENT_EDIT_TASK_ID) }y override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) }c
  49. private val viewModel: TaskViewModel by lazy { ViewModelProviders .of(this, ViewModelFactory())

    .get(TaskViewModel::class.java) }x private val taskId: String by lazy { arguments?.getString(ARGUMENT_EDIT_TASK_ID) }y override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) }c
  50. private val viewModel: TaskViewModel by lazy { ViewModelProviders .of(this, ViewModelFactory())

    .get(TaskViewModel::class.java) }x private val taskId: String by lazy { arguments?.getString(ARGUMENT_EDIT_TASK_ID) }y override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) }
  51. class TaskDetailFragment : Fragment() { companion object { fun newInstance(taskId:

    String): TaskDetailFragment { val fragment = TaskDetailFragment() val bundle = Bundle() bundle.putString(ARGUMENT_TASK_ID, taskId) fragment.arguments = bundle return fragment }a }b }c
  52. class TaskDetailFragment : Fragment() { companion object { fun newInstance(taskId:

    String): TaskDetailFragment { val fragment = TaskDetailFragment() val bundle = Bundle() bundle.putString(ARGUMENT_TASK_ID, taskId) fragment.arguments = bundle return fragment }a }b }c val fragment = TaskDetailFragment.newInstance(taskId)
  53. class TaskDetailFragment : Fragment() { companion object { fun newInstance(taskId:

    String): TaskDetailFragment { val fragment = TaskDetailFragment() val bundle = Bundle() bundle.putString(ARGUMENT_TASK_ID, taskId) fragment.arguments = bundle return fragment }a }b }c val fragment = TaskDetailFragment.newInstance(taskId)
  54. class TaskDetailFragment : Fragment() { companion object { operator fun

    invoke(taskId: String): TaskDetailFragment { val fragment = TaskDetailFragment() val bundle = Bundle() bundle.putString(ARGUMENT_TASK_ID, taskId) fragment.arguments = bundle return fragment }a }b }c val fragment = TaskDetailFragment.newInstance(taskId)
  55. class Foo() { operator fun invoke(): String { return "invoked

    !" } } fun main(args: Array<String>) { val foo: Foo = Foo() println(foo()) // invoked ! }
  56. class TaskDetailFragment : Fragment() { companion object { operator fun

    invoke(taskId: String): TaskDetailFragment { val fragment = TaskDetailFragment() val bundle = Bundle() bundle.putString(ARGUMENT_TASK_ID, taskId) fragment.arguments = bundle return fragment }a }b }c val fragment = TaskDetailFragment.newInstance(taskId)
  57. class TaskDetailFragment : Fragment() { companion object { operator fun

    invoke(taskId: String): TaskDetailFragment { val fragment = TaskDetailFragment() val bundle = Bundle() bundle.putString(ARGUMENT_TASK_ID, taskId) fragment.arguments = bundle return fragment }a }b }c val fragment = TaskDetailFragment.newInstance(taskId)
  58. class TaskDetailFragment : Fragment() { companion object { operator fun

    invoke(taskId: String): TaskDetailFragment { val fragment = TaskDetailFragment() val bundle = Bundle() bundle.putString(ARGUMENT_TASK_ID, taskId) fragment.arguments = bundle return fragment }a }b }c val fragment = TaskDetailFragment(taskId)
  59. class TaskDetailFragment : Fragment() { companion object { operator fun

    invoke(taskId: String): TaskDetailFragment { val fragment = TaskDetailFragment() val bundle = Bundle() bundle.putString(ARGUMENT_TASK_ID, taskId) fragment.arguments = bundle return fragment }a }b }c val fragment = TaskDetailFragment(taskId)
  60. class TaskDetailFragment : Fragment() { companion object { operator fun

    invoke(taskId: String): TaskDetailFragment { val fragment = TaskDetailFragment() val bundle = Bundle() bundle.putString(ARGUMENT_TASK_ID, taskId) fragment.arguments = bundle return fragment }a }b }c val fragment = TaskDetailFragment(taskId)
  61. class TaskDetailFragment : Fragment() { companion object { operator fun

    invoke(taskId: String): TaskDetailFragment { val fragment = TaskDetailFragment() val bundle = Bundle().apply { this.putString(ARGUMENT_TASK_ID, taskId) }p fragment.arguments = bundle return fragment }a }b }c val fragment = TaskDetailFragment(taskId)
  62. class TaskDetailFragment : Fragment() { companion object { operator fun

    invoke(taskId: String): TaskDetailFragment { val fragment = TaskDetailFragment() val bundle = Bundle().apply { putString(ARGUMENT_TASK_ID, taskId) }p fragment.arguments = bundle return fragment }a }b }c val fragment = TaskDetailFragment(taskId)
  63. class TaskDetailFragment : Fragment() { companion object { operator fun

    invoke(taskId: String): TaskDetailFragment { val fragment = TaskDetailFragment() fragment.arguments = Bundle().apply { putString(ARGUMENT_TASK_ID, taskId) }p return fragment }a }b }c val fragment = TaskDetailFragment(taskId)
  64. class TaskDetailFragment : Fragment() { companion object { operator fun

    invoke(taskId: String): TaskDetailFragment { val fragment = TaskDetailFragment().apply { arguments = Bundle().apply { putString(ARGUMENT_TASK_ID, taskId) }p }p return fragment }a }b }c val fragment = TaskDetailFragment(taskId)
  65. class TaskDetailFragment : Fragment() { companion object { operator fun

    invoke(taskId: String): TaskDetailFragment { return TaskDetailFragment().apply { arguments = Bundle().apply { putString(ARGUMENT_TASK_ID, taskId) }p }p }a }b }c val fragment = TaskDetailFragment(taskId)
  66. class TaskDetailFragment : Fragment() { companion object { operator fun

    invoke(taskId: String) = TaskDetailFragment().apply { arguments = Bundle().apply { putString(ARGUMENT_TASK_ID, taskId) }p }p }a }b }c val fragment = TaskDetailFragment(taskId)
  67. class TaskDetailFragment : Fragment() { companion object { operator fun

    invoke(taskId: String) = TaskDetailFragment().apply { arguments = Bundle().apply { putString(ARGUMENT_TASK_ID, taskId) }p }p }a }b }c val fragment = TaskDetailFragment(taskId)