Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

GDG First - about this day

Slide 3

Slide 3 text

#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.

Slide 4

Slide 4 text

#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 :)

Slide 5

Slide 5 text

#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

Slide 6

Slide 6 text

#gdg_kharkiv_center Worldwide GDG Community

Slide 7

Slide 7 text

#gdg_kharkiv_center GDG in Ukraine

Slide 8

Slide 8 text

#gdg_kharkiv_center Upcoming Events - GDG DevFest Ukraine

Slide 9

Slide 9 text

#gdg_kharkiv_center Upcoming Events - Google I/O

Slide 10

Slide 10 text

#gdg_kharkiv_center Upcoming Events - Google Developer Day

Slide 11

Slide 11 text

Firebase Overview

Slide 12

Slide 12 text

#gdg_kharkiv_center History September 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

Slide 13

Slide 13 text

#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.

Slide 14

Slide 14 text

#gdg_kharkiv_center Overview

Slide 15

Slide 15 text

Project setup Click-click-boom

Slide 16

Slide 16 text

#gdg_kharkiv_center Project setup in console

Slide 17

Slide 17 text

#gdg_kharkiv_center Project setup in console

Slide 18

Slide 18 text

#gdg_kharkiv_center Project setup in console

Slide 19

Slide 19 text

#gdg_kharkiv_center Project setup in console

Slide 20

Slide 20 text

#gdg_kharkiv_center Project setup in console

Slide 21

Slide 21 text

#gdg_kharkiv_center Android Studio Assistant

Slide 22

Slide 22 text

#gdg_kharkiv_center Android Studio Assistant

Slide 23

Slide 23 text

#gdg_kharkiv_center Android Studio Assistant

Slide 24

Slide 24 text

#gdg_kharkiv_center Android Studio Assistant

Slide 25

Slide 25 text

Authentication Be together, not the same!

Slide 26

Slide 26 text

#gdg_kharkiv_center Authentication

Slide 27

Slide 27 text

#gdg_kharkiv_center Enable Authentication types

Slide 28

Slide 28 text

#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)

Slide 29

Slide 29 text

#gdg_kharkiv_center AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null); mAuth.signInWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener() { @Override public void onComplete(@NonNull Task 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)

Slide 30

Slide 30 text

#gdg_kharkiv_center FirebaseAuth.getInstance().signOut(); Sign out

Slide 31

Slide 31 text

#gdg_kharkiv_center Rules

Slide 32

Slide 32 text

Firebase Database, pt1 Basics

Slide 33

Slide 33 text

#gdg_kharkiv_center With FirebaseDatabase private DatabaseReference mDatabase; // ... mDatabase = FirebaseDatabase.getInstance().getReference(); Get Database reference

Slide 34

Slide 34 text

#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

Slide 35

Slide 35 text

#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

Slide 36

Slide 36 text

#gdg_kharkiv_center Written values visible in console

Slide 37

Slide 37 text

#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

Slide 38

Slide 38 text

#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

Slide 39

Slide 39 text

Firebase Database, pt2 How to make queries in NoSQL

Slide 40

Slide 40 text

#gdg_kharkiv_center It’s a JSON tree { "users": { "droid_ninja": { "name": "Android Ninja", "battery_status": { "74": true }, }, "raspberry_pike": { ... }, "fire_tamplin": { ... } } } Structure data

Slide 41

Slide 41 text

#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

Slide 42

Slide 42 text

#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

Slide 43

Slide 43 text

#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

Slide 44

Slide 44 text

#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

Slide 45

Slide 45 text

#gdg_kharkiv_center "groups": { "gdg": { "name": "GDG", "members": { "droid_ninja": true, "fire_tamplin": true, "raspberry_pike": true } }, ... } } Use an index

Slide 46

Slide 46 text

#gdg_kharkiv_center firebase.database.Query class

Slide 47

Slide 47 text

#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); Lists Just push()

Slide 48

Slide 48 text

#gdg_kharkiv_center Ordering

Slide 49

Slide 49 text

#gdg_kharkiv_center // My top posts by number of stars String 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

Slide 50

Slide 50 text

#gdg_kharkiv_center Filtering

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

#gdg_kharkiv_center // My top posts by number of stars myTopPostsQuery.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

Slide 53

Slide 53 text

#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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

#gdg_kharkiv_center Samples

Slide 56

Slide 56 text

#gdg_kharkiv_center And even more...

Slide 57

Slide 57 text

#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

Slide 58

Slide 58 text

Constantine Mars @DataArt @ConstantineMars +ConstantineMars Q&A Get ready for more...