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 / restClientContract.java
Created November 30, 2017 19:06
Rest client interface
interface RESTClient{
void getUsersList(RESTListener<List<User>> listListener);
void createUser(CreateUserForm userForm, RESTListener<User> listener);
void updateUser(String userId, CreateUserForm userForm, RESTListener<User> listener);
void getUserDetails(String userId, RESTListener<User> listener);
void deleteUser(String userId, RESTListener<String> listener);
}
@abdurahmanadilovic
abdurahmanadilovic / launchBlock.kt
Last active November 30, 2017 19:07
Example of coroutine launch block
launch {
// some code
val value = dataStorageImplementation.getStringValue("some_key")
// some code
}
@abdurahmanadilovic
abdurahmanadilovic / uiCoroutineContext.kt
Created November 30, 2017 19:12
Coroutine UI context
launch {
// some code
val value = dataStorageImplementation.getStringValue("some_key")
// some code
launch (UI) {
textView.text = value
}
}
@abdurahmanadilovic
abdurahmanadilovic / RestListener.java
Last active December 2, 2017 05:55
Generic REST Listener Interface in Java
interface RESTListener<T>{
void operationFailed(String reason);
void operationSuccess(T object);
}
@abdurahmanadilovic
abdurahmanadilovic / DataStorageContract.kt
Last active December 2, 2017 05:56
Interface for data storage layer in kotlin
interface DataStorageContract {
suspend fun storeValue(key: String, value: String)
suspend fun storeValue(key: String, value: Int)
suspend fun getStringValue(key: String): String
suspend fun getIntValue(key: String): Int
}
@abdurahmanadilovic
abdurahmanadilovic / runCoroutineBuilder.kt
Created December 2, 2017 08:03
run coroutine builder
launch(UI) {
val value = run(CommonPool) {
dataStorageImplementation.getStringValue("some_key")
}
textView.text = value
}
compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.19.3"
compile "org.jetbrains.kotlinx:kotlinx-coroutines-android:0.19.3"
@abdurahmanadilovic
abdurahmanadilovic / coroutinesEnableGradleBuild.gradle
Last active December 9, 2017 19:28
enable coroutine in build gradle file
kotlin {
experimental {
coroutines "enable"
}
}
@abdurahmanadilovic
abdurahmanadilovic / launchWithRetrofit.kt
Last active December 17, 2017 20:43
Exception handling with kotlin launch coroutine
fun sendCreateUserRequest(firstName: String, lastName: String) {
launch(parentJob + UI) {
try {
val listOfUsers = run(CommonPool) {
val user = User(firstName, lastName)
RestClient.apiDefinition.createUser(user).execute()
}
toast("Created user id is ${listOfUsers.body()?.id}")
} catch (ex: IOException) {
@abdurahmanadilovic
abdurahmanadilovic / launchExceptionHandler.kt
Created December 17, 2017 20:46
Exception handling with launch coroutine and exception handler
private val exceptionHandler: CoroutineContext = CoroutineExceptionHandler { _, throwable ->
alertWithOkButton("Exception caught inside the exception handler")
throwable.printStackTrace()
}
private fun getAllUsers() {
launch(parentJob + UI + exceptionHandler) {
val listOfUsers = run(CommonPool) {
RestClient.apiDefinition.listUsers().execute()
}