This file contains 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
const val ID = "id" | |
class MyActivity : Activity() { | |
private val id1 by extra<String>(ID) // String? | |
private val id2 by extraNotNull<String>(ID) // String | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
requireNotNull(id1, id2) |
This file contains 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
inline fun <reified T: Any> Fragment.extra(key: String, default: T? = null) = lazy { | |
val value = arguments?.get(key) | |
if (value is T) value else default | |
} | |
inline fun <reified T: Any> Fragment.extraNotNull(key: String, default: T? = null) = lazy { | |
val value = arguments?.get(key) | |
requireNotNull(if (value is T) value else default) { key } | |
} |
This file contains 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
inline fun <reified T: Any> Activity.extra(key: String, default: T? = null) = lazy { | |
val value = intent?.extras?.get(key) | |
if (value is T) value else default | |
} | |
inline fun <reified T: Any> Activity.extraNotNull(key: String, default: T? = null) = lazy { | |
val value = intent?.extras?.get(key) | |
requireNotNull(if (value is T) value else default) { key } | |
} |
This file contains 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 class HttpInterceptor implements Interceptor { | |
@Override | |
public Response intercept(Chain chain) throws IOException { | |
Request request = chain.request(); | |
//Build new request | |
Request.Builder builder = request.newBuilder(); | |
builder.header("Accept", "application/json"); //if necessary, say to consume JSON | |
This file contains 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
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
public class JavaMD5 { | |
public static void main(String[] args) { | |
String passwordToHash = "MyPassword123"; | |
String generatedPassword = null; | |
try { | |
MessageDigest md = MessageDigest.getInstance("MD5"); |
This file contains 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
@Test | |
public void switchMap() throws Exception { | |
final List<String> items = Lists.newArrayList("a", "b", "c", "d", "e", "f"); | |
final TestScheduler scheduler = new TestScheduler(); | |
Observable.from(items) | |
.concatMap( s -> { | |
final int delay = new Random().nextInt(10); | |
return Observable.just(s + "x") |
This file contains 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
@Test | |
public void switchMap() throws Exception { | |
final List<String> items = Lists.newArrayList("a", "b", "c", "d", "e", "f"); | |
final TestScheduler scheduler = new TestScheduler(); | |
Observable.from(items) | |
.switchMap( s -> { | |
final int delay = new Random().nextInt(10); | |
return Observable.just(s + "x") |
This file contains 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
@file:Suppress("NOTHING_TO_INLINE") | |
import android.util.Log | |
import io.reactivex.* | |
inline fun <reified T> printEvent(tag: String, success: T?, error: Throwable?) = | |
when { | |
success == null && error == null -> Log.d(tag, "Complete") /* Only with Maybe */ | |
success != null -> Log.d(tag, "Success $success") | |
error != null -> Log.d(tag, "Error $error") |
This file contains 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
/** | |
* Timber log tree for production. | |
*/ | |
public class CrashReportingTree extends Timber.Tree{ | |
@Override protected void log(int priority, String tag, String message, Throwable throwable) { | |
switch (priority) { | |
case Log.VERBOSE: | |
case Log.DEBUG: | |
case Log.INFO: |