This file contains hidden or 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
async(parentJob + UI) { | |
val task = async(parentJob + CommonPool) { | |
RestClient.apiDefinition.listPosts().execute() | |
} | |
val result = task.await() | |
val numberOfPosts = "number of posts ${result.body()?.size}" | |
numberOfPostsTextView.text = numberOfPosts | |
}.invokeOnCompletion { it: Throwable? -> |
This file contains hidden or 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
private val parentJob = Job() | |
override fun onResume() { | |
super.onResume() | |
getAllUsers() | |
getAllPosts() | |
} | |
override fun onPause() { | |
super.onPause() |
This file contains hidden or 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
override suspend fun getBooleanValue(key: String, default: Boolean): Boolean = suspendCoroutine { continuation -> | |
fireBaseDb.child("keys").child(key).addListenerForSingleValueEvent(object : ValueEventListener { | |
override fun onCancelled(p0: DatabaseError?) { | |
continuation.resume(default) | |
} | |
override fun onDataChange(p0: DataSnapshot?) { | |
if (p0 != null) { | |
continuation.resume(p0.getValue(Boolean::class.java) ?: default) | |
} else { |
This file contains hidden or 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
public static long minutesAgo(Calendar before, Calendar after){ | |
long diff = after.getTimeInMillis() - before.getTimeInMillis(); | |
return TimeUnit.MILLISECONDS.toMinutes(diff); | |
} | |
// junit test for the method above | |
@Test | |
public void testThreeMinutesAgo() throws Exception { | |
Calendar now = Calendar.getInstance(); | |
Calendar threeMinutesAgo = (Calendar) now.clone(); |
This file contains hidden or 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
public class PrettyFormatCalendar{ | |
private Calendar before; | |
public PrettyFormatCalendar(Calendar before) { | |
this.before = before; | |
} | |
public long minutesAgoFrom(Calendar other){ | |
long diff = other.getTimeInMillis() - before.getTimeInMillis(); | |
return TimeUnit.MILLISECONDS.toMinutes(diff); |
This file contains hidden or 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
// run the code below on https://try.kotlinlang.org with JUnit env | |
class CalendarUtilsTests{ | |
@Test | |
fun testUtilsClass(){ | |
val now = Calendar.getInstance() | |
val threeMinutesAgo = now.clone() as Calendar | |
threeMinutesAgo.add(Calendar.MINUTE, -3) | |
assertEquals("3 minutes ago", "${threeMinutesAgo.minutesAgoFrom(now)} minutes ago") | |
} |
This file contains hidden or 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 <T> MutableList<T>.swap(index1: Int, index2: Int) { | |
val tmp = this[index1] // 'this' corresponds to the list | |
this[index1] = this[index2] | |
this[index2] = tmp | |
} |
This file contains hidden or 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 Person(val firstName: String, val lastName: String) |
This file contains hidden or 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 SecondaryConstructor(){ | |
val blog = "www.codingstoic.com" | |
val city = "Sarajevo" | |
constructor(aValue: Int) : this(){ | |
println("a second constructor") | |
} | |
init{ | |
println("inside the first init block") |
This file contains hidden or 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 Person(val firstName: String){ | |
val blog = "www.codingstoic.com" | |
val city = "Sarajevo" | |
init{ | |
println("by $firstName") | |
println("inside init block") | |
println("blog is $blog and city is $city") | |
} | |