Skip to content

Instantly share code, notes, and snippets.

View gpeal's full-sized avatar

Gabriel Peal gpeal

View GitHub Profile
@gpeal
gpeal / detekt.dangerfile.js
Created January 17, 2021 07:28
Tonal Dangerbot
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) {
allprojects {
gradle.projectsEvaluated {
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
kotlinOptions {
allWarningsAsErrors = true
}
}
}
}
#!/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
:
#!/bin/bash
java -jar libs/detekt-cli-1.10.0-all.jar --build-upon-default-config --includes "**/src/main/**/*.kt" -c detekt.yml -p libs/detekt-formatting-1.10.0-RC1.jar --parallel -r xml:build/reports/detekt/detekt-checkstyle.xml "$@"
build:
maxIssues: 0
processors:
active: true
console-reports:
active: true
formatting:
class MyClass {
// This will automatically have the TAG "MyClass"
private val log by timber()
fun logSomething() {
log.i("Hello")
log.w(Exception(), "World")
}
}
import timber.log.Timber
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
class MyClass {
// This will automatically have the TAG "MyClass"
private val log by timber()
fun logSomething() {
log.i("Hello")
@gpeal
gpeal / MyClass.kt
Created January 17, 2021 06:30
Conflated Job Example
class MyClass(private val scope: CoroutineScope) {
private val job = ConflatedJob()
fun retry() {
retryJob += scope.launch {
delay(Long.MAX_VALUE)
}
}
}
@gpeal
gpeal / ConflatedJob.kt
Last active September 5, 2023 19:10
Conflated Job
class MyClass(private val scope: CoroutineScope) {
private val job = ConflatedJob()
fun retry() {
retryJob += scope.launch {
delay(Long.MAX_VALUE)
}
}
}
@gpeal
gpeal / BroadcastReceiver.kt
Last active February 21, 2023 11:07
Coroutine Broadcast Receivers
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
}
/**
* Register a broadcast receiver in the given coroutine scope for any of the specified actions
* and call the callback when it is invoked.
*/
fun Context.registerReceiverInScope(