Skip to content

Instantly share code, notes, and snippets.

View abdurahmanadilovic's full-sized avatar
🏠
Working from home

Abdurahman Adilovic abdurahmanadilovic

🏠
Working from home
View GitHub Profile
@abdurahmanadilovic
abdurahmanadilovic / asyncCoroutineExceptionHandling.kt
Created December 17, 2017 20:48
Exception handling with async coroutine
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? ->
@abdurahmanadilovic
abdurahmanadilovic / SomeActivity.kt
Created December 17, 2017 20:58
Canceling all coroutine jobs
private val parentJob = Job()
override fun onResume() {
super.onResume()
getAllUsers()
getAllPosts()
}
override fun onPause() {
super.onPause()
@abdurahmanadilovic
abdurahmanadilovic / suspendCoroutine.kt
Created December 17, 2017 21:07
suspendCoroutine example with firebase
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 {
@abdurahmanadilovic
abdurahmanadilovic / CalendarUtils.java
Created January 20, 2018 08:16
Minutes ago helper function for Calendar objects
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();
@abdurahmanadilovic
abdurahmanadilovic / PrettyFormatCalendar.java
Created January 20, 2018 08:43
Pretty minutes ago formatter for Calendar bject
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);
@abdurahmanadilovic
abdurahmanadilovic / CalendarUtils.kt
Created January 20, 2018 10:01
Extension function for "minutes ago" calendar formatting
// 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")
}
@abdurahmanadilovic
abdurahmanadilovic / SwapExtensionFunction.kt
Created January 20, 2018 21:29
Swap extension function with generics
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
}
@abdurahmanadilovic
abdurahmanadilovic / PrimaryKonstructorKotlin.kt
Created January 24, 2018 07:15
Kotlin primary constructor
class Person(val firstName: String, val lastName: String)
@abdurahmanadilovic
abdurahmanadilovic / KotlinInitBlock.kt
Last active January 27, 2018 06:56
Kotlin init block
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")
}
@abdurahmanadilovic
abdurahmanadilovic / secondaryConstructor.kt
Last active January 27, 2018 07:30
Kotlin secondary constructor
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")