Skip to content

Instantly share code, notes, and snippets.

@externvoid
Last active May 11, 2019 10:01
Show Gist options
  • Select an option

  • Save externvoid/95609fe565b92f501fd4e65c79873109 to your computer and use it in GitHub Desktop.

Select an option

Save externvoid/95609fe565b92f501fd4e65c79873109 to your computer and use it in GitHub Desktop.
learning Kotlin@2019 04 24

Who ur?

var p = 1
print(p::class.java) // => int, java.lang.Class<out kotlin.Int> = int@REPL

get context

this@MainActivity

you can find class hierarchy diagram with following procedure at Android Studio

in code editor, selecting the class name such as AppCompatActivity
at Menu Bar, tracing Navigate - Type Hierarchy or type ctrl-H, then the diagram appears.

related folders(just below Home directory, total 8folders)

1. /Library/Preferences/AndroidStudio*
- initial settings
2. /Library/Preferences/com.google.android.studio.plist
- android studio settings
3. /Library/Application\ Support/AndroidStudio*
- Kotlin-plugin
4. /Library/Android/
- sdk
5. /Libaray/Logs/AndroidStudio*
6. /Library/Cashes/AndroidStudio*
7. .gradle
8. .android

My first custom defined class

// constructor args must not be specified with var, val?
class Person(firstName: String, lastName: String) {

  var firstName = firstName
  var lastName = lastName

  val fullName: String
    get(): String {
      return StringBuilder()
    .append(this.firstName).append(" ").append(this.lastName).toString()
  }

  fun putName(name: String) {
    this.firstName = name
  }
}

fun main(args: Array<String>) {
  val b = Person("Foo", "Hirosho")
  b.putName("OK")
  println(b.fullName)
}
// args of primary constructor is not properties of class. couresy of 30min@Qiit
a   

confirm Class Hierarchy

tracking Navigation menu & Type Hierarchy allow you see this bellow.

Default Keymap of Android Studio

pdffile

What's annotated property such as @Inject var name?.

It's a kind of Unit test technology, more specifically it's a kind of DI. Following article explain detailly. 僕がDIを否定する理由

Terminologies

package and module are similar to library and application. module = one kotlin file like hello.kt while package consist from some modules.

enum class

enum class Direction {
  NORTH, SOUTH, WEST, EAST
}
println(Direction.NORTH.ordinal) // => 0
println(Direction.NORTH.name)// => NORTH   

Sample of Makefile to compile and run Kotlin source code

CC := kotlin
CFLAGS1 := -cp jsoup-1.11.3.jar -include-runtime
CFLAGS2 := -cp .:jsoup-1.11.3.jar
SRC := jsoup

all: go
go:
  $(CC)c $(SRC).kt $(CFLAGS1)
  $(CC) $(CFLAGS2) JsoupKt

First charactor below a target section must be tab code. Otherwise, The make command will stop with the message below,

"mkfile1:2: *** missing separator.  Stop."

Great Kotlin tutorial in Japanese

30分で覚えるKotlin文法

Kotlinまとめ

はじめてのKotlin。Javaと比較してみた

勉強部屋Wiki

Kotlinの公式リファレンスを日本語化してみた[前編]

Swift to Kotlinチートシート

Android Studio Related Folder in macOS, only mentioned the folders below Home Directry

AppStore GooglePlay
1. /Library/Preferences/AndroidStudio* initial settings
2. /Library/Preferences/com.google.android.studio.plist android studio settings
3. /Library/Application\ Support/AndroidStudio* Kotlin-plugin
4. /Library/Android/ sdk
5. /Libaray/Logs/AndroidStudio* Gradle sync failed log, etc
6. /Library/Cashes/AndroidStudio* build caches?
7. .gradle Gradle related
8. .android ADV, Android Virtual device

Andorid SDK related tips

Android はじめてのFragment

Kotlin時代のDataBindingのすゝめ

Gradle build dependencies description manner

dependencies dependency propagation required plugin
compile Yes Java Plugin
implementation No same above
api Yes Java Library plugin

dependencies description manner

Gradleをざっくりと

Project, Module, app in Android Project

ライブラリの追加方法

obtain & release device context(canvas)

private fun drawCanvas() {
        val canvas = surfaceView.holder.lockCanvas()
        canvas.drawColor(Color.YELLOW)
        canvas.drawCircle(ballX, ballY, radius, Paint().apply {
            this.setColor(Color.MAGENTA)
            color = Color.MAGENTA
        })
        surfaceView.holder.unlockCanvasAndPost(canvas)
    }

retrieve & save preferences at typically onCreate and onSaveTapped(Button)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val pref = PreferenceManager.getDefaultSharedPreferences(this)
        pref.apply {
            val editNeck = getString("NECK", "")
            val editSleeve = getString("SLEEVE", "")
            val editWaist = getString("WAIST", "")
            val editInseam = getString("INSEAM", "")

            neck.setText(editNeck)
            sleeve.setText(editSleeve)
            waist.setText(editWaist)
            inseam.setText(editInseam)
        }
    }
    private fun onSaveTapped() {
        val pref = PreferenceManager.getDefaultSharedPreferences(this)
        val editor = pref.edit()
        editor.putString("NECK", neck.text.toString())
                .putString("SLEEVE", sleeve.text.toString())
                .putString("WAIST", waist.text.toString())
                .putString("INSEAM", inseam.text.toString())
                .apply()
    }

SAM conversion & object notation

tapHere: Button, textView: TextView

tapHere.setOnClickListener(
  object: View.onClickListener {
    override fun onClick(v: View?): Unit {
      textView.text = "OK"
    }
  }
)

setOnClickListenerの引数はView.onClickListenerインターフェース実装のインスタンス。 Javaのインターフェースでメソッドを一つしか持たないものはSAM(Single Abstruct Method)インターフェースと呼ばれ、Lambda式に置き換える事ができる。

tapHere.setOnClickListener(
  { v -> textView.text = "OK" }
)

Lambda式の引数が一つなら省略可能(it)

tapHere.setOnClickListener(
  { textView.text = "OK" }
)

引数の最後がLambda式なら()の外に

tapHere.setOnClickListener() { textView.text = "OK" }

更に、Lambda式が唯一の引数なら()が省略可能に。

tapHere.setOnClickListener{ textView.text = "OK" }

🔹クラスAをインターフェースBによって僅かに修正したクラスCのインスタンスが必要となった時に、サブクラスCを宣言せずにそのインスタンスabを生成できる。

open class A(x: Int) {
  public open val y: Int = x
}

interface B {...}

val ab: A = object : A(1), B {
  override val y = 15
}

オブジェクト式と宣言

Concurrent Programming on Android

1. Low Level Concurrency Thread, MessageQueue, Looper, Handler
2. High Level Concurrency HandlerThread, AsyncTask

Androidのバックグラウンドを使いこなす Thread, Looper, Handler by Eunjoo Im Nov 10 2015

courtesy of imgur

日本語投稿フォーム

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment