Skip to content

Instantly share code, notes, and snippets.

@faruktoptas
faruktoptas / debounce.kt
Created March 5, 2020 06:28
Kotlin coroutine debounce for EditText
fun <T> debounce(
waitMs: Long = 300L,
scope: CoroutineScope,
destinationFunction: (T) -> Unit
): (T) -> Unit {
var debounceJob: Job? = null
return { param: T ->
debounceJob?.cancel()
debounceJob = scope.launch {
delay(waitMs)
@Zhuinden
Zhuinden / FragmentViewBindingDelegate.kt
Last active February 11, 2025 09:25
Fragment view binding delegate
// https://github.com/Zhuinden/fragmentviewbindingdelegate-kt
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.viewbinding.ViewBinding
import kotlin.properties.ReadOnlyProperty
@emmaguy
emmaguy / textcolor-stats.kts
Created February 17, 2020 12:41
Look at all Android layout files and collate what value is set for 'android:textColor' and count the usages of each
val textColorsUsed = mutableMapOf<String, Int>()
File("../").walkTopDown().forEach { file ->
if (file.isFile && file.extension == "xml" && file.path.contains("res/layout")) {
val builder: DocumentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
val xmlInput = InputSource(StringReader(file.readText()))
val doc: Document = builder.parse(xmlInput)
val androidViews = XPathFactory.newInstance()
.newXPath()
@gabrielemariotti
gabrielemariotti / README.MD
Last active September 24, 2024 09:22
How to use the ShapeableImageView.

The Material Components Library introduced with the 1.2.0-alpha03 the new ShapeableImageView.

In your layout you can use:

 <com.google.android.material.imageview.ShapeableImageView
      android:id="@+id/image_view"
      app:srcCompat="@drawable/..." />

Then in your code apply the ShapeAppearanceModel to define your custom corners:

@hector6872
hector6872 / ComposeStore.kt
Last active February 22, 2023 09:49
Redux implementation for Kotlin :)
private val LocalStore: ProvidableCompositionLocal<Store<*>> = compositionLocalOf { error("Store not provided") }
@Composable
fun <STATE : State> StoreProvider(store: Store<STATE>, content: @Composable Store<STATE>.() -> Unit) {
CompositionLocalProvider(LocalStore provides store) {
store.content()
}
}
@Composable
@hector6872
hector6872 / WindowInsetsFrameLayout.kt
Created December 2, 2019 10:03
A fragment container enabling the use of android:fitsSystemWindows in fragment layouts
class WindowInsetsFrameLayout @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
init {
setOnHierarchyChangeListener(object : OnHierarchyChangeListener {
override fun onChildViewAdded(
parent: View,
@hector6872
hector6872 / LineView.kt
Last active December 10, 2019 20:48
Dash/LineView custom view for Android
class LineView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
companion object {
const val ORIENTATION_VERTICAL = 0
const val ORIENTATION_HORIZONTAL = 1
private const val DEFAULT_ORIENTATION = ORIENTATION_HORIZONTAL
@hector6872
hector6872 / KotlinDsl.kt
Last active December 10, 2019 20:49
Kotlin DSL example
@DslMarker
annotation class HorizontalSelectorGroups
interface HorizontalSelectorHelper {
fun groups(block: GroupsBuilder.() -> Unit): List<Pair<Header, List<Item>>> = GroupsBuilder().apply(block).build()
@HorizontalSelectorGroups
class GroupsBuilder {
private val groups = mutableListOf<Group>()
@burntcookie90
burntcookie90 / linguist-runner.sh
Last active February 23, 2021 03:51
runs linguist on ever commit since a date
#!/usr/bin/bash
for commit in $(git --no-pager log --reverse --after="2016-10-01T10:36:00-07:00" --pretty=format:%H)
do
echo $commit
git checkout $commit
#write linguist data to a file
echo "" >> ~/repo-linguist-report.txt
echo "commit: $commit" >> ~/repo-linguist-report.txt
@hector6872
hector6872 / StickyHeaderItemDecoration.kt
Last active September 11, 2019 14:15
ItemDecorator for sticky headers in Kotlin
import android.graphics.*
import android.view.*
import android.view.MotionEvent.ACTION_DOWN
import androidx.recyclerview.widget.RecyclerView
class StickyHeaderItemDecoration(
parent: RecyclerView,
private val isHeader: (position: Int) -> Boolean
) : RecyclerView.ItemDecoration() {