Jump into cross platform development with firebase

58
Jump into cross-platform development with Firebase Dynamic Database, Authentication and much more... Constantine Mars Team Lead, Senior Developer @ DataArt Organizer @ GDG Dnipro-Art

Transcript of Jump into cross platform development with firebase

Page 1: Jump into cross platform development with firebase

Jump into cross-platform development with FirebaseDynamic Database, Authentication and much more...

Constantine MarsTeam Lead, Senior Developer @ DataArtOrganizer @ GDG Dnipro-Art

Page 2: Jump into cross platform development with firebase

GDGFirst - about this day

Page 3: Jump into cross platform development with firebase

#gdg_kharkiv_center

What is a GDG?

Google Developer Groups (GDGs) are for developers who are interested in Google's developer

technology; everything from the Android, App Engine, and Google Chrome platforms, to product APIs

like the Maps API, YouTube API and Google Calendar API.

A GDG can take many forms -- from just a few people getting together to watch our latest video, to

large gatherings with demos and tech talks, to events like code sprints and hackathons. However, at

the core, GDGs are focused on developers and technical content, and the core audience should be

developers.

Page 4: Jump into cross platform development with firebase

#gdg_kharkiv_center

A GDG is

● Run by passionate individuals in the developer community

● A place to learn about Google Technologies and Tools for developers.

● A place to see what local companies and developers are doing with these

technologies

● Focused on developers and educational technical content

● Open to the public with a public membership

● A place to meet cool and smart people in tech :)

Page 5: Jump into cross platform development with firebase

#gdg_kharkiv_center

A GDG is NOT

● Run by a corporation

● A place to hear a very salesy pitch at any time

● Focused on end users or consumer content

● A closed group

Page 6: Jump into cross platform development with firebase

#gdg_kharkiv_center

Worldwide GDG Community

Page 7: Jump into cross platform development with firebase

#gdg_kharkiv_center

GDG in Ukraine

Page 8: Jump into cross platform development with firebase

#gdg_kharkiv_center

Upcoming Events - GDG DevFest Ukraine

Page 9: Jump into cross platform development with firebase

#gdg_kharkiv_center

Upcoming Events - Google I/O

Page 10: Jump into cross platform development with firebase

#gdg_kharkiv_center

Upcoming Events - Google Developer Day

Page 11: Jump into cross platform development with firebase

FirebaseOverview

Page 12: Jump into cross platform development with firebase

#gdg_kharkiv_center

HistorySeptember 2011 - James Tamplin and Andrew Lee

founded Envolve, that provided developers an API

that let them integrate online chat into their

websites

April 2012 - Tamplin and Lee decided to separate

the chat system and the real-time architecture that

powered it, founding Firebase as a separate

company

21 October 2014 - Firebase announced it had been

acquired by Google

Page 13: Jump into cross platform development with firebase

#gdg_kharkiv_center

History

February 16, 2016 - David East announced Firecasts

video series for Firebase developers

May 18, 2016 - James Tamplin and Francis Ma

present Firebase Overview on Google I/O ‘16

January 18, 2017 - Google announced that it signed

an agreement to acquire Fabric and Crashlytics from

Twitter, and that those services would join the

Firebase team.

Page 14: Jump into cross platform development with firebase

#gdg_kharkiv_center

Overview

Page 15: Jump into cross platform development with firebase

Project setupClick-click-boom

Page 16: Jump into cross platform development with firebase

#gdg_kharkiv_center

Project setup in console

Page 17: Jump into cross platform development with firebase

#gdg_kharkiv_center

Project setup in console

Page 18: Jump into cross platform development with firebase

#gdg_kharkiv_center

Project setup in console

Page 19: Jump into cross platform development with firebase

#gdg_kharkiv_center

Project setup in console

Page 20: Jump into cross platform development with firebase

#gdg_kharkiv_center

Project setup in console

Page 21: Jump into cross platform development with firebase

#gdg_kharkiv_center

Android Studio Assistant

Page 22: Jump into cross platform development with firebase

#gdg_kharkiv_center

Android Studio Assistant

Page 23: Jump into cross platform development with firebase

#gdg_kharkiv_center

Android Studio Assistant

Page 24: Jump into cross platform development with firebase

#gdg_kharkiv_center

Android Studio Assistant

Page 25: Jump into cross platform development with firebase

AuthenticationBe together, not the same!

Page 26: Jump into cross platform development with firebase

#gdg_kharkiv_center

Authentication

Page 27: Jump into cross platform development with firebase

#gdg_kharkiv_center

Enable Authentication types

Page 28: Jump into cross platform development with firebase

#gdg_kharkiv_center

@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { …

if (requestCode == RC_SIGN_IN) { GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); if (result.isSuccess()) { // Google Sign In was successful, authenticate with Firebase GoogleSignInAccount account = result.getSignInAccount(); firebaseAuthWithGoogle(account); } else { // Google Sign In failed, update UI appropriately // ... } } }

Authentication (Google Sign In)

Page 29: Jump into cross platform development with firebase

#gdg_kharkiv_center

AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);mAuth.signInWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());

// If sign in fails, display a message to the user. If sign in succeeds // the auth state listener will be notified and logic to handle the // signed in user can be handled in the listener. if (!task.isSuccessful()) { Log.w(TAG, "signInWithCredential", task.getException()); Toast.makeText(GoogleSignInActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); } // ... } });

Authentication (Google Sign In)

Page 30: Jump into cross platform development with firebase

#gdg_kharkiv_center

FirebaseAuth.getInstance().signOut();

Sign out

Page 31: Jump into cross platform development with firebase

#gdg_kharkiv_center

Rules

Page 32: Jump into cross platform development with firebase

Firebase Database, pt1Basics

Page 33: Jump into cross platform development with firebase

#gdg_kharkiv_center

With FirebaseDatabase

private DatabaseReference mDatabase;

// ...

mDatabase = FirebaseDatabase.getInstance().getReference();

Get Database reference

Page 34: Jump into cross platform development with firebase

#gdg_kharkiv_center

public class BatteryStatus { public int level; public int status;

public BatteryStatus() { }

public BatteryStatus(int level, int status) { this.level = level; this.status = status; }}

Use POJOs or primitive types

Page 35: Jump into cross platform development with firebase

#gdg_kharkiv_center

With FirebaseDatabase

BatteryStatus batteryStatus = new

BatteryStatus(lastBatteryLevel, lastIsChargingStatus);

mDatabase.child("battery_status").child("current_status").setVal

ue(batteryStatus);

mDatabase.child("voting").setValue(lastVote);

Write values

Page 36: Jump into cross platform development with firebase

#gdg_kharkiv_center

Written values visible in console

Page 37: Jump into cross platform development with firebase

#gdg_kharkiv_center

mDatabase.child("battery_status").addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { BatteryStatus batteryStatus1 = dataSnapshot.child("current_status").getValue(BatteryStatus.class); lastBatteryLevel = batteryStatus1.level; }

@Override public void onCancelled(DatabaseError databaseError) {

} });

Read values = listen to changes

Page 38: Jump into cross platform development with firebase

#gdg_kharkiv_center

private void onPerformTransaction(DatabaseReference postRef) { postRef.runTransaction(new Transaction.Handler() { @Override public Transaction.Result doTransaction(MutableData mutableData) { Voting p = mutableData.getValue(Voting.class); if (p == null) { return Transaction.success(mutableData); }

p.setValue(true); // Set value and report transaction success mutableData.setValue(p); return Transaction.success(mutableData); }

@Override public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) { // Transaction completed } });}

Transactions

Page 39: Jump into cross platform development with firebase

Firebase Database, pt2How to make queries in NoSQL

Page 40: Jump into cross platform development with firebase

#gdg_kharkiv_center

It’s a JSON tree

{ "users": { "droid_ninja": { "name": "Android Ninja", "battery_status": { "74": true }, }, "raspberry_pike": { ... }, "fire_tamplin": { ... } }}

Structure data

Page 41: Jump into cross platform development with firebase

#gdg_kharkiv_center

{ // This is a poorly nested data architecture, because iterating the children // of the "chats" node to get a list of conversation titles requires // potentially downloading hundreds of megabytes of messages "chats": { "one": { "title": "Phone battery discussions", "messages": { "m1": { "sender": "droid_ninja", "message": "Looks like Doze Mode doesn’t help" }, "m2": { ... }, // a very long list of messages } }, "two": { ... } }}

Avoid nesting

Page 42: Jump into cross platform development with firebase

#gdg_kharkiv_center

// Conversation members are easily accessible // and stored by chat conversation ID "members": { // we'll talk about indices like this below "one": { "droid_ninja": true, "fire_tamplin": true, "raspbery_pike": true }, "two": { ... }, "three": { ... } },

Flatten data structures

Page 43: Jump into cross platform development with firebase

#gdg_kharkiv_center

{ // Chats contains only meta info about each conversation // stored under the chats's unique ID "chats": { "one": { "title": "Battery talks", "lastMessage": "ninja: Battery exhausting", "timestamp": 1459361875666 }, "two": { ... }, "three": { ... } },

...

Flatten data structures

Page 44: Jump into cross platform development with firebase

#gdg_kharkiv_center

// An index to track Ada's memberships{ "users": { "droid_ninja": { "name": "Droid Ninja", // Index Ada's groups in her profile "groups": { // the value here doesn't matter, just that the key exists "gdg": true, "geeks": true

"foreigners": false } }, ... },

Use an index

Page 45: Jump into cross platform development with firebase

#gdg_kharkiv_center

"groups": { "gdg": { "name": "GDG", "members": { "droid_ninja": true, "fire_tamplin": true, "raspberry_pike": true } }, ... }}

Use an index

Page 46: Jump into cross platform development with firebase

#gdg_kharkiv_center

firebase.database.Query class

Page 47: Jump into cross platform development with firebase

#gdg_kharkiv_center

ChildEventListener childEventListener = new ChildEventListener() { @Override public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {}

@Override public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {}

@Override public void onChildRemoved(DataSnapshot dataSnapshot) {}...};ref.addChildEventListener(childEventListener);

ListsJust push()

Page 48: Jump into cross platform development with firebase

#gdg_kharkiv_center

Ordering

Page 49: Jump into cross platform development with firebase

#gdg_kharkiv_center

// My top posts by number of starsString myUserId = getUid();Query myTopPostsQuery = databaseReference.child("user-posts").child(myUserId) .orderByChild("starCount");myTopPostsQuery.addChildEventListener(new ChildEventListener() { // TODO: implement the ChildEventListener methods as documented above // ...});

Ordering

Page 50: Jump into cross platform development with firebase

#gdg_kharkiv_center

Filtering

Page 51: Jump into cross platform development with firebase

#gdg_kharkiv_center

/ Last 100 posts, these are automatically the 100 most recent// due to sorting by push() keysQuery recentPostsQuery = databaseReference.child("posts") .limitToFirst(100);

Limit

Page 52: Jump into cross platform development with firebase

#gdg_kharkiv_center

// My top posts by number of starsmyTopPostsQuery.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) { // TODO: handle the post } }

@Override public void onCancelled(DatabaseError databaseError) {}});

Filtering example

Page 53: Jump into cross platform development with firebase

#gdg_kharkiv_center

Query biggest value of “weight”

ref

.orderByChild("weight")

.limitToLast(2)

.on("child_added", function(snapshot) {

console.log(snapshot.key());

});

More query examples

Page 54: Jump into cross platform development with firebase

And more...There are a lot of tools in the box

Page 55: Jump into cross platform development with firebase

#gdg_kharkiv_center

Samples

Page 56: Jump into cross platform development with firebase

#gdg_kharkiv_center

And even more...

Page 57: Jump into cross platform development with firebase

#gdg_kharkiv_center

Links

● Firebase home: https://firebase.google.com/

● Firebase docs: https://firebase.google.com/docs/

● Firebase blog: https://firebase.googleblog.com/

● Firecasts Youtube Channel: https://goo.gl/9giPHG

● Firecasts for iOS: https://goo.gl/eNioSp

● Firecasts for Android: https://goo.gl/M7UPTv

● Firebase for SQL Developers on Youtube:

https://youtu.be/WacqhiI-g_o?list=PLl-K7zZEsYLlP-k-RKFa7RyNPa9_wCH2s

Page 58: Jump into cross platform development with firebase

Constantine Mars@DataArt@ConstantineMars+ConstantineMars

Q&A

Get ready for more...