Skip to content

Instantly share code, notes, and snippets.

View lucassales2's full-sized avatar

Lucas Sales lucassales2

View GitHub Profile
@lucassales2
lucassales2 / SharedPreferenceLiveData.kt
Last active August 4, 2020 13:44 — forked from rharter/SharedPreferenceLiveData.kt
Creates LiveData objects that observe a value in SharedPreferences while they have active listeners.
import android.content.SharedPreferences
import androidx.lifecycle.LiveData
abstract class SharedPreferenceLiveData<T>(
val sharedPrefs: SharedPreferences,
val key: String,
val defValue: T
) : LiveData<T>() {
private val preferenceChangeListener =
@Suppress("FunctionName")
fun BroadcastReceiverFlow(c: Context, intentFilter: IntentFilter): Flow<Intent> {
return callbackFlow {
val broadcastReceiver = object : android.content.BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
offer(intent)
}
}
c.registerReceiver(broadcastReceiver, intentFilter)
awaitClose { c.unregisterReceiver(broadcastReceiver) }
@lucassales2
lucassales2 / build.gradle
Last active August 14, 2019 13:38
Android SDK build.gradle
dependencies {
implementation project(':core')
implementation project(':view')
implementation project(':live-edit')
}
@lucassales2
lucassales2 / build.gradle
Created August 14, 2019 12:33
Build gradle for core module
apply plugin: 'kotlin'
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'com.squareup.okhttp3:okhttp:3.12.3'
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.3'
implementation 'com.google.code.gson:gson:2.8.5'
}
fun Language(jsonObject: JSONObject) = Language(
id = jsonObject.getInt("id"),
name = jsonObject.getString("name"),
locale = Locale(jsonObject.getString("locale")),
direction = jsonObject.getString("direction"),
isDefault = jsonObject.getBoolean("is_default"),
isBestFit = jsonObject.getBoolean("is_best_fit")
)
data class Language(
val id: Int,
val name: String,
val locale: Locale,
val direction: String,
val isDefault: Boolean,
val isBestFit: Boolean
) {
constructor(jsonObject: JSONObject) : this(
id = jsonObject.getInt("id"),
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
minSdkVersion 19
targetSdkVersion 28
versionCode 230
import androidx.work.ListenableWorker
import dagger.MapKey
import kotlin.reflect.KClass
@MapKey
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION)
annotation class WorkerKey(val value: KClass<out ListenableWorker>)
import android.content.Context
import androidx.work.ListenableWorker
import androidx.work.WorkerFactory
import androidx.work.WorkerParameters
import javax.inject.Inject
import javax.inject.Provider
class DaggerWorkerFactory @Inject constructor(
private val creators: Map<Class<out ListenableWorker>, @JvmSuppressWildcards Provider<ChildWorkerFactory>>
) : WorkerFactory() {