<|start_filename|>app/src/main/java/com/antonioleiva/kotlinandroidtraining/Extensions.kt<|end_filename|>
package com.antonioleiva.kotlinandroidtraining

import android.content.Context
import android.support.design.widget.Snackbar
import android.view.View
import android.widget.Toast

fun Context.toast(message: String, length: Int = Toast.LENGTH_SHORT) {
    Toast.makeText(this, message, length).show()
}

fun View.snack(message: String, length: Int = Snackbar.LENGTH_SHORT, body: Snackbar.() -> Unit) {
    with(Snackbar.make(this, message, length)) {
        body()
        show()
    }
}

<|start_filename|>app/src/main/java/com/antonioleiva/kotlinandroidtraining/LoginRequest.kt<|end_filename|>
package com.antonioleiva.kotlinandroidtraining

sealed class Response
object Success : Response()
data class Failure(val error: String) : Response()

class LoginRequest {

    fun doLogin(user: String?, password: String?, callback: (Response) -> Unit) {
        val success = !user.isNullOrEmpty() && !password.isNullOrEmpty()
        callback(if (success) Success else Failure("User or password is empty"))
    }
}

<|start_filename|>app/src/main/java/com/antonioleiva/kotlinandroidtraining/MainActivity.kt<|end_filename|>
package com.antonioleiva.kotlinandroidtraining

import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private val loginRequest = LoginRequest()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        supportActionBar?.title = "Kotlin rules"
        submit.setOnClickListener { onSubmitClicked() }
    }

    private fun onSubmitClicked() {
        loginRequest.doLogin(username.text?.toString(), password.text?.toString()) { response ->

            when (response) {
                Success -> toast("Success")
                is Failure -> showFailureSnackbar(response.error)
            }
        }
    }

    private fun showFailureSnackbar(error: String) {
        container.snack(error, Snackbar.LENGTH_INDEFINITE) {
            setAction("Retry") { toast("Retrying") }
        }
    }
}