Skip to content

Instantly share code, notes, and snippets.

View PierceZ's full-sized avatar

Pierce Zaifman PierceZ

  • Canada
View GitHub Profile
@PierceZ
PierceZ / MainEmptyActivity.java
Created March 31, 2017 14:22
Empty main activity to decide which activity to launch.
public class MainEmptyActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent activityIntent;
// go straight to main if a token is stored
if (Util.getToken() != null) {
activityIntent = new Intent(this, MainActivity.class);
@PierceZ
PierceZ / LiveDataBus.java
Last active March 28, 2019 22:49
An event bus using Google's LiveData and LifecycleRegistryOwner.
public final class LiveDataBus {
private static SparseArray<EventLiveData> sSubjectMap = new SparseArray<>();
public static final int SUBJECT_DATA_LOADED = 0, SUBJECT_DOWNLOAD_COMPLETE = 1;
@Retention(SOURCE)
@IntDef({SUBJECT_DATA_LOADED, SUBJECT_DOWNLOAD_COMPLETE})
@interface Subject {
@PierceZ
PierceZ / Observable.java
Created May 14, 2017 13:30
Simple RxJava observable example to make a network request.
Observable.fromCallable(() -> {
Request request = new Request.Builder()
.url(url)
.build();
try {
Response response = sHttpClient.newCall(request).execute();
return response.isSuccessful();
} catch (IOException e) {
Log.e("Network request", "Failure", e);
}
@PierceZ
PierceZ / activity_constraint_percent.xml
Created August 24, 2017 13:58
A ConstraintLayout that uses a percentage width.
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.constraint.Guideline
android:id="@+id/guideline1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.25"/>
@PierceZ
PierceZ / EventLiveData.java
Created September 20, 2017 13:00
Another approach where EventLiveData contains a reference to its lifecycle.
public class EventLiveData extends LiveData<Object> {
private final int mSubject;
private final LifecycleRegistryOwner mLifecycle;
public EventLiveData(@LiveDataBus.Subject int subject, @NonNull LifecycleRegistryOwner lifecycle) {
mSubject = subject;
mLifecycle = lifecycle;
}
@PierceZ
PierceZ / EventLiveData.java
Last active April 17, 2018 08:12
A LiveData implementation for my event bus to send Objects.
public class EventLiveData extends LiveData<Object> {
private final int mSubject;
public EventLiveData(@LiveDataBus.Subject int subject) {
mSubject = subject;
}
public void update(Object object) {
postValue(object);
@PierceZ
PierceZ / LiveDataBusExample.java
Last active April 17, 2018 08:12
An example of how to use the LiveDataBus class.
//Example Activity
public class MyActivity extends BaseActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);
// with lambda
LiveDataBus.subscribe(LiveDataBus.SUBJECT_DATA_LOADED, this, (data) -> {
public class ZooDAO {
private static Box<Zoo> getZooBox() {
BoxStore boxStore = App.getBoxStore();
return boxStore.boxFor(Zoo.class);
}
public static DataSubscription subscribeToZooList(DataObserver<List<Zoo>> observer) {
return getZooBox().query().build().subscribe().on(AndroidScheduler.mainThread()).observer(observer);
}
@PierceZ
PierceZ / ParallelRxJavaUtil.java
Last active March 16, 2018 18:59
Util method to process a list of data in parallel.
/**
* Will process the data with the callable by splitting the data into the specified number of threads.
* <b>T</b> is ths type of data being parsed, and <b>U</b> is the type of data being returned.
*/
public static <T, U> Iterable<U> parseDataInParallel(@NonNull List<T> data, Function<List<T>, ObservableSource<U>> worker) {
int threadCount = Runtime.getRuntime().availableProcessors();
ExecutorService threadPoolExecutor = Executors.newFixedThreadPool(threadCount);
Scheduler scheduler = Schedulers.from(threadPoolExecutor);
AtomicInteger groupIndex = new AtomicInteger();
@PierceZ
PierceZ / RxBus.java
Last active December 6, 2017 15:47
An event bus made with RxJava and RxAndroid
/**
* Used for subscribing to and publishing to subjects. Allowing you to send data between activities, fragments, etc.
* <p>
* Created by Pierce Zaifman on 2017-01-02.
*/
public final class RxBus {
private static SparseArray<PublishSubject<Object>> sSubjectMap = new SparseArray<>();
private static Map<Object, CompositeDisposable> sSubscriptionsMap = new HashMap<>();