princessdharmy avatar

Triviums Mobile App (UPDATE: Show Users)

princessdharmy

Published: 03 Dec 2018 › Updated: 03 Dec 2018Triviums Mobile App (UPDATE: Show Users)

Triviums Mobile App (UPDATE: Show Users)

Repository

https://github.com/princessdharmy/triviums

History

Fixes

One of the major problems Android Developers encounter is App Crash with some Exceptions. In order to tackle this problem, here is my implementation:

  • Firstly, I added a NetworkBoundResource class FirestoreResultResponse.javato encapsulate both the data and state
public class FirestoreResultResponse {

    Map<String, Object> data;
    String error;

    public FirestoreResultResponse() {
    }

    public FirestoreResultResponse(Map<String, Object> data, String error) {
        this.data = data;
        this.error = error;
    }
}
  • Secondly, I had some issues while fetching data from Firestore; once there is a bad internet connection, the app crashes. So, in order to avoid the FirebaseFirestoreException, I used a try-catch in the StageRepository.java class :
private void fetchProgress(String categoryName, ProgressCallback callback) {
        ...
        docRef.get().addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
            }
            try {
                callback.onCallback(new FirestoreResultResponse(task.getResult().getData(), null));
            } catch (Exception e) {
                if (e instanceof FirebaseFirestoreException) {
                    callback.onCallback(new FirestoreResultResponse(null, e.getLocalizedMessage()));
                }
                e.printStackTrace();
            }
        })
                .addOnFailureListener(error -> {
                    callback.onCallback(new FirestoreResultResponse(null, error.toString()));
                });

    }

New Features

  • Show Users
    As stated in the roadmap of my previous post, it's somewhat interesting to know how many people are playing the game you are involved in. So, I decided to get and display all users that are using the app or playing the game.

Implementation

  • This code snippet fetches all the users in the database - Firestore in the LeaderboardRepository.java
private void fetchUsers(UserCallback callback){

        CollectionReference collection = firestore.collection("users");

        collection.get().addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
                document = task.getResult().getDocuments();
            }
            try {
                callback.onCallback(document);
            } catch (Exception e) {
                if (e instanceof FirebaseFirestoreException) {

                }
                e.printStackTrace();
            }
        })
                .addOnFailureListener(error -> {

                });
    }
  • The LeaderboardFragment.java class gets the user data and display
public void getUsers() {
        viewModel.getUsers().observe(getViewLifecycleOwner(), response -> {
            binding.loader.setVisibility(View.GONE);
            binding.peopleRecyclerview.setVisibility(View.VISIBLE);
            if (response != null) {
                adapter.updateData(response);
            }
        });

    }

Important resources

Roadmap

  • Leaderboard implementation:
    Display all users
    Add users to compete
    Remove users from competition
  • Display of users' quiz achievements

Leave Triviums Mobile App (UPDATE: Show Users) to:

Written by

Android Developer

Read more #utopian-io posts


Best Posts From princessdharmy

We have not curated any of princessdharmy's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From princessdharmy