Skip to content

Instantly share code, notes, and snippets.

@Andrew0000
Andrew0000 / AStar.cs
Last active December 9, 2021 20:48
A Star C# Unity
using System;
using System.Collections.Generic;
using UnityEngine;
// Similar to https://lsreg.ru/realizaciya-algoritma-poiska-a-na-c/
/*
List<Vector2Int> path = AStar.FindPath(
current, target, 200, cell => { return isCleanToMovement(cell); }
@Andrew0000
Andrew0000 / AStar.cs
Created November 22, 2021 18:36
A Star with sorting by full path C# Unity
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// Similar to https://lsreg.ru/realizaciya-algoritma-poiska-a-na-c/
public class AStar
{
@Andrew0000
Andrew0000 / Example.kt
Created April 9, 2022 21:04
LiveData usage in View layer
viewModel.someValue.observe(viewLifecycleOwner) {
binding.someView.text = it
}
fun <T : Any> Observable<T>.observeWhenStarted(
lifecycleOwner: LifecycleOwner,
onNext: (T) -> Unit,
) {
lifecycleOwner.lifecycle.addObserver(object : LifecycleObserver {
private var disposable: Disposable? = null
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onStart() {
disposable = subscribe {
@Andrew0000
Andrew0000 / Utils.kt
Created May 14, 2022 12:52
runCoroutinesCatching
/**
* Like [runCatching] but doesn't change the coroutines cancellation mechanism that relies on [CancellationException].
*
* See: [Documentation](https://kotlinlang.org/docs/exception-handling.html)
*/
inline fun <R> runCoroutinesCatching(block: () -> R): Result<R> {
return try {
Result.success(block())
} catch (e: Throwable) {
if (e is CancellationException) {
@Andrew0000
Andrew0000 / Utils.kt
Created May 14, 2022 12:53
observeWhenStarted
fun <T> Flow<T>.observeWhenStarted(activity: FragmentActivity, action: (T) -> Unit) {
activity.lifecycleScope.launch {
activity.repeatOnLifecycle(Lifecycle.State.STARTED) {
[email protected] {
action(it)
}
}
}
}
@Andrew0000
Andrew0000 / TextMeasure
Last active November 22, 2022 15:28
TextMeasure
import android.graphics.Paint
import android.util.TypedValue
import android.widget.TextView
/**
* Use it for [android.widget.EditText], because on it autoSize doesn't work
*/
class TextMeasure(
private val textView: TextView,
@Andrew0000
Andrew0000 / remember {}
Last active January 27, 2023 19:14
remember {} with key is same as derivedStateOf
@Composable
fun ATest() {
val frequentlyChangedState = remember { mutableStateOf(0) }
LaunchedEffect(Unit) {
launch {
delay(5000) // delay so you can attach with Layout Inspector if you don't trust logs enough.
while (frequentlyChangedState.value < 10) {
delay(200)
frequentlyChangedState.value++
@Andrew0000
Andrew0000 / RxRetry.kt
Last active January 13, 2023 12:46
RxRetry with skip retry for 401
import io.reactivex.rxjava3.core.Completable
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.core.Single
import timber.log.Timber
import java.util.concurrent.TimeUnit
fun Completable.networkCommonRetrying() =
withRetrying(3, { 4000L * it }, networkRetryCheck)
@Andrew0000
Andrew0000 / DebugAssert.kt
Last active October 13, 2023 13:14
DebugAssert
@Suppress("Unused")
object DebugAssert {
fun ensure(predicate: () -> Boolean) {
ensure(predicate, null)
}
fun ensure(predicate: () -> Boolean, description: String? = null) {
if (BuildConfig.DEBUG && !predicate()) {