Skip to content

Instantly share code, notes, and snippets.

Avatar

Gabriel Peal gpeal

View GitHub Profile
View wifiBroadcastReceiver.kt
context.registerReceiverInScope(scope, WifiManager.WIFI_STATE_CHANGED_ACTION) { intent ->
val state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED)
// Use wifi state here
}
@gpeal
gpeal / Label.kt
Created Mar 14, 2021
Unique Observable Usage
View Label.kt
class Label @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
var inverted by uniqueObservable(false) { refreshDrawableState() }
...
}
View uniqueObservable.kt
/**
* Like Delegates.observable except it only calls the callback when the value actually changes.
*/
public inline fun <T> uniqueObservable(initialValue: T, emitInitial: Boolean = false, crossinline onChange: (value: T) -> Unit): ReadWriteProperty<Any?, T> {
if (emitInitial) onChange(initialValue)
return object : ObservableProperty<T>(initialValue) {
override fun afterChange(property: KProperty<*>, oldValue: T, newValue: T) {
if (oldValue != newValue) onChange(newValue)
}
}
View MyComposable.kt
@Composable
fun MyComposable() {
val viewModel: MyViewModel = mavericksViewModel()
val state by viewModel.collectAsState()
AnotherComposable(
state.value,
onClick = viewModel.onClick()
)
}
View fadeTo.kt
// Will fade myView in or out.
// You can call this repeatedly with the same value and it won't interrupt the ongoing animation.
myView.fadeTo(true)
myView.fadeTo(false)
myView.fadeTo(true, toAlpha = 0.8f)
myView.fadeTo(true, startDelay = 300)
myView.fadeTo(true, duration = 500)
View updatePadding.kt
recyclerView.updatePadding(top = 14.dp.toInt())
View dp.kt
/**
* Call this function on a dp value and it will return the equivalent
* number of pixels for the current display.
* e.g. 8.dp
*/
val Number.dp get() = toFloat() * (Resources.getSystem().displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)
View detekt.dangerfile.js
import { fail, warn } from "danger";
const fs = require("fs");
const xml2js = require("xml2js");
const detektReportFile = fs.readFileSync(
"../build/reports/detekt/detekt-checkstyle.xml"
);
const parser = new xml2js.Parser({ async: false, attrkey: "attrs" });
parser.parseString(detektReportFile, (err, report) => {
if (err != null) {
View build.gradle
allprojects {
gradle.projectsEvaluated {
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
kotlinOptions {
allWarningsAsErrors = true
}
}
}
}
View pre-push
#!/bin/bash -e
# Run Detekt on pre-push on changed files
cd Android
while read -r local_ref local_sha remote_ref remote_sha; do
if [[ "$local_sha" =~ ^0+$ ]]; then
# Handle delete
: