Skip to content

Instantly share code, notes, and snippets.

View PierceZ's full-sized avatar

Pierce Zaifman PierceZ

  • Canada
View GitHub Profile
@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 / LiveDataBus.java
Created September 20, 2017 12:59
Another approach to a LiveData event bus. Where each LiveData instance is tied to a lifecycle.
public final class LiveDataBus {
private static SparseArray<Map<LifecycleRegistryOwner, 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 / RelationUsageExample.java
Last active September 5, 2017 06:07
An example of how to use relations in ObjectBox.
Zoo myZoo = new Zoo();
Animal elephant = new Animal();
Animal giraffe = new Animal();
// To-one relation: Set the Zoo that an animal belongs to and save it to the database
elephant.zoo.setTarget(myZoo);
animalBox.put(elephant);
// To-one relation: Get the Zoo that an animal belongs to
@PierceZ
PierceZ / RelationExample.java
Created September 3, 2017 14:45
A one-to-many example for ObjectBox.
@Entity
public class Zoo {
@Id
private long id;
// a Zoo can have many Animals
@Backlink
ToMany<Animal> animals;
@PierceZ
PierceZ / ObjectBoxExamples.java
Created September 2, 2017 14:53
Some example usage of ObjectBox.
BoxStore boxStore = App.getApp().getBoxStore();
Box<Animal> animalBox = boxStore.boxFor(Animal.class);
// loads all animals
List<Animal> animals = animalBox.getAll();
// find a specific animal in the database
long myDogId = 12;
Animal myDog = animalBox.get(myDogId);
@PierceZ
PierceZ / App.java
Created September 2, 2017 14:22
An example Application class to be used with ObjectBox.
public class App extends Application {
private static App sApp;
private BoxStore mBoxStore;
@Override
public void onCreate() {
super.onCreate();
sApp = this;
mBoxStore = MyObjectBox.builder().androidContext(App.this).build();
@PierceZ
PierceZ / ExampleModel.java
Created September 2, 2017 14:13
An example data model to be used for ObjectBox.
@Entity
public class Animal {
@Id(assignable = true)
private long id;
private String name;
private boolean flying;
@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 / activity_constraint.xml
Created August 24, 2017 13:36
An example ConstraintLayout
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
android:background="@android:color/holo_blue_bright"
@PierceZ
PierceZ / activity_relative.xml
Created August 24, 2017 13:20
An example to compare RelativeLayout to ConstraintLayout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@android:color/holo_blue_bright"