Skip to content

Instantly share code, notes, and snippets.

View arifvn's full-sized avatar
🥦
Focusing

arifvn arifvn

🥦
Focusing
View GitHub Profile
{
"employees":[
{
"name" : {
"firstName" : "mahera",
"lastName" : "abdi"
},
"isActive" : true,
"age" : 21,
"active" : [ "Monday", "Tuesday" ],
@arifvn
arifvn / Delegate.kt
Last active November 15, 2020 09:04
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()
}
@arifvn
arifvn / Car.kt
Last active November 15, 2020 09:07
class Car {
var name:String by Delegate()
var brand:String by Delegate()
var origin:String by Delegate()
}
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 }
}
class MainActivity: AppCompatActivity {
val buttonLogin by id(R.id.btn_login)
// other stuff
// ..
// ..
fun AppCompatActivity.id(viewId: Int) = ViewDelegate(viewId)
}
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)
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();
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");
}
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")
@arifvn
arifvn / inline.kt
Last active November 15, 2020 20:10
// declaration
inline fun inlineFunction(callback: () -> Unit) {
println("before callback")
callback()
println("after callback")
}
// calling function
btnPrintMessage.setOnClickListener(){
inlineFunction(){