Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Various possible functions possible with kotlin
package co.kodparadise.mausam.utils
import android.app.Notification
import android.content.Context
import android.content.SharedPreferences
import co.kodparadise.mausam.R
import com.afollestad.materialdialogs.MaterialDialog
/**
* Created by rkodekar on 8/19/17.
*/
class Functions(val context: Context) {
fun main(args: Array<String>) {
val preference = context.getSharedPreferences("Example", 0)
preference.edit {
set("key" to "value")
}
notification(context) {
setContentTitle("Example notification")
setContentText("This is an example notification")
setSmallIcon(R.mipmap.ic_launcher)
}
dialog(context) {
title("Example dialog")
content("This is an example dialog")
positiveText("okay")
cancelable(false)
canceledOnTouchOutside(false)
onPositive { dialog, which ->
dialog.dismiss()
}
onNegative { dialog, which ->
dialog.dismiss()
}
}
using {
execute()
}
}
@Throws(Exception::class)
fun execute() {
println("functions throws and exception")
}
fun SharedPreferences.Editor.set(pair : Pair<String, String>) {
putString(pair.first, pair.second)
}
inline fun SharedPreferences.edit(func: SharedPreferences.Editor.() -> Unit) {
val editor = edit()
editor.func()
editor.apply()
}
inline fun notification(context: Context, func: Notification.Builder.() -> Unit): Notification {
val builder = Notification.Builder(context)
builder.func()
return builder.build()
}
inline fun dialog(context: Context, func: MaterialDialog.Builder.() -> Unit): MaterialDialog {
val builder = MaterialDialog.Builder(context)
builder.func()
return builder.build()
}
inline fun using(func: () -> Unit) {
try {
func()
}catch (e: Exception) {
e.message!!
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.