Skip to content

Instantly share code, notes, and snippets.

View diefferson's full-sized avatar
🎯
Focusing

Diefferson Santos diefferson

🎯
Focusing
View GitHub Profile
@diefferson
diefferson / SecurePreferences.kt
Created October 28, 2019 19:36
Secure preferences
package br.com.juno.data.repository.preferences
import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import android.security.KeyChain
import android.security.keystore.KeyGenParameterSpec
import android.security.keystore.KeyInfo
import android.security.keystore.KeyProperties
import android.util.Base64
@diefferson
diefferson / Activity.kt
Last active October 18, 2019 17:13
Lifecycle Safe delegate
class Activity:AppCompatActivity{
val actionsAdapter:MyListAdapter by lifecycleSafe(this){
MyListAdapter()
}
}
@diefferson
diefferson / ResultAsync.kt
Last active October 19, 2020 19:20
Async Catching
package br.com.example.domain.interactor.base
import br.com.exmaple.domain.exception.ErrorHandler
import br.com.exmaple.domain.exception.MyCustomException
import kotlinx.coroutines.*
import org.koin.core.KoinComponent
import org.koin.core.inject
class ResultAsync<T> private constructor(
scope: CoroutineScope, val action: suspend () -> T) : KoinComponent {
@diefferson
diefferson / animation_utils.kt
Created September 10, 2019 17:25
Animations Utils
import android.content.Context
import android.util.DisplayMetrics
fun linearMap(source: Float, sourceMin: Float, sourceMax: Float, targetMin: Float, targetMax: Float): Float {
return ((source - sourceMin) / (sourceMax - sourceMin)) * (targetMax - targetMin) + targetMin
}
fun clampedLinearPercent(percent: Float, expandAnchor: Float, collapseAnchor: Float) : Float {
return ((Math.max(percent, collapseAnchor) - collapseAnchor) / (expandAnchor - collapseAnchor)) * expandAnchor
}
@diefferson
diefferson / Authenticate.kt
Created July 22, 2019 15:09
Kotlin Use Case
class Authenticate(private val userRepository: UserRepository) :UseCase<Boolean, Authenticate.Params>(){
override suspend fun run(params: Params): Boolean {
return userRepository.authenticate(params.username, params.password)
}
class Params(val username:String, val password:String)
}
@diefferson
diefferson / Builder
Created November 30, 2018 17:45
builder
data class Address internal constructor(
val addressLine1: String,
val addressLine2: String = "",
val city: String,
val state: String = "",
val province: String = "",
val zip: String,
val country: String = "USA") {
class Builder {
@diefferson
diefferson / AndroidManifest.xml
Last active November 30, 2018 17:26
TranslucentStatusBar
<activity
android:name=".ui.settings.help.AboutActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.TranslucentStatusBar"/>
/*
Status dos jogos
aberto - entre 1 e 36 horas
fechando - entre 60 e 15 minutos
nao palpitou - menos de 15 minutos -
fechado - mais que 36 horas de diferença
nao craque - para jogos marcados como craque
@diefferson
diefferson / ResultAsync.kt
Last active November 28, 2018 13:49
Result Patern to Coroutines
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
class ResultAsync<T> private constructor(action: suspend () -> T, scope: CoroutineScope) {
internal var onSuccess : (T) -> Unit = {}
internal var onError : (e: Throwable) -> Unit = {}
companion object {
fun <T> with(action: suspend () -> T, scope: CoroutineScope) :ResultAsync<T>{
@diefferson
diefferson / startServiceSample.kt
Last active November 23, 2018 13:57
Start Service Android O
fun startService(context: Context){
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(Intent(context, MyService::class.java))
} else {
context.startService(Intent(context, MyService::class.java))
}
}catch (e :Exception){
e.stackTrace
}