This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// usage: | |
// delayInResumed(10L) { | |
// binding.appBottomBar.updateTabs() | |
// } | |
fun Activity.delayInResumed( | |
durationMs: Long = 0, | |
dispatcher: CoroutineDispatcher = Dispatchers.Main, | |
block: () -> Unit | |
) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// usage in fragment: val color = if(requireContext().isDarkThemeOn()) Color.DKGRAY else Color.WHITE | |
// source: https://stackoverflow.com/questions/55787035/is-there-an-api-to-detect-which-theme-the-os-is-using-dark-or-light-or-other | |
fun Context.isDarkThemeOn() = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private val ARGS_SCROLL_STATE = "recyclerState" | |
override fun onSaveInstanceState(outState: Bundle) { | |
super.onSaveInstanceState(outState) | |
outState.putParcelable(ARGS_SCROLL_STATE, recyclerView.getLayoutManager()?.onSaveInstanceState()) | |
} | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) |
NewerOlder