Skip to content

Instantly share code, notes, and snippets.

View krossovochkin's full-sized avatar
👌

Vasya Drobushkov krossovochkin

👌
View GitHub Profile
@krossovochkin
krossovochkin / RxJava.kt
Created May 15, 2020 13:53
map suspend rxjava
View RxJava.kt
fun hiThere(): Single<Int> {
return Completable.timer(1, TimeUnit.SECONDS)
.andThen(Single.just(40))
.subscribeOn(computation())
}
fun test() {
Observable.just(1)
.flatMapSingle { hiThere() }
.subscribeOn(io())
.subscribe { println(it) }
@krossovochkin
krossovochkin / Kotlin.kt
Created May 15, 2020 13:50
map suspend kotlin
View Kotlin.kt
suspend fun hiThere(): Int {
return withContext(Dispatchers.Default) {
delay(1000)
40
}
}
@Test
fun test() {
CoroutineScope(Job()).launch {
flowOf(1)
@krossovochkin
krossovochkin / ViewModelScope.kt
Created May 13, 2020 17:31
RxJava ViewModelScope
View ViewModelScope.kt
class TestViewModel : MyViewModel() {
init {
scope.launch(
Observable.just("10")
.subscribe { println("Something") }
)
Observable.just("10")
.doOnNext { println("something") }
@krossovochkin
krossovochkin / Throttle.kt
Created February 23, 2020 12:54
RxJava to Kotlin Flow: Throttling
View Throttle.kt
package by.krossovochkin.testflow
import io.reactivex.Observable
import io.reactivex.schedulers.Schedulers.computation
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.produce
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.selects.select
@krossovochkin
krossovochkin / Error.kt
Created February 23, 2020 08:15
From RxJava to Kotlin Flow: Error Handling
View Error.kt
package by.krossovochkin.testflow
import io.reactivex.Observable
import io.reactivex.exceptions.CompositeException
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import org.junit.Test
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.CountDownLatch
View MainActivity.kt
private fun setTheme(theme: ThemeManager.Theme, animate: Boolean = true) {
if (!animate) {
ThemeManager.theme = theme
return
}
if (imageView.isVisible) {
return
}
View MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
LayoutInflaterCompat.setFactory2(
LayoutInflater.from(this),
MyLayoutInflater(delegate)
)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
View MainActivity.kt
class MyLayoutInflater(
private val delegate: AppCompatDelegate
) : LayoutInflater.Factory2 {
override fun onCreateView(
parent: View?,
name: String,
context: Context,
attrs: AttributeSet
): View? {
View MainActivity.kt
class MyTextView
@JvmOverloads
constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : AppCompatTextView(context, attrs, defStyleAttr),
ThemeManager.ThemeChangedListener {
override fun onFinishInflate() {
View MainActivity.kt
object ThemeManager {
private val listeners = mutableSetOf<ThemeChangedListener>()
var theme = Theme.LIGHT
set(value) {
field = value
listeners.forEach { listener -> listener.onThemeChanged(value) }
}
interface ThemeChangedListener {