Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

@jeremybbonze Android Engineer at WorkWell @xgouchet Lead Android Engineer at WorkWell

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

“Do not treat Offline as an error condition. Offline is the default state, Online is icing on the cake.”

Slide 12

Slide 12 text

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.

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

Slide 22

Slide 22 text

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Slide 28

Slide 28 text

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

data class Item ( // … val lastModified: Long, val timeToLive: Long )

Slide 31

Slide 31 text

interface ItemRemoteSource { fun get(id: String): Single fun getAll(): Single> } interface ItemRemoteSink { fun create(item: Item): Single fun update(item: Item): Single fun delete(id: String): Single }

Slide 32

Slide 32 text

interface ItemLocalSource { fun get(id: String): Single> fun getAll(): Single>> } interface ItemLocalSink { fun create(item: Item): Single fun update(item: Item): Single fun delete(id: String): Single }

Slide 33

Slide 33 text

sealed class LocalResult { abstract fun get(): T class Present(private val value: T): LocalResult() { override fun get(): T = value } class Obsolete(private val value: T): LocalResult() { override fun get(): T = value } class NotFound: LocalResult() { override fun get(): T = throw Resources.NotFoundException("No value") } }

Slide 34

Slide 34 text

interface ItemGlobalSource { fun get(id: String): Single fun getAll(): Single> } interface ItemGlobalSink { fun create(item: Item): Single fun update(item: Item): Single fun delete(id: String): Single }

Slide 35

Slide 35 text

fun Observable>.flatMapOfflineFirst( fallback: RemoteSource.() -> Observable, update: LocalSink.(O) -> Observable ): Observable { return flatMap { result -> when (result) { // … } } }

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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()) } } // … } }

Slide 38

Slide 38 text

return flatMap { result -> when (result) { // … is LocalResult.NotFound -> { if (networkWatcher.isNetworkAvailable()) { remoteSource.fallback() .flatMap { localSink.update(it) } } else { Observable.empty() } } } }

Slide 39

Slide 39 text

fun List>.keepPresentOnly() : LocalResult> { return LocalResult.Present( filter { it is LocalResult.Present } .map { it.get() } ) }

Slide 40

Slide 40 text

fun LocalResult>.notFountIfEmpty() : LocalResult> { return if (this is LocalResult.NotFound || get().isNotEmpty()) { This } else { LocalResult.NotFound() } }

Slide 41

Slide 41 text

fun List>.allPresentOrNone() : LocalResult> { 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() } }) } }

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

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() } } }

Slide 46

Slide 46 text

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 }

Slide 47

Slide 47 text

● Same pagination logic on Remote and Local source

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

● Balance between disk usage / available data ● Only deals with part of the problem

Slide 50

Slide 50 text

● They do actually mean something ● 404 (NOT FOUND) or 410 (GONE) → delete locally

Slide 51

Slide 51 text

● Request (once in a while) recently deleted items ● Use a Hash / Timestamp to only get the relevant changeset

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

● The main solution to handle offline actions is to schedule jobs

Slide 54

Slide 54 text

● JobScheduler ● Android-Job (Evernote) ● JobDispatcher (Firebase) [Deprecated] ● WorkManager

Slide 55

Slide 55 text

● Set syncStatus in local DB ● Launch sync job as an internet connection is back ● Once remote and local DB are synced, update syncStatus

Slide 56

Slide 56 text

data class Item ( // … val lastModified: Long, val timeToLive: Long, val syncStatus: SyncStatus ) enum class SyncStatus { TO_BE_SYNCED, SYNCED }

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

“Do not treat Offline as an error condition. Offline is the default state, Online is icing on the cake.”

Slide 62

Slide 62 text

● Measure your network usage ● Test your own application with lousy network conditions ● Test your own application in airplane mode ● Use your own application

Slide 63

Slide 63 text

● Offline First Slack Community ○ http://offlinefirst.org/chat/ ● Offline resources and articles ○ https://medium.com/offline-camp/ ○ http://offlinefirst.org/

Slide 64

Slide 64 text

No content