Skip to content

Examples v1

Mk5 edited this page Feb 26, 2019 · 1 revision

Here you can find some examples of using GdxFireapp API.

Analytics

Log some button tap event

Map<String, String> params = new HashMap<String, String>();
params.put(AnalyticsParam.CONTENT_TYPE, "button");
params.put(AnalyticsParam.ITEM_ID, "my super button");
GdxFIRAnalytics.instance().logEvent(AnalyticsEvent.SELECT_CONTENT, params);

Auth

Sign-in anonymously

GdxFIRAuth.instance().signInAnonymously(new AuthCallback() {
  @Override
  public void onSuccess(GdxFirebaseUser user) {
    // Deal with with current user.
    String userDisplayName = user.getUserInfo().getDisplayName();
  }
 
  @Override
  public void onFail(Exception e) {
    // handle failure
  }
});

Sign-in via token

GdxFIRAuth.instance().signInWithToken("abcde", new AuthCallback() {
  @Override
  public void onSuccess(GdxFirebaseUser user) {
    // Deal with with current user.'
    String userDisplayName = user.getUserInfo().getDisplayName();
  }
 
  @Override
  public void onFail(Exception e) {
    // handle failure
  }
});

Sign-in with email and password

GdxFIRAuth.instance().signInWithEmailAndPassword("email", new char[] {'p','a','s','s','1','2','3'}, new AuthCallback() {
  @Override
  public void onSuccess(GdxFirebaseUser user) {
    
  }
 
  @Override
  public void onFail(Exception e) {
    // handle failure
  }
});

Create user with email and password

GdxFIRAuth.instance().createUserWithEmailAndPassword("email", new char[] {'p','a','s','s','1','2','3'}, new AuthCallback() {
  @Override
  public void onSuccess(GdxFirebaseUser user) {
    
  }
 
  @Override
  public void onFail(Exception e) {
    // handle failure
  }
});

Sign-in via Google

GdxFIRAuth.instance().google().signIn(new AuthCallback() {
  @Override
  public void onSuccess(GdxFirebaseUser user) {
    // Do something
  }

  @Override
  public void onFail(Exception e) {
  }
});

User menage

GdxFirebaseUser user = GdxFIRAuth.instance().getCurrentUser();

// Update the email
user.updateEmail("new-email@com", null);

// Send email verification
user.sendEmailVerification(null);

// Update the password
user.updatePassword(new char[] {'n', 'e', 'w', 'p', 'a', 's', 's'}, null);

// Delete 
user.delete(null);

// Reload 
user.reload(null);

// Send password reset email
GdxFIRAuth.instance().sendPasswordResetEmail("bob@com.pl", null);

Storage

Download byte data:

GdxFIRStorage.instance().download("/file.data", Long.MAX_VALUE, new DownloadCallback<byte[]>() {
  @Override
  public void onSuccess(byte[] result) {
    // Process just downloaded byte[] data.
  }

  @Override
  public void onFail(Exception e) {
    e.printStackTrace();
  }
});

Download texture:

GdxFIRStorage.instance().downloadImage("/img.png", new DownloadCallback<TextureRegion>() {
    @Override
    public void onSuccess(TextureRegion region) {
        // Do something with region.
        // ....
        // Remember to dispose texture when you done! (now or later)
        region.getTexture().dispose();
    }

     @Override
     public void onFail(Exception e) {
       e.printStackTrace();
     }
});

If your storage requires authorization do not forget about it, more info here.

Realtime Database

Sample database usage, with following POJO class

public class User{
  public String username;
  public String email;
  public User(){}
  public User(String username, String email){
    this.username = username;
    this.email = email;
  }
}

Put into database:

GdxFIRDatabase.instance().inReference("users)
.push()
.setValue(new User("Bob", "bob@bob.com"));

Listen for data change:

GdxFIRDatabase.instance().inReference("users/"+userId)
.onDataChange(User.class, new DataChangeListener<User>() {
  
    @MapConversion(User.class)
    @Override
    public void onChange(User user) {
    }
            
    @Override
    public void onCanceled(Exception e) {
    
    }
});

Read a list:

GdxFIRDatabase.instance().inReference("users")
.filter(FilterType.LIMIT_FIRST, 5)
.readValue(List.class, new DataCallback<List<User>>(){
  
  @MapConversion(User.class)
  @Override
  public void onData(List<User> list) {
    // Do something with list..
  }
  
  @Override
  public void onError(Exception e) {
    // Handle failure
  }
});

When you want to fetch POJO from database you should use @MapConversion annotation to tell the API what type do you expect instead of Map, you can read more about it here: Database POJO Conversion wiki.

Crash raporting

Any custom errors or logs you can report as follow:

GdxFIRCrash.instance().log("i'm custom log.")
Clone this wiki locally