Created
August 20, 2017 10:27
-
-
Save mkodekar/6af9ec447996bb4f7b552536c830e03a to your computer and use it in GitHub Desktop.
Various possible functions possible with kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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