Skip to content

Instantly share code, notes, and snippets.

View jeziellago's full-sized avatar
🇧🇷

Jeziel Lago jeziellago

🇧🇷
View GitHub Profile
fun loadTask() = launch(UI) { // executa na main thread
// executa em uma thread separada
val result1 = withContext(CommonPool) { // work! }
// executa na mesma thread
val result2 = withContext(Unconfined) { // work! }
}
fun load() = launch(UI) {
try {
val result = withContext(CommonPool) { // work! }
...
} catch (e: IOException) { // exception que pode gerar
// exception here!
}
}
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.withContext
import retrofit2.Call
import retrofit2.Retrofit
...
class Api {
...
fun performRequest() {
api.request(
call = service.getUser("jeziellago"),
onSuccess = { user -> // fazer algo com o usuário },
onFailure = { error -> // exibir o erro }
)
}
interface ApiService {
@GET("/users/{username}")
fun getUser(@Path("username") username: String): Call<User>
}
dependencies {
// ML Kit Smart Reply
implementation 'com.google.firebase:firebase-ml-natural-language:18.2.0'
implementation 'com.google.firebase:firebase-ml-natural-language-smart-reply-model:18.0.0'
}
android {
// ...
aaptOptions {
noCompress "tflite"
}
}
val localMessage = FirebaseTextMessage.createForLocalUser("Hi, I'm local user.",
System.currentTimeMillis())
val remoteUserId = "1234abcde"
val remoteMessage = FirebaseTextMessage.createForRemoteUser("Hi, I'm remote user.",
System.currentTimeMillis(),
remoteUserId)
// create smart-reply instance
val smartReply = FirebaseNaturalLanguage.getInstance().smartReply
// message list
val conversation = ArrayList<FirebaseTextMessage>()
// add messages on conversation
...
// get suggested messages
@jeziellago
jeziellago / channel interfaces.kt
Created December 10, 2019 03:27
Channel Interfaces
interface SendChannel<in E> {
suspend fun send(element: E)
fun close(): Boolean
}
interface ReceiveChannel<out E> {
suspend fun receive(): E
}
interface Channel<E> : SendChannel<E>, ReceiveChannel<E>