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
{ | |
"employees":[ | |
{ | |
"name" : { | |
"firstName" : "mahera", | |
"lastName" : "abdi" | |
}, | |
"isActive" : true, | |
"age" : 21, | |
"active" : [ "Monday", "Tuesday" ], |
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
class Delegate : ReadWriteProperty<Any, String> { | |
private var stringResult: String = "" | |
override fun getValue(thisRef: Any, property: KProperty<*>): String = | |
stringResult | |
override fun setValue(thisRef: Any, property: KProperty<*>, value: String) { | |
val formatedString = "${property.name}'s value is $value" | |
stringResult = formatedString.toLowerCase().capitalize() | |
} |
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
class Car { | |
var name:String by Delegate() | |
var brand:String by Delegate() | |
var origin:String by Delegate() | |
} |
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
class ViewDelegate( | |
private val viewId: Int | |
): ReadOnlyProperty<AppCompatActivity, String> { | |
private var result: String? = null | |
override fun getValue(thisRef: AppCompatActivity, property: KProperty<*>): String = | |
result ?: thisRef.findViewById(viewId).also { result = it } | |
} |
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
class MainActivity: AppCompatActivity { | |
val buttonLogin by id(R.id.btn_login) | |
// other stuff | |
// .. | |
// .. | |
fun AppCompatActivity.id(viewId: Int) = ViewDelegate(viewId) | |
} |
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
class MainActivity : AppCompatActivity() { | |
private val tvUsername: TextView by lazy { findViewById(R.id.tv_username) } | |
private var text: String by Delegates.observable("Initial Value") { property, oldvalue, newValue -> | |
tvUsername.text = newValue | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) |
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
fun nonInline(callback: () -> Unit) { | |
println("before callback") | |
callback() | |
println("after callback") | |
} | |
// in java code | |
public void nonInline(Function callback) { | |
System.out.println("before callback"); | |
callback.invoke(); |
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
nonInline { | |
println("Noninline function has been called") | |
} | |
// in java code | simplified version | |
nonInline(new Function() { | |
@Override | |
public void invoke() { | |
System.out.println("Noninline function has been called"); | |
} |
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
btnPrintMessage.setOnClickListener { | |
nonInline { println("Noninline function has been called") } | |
} | |
// in java code | simplified version | |
btnPrintMessage.setOnClickListener(){ | |
@override | |
void onClick(View v){ | |
noninline(new Function { | |
println("Noninline function has been called") |
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
// declaration | |
inline fun inlineFunction(callback: () -> Unit) { | |
println("before callback") | |
callback() | |
println("after callback") | |
} | |
// calling function | |
btnPrintMessage.setOnClickListener(){ | |
inlineFunction(){ |
OlderNewer