Trying to Build Android MVP App in Kotlin

What is Kotlin ?

Kotlin Syntax in Android

Kotlin is a concise and safe typed programming language.

class SourceActivity : AppCompatActivity(), SourceContract.View

Declare Variable and Null Safety

// implicit type
var a = 100 -> assign value
a = 30 -> reassign value
val b = 100 -> final value
b = 30 -> error, because b is final value
//explicit type
var name : String = "Eminarti" -> explicit type declare String
val age : Int = 22 -> explicit type declare Int
// nullable
var middleName : String? = null -> nullable, safe type
var name : String? = null
var length = name.length -> error! name might be null
var length = name?.length -> type length nullable int
var length = name?.length ?: throw NullPointerException()
var length = name!!.length -> throw npe if name is null

Elvis Operator

return name?.length ?: -1          -> if null, return -1

Properties and Fields

val intent = Intent(applicationContext, NewsActivity::class.java)
supportActionBar                   -> from getSupportActionBar
supportActionBar.title = "Title" -> from supportActionBar.setTitle
val title = supportActionBar.title -> from supportActionBar.getTitle

Data Classes

@GET("sources")
fun getSources() : Call<SourceResponse>
class SourceResponse(val status: String, val sources: List<Sources>)

BindingView

// In Java using findViewById
setContentView(R.layout.activity_source);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
RecyclerView list = (RecyclerView) findViewById(R.id.list)
// In Java using ButterKnife
@Bind(R.id.toolbar) Toolbar toolbar;
@Bind(R.id.list) RecyclerView list;

setSupportActionBar(toolbar);
adapter = new SourceAdapter();
list.setLayoutManager(new GridLayoutManager(this, 2));
list.setAdapter(adapter);
list = view?.findViewById(R.id.list) as RecyclerView?

Delegated Properties

private val list: RecyclerView by lazy {
view?.findViewById(R.id.list) as RecyclerView
}

Binding View with Kotlin Android Extension

import kotlinx.android.synthetic.main.activity_news.*setContentView(R.layout.activity_news)
setSupportActionBar(this.toolbar)
this.list.adapter = adapter
this.list.layoutManager = LinearLayoutManager(this)
apply plugin: 'kotlin-android-extensions'
import kotlinx.android.synthetic.main.<layout>.*

Property Initialization Feature

@Inject lateinit var repository : NewsRepository

Android Repository Pattern in Kotlin

Structure Folder. (Left) Java and (Right) Kotlin.

Contract Class for Presenter dan View

class SourceActivity : AppCompatActivity(), SourceContract.View {
}
class SourcePresenter(private val view : SourceContract.View,
private val repository: SourceRepository)
: SourceContract.Presenter {
}

Repository Pattern

class SourcePresenter(val repository: SourceRepository)
: SourceContract.Presenter {

override fun getData() {
repository.getSource(
// in this block we call view for bind data
)
}

Dependency Injection

@Inject lateinit var repository : SourceRepository

Conclusion

- Software Engineer -

Get the Medium app