Skip to content

Instantly share code, notes, and snippets.

View ZacSweers's full-sized avatar

Zac Sweers ZacSweers

View GitHub Profile
@ZacSweers
ZacSweers / Sample.kt
Created November 19, 2017 02:05
Stars in notifications
fun notification() {
val channelId = "stars"
val notificationManager = activity.getSystemService<NotificationManager>()
if (VERSION.SDK_INT >= VERSION_CODES.O) {
val channels = notificationManager.notificationChannels
if (channels.none { it.id == channelId }) {
NotificationChannel(
channelId, "stars", NotificationManager.IMPORTANCE_HIGH)
.apply {
description = "Demo"
@ZacSweers
ZacSweers / DiscussionGuidelines.md
Created November 20, 2017 07:04
Discussion guidelines

Guidelines

To keep the arguments and examples to the point there are few helpful rules:

  • No abstract examples/arguments. These cause the discussion to lose focus and make examples harder to follow. The example/argument must be traceable to a real-world problem - ___ is intended to solve real problems, not imaginary ones.
  • Examples must show the full complexity of the problem domain. Simplified examples trivialize the problems and the solutions intended to solve those simplified examples may not work for the complex problems.
  • Examples of problematic ___ code must be the “best way” of writing the code in ___ - if it can be improved then the improved version should be used instead.
  • Arguments must be straight to the point and as concise as possible.
  • Arguments should take the point of view of an average programmer - not the über-programmer who doesn’t make design mistakes.
// Sample JSON
// { "bar": "hello world" }
// The Foo class
@JsonClass(generateAdapter = true)
data class Foo(
val bar: String
)
// Generated by Moshi Kotlin Code Gen
@JsonClass(generateAdapter = true)
data class Foo<T>(
@Json(name = "barString") val bar: String,
val defaultValue: Int = 0,
val nullableString: String?,
val typeParam: T? = null,
val tList: List<T>
)
// Generated by Moshi Kotlin Code Gen
val asyncMainThreadScheduler = AndroidSchedulers.from(Looper.getMainLooper(), true)
RxAndroidPlugins.setInitMainThreadSchedulerHandler { asyncMainThreadScheduler }
// Or if the default scheduler is already initialiazed
RxAndroidPlugins.setMainThreadSchedulerHandler { asyncMainThreadScheduler }
Scheduler asyncMainThreadScheduler = AndroidSchedulers.from(Looper.getMainLooper(), true);
RxAndroidPlugins.setInitMainThreadSchedulerHandler(callable -> asyncMainThreadScheduler);
// Or if the default scheduler is already initialiazed
RxAndroidPlugins.setMainThreadSchedulerHandler(scheduler -> asyncMainThreadScheduler);
@ZacSweers
ZacSweers / GuardTest.kt
Last active September 14, 2018 23:05
Demo of how the Nothing type in Kotlin can allow a Swift-style guard function
import com.google.common.truth.Truth.assertThat
import org.junit.Test
inline fun <T> guard(receiver: T?, block: () -> Nothing): T {
if (receiver == null) {
block()
}
return receiver
}
@ZacSweers
ZacSweers / OkioAtomicFile.kt
Created September 16, 2018 06:02
Okio/AtomicFile interop kotlin extensions
import android.util.AtomicFile
import okio.Buffer
import okio.Sink
import okio.sink
import okio.source
import java.io.FileOutputStream
import java.io.IOException
fun AtomicFile.source() = openRead().source()
> Task :compileJava
warning: [options] bootstrap class path not set in conjunction with -source 1.6
warning: [options] source value 1.6 is obsolete and will be removed in a future release
warning: [options] target value 1.6 is obsolete and will be removed in a future release
warning: [options] To suppress warnings about obsolete options, use -Xlint:-options.
/Users/zsweers/dev/java/contributing/RxJava/src/main/java/io/reactivex/Single.java:135: error: [NullAway] passing @Nullable parameter 'null' where @NonNull is required
return RxJavaPlugins.onAssembly(new SingleAmb<T>(null, sources));
^
(see http://t.uber.com/nullaway )
/Users/zsweers/dev/java/contributing/RxJava/src/main/java/io/reactivex/Single.java:163: error: [NullAway] passing @Nullable parameter 'null' where @NonNull is required
@ZacSweers
ZacSweers / BlurrinessDetection.kt
Last active August 5, 2022 03:18
Demo implementation of client-side image blurriness detection on Android using renderscript.
/*
* Copyright (c) 2018. Uber Technologies
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software