SlideShare una empresa de Scribd logo
1 de 44
Descargar para leer sin conexión
HOW DO WE WORK
OFFLINE
JUAN CARLOS GARCÍA SECO
▸ Android Developer
▸ jcgseco@gmail.com
▸ @ikarimeister
USE CASE
ACCESS
CONTROL
TICKET SALES
ON BOARD
WHY OFFLINE?
TUNNELS
REMOTE PLACES
GOOD SIGNAL
HIGH SPEED
NO CONNECTION
GOING OFFLINE
BACKEND AS SOURCE
OF TRUTH
PERSISTING OFFLINE
REQUESTS
sealed class NetworkError : Errors()
data class NoInternetConnection(
val request: String,
val type: OfflineRequestType
) : Errors()
object AlreadyCreated : NetworkError()
object BadRequest : NetworkError()
object Forbidden : NetworkError()
suspend fun createSell(sale : SaleRequestDto)
: Either<NetworkError, SaleResponseDto>
apiClient.createSell(sale)
.mapLeft { it.mapToDomainError() }
fun NetworkError.mapToDomainError(): DomainError =
when (this) {
is NoInternetConnection -> {
repository?.save(this.request,
this.type)
DomainError
}
AlreadyCreated -> DomainError
BadRequest -> DomainError
Forbidden -> DomainError
}
INITIAL DATA LOAD
WE NEED SOME DATA
TO WORK
BE AWARE IF YOU NEED THE
DATA FOR YOUR FEATURE
DON’T BURN THE
BACKEND
SYNCHRONIZING
ON DEMAND SYNC
PERIODIC SYNC
fun initSync() {
val myIntent = Intent(this,
SyncService::class.java)
val pendingIntent =
PendingIntent.getService(this, 0, myIntent, 0)
getSystemService(Context.ALARM_SERVICE)
.setRepeating(
AlarmManager.RTC_WAKEUP, 0,
frequency, pendingIntent)
}
class SyncService : IntentService(SyncService::class.java.name) {
lateinit var repository : OfflineRepository
lateinit var salesSync : SalesSyncUseCase
lateinit var accessSync : AccessControlSyncUseCase
lateinit var refundSync : RefundSyncUseCase
override fun onHandleIntent(intent: Intent) {
if (hasConnection()) {
repository.getAll().map{ offlineRequest ->
when(offlineRequest.type){
OfflineRequestType.SALES ->
salesSync(offlineRequest.request)
OfflineRequestType.REFUND -> refundSync(offlineRequest.request)
OfflineRequestType.ACCESS_CONTROL -> accessSync(offlineRequest.request)
}
}
}
}
}
IMPROVING UX
OFFLINE MODE
i
apiClient.createSell(sale)
.mapLeft { it.mapToDomainError() }
LOCAL STORAGE AS
SOURCE OF TRUTH
REMOVING
PROGRESS BAR
OPTIMISTIC
FEEDBACK
JETPACK TO RESCUE
WORK MANAGER
class RemoveLocalCopy(
context: Context,
params: WorkerParameters
) : Worker(context, params) {
private val use case = UseCase()
override fun doWork(): Result =
usecase(inputData.keyValueMap[PARAM] as String).fold(
{
when (it) {
IOError -> Result.retry()
ForbiddenOperationError, FatalError, ResourceNotFound -> Result.failure()
}
},
{
Result.success(workDataOf(PARAM to it))
}
)
}
CONSTRAINTS &
BACKOFF POLICIES
fun OneTimeWorkRequest.Builder.requireInternetWork(data:
Data, retry: Long)
: OneTimeWorkRequest =
this.setConstraints(
Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
)
.setBackoffCriteria(BackoffPolicy.LINEAR, retry,
TimeUnit.SECONDS)
.setInputData(data)
.build()
UNIQUE WORKS &
EXISTING POLICIES
val work: WorkContinuation =
WorkManager.getInstance()
.beginUniqueWork(uniqueName,
ExistingWorkPolicy.KEEP, request1)
.then(request2)
.then(request3)
.then(listOf(request4, request5))
work.enqueue()
NEXT STEPS
PERIODIC SYNC TO
WORKERS
OPTIMIZE DATA
LOADS
THANKS!
ANY QUESTIONS?
You can find me at:
▸jcgseco@gmail.com
▸@ikarimeister
CREDITS AND RESOURCES
▸ Saúl Díaz: Offline Second
▸ Android Dev Podcast: Offline First
▸ Android Dev Podcast: Workers & Room
▸ Android Developers: https://developer.android.com/topic/
libraries/architecture/workmanager
▸ Working with WorkManager (Android Dev Summit ’18):
https://www.youtube.com/watch?v=83a4rYXsDs0
IMAGES
▸ Barrier Photo: https://www.flickr.com/photos/48603151@N00/
▸ Box-Office Photos: https://www.flickr.com/photos/incinema/
▸ Train Photos: https://www.flickr.com/photos/jtcurses9/
▸ Vader Photo: https://www.flickr.com/photos/jdhancock/
▸ Offline Photo: https://www.flickr.com/photos/no-luogo/

Más contenido relacionado

Similar a How to work_offline

An Introduction To jQuery
An Introduction To jQueryAn Introduction To jQuery
An Introduction To jQueryAndy Gibson
 
Internet of Things Magic Show
Internet of Things Magic ShowInternet of Things Magic Show
Internet of Things Magic ShowStephen Chin
 
Data Mining Open Ap Is
Data Mining Open Ap IsData Mining Open Ap Is
Data Mining Open Ap Isoscon2007
 
Creating Responsive Experiences
Creating Responsive ExperiencesCreating Responsive Experiences
Creating Responsive ExperiencesTim Kadlec
 
Letswift18 워크숍#1 스위프트 클린코드와 코드리뷰
Letswift18 워크숍#1 스위프트 클린코드와 코드리뷰Letswift18 워크숍#1 스위프트 클린코드와 코드리뷰
Letswift18 워크숍#1 스위프트 클린코드와 코드리뷰Jung Kim
 
OSDC 2019 | The Benefits of Orchestration in Distributed Systems by Niall Deehan
OSDC 2019 | The Benefits of Orchestration in Distributed Systems by Niall DeehanOSDC 2019 | The Benefits of Orchestration in Distributed Systems by Niall Deehan
OSDC 2019 | The Benefits of Orchestration in Distributed Systems by Niall DeehanNETWAYS
 
Engage 2013 - Multi Channel Data Collection
Engage 2013 - Multi Channel Data CollectionEngage 2013 - Multi Channel Data Collection
Engage 2013 - Multi Channel Data CollectionWebtrends
 
SproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsSproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsMike Subelsky
 
Making your Angular.js Application accessible
Making your Angular.js Application accessibleMaking your Angular.js Application accessible
Making your Angular.js Application accessibleDirk Ginader
 
Functional Principles for OO Developers
Functional Principles for OO DevelopersFunctional Principles for OO Developers
Functional Principles for OO Developersjessitron
 
Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013
Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013
Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013Gregg Donovan
 
DOM Scripting Toolkit - jQuery
DOM Scripting Toolkit - jQueryDOM Scripting Toolkit - jQuery
DOM Scripting Toolkit - jQueryRemy Sharp
 
Scala meetup
Scala meetupScala meetup
Scala meetup扬 明
 
There is garbage in our code - Talks by Softbinator
There is garbage in our code - Talks by SoftbinatorThere is garbage in our code - Talks by Softbinator
There is garbage in our code - Talks by SoftbinatorAdrian Oprea
 

Similar a How to work_offline (20)

An Introduction To jQuery
An Introduction To jQueryAn Introduction To jQuery
An Introduction To jQuery
 
Internet of Things Magic Show
Internet of Things Magic ShowInternet of Things Magic Show
Internet of Things Magic Show
 
Data Mining Open Ap Is
Data Mining Open Ap IsData Mining Open Ap Is
Data Mining Open Ap Is
 
Creating Responsive Experiences
Creating Responsive ExperiencesCreating Responsive Experiences
Creating Responsive Experiences
 
Letswift18 워크숍#1 스위프트 클린코드와 코드리뷰
Letswift18 워크숍#1 스위프트 클린코드와 코드리뷰Letswift18 워크숍#1 스위프트 클린코드와 코드리뷰
Letswift18 워크숍#1 스위프트 클린코드와 코드리뷰
 
OSDC 2019 | The Benefits of Orchestration in Distributed Systems by Niall Deehan
OSDC 2019 | The Benefits of Orchestration in Distributed Systems by Niall DeehanOSDC 2019 | The Benefits of Orchestration in Distributed Systems by Niall Deehan
OSDC 2019 | The Benefits of Orchestration in Distributed Systems by Niall Deehan
 
Engage 2013 - Multi Channel Data Collection
Engage 2013 - Multi Channel Data CollectionEngage 2013 - Multi Channel Data Collection
Engage 2013 - Multi Channel Data Collection
 
SproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsSproutCore and the Future of Web Apps
SproutCore and the Future of Web Apps
 
Making your Angular.js Application accessible
Making your Angular.js Application accessibleMaking your Angular.js Application accessible
Making your Angular.js Application accessible
 
Living with garbage
Living with garbageLiving with garbage
Living with garbage
 
Api
ApiApi
Api
 
Functional Principles for OO Developers
Functional Principles for OO DevelopersFunctional Principles for OO Developers
Functional Principles for OO Developers
 
Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013
Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013
Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013
 
PHPUG Presentation
PHPUG PresentationPHPUG Presentation
PHPUG Presentation
 
DOM Scripting Toolkit - jQuery
DOM Scripting Toolkit - jQueryDOM Scripting Toolkit - jQuery
DOM Scripting Toolkit - jQuery
 
huhu
huhuhuhu
huhu
 
mobl
moblmobl
mobl
 
Web-First Design Patterns
Web-First Design PatternsWeb-First Design Patterns
Web-First Design Patterns
 
Scala meetup
Scala meetupScala meetup
Scala meetup
 
There is garbage in our code - Talks by Softbinator
There is garbage in our code - Talks by SoftbinatorThere is garbage in our code - Talks by Softbinator
There is garbage in our code - Talks by Softbinator
 

How to work_offline