Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Firebase & Jetpack: fit like a glove (360|AnDev...

Firebase & Jetpack: fit like a glove (360|AnDev, Denver)

Doug Stevenson

July 19, 2018
Tweet

More Decks by Doug Stevenson

Other Decks in Technology

Transcript

  1. Firebase & Jetpack: fit like a glove Doug Stevenson @CodingDoug

    Get the code: Get the app: bit.ly/2NtAuQP bit.ly/2NtGedo
  2. @CodingDoug A simple example … or is it? val firestore

    = FirebaseFirestore.getInstance() val ref = firestore.collection("coll").document("id") ref.get() .addOnSuccessListener { snapshot -> // We have data! Now what? } .addOnFailureListener { exception -> // Oh, snap } How should I do async programming? Do I directly update my views here? What manages this singleton?
  3. @CodingDoug Handle stock price changes from a document in Firestore

    val firestore = FirebaseFirestore.getInstance()
  4. @CodingDoug Handle stock price changes from a document in Firestore

    val firestore = FirebaseFirestore.getInstance() val ref = firestore.collection("stocks-live").document("HSTK")
  5. @CodingDoug Handle stock price changes from a document in Firestore

    val firestore = FirebaseFirestore.getInstance() val ref = firestore.collection("stocks-live").document("HSTK") ref.addSnapshotListener { snapshot, exception -> if (snapshot != null) { } else if (exception != null) { } }
  6. @CodingDoug Handle stock price changes from a document in Firestore

    val firestore = FirebaseFirestore.getInstance() val ref = firestore.collection("stocks-live").document("HSTK") ref.addSnapshotListener { snapshot, exception -> if (snapshot != null) { val model = StockPrice( ticker = snapshot.id, price = snapshot.getDouble("price")!!.toFloat() ) } else if (exception != null) { } }
  7. @CodingDoug Handle stock price changes from a document in Firestore

    val firestore = FirebaseFirestore.getInstance() val ref = firestore.collection("stocks-live").document("HSTK") ref.addSnapshotListener { snapshot, exception -> if (snapshot != null) { val model = StockPrice( ticker = snapshot.id, price = snapshot.getDouble("price")!!.toFloat() ) } else if (exception != null) { TODO("This is just a simple code sample, ain't got time for this.") } }
  8. @CodingDoug Handle stock price changes from a document in Firestore

    val firestore = FirebaseFirestore.getInstance() val ref = firestore.collection("stocks-live").document("HSTK") ref.addSnapshotListener { snapshot, exception -> if (snapshot != null) { val model = StockPrice( ticker = snapshot.id, price = snapshot.getDouble("price")!!.toFloat() ) someTextView.text = model.ticker } else if (exception != null) { TODO("This is just a simple code sample, ain't got time for this.") } }
  9. @CodingDoug Handle stock price changes from a document in Firestore

    val firestore = FirebaseFirestore.getInstance() val ref = firestore.collection("stocks-live").document("HSTK") ref.addSnapshotListener { snapshot, exception -> if (snapshot != null) { val model = StockPrice( ticker = snapshot.id, price = snapshot.getDouble("price")!!.toFloat() ) someTextView.text = model.ticker throw PoorArchitectureException("let's rethink mixing data store and views”) } else if (exception != null) { TODO("This is just a simple code sample, ain't got time for this.") } }
  10. @CodingDoug @CodingDoug LiveData Observable Code can register observers to receive

    updates. Genericized Subclasses must specify a type of object containing updates. Lifecycle-aware Handles component start, stop, and destroy states automatically.
  11. @CodingDoug Keep views up to date with LiveData class MyActivity

    : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.stuff) val tickerTextView = findViewById<TextView>(R.id.tv_ticker) val priceTextView = findViewById<TextView>(R.id.tv_price) } }
  12. @CodingDoug Keep views up to date with LiveData class MyActivity

    : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.stuff) val tickerTextView = findViewById<TextView>(R.id.tv_ticker) val priceTextView = findViewById<TextView>(R.id.tv_price) val liveData: LiveData<StockPrice> = getLiveDataForMyTicker("HSTK") } }
  13. @CodingDoug Keep views up to date with LiveData class MyActivity

    : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.stuff) val tickerTextView = findViewById<TextView>(R.id.tv_ticker) val priceTextView = findViewById<TextView>(R.id.tv_price) val liveData: LiveData<StockPrice> = getLiveDataForMyTicker("HSTK") liveData.observe(this@MyActivity, Observer<StockPrice> { stockPrice -> }) } }
  14. @CodingDoug Keep views up to date with LiveData class MyActivity

    : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.stuff) val tickerTextView = findViewById<TextView>(R.id.tv_ticker) val priceTextView = findViewById<TextView>(R.id.tv_price) val liveData: LiveData<StockPrice> = getLiveDataForMyTicker("HSTK") liveData.observe(this@MyActivity, Observer<StockPrice> { stockPrice -> if (stockPrice != null) { tickerTextView.text = stockPrice.ticker priceTextView.text = stockPrice.price.toString() } }) } }
  15. @CodingDoug LiveData is aware of the Android Activity lifecycle override

    fun onStop() { super.onStop() // LivaData becomes inactive, doesn't notify observers }
  16. @CodingDoug LiveData is aware of the Android Activity lifecycle override

    fun onStop() { super.onStop() // LivaData becomes inactive, doesn't notify observers } override fun onStart() { super.onStart() // LivaData becomes active again, notifies observers with latest data }
  17. @CodingDoug LiveData is aware of the Android Activity lifecycle override

    fun onStop() { super.onStop() // LivaData becomes inactive, doesn't notify observers } override fun onStart() { super.onStart() // LivaData becomes active again, notifies observers with latest data } override fun onDestroy() { super.onDestroy() // LiveData removes all activity-scoped observers - no leaks! }
  18. @CodingDoug Implement LiveData with Firestore document updates class StockPriceLiveData(private val

    documentReference: DocumentReference) : LiveData<StockPrice>() { }
  19. @CodingDoug Implement LiveData with Firestore document updates class StockPriceLiveData(private val

    documentReference: DocumentReference) : LiveData<StockPrice>() { override fun onActive() { // # observers 0 -> 1 woo-hoo! someone cares! } }
  20. @CodingDoug Implement LiveData with Firestore document updates class StockPriceLiveData(private val

    documentReference: DocumentReference) : LiveData<StockPrice>() { private var listenerRegistration: ListenerRegistration? = null override fun onActive() { // # observers 0 -> 1 woo-hoo! someone cares! listenerRegistration = documentReference.addSnapshotListener(....) } }
  21. @CodingDoug Implement LiveData with Firestore document updates class StockPriceLiveData(private val

    documentReference: DocumentReference) : LiveData<StockPrice>() { private var listenerRegistration: ListenerRegistration? = null override fun onActive() { // # observers 0 -> 1 woo-hoo! someone cares! listenerRegistration = documentReference.addSnapshotListener(....) } override fun onInactive() { // # observers 1 -> 0 awww, everyone left... } }
  22. @CodingDoug Implement LiveData with Firestore document updates class StockPriceLiveData(private val

    documentReference: DocumentReference) : LiveData<StockPrice>() { private var listenerRegistration: ListenerRegistration? = null override fun onActive() { // # observers 0 -> 1 woo-hoo! someone cares! listenerRegistration = documentReference.addSnapshotListener(....) } override fun onInactive() { // # observers 1 -> 0 awww, everyone left... listenerRegistration!!.remove() } }
  23. @CodingDoug Implement LiveData with Firestore document updates class StockPriceLiveData(private val

    documentReference: DocumentReference) : LiveData<StockPrice>(), EventListener<DocumentSnapshot> { private var listenerRegistration: ListenerRegistration? = null override fun onActive() { // # observers 0 -> 1 woo-hoo! someone cares! listenerRegistration = documentReference.addSnapshotListener(this) } override fun onInactive() { // # observers 1 -> 0 awww, everyone left... listenerRegistration!!.remove() } override fun onEvent(snap: DocumentSnapshot?, e: FirebaseFirestoreException?) {} }
  24. @CodingDoug Implement LiveData with Firestore document updates override fun onEvent(snap:

    DocumentSnapshot?, e: FirebaseFirestoreException?) { if (snap != null && snap.exists()) { val model = StockPrice( snap.id, snap.getDouble("price")!!.toFloat() ) } }
  25. @CodingDoug Implement LiveData with Firestore document updates override fun onEvent(snap:

    DocumentSnapshot?, e: FirebaseFirestoreException?) { if (snap != null && snap.exists()) { val model = StockPrice( snap.id, snap.getDouble("price")!!.toFloat() ) // Here you go, all my admiring observers! Go update your UI! setValue(model) } }
  26. @CodingDoug Implement LiveData with Firestore document updates override fun onEvent(snap:

    DocumentSnapshot?, e: FirebaseFirestoreException?) { if (snap != null && snap.exists()) { val model = StockPrice( snap.id, snap.getDouble("price")!!.toFloat() ) // Here you go, all my admiring observers! Go update your UI! setValue(model) } else if (e != null) { TODO("You should handle errors. Do as I say, not as I do.") } }
  27. @CodingDoug • Who creates the instance of LiveData? ◦ StockPriceLiveData

    constructor exposes Firestore implementation details. ◦ Remember: views should know nothing of data store. • LiveData loses its data on configuration change. ◦ Would rather have immediate LiveData results after configuration change. Two problems to resolve
  28. @CodingDoug @CodingDoug ViewModel Survives configuration changes Same ViewModel instance appears

    in reconfigured activities Shared & Managed System manages the scope of instances, may be shared Lifecycle-aware Automatically cleaned up on final destroy
  29. @CodingDoug Implement a ViewModel that yields a LiveData class StocksViewModel

    : ViewModel() { fun getStockPriceLiveData(ticker: String): StockPriceLiveData { } }
  30. @CodingDoug Implement a ViewModel that yields a LiveData class StocksViewModel

    : ViewModel() { // Find a repository object using dependency injection private val repository: StockDataRepository = ... fun getStockPriceLiveData(ticker: String): StockPriceLiveData { } }
  31. @CodingDoug Implement a ViewModel that yields a LiveData class StocksViewModel

    : ViewModel() { // Find a repository object using dependency injection private val repository: StockDataRepository = ... fun getStockPriceLiveData(ticker: String): StockPriceLiveData { } } // StockDataRepository knows where the data actually comes from interface StockDataRepository { fun getStockPriceLiveData(ticker: String): StockPriceLiveData }
  32. @CodingDoug Implement a ViewModel that yields a LiveData class StocksViewModel

    : ViewModel() { // Find a repository object using dependency injection private val repository: StockDataRepository = ... fun getStockPriceLiveData(ticker: String): StockPriceLiveData { return repository.getStockPriceLiveData(ticker) } } // StockDataRepository knows where the data actually comes from interface StockDataRepository { fun getStockPriceLiveData(ticker: String): StockPriceLiveData }
  33. @CodingDoug Implement a repository backed by Firestore interface StockDataRepository {

    fun getStockPriceLiveData(ticker: String): StockPriceLiveData }
  34. @CodingDoug Implement a repository backed by Firestore interface StockDataRepository {

    fun getStockPriceLiveData(ticker: String): StockPriceLiveData } class FirestoreStockDataRepository : StockDataRepository { override fun getStockPriceLiveData(ticker: String): StockPriceLiveData { } }
  35. @CodingDoug Implement a repository backed by Firestore interface StockDataRepository {

    fun getStockPriceLiveData(ticker: String): StockPriceLiveData } class FirestoreStockDataRepository : StockDataRepository { // Should use DI for this too private val firestore = FirebaseFirestore.getInstance() override fun getStockPriceLiveData(ticker: String): StockPriceLiveData { } }
  36. @CodingDoug Implement a repository backed by Firestore interface StockDataRepository {

    fun getStockPriceLiveData(ticker: String): StockPriceLiveData } class FirestoreStockDataRepository : StockDataRepository { // Should use DI for this too private val firestore = FirebaseFirestore.getInstance() override fun getStockPriceLiveData(ticker: String): StockPriceLiveData { val ref = firestore.collection("stocks-live").document(ticker) return StockPriceLiveData(ref) } }
  37. @CodingDoug Use ViewModel and LiveData in an activity class MyActivity

    : AppCompatActivity() { private lateinit var tickerTextView: TextView private lateinit var priceTextView: TextView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Redacted: inflate and initialize views... } }
  38. @CodingDoug Use ViewModel and LiveData in an activity class MyActivity

    : AppCompatActivity() { private lateinit var tickerTextView: TextView private lateinit var priceTextView: TextView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Inflate and initialize views... val viewModel = ViewModelProviders.of(this).get(StocksViewModel::class.java) } }
  39. @CodingDoug Use ViewModel and LiveData in an activity class MyActivity

    : AppCompatActivity() { private lateinit var tickerTextView: TextView private lateinit var priceTextView: TextView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Inflate and initialize views... val viewModel = ViewModelProviders.of(this).get(StocksViewModel::class.java) val liveData = viewModel.getStockPriceLiveData("HSTK") } }
  40. @CodingDoug Use ViewModel and LiveData in an activity class MyActivity

    : AppCompatActivity() { private lateinit var tickerTextView: TextView private lateinit var priceTextView: TextView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Inflate and initialize views... val viewModel = ViewModelProviders.of(this).get(StocksViewModel::class.java) val liveData = viewModel.getStockPriceLiveData("HSTK") liveData.observe(this, Observer<StockPrice> { stockPrice -> }) } }
  41. @CodingDoug Use ViewModel and LiveData in an activity class MyActivity

    : AppCompatActivity() { private lateinit var tickerTextView: TextView private lateinit var priceTextView: TextView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Inflate and initialize views... val viewModel = ViewModelProviders.of(this).get(StocksViewModel::class.java) val liveData = viewModel.getStockPriceLiveData("HSTK") liveData.observe(this, Observer<StockPrice> { stockPrice -> if (stockPrice != null) { tickerTextView.text = stockPrice.ticker priceTextView.text = stockPrice.price.toString() } }) } }
  42. @CodingDoug • LiveData<StockPrice> doesn’t propagate errors • Errors need to

    bubble up to the UI 
 class DataOrException<T, E: Exception?>(val data: T?, val exception: E?) typealias StockPriceOrException = DataOrException<StockPrice, Exception> LiveData<StockPrice> => LiveData<StockPriceOrException> What about errors?
  43. @CodingDoug • https://github.com/CodingDoug/firebase-jetpack • Sync individual documents/nodes, and queries ◦

    Query → RecyclerView support is inefficient and still needs work • Implementations for both Realtime Database and Firestore • Background sync with WorkManager • Backend implemented with TypeScript for node and Cloud Functions • Stay tuned for more! Lots more in the repo