Skip to content

Instantly share code, notes, and snippets.

View truedem's full-sized avatar

Pavel Kuznetsov truedem

View GitHub Profile
@truedem
truedem / ViewModelFactory.kt
Last active December 14, 2020 13:20
ViewModelFactory for Android Jetpack
class ViewModelFactory constructor(
private val baseRepository: BaseRepository, // your repository class to handle database, network, shared preferences, etc.
owner: SavedStateRegistryOwner,
defaultArgs: Bundle? = null
) : AbstractSavedStateViewModelFactory(owner, defaultArgs) {
// usage in activity:
// val sharedVM by viewModels<MySharedViewModel> { ViewModelFactory(App.getRep(), this) }
// App.getRep() returns instance of BaseRepository, replace with your actual logic
@truedem
truedem / EditTextAsStateFlow.kt
Last active March 12, 2021 00:21
Extention function tu turn EditText input on Android into StateFlow
// source: https://github.com/HamdiBoumaiza/Stars/blob/main/app/src/main/java/com/hb/stars/utils/Extensions.kt
// extention function anywhere:
fun EditText.getTextChangeStateFlow(): StateFlow<String> {
val query = MutableStateFlow("")
addTextChangedListener {
query.value = it.toString()
}
return query
}
@truedem
truedem / VerticalSeekbar.kt
Created April 14, 2021 03:40
Vertical SeekBar for Android
class VerticalSeekBar @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : AppCompatSeekBar(context, attrs, defStyleAttr) {
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(h, w, oldh, oldw)
}
override fun setProgress(progress: Int) {
val oldWidth = width
@truedem
truedem / AppCfg.kt
Last active July 15, 2021 10:47
Universal Android Confirmation Dialog
object AppCfg {
const val CONFIRMATION_PARAM = "resultKey"
const val CONFIRMATION_TITLE = "title"
const val CONFIRMATION_TEXT = "content"
const val CONFIRMATION_POSITIVE = "positiveLabel"
const val CONFIRMATION_NEGATIVE = "negativeLabel"
const val CONFIRMATION_POSITIVE_ICON = "positiveIcon"
const val IS_DELETED = "IS_DELETED"
const val IS_EXPORTED = "IS_EXPORTED"
@truedem
truedem / StringUtils.kt
Created August 21, 2021 21:16
Kotlin extentions to check out if all words are presented in the string
// Usage:
// if (fullname.containsWords(searchString)) { doStuff() }
fun String.containsWords(str: String?): Boolean {
val words = str?.trim()?.lowercase()?.split("\\s".toRegex())?.toTypedArray()
return Arrays.stream(words).allMatch { s: CharSequence? -> this.lowercase().contains(s.toString().lowercase()!!) }
}
// Usage:
// val words = searchString.trim().split("\\s".toRegex()).toTypedArray()
@truedem
truedem / EditTextAutoSizeFonts.kt
Created April 19, 2022 06:35
EditText extention to auto-resize fonts (+ in other fields) if the text is too long
package com.utils.texts
import android.graphics.Paint
import android.graphics.Rect
import android.text.Editable
import android.text.TextWatcher
import android.util.TypedValue
import android.widget.EditText
import kotlin.math.max
import kotlin.math.min
@truedem
truedem / AutoResizingEditTextExt.kt
Last active October 8, 2022 13:54
Auto-resizing EditText font with callback
// to change text size in related fields (like "+" in front of phone numbers or quasi-hint text in the field below):
// binding.textEdit.autocorrectSize { size ->
// binding.phoneCountryPrefixTextField.setTextSize(TypedValue.COMPLEX_UNIT_PX, size)
// binding.hintBelowInputTextField.setTextSize(TypedValue.COMPLEX_UNIT_PX, size)
// }
// or just myEditText.autocorrectSize() somewhere in onViewCreated()
fun EditText.autocorrectSize(callback: ((Float) -> Unit)? = null) {
val originalTextSize = this.textSize
@truedem
truedem / coroutinesExt.kt
Last active October 3, 2024 09:30
Delayed operation till resumed stage in Android activity and fragment
// usage:
// delayInResumed(10L) {
// binding.appBottomBar.updateTabs()
// }
fun Activity.delayInResumed(
durationMs: Long = 0,
dispatcher: CoroutineDispatcher = Dispatchers.Main,
block: () -> Unit
) {