Skip to content

Instantly share code, notes, and snippets.

@davidliu
davidliu / RxJavaTestSchedulerRule.java
Created January 22, 2018 00:59
Rule for running RxJava synchronously for JUnit Tests
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import io.reactivex.android.plugins.RxAndroidPlugins;
import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.schedulers.TestScheduler;
/**
* Created by davidliu on 6/5/17.
@davidliu
davidliu / Lce.java
Created January 22, 2018 01:00
Loading/Content/Error object for RxJava
import android.support.annotation.Nullable;
import io.reactivex.ObservableTransformer;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import retrofit2.HttpException;
import retrofit2.Response;
import android.support.annotation.Nullable;
import io.reactivex.ObservableTransformer;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import retrofit2.HttpException;
import retrofit2.Response;
public class LocalProfileAPI implements ProfileAPI {
public Profile retrieveProfile() {
/*... retrieve from DB or something ...*/
return new Profile();
}
}
@davidliu
davidliu / layout.xml
Last active March 31, 2019 20:07
Example of taking advantage of app bar layout behavior for header scrolling with anything.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
@davidliu
davidliu / MainActivity.kt
Created March 13, 2019 19:11
RecyclerView in a ScrollView
package com.test.scrollviewrecyclerview
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.random.Random
@davidliu
davidliu / MainActivity.kt
Created March 27, 2019 01:17
Random access through SAF
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.type = "*/*"
intent.addCategory(Intent.CATEGORY_OPENABLE)
startActivityForResult(intent, 1)
}
public class DelegatingLayout extends FrameLayout {
private boolean mIsDelegating;
private ViewGroup mDelegateView;
private int[] mOriginalOffset = new int[2];
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
// Clear delegating flag on touch start/end
@davidliu
davidliu / CompiledView.java
Created April 4, 2019 21:57
ViewCompiler input <-> output
package is.shortcut;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.util.AttributeSet;
import android.util.Xml;
import android.view.*;
import android.widget.*;
public final class CompiledView {
class UserPushSettingsSyncWorkerTest : DonkeyTestCase() {
@get:Rule
var rxRule = RxJavaTestSchedulerRule()
@Mock
lateinit var fetcher: MediumServiceProtos.ObservableMediumService
@Mock
lateinit var notificationManager: NotificationManager