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

Make your app work perfectly off— Connection In...

Make your app work perfectly off— Connection Interrupted

An introduction to offline first development on Android, given with Jérémy Bartolomeu Bonze

Xavier Gouchet

April 24, 2019
Tweet

More Decks by Xavier Gouchet

Other Decks in Programming

Transcript

  1. “Do not treat Offline as an error condition. Offline is

    the default state, Online is icing on the cake.”
  2. accessibility /əksɛsɪˈbɪlɪti/ n. • the quality of being able to

    be reached or entered. • the quality of being easy to obtain or use.
  3. interface ItemRemoteSource { fun get(id: String): Single<Item> fun getAll(): Single<List<Item>>

    } interface ItemRemoteSink { fun create(item: Item): Single<Item> fun update(item: Item): Single<Item> fun delete(id: String): Single<Boolean> }
  4. interface ItemLocalSource { fun get(id: String): Single<LocalResult<Item>> fun getAll(): Single<List<LocalResult<Item>>>

    } interface ItemLocalSink { fun create(item: Item): Single<Item> fun update(item: Item): Single<Item> fun delete(id: String): Single<Boolean> }
  5. sealed class LocalResult<T> { abstract fun get(): T class Present<T>(private

    val value: T): LocalResult<T>() { override fun get(): T = value } class Obsolete<T>(private val value: T): LocalResult<T>() { override fun get(): T = value } class NotFound<T>: LocalResult<T>() { override fun get(): T = throw Resources.NotFoundException("No value") } }
  6. interface ItemGlobalSource { fun get(id: String): Single<Item> fun getAll(): Single<List<Item>>

    } interface ItemGlobalSink { fun create(item: Item): Single<Item> fun update(item: Item): Single<Item> fun delete(id: String): Single<Boolean> }
  7. fun <O> Observable<LocalResult<O>>.flatMapOfflineFirst( fallback: RemoteSource.() -> Observable<O>, update: LocalSink.(O) ->

    Observable<O> ): Observable<O> { return flatMap { result -> when (result) { // … } } }
  8. return flatMap { result -> when (result) { is LocalResult.Present

    -> Observable.just(result.get()) // … } }
  9. return flatMap { result -> when (result) { is LocalResult.Obsolete

    -> { if (networkWatcher.isNetworkAvailable()) { remoteSource.fallback() .flatMap { localSink.update(it) } .startWith(result.get()) } else { Observable.just(result.get()) } } // … } }
  10. return flatMap { result -> when (result) { // …

    is LocalResult.NotFound -> { if (networkWatcher.isNetworkAvailable()) { remoteSource.fallback() .flatMap { localSink.update(it) } } else { Observable.empty() } } } }
  11. fun <T : Any> LocalResult<List<T>>.notFountIfEmpty() : LocalResult<List<T>> { return if

    (this is LocalResult.NotFound || get().isNotEmpty()) { This } else { LocalResult.NotFound() } }
  12. fun <T : Any> List<LocalResult<T>>.allPresentOrNone() : LocalResult<List<T>> { return if

    (isNotEmpty() && all { it is LocalResult.Present }) { LocalResult.Present(map { it.get() }) } else { LocalResult.Obsolete (mapNotNull { if (it is LocalResult.NotFound) { null } else { it.get() } }) } }
  13. abstract class PaginationScrollListener( private val srLayout: SwipeRefreshLayout, private val onLoadNextPage:

    () -> Unit ) : RecyclerView.OnScrollListener() { override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { super.onScrolled(recyclerView, dx, dy) if (isLastItemVisible() && !isFirstItemVisible() && !srLayout.isRefreshing) { onLoadNextPage() } } }
  14. override fun loadNextPage(context: ContextC, currentListSize: Int) { val pageToLoad =

    computePageToLoad(currentListSize) if (pageToLoad >= 0) { loadPage(context, pageToLoad, false) } } private fun computePageToLoad(currentListSize: Int): Int { val remainder = currentListSize % PAGE_SIZE val pageCount = (currentListSize - remainder) / PAGE_SIZE return if (remainder > 0) -1 else pageCount + 1 }
  15. • Request (once in a while) recently deleted items •

    Use a Hash / Timestamp to only get the relevant changeset
  16. • Set syncStatus in local DB • Launch sync job

    as an internet connection is back • Once remote and local DB are synced, update syncStatus
  17. data class Item ( // … val lastModified: Long, val

    timeToLive: Long, val syncStatus: SyncStatus ) enum class SyncStatus { TO_BE_SYNCED, SYNCED }
  18. “Do not treat Offline as an error condition. Offline is

    the default state, Online is icing on the cake.”
  19. • Measure your network usage • Test your own application

    with lousy network conditions • Test your own application in airplane mode • Use your own application
  20. • Offline First Slack Community ◦ http://offlinefirst.org/chat/ • Offline resources

    and articles ◦ https://medium.com/offline-camp/ ◦ http://offlinefirst.org/