Convert Java File to Kotlin の後に その1「メンバとコンストラクタ」

まずは「Convert Java File to Kotlin」で変換してなんとなくは動いていますが.

一通り公式リファレンスは流し読みしていましたが

コードをみているとKotolinの持つ恩恵を得ているような気がしません.

逆に分かりづらくなったようにも思えたりします.

先人たちのコードを見ながら自然なKotlinコードに書き換えていきましょう.

メンバとコンストラクタ

以下のようなJavaコード.


public class TasksPresenter implements TasksContract.Presenter {

    private final TasksRepository mTasksRepository;

    private final TasksContract.View mTasksView;

    private TasksFilterType mCurrentFiltering = TasksFilterType.ALL_TASKS;

    private boolean mFirstLoad = true;

    public TasksPresenter(@NonNull TasksRepository tasksRepository, @NonNull TasksContract.View tasksView) {
        mTasksRepository = checkNotNull(tasksRepository, "tasksRepository cannot be null");
        mTasksView = checkNotNull(tasksView, "tasksView cannot be null!");

        mTasksView.setPresenter(this);
    }

android-architecture/TasksPresenter.java at todo-mvp · googlesamples/android-architecture

Kotlin のプロパティを使って適切に初期化します. ほとんどのメンバ変数に割り当てられたコンストラクタパラメータは必要ありません.


class TasksPresenter(val tasksRepository: TasksRepository, val tasksView: TasksContract.View)
    : TasksContract.Presenter {

    override var filtering = TasksFilterType.ALL_TASKS

    private var mFirstLoad = true

    init {
        tasksView.setPresenter(this)
    }

主コンストラクタに注釈や可視性修飾子がない場合は、コンストラクタキーワードを省略できます。

Classes and Inheritance - Kotlin Programming Language

Convert Java File to Kotlin の後に その1「メンバとコンストラクタ」

Convert Java File to Kotlin の後に その2 「apply」

Convert Java File to Kotlin の後に その3 「Null Safety」

Convert Java File to Kotlin の後に その4 「lateinit」

Convert Java File to Kotlin の後に その5 「String Templates」


material.io「COLOR TOOL」で視覚的に配色をすばやく確認する

デザイナーでもプログラマーでなくても素早くブラウザで確認できます.

Color Tool - Material Design

自分が決めた配色のイメージを簡単に右上のアイコンリンクで他人に伝えることができます.

こんな感じで

あとは, 開発者向けに一括で出力できたらなあ.


新しくなった Android Asset Studio でマテリアルアイコンを自在に加工するべし

作者の Roman Nurik さんがつぶやいています.

使ってみましょう.

Android Asset Studio

Google から公開されているマテリアルデザインのアイコンを元に色やサイズやエフェクトをお好みにブラウザ上で設定してダウンロードできます.

編集後は, resディレクトリ以下解像度別のディレクトリの構造に合わせて作成されたzipアーカイブをダウンロードできます.


$ zipinfo -1 ic_launcher.zip
res/mipmap-xxxhdpi/ic_launcher.png
web_hi_res_512.png
res/mipmap-xxhdpi/ic_launcher.png
res/mipmap-xhdpi/ic_launcher.png
res/mipmap-hdpi/ic_launcher.png
res/mipmap-mdpi/ic_launcher.png


$ zipinfo -v ic_launcher.zip
Archive:  ic_launcher.zip
There is no zipfile comment.

End-of-central-directory record:
-------------------------------

  Zip archive file size:                    129862 (000000000001FB46h)
  Actual end-cent-dir record offset:        129840 (000000000001FB30h)
  Expected end-cent-dir record offset:      129840 (000000000001FB30h)
  (based on the length of the central directory and its expected offset)

  This zipfile constitutes the sole disk of a single-part archive; its
  central directory contains 6 entries.
  The central directory is 455 (00000000000001C7h) bytes long,
  and its (expected) offset in bytes from the beginning of the zipfile
  is 129385 (000000000001F969h).


Central directory entry #1:
---------------------------

  res/mipmap-xxxhdpi/ic_launcher.png

  offset of local header from start of archive:   0
                                                  (0000000000000000h) bytes
  file system or operating system of origin:      MS-DOS, OS/2 or NT FAT
  version of encoding software:                   2.0
  minimum file system compatibility required:     MS-DOS, OS/2 or NT FAT
  minimum software version required to extract:   2.0
  compression method:                             deflated
  compression sub-type (deflation):               normal
  file security status:                           not encrypted
  extended local header:                          yes
  file last modified on (DOS date/time):          2017 Jan 18 12:53:42
  32-bit CRC value (hex):                         5e505e31
  compressed size:                                20038 bytes
  uncompressed size:                              20028 bytes
  length of filename:                             34 characters
  length of extra field:                          0 bytes
  length of file comment:                         0 characters
  disk number on which file begins:               disk 1
  apparent file type:                             binary
  non-MSDOS external file attributes:             000000 hex
  MS-DOS file attributes (00 hex):                none

  There is no file comment.

Central directory entry #2:
---------------------------

  There are an extra 16 bytes preceding this file.

  web_hi_res_512.png

  offset of local header from start of archive:   20118
                                                  (0000000000004E96h) bytes
  file system or operating system of origin:      MS-DOS, OS/2 or NT FAT
  version of encoding software:                   2.0
  minimum file system compatibility required:     MS-DOS, OS/2 or NT FAT
  minimum software version required to extract:   2.0
  compression method:                             deflated
  compression sub-type (deflation):               normal
  file security status:                           not encrypted
  extended local header:                          yes
  file last modified on (DOS date/time):          2017 Jan 18 12:53:42
  32-bit CRC value (hex):                         bb74dba5
  compressed size:                                74680 bytes
  uncompressed size:                              75392 bytes
  length of filename:                             18 characters
  length of extra field:                          0 bytes
  length of file comment:                         0 characters
  disk number on which file begins:               disk 1
  apparent file type:                             binary
  non-MSDOS external file attributes:             000000 hex
  MS-DOS file attributes (00 hex):                none

  There is no file comment.

Central directory entry #3:
---------------------------

  There are an extra 16 bytes preceding this file.

  res/mipmap-xxhdpi/ic_launcher.png

  offset of local header from start of archive:   94862
                                                  (000000000001728Eh) bytes
  file system or operating system of origin:      MS-DOS, OS/2 or NT FAT
  version of encoding software:                   2.0
  minimum file system compatibility required:     MS-DOS, OS/2 or NT FAT
  minimum software version required to extract:   2.0
  compression method:                             deflated
  compression sub-type (deflation):               normal
  file security status:                           not encrypted
  extended local header:                          yes
  file last modified on (DOS date/time):          2017 Jan 18 12:53:42
  32-bit CRC value (hex):                         ca100bd5
  compressed size:                                15772 bytes
  uncompressed size:                              15790 bytes
  length of filename:                             33 characters
  length of extra field:                          0 bytes
  length of file comment:                         0 characters
  disk number on which file begins:               disk 1
  apparent file type:                             binary
  non-MSDOS external file attributes:             000000 hex
  MS-DOS file attributes (00 hex):                none

  There is no file comment.

Central directory entry #4:
---------------------------

  There are an extra 16 bytes preceding this file.

  res/mipmap-xhdpi/ic_launcher.png

  offset of local header from start of archive:   110713
                                                  (000000000001B079h) bytes
  file system or operating system of origin:      MS-DOS, OS/2 or NT FAT
  version of encoding software:                   2.0
  minimum file system compatibility required:     MS-DOS, OS/2 or NT FAT
  minimum software version required to extract:   2.0
  compression method:                             deflated
  compression sub-type (deflation):               normal
  file security status:                           not encrypted
  extended local header:                          yes
  file last modified on (DOS date/time):          2017 Jan 18 12:53:42
  32-bit CRC value (hex):                         6551b330
  compressed size:                                8409 bytes
  uncompressed size:                              8404 bytes
  length of filename:                             32 characters
  length of extra field:                          0 bytes
  length of file comment:                         0 characters
  disk number on which file begins:               disk 1
  apparent file type:                             binary
  non-MSDOS external file attributes:             000000 hex
  MS-DOS file attributes (00 hex):                none

  There is no file comment.

Central directory entry #5:
---------------------------

  There are an extra 16 bytes preceding this file.

  res/mipmap-hdpi/ic_launcher.png

  offset of local header from start of archive:   119200
                                                  (000000000001D1A0h) bytes
  file system or operating system of origin:      MS-DOS, OS/2 or NT FAT
  version of encoding software:                   2.0
  minimum file system compatibility required:     MS-DOS, OS/2 or NT FAT
  minimum software version required to extract:   2.0
  compression method:                             deflated
  compression sub-type (deflation):               normal
  file security status:                           not encrypted
  extended local header:                          yes
  file last modified on (DOS date/time):          2017 Jan 18 12:53:42
  32-bit CRC value (hex):                         44371c4a
  compressed size:                                6539 bytes
  uncompressed size:                              6534 bytes
  length of filename:                             31 characters
  length of extra field:                          0 bytes
  length of file comment:                         0 characters
  disk number on which file begins:               disk 1
  apparent file type:                             binary
  non-MSDOS external file attributes:             000000 hex
  MS-DOS file attributes (00 hex):                none

  There is no file comment.

Central directory entry #6:
---------------------------

  There are an extra 16 bytes preceding this file.

  res/mipmap-mdpi/ic_launcher.png

  offset of local header from start of archive:   125816
                                                  (000000000001EB78h) bytes
  file system or operating system of origin:      MS-DOS, OS/2 or NT FAT
  version of encoding software:                   2.0
  minimum file system compatibility required:     MS-DOS, OS/2 or NT FAT
  minimum software version required to extract:   2.0
  compression method:                             deflated
  compression sub-type (deflation):               normal
  file security status:                           not encrypted
  extended local header:                          yes
  file last modified on (DOS date/time):          2017 Jan 18 12:53:42
  32-bit CRC value (hex):                         4fce9139
  compressed size:                                3492 bytes
  uncompressed size:                              3487 bytes
  length of filename:                             31 characters
  length of extra field:                          0 bytes
  length of file comment:                         0 characters
  disk number on which file begins:               disk 1
  apparent file type:                             binary
  non-MSDOS external file attributes:             000000 hex
  MS-DOS file attributes (00 hex):                none

かんたんなアイコンであれば簡単に素早く作成できます.

Windows版デスクトップアプリ(.exe形式)もある模様.

romannurik/AndroidAssetStudio: A set of web-based tools for generating graphics and other assets that would eventually be in an Android application's res/ directory.