Skip to content

Instantly share code, notes, and snippets.

View dmersiyanov's full-sized avatar

Dmitry Mersiyanov dmersiyanov

  • Moscow
View GitHub Profile
@dmersiyanov
dmersiyanov / Fragment.kt
Last active March 22, 2023 12:43
Handle dynamic ViewPager2 page height
class Fragment() {
private fun initViews() = with(binding) {
pager.setPageTransformer { page, pos ->
if (pos == 0.0F) {
updatePagerHeightForChild(page, pager)
}
}
}
@dmersiyanov
dmersiyanov / SavedState.kt
Created July 12, 2022 10:47
Save state for android custom view
internal class SavedState : BaseSavedState {
var isExpanded: Boolean = false
constructor(superState: Parcelable) : super(superState)
constructor(source: Parcel) : super(source) {
isExpanded = source.readBoolean()
}
constructor(source: Parcel?, loader: ClassLoader?) : super(source, loader)
@dmersiyanov
dmersiyanov / BundleExtensions.kt
Created June 9, 2022 06:20
Kotlin android bundle extensions
import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import timber.log.Timber
import java.io.Serializable
inline fun <reified T : Parcelable> Bundle.putWithClassNameKey(parcelable: T?) {
parcelable?.let { putParcelable(T::class.java.simpleName, parcelable) }
}
@dmersiyanov
dmersiyanov / FragmentArgumentDelegate.kt
Last active June 9, 2022 06:22
Delegate for fragment arguments
import android.os.Bundle
import android.os.Parcelable
import java.io.Serializable
import androidx.fragment.app.Fragment
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
class FragmentArgumentDelegate<T : Any> :
ReadWriteProperty<Fragment, T> {
@dmersiyanov
dmersiyanov / SaveBitmapToStorage.kt
Created May 31, 2022 07:58
Save bitmap to external storage android
import android.content.ContentValues
import android.graphics.Bitmap
import android.os.Build
import android.provider.MediaStore
import androidx.fragment.app.Fragment
import timber.log.Timber
import java.io.IOException
/**
@dmersiyanov
dmersiyanov / RxConnectionChecker.kt
Last active May 25, 2022 08:35
RxConnectionCheckehelp observe network state on android with RxJava Subjects
import android.annotation.SuppressLint
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import android.os.Build
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.subjects.BehaviorSubject
import timber.log.Timber
import java.util.concurrent.atomic.AtomicInteger
@dmersiyanov
dmersiyanov / ConnectionStateManager.kt
Created May 25, 2022 08:29
ConnectionStateManager help observe network state on android with kotlin flow
import android.annotation.SuppressLint
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import android.os.Build
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
@dmersiyanov
dmersiyanov / Manifest.xml
Last active May 25, 2022 08:32
Android sharing with image preview
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="appPackage">
<application
android:name=".app.App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
@dmersiyanov
dmersiyanov / PercentVisibleCardView.kt
Created April 28, 2022 10:17
PercentVisibleCardView
import android.content.Context
import android.util.AttributeSet
import android.view.ViewTreeObserver
import androidx.cardview.widget.CardView
import ru.dpd.core.extension.getViewVisibilityPercent
class PercentVisibleCardView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null
) : CardView(context, attrs) {
@dmersiyanov
dmersiyanov / RxRetryExtensions.kt
Last active May 25, 2022 08:31
Rx retry with delay operator
private const val REPO_DEFAULT_RETRY_COUNT = 5 // 62 sec total ~1 min
private val zipper = BiFunction { t: Throwable, u: Int -> Pair(t, u) }
fun Completable.networkStateRetry(retryAttempts: Int = REPO_DEFAULT_RETRY_COUNT): Completable =
retryWhen {
// retryAttempts + 1 для того чтобы была выброшена ошибка
it.zipWith(Flowable.range(1, retryAttempts + 1), zipper)
.flatMap { result ->
val delayTime = 1L * 2.0.pow(result.second.toDouble()).roundToLong()
return@flatMap (