Skip to content

Repository API

TaylorChristie edited this page Oct 27, 2019 · 1 revision

I figured I'd write up a simple API so you guys have reference on how the repository works. At first it may seem complicated, but I assure you it's the easiest way to deal with firestore.

Repository Class

To build the repository class, you simply have to call:

MoodRepository mood = new MoodRepository();

After building the class you have a couple public methods you can use. Below is the methodology on using them all

Querying/Get

Querying and get requests are probably the biggest use case for most of this application. By default, you can run:

mood.get().addOnSuccessListener(new OnSuccessListener<List<ModelInterface>>() {
            @Override
            public void onSuccess(List<ModelInterface> modelInterfaces) {
                for (ModelInterface m : modelInterfaces) {
                    MoodModel s = (MoodModel) m;
                    Log.d("RESULT/GET", s.getInternalId() + s.getName());
                }
            }
        });

To get a list of all your moods, note that you have to type-cast to the MoodModel otherwise you will not get all the model classes exposed to you. The internal Id is used by firestore to keep track of the document so we can update/delete the document easily. Note that when entities are updated, the query will update as well and the data will change.

.get() can be augmented with the .where() and .limit() methods to fine-tune your returned values. Let's say I want to get moodEvent's of a specific user of a certain mood.

moodEvents.where('username', "taylor").where('moodName', "happy").get().addOnSuccessListener(new OnSuccessListener<List<ModelInterface>>() {
            @Override
            public void onSuccess(List<ModelInterface> modelInterfaces) {
                for (ModelInterface m : modelInterfaces) {
                    MoodEventModel s = (MoodEventModel) m;
                    Log.d("RESULT/GET", s.getInternalId());
                }
            }
        });

All queries need to be wrapped in this listener, because firestore is asynchronous and we can't run it on the main thread - meaning that returning/updating data are not static calls to the repository class, rather a Task is returned, and you listen for when a task is completed. This concept might be difficult to get used to, but once you understand how it works, it's not difficult to work with.

Creating

Creating new entities in firestore is extremely simple. All you have to do is create a model with the values you need, then call the .create() method to add it into firestore.

MoodModel model = new MoodModel();
model.setColor("red");
model.setEmoji(":angry:");
model.setName("Angry");

mood.create(model).addOnSuccessListener(new OnSuccessListener<ModelInterface>() {
            @Override
            public void onSuccess(ModelInterface modelInterface) {
                final MoodModel m = (MoodModel) modelInterface;
                Log.d("RESULT/CREATE", m.getInternalId());
            }
        });

This will create the model, and add the internalId on the returned value with your fields, so you know it was created.

Deleting

Deleting is pretty much the same as adding on a usability basis.

                mood.delete(model).addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Log.d("RESULT/DELETE", "Model Deleted");
                    }
                });

This will return no data because it's being deleted, but the listener will only fire successfully if it is actually deleted from firestore.

Updating

You can also update an existing model, as long as it was previously queried from firestore at some point (ie: it has an internalId attached to it).

mood.where('name', 'Happy').one().addOnCompleteListener(new OnCompleteListener<ModelInterface>() {
            @Override
            public void onComplete(ModelInterface modelInterface) {
                MoodModel m = (MoodModel) modelInterface;
                Log.d("RESULT/ONE", m.getInternalId() + m.getColor());
                m.setColor("green");
                mood.update(m).addOnCompleteListener(new OnCompleteListener<ModelInterface>() {
                    @Override
                    public void onComplete(ModelInterface modelInterface) {
                        MoodModel s = (MoodModel) modelInterface;
                        Log.d("RESULT/ONE", s.getInternalId() + s.getColor());
                    }
                }
                

            }
        });

Note we are nesting this get query, which isn't necessarily a good thing when using onSuccessListener (since it fires every-time an object is updated when using a query/get. In this case, a onCompleteListener is a better idea since it should only fire once and not cause a loop.

Clone this wiki locally