Skip to content

Instantly share code, notes, and snippets.

View cioccarellia's full-sized avatar

Andrea Cioccarelli cioccarellia

View GitHub Profile
public static final String Prefs = "MyPrefsFile";
SharedPreferences.Editor editor = getSharedPreferences(Prefs, MODE_PRIVATE).edit();
editor.putString("name", "Andrea");
editor.putInt("id", 107);
editor.apply();
public static final String Prefs = "MyPrefsFile";
SharedPreferences prefs = this.getSharedPreferences(Prefs, Context.MODE_PRIVATE);
String user = prefs.getString("name", null);
int userId = prefs.getInt("id", null);
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="name">Andrea</string>
<int name="id" value="1" />
</map>
/**
* Calls the specified function [block] with `this` value as its argument and returns its result.
*/
public inline fun <T, R> T.let(block: (T) -> R): R {
return block(this)
}
/**
* Calls the specified function [block] with `this` value as its receiver and returns `this` value.
*/
public inline fun <T> T.apply(block: T.() -> Unit): T {
block()
return this
}
val window = ApplicationWindow()
window.content.loadImage("whatever.png")
window.header.buttons.add("X")
window.header.buttons.add("_")
window.header.buttons.add("□")
window.onWindowClose = {
println("Bye bye")
val window = ApplicationWindow().apply {
content.loadImage("whatever.png")
header.buttons.run {
add("X")
add("_")
add("□")
}
onWindowClose = {
val window = ApplicationWindow()
// 'this' is the global scope
window.apply {
// 'this' scopes to 'window'
this.content.loadImage("whatever.png")
// 'this' scopes to 'window'
this.header.buttons.run {
// 'this' scopes to 'window.header.buttons'
add("X")
// Standard
val alice = Person("Alice", 20, "Amsterdam")
println(alice)
alice.moveTo("Detroit")
alice.incrementAge()
alice.alias = "Ally"
println(alice)
// Using let
Person("Bob", 21, "Bruges").let { it ->
/**
* Calls the specified function [block] and returns its result.
*/
public inline fun <R> run(block: () -> R): R {
return block()
}
/**
* Calls the specified function [block] with `this` value as its receiver and returns its result.
*/