Slide 1

Slide 1 text

Building Offline First Apps Saket Narayan @saketme uncommon.is

Slide 2

Slide 2 text

Never make the user wait, • when consuming content • when producing content

Slide 3

Slide 3 text

Parts 1. Architecture 2. User Experience 3. Libraries

Slide 4

Slide 4 text

Part 1, Architecture

Slide 5

Slide 5 text

Simplifying state management is the key to offline support

Slide 6

Slide 6 text

But state management is difficult

Slide 7

Slide 7 text

Source: Jake Wharton

Slide 8

Slide 8 text

Source: Jake Wharton

Slide 9

Slide 9 text

Solution, Persistence as the Single Source of Truth

Slide 10

Slide 10 text

Some graphs to prove my point

Slide 11

Slide 11 text

Time Productivity

Slide 12

Slide 12 text

Time Productivity

Slide 13

Slide 13 text

Time Productivity

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

Usecases

Slide 18

Slide 18 text

Usecase 1, Login

Slide 19

Slide 19 text

LoginListener listener = (authToken) -> { database.saveAuthToken(authToken); startActivity(ContentFeedActivity.startIntent(this)); }; api.login(username, password, listener); The usual way

Slide 20

Slide 20 text

LoginListener listener = (authToken) -> { database.saveAuthToken(authToken); startActivity(ContentFeedActivity.startIntent(this)); }; api.login(username, password, listener); The usual way

Slide 21

Slide 21 text

LoginListener listener = (authToken) -> { database.saveAuthToken(authToken); startActivity(ContentFeedActivity.startIntent(this)); }; api.login(username, password, listener); The usual way

Slide 22

Slide 22 text

LoginListener listener = (authToken) -> { database.saveAuthToken(authToken); startActivity(ContentFeedActivity.startIntent(this)); }; api.login(username, password, listener); The usual way

Slide 23

Slide 23 text

LoginListener listener = (authToken) -> { database.saveAuthToken(authToken); startActivity(ContentFeedActivity.startIntent(this)); }; api.login(username, password, listener); The usual way

Slide 24

Slide 24 text

Call call = api.login(…); void onDestroy() { call.cancel(); } What if the Activity gets destroyed?

Slide 25

Slide 25 text

Memory leaks Lifecycle issues when using Fragments No support for orientation changes Response may come while Activity is minimized Problems

Slide 26

Slide 26 text

void onSubmitClick() { LoginListener listener = (authToken) -> { database.saveAuthToken(authToken); startActivity(ContentFeedActivity.startIntent(this)); }; authRepository.login(username, password, listener); } The PSST way

Slide 27

Slide 27 text

void onSubmitClick() { authRepository.login(username, password); } The PSST way

Slide 28

Slide 28 text

void onSubmitClick() { authRepository.login(username, password); } @Singleton public class AuthRepository { api.login(username, password) .doOnSuccess(authToken -> database.saveAuthToken(authToken)) .subscribe(); } The PSST way

Slide 29

Slide 29 text

void onSubmitClick() { authRepository.login(username, password); } @Singleton public class AuthRepository { api.login(username, password) .doOnSuccess(authToken -> database.saveAuthToken(authToken)) .subscribe(); } The PSST way

Slide 30

Slide 30 text

void onStart() { authRepository.streamUserSession() .takeUntil(lifecycle.onStop()) .subscribe(userSession -> { if (userSession.isLoggedIn()) { // Start next Activity. } else { // Show login form. } }); } The PSST way

Slide 31

Slide 31 text

void onStart() { authRepository.streamUserSession() .takeUntil(lifecycle.onStop()) .subscribe(userSession -> { if (userSession.isLoggedIn()) { // Start next Activity. } else { // Show login form. } }); } The PSST way Observable

Slide 32

Slide 32 text

void onStart() { authRepository.streamUserSession() .takeUntil(lifecycle.onStop()) .subscribe(userSession -> { if (userSession.isLoggedIn()) { // Start next Activity. } else { // Show login form. } }); } The PSST way

Slide 33

Slide 33 text

void onStart() { authRepository.streamUserSession() .takeUntil(lifecycle.onStop()) .subscribe(userSession -> { if (userSession.isLoggedIn()) { // Start next Activity. } else { // Show login form. } }); } The PSST way

Slide 34

Slide 34 text

Flow of data is in a single place and not scattered. Every problem can be traced to an event, action, result, or model. Advantages

Slide 35

Slide 35 text

Usecase 2, Trello

Slide 36

Slide 36 text

void onAddNewCard(String title) {
 NetworkListener listener = () -> {
 hideProgressDialog();
 
 database.saveCard(TrelloCard.create(title));
 reloadBoardFromDatabase();
 };
 
 showProgressDialog();
 api.saveNewCard(title, listener);
 } The usual way

Slide 37

Slide 37 text

void onAddNewCard(String title) {
 NetworkListener listener = () -> {
 hideProgressDialog();
 
 database.saveCard(TrelloCard.create(title));
 reloadBoardFromDatabase();
 };
 
 showProgressDialog();
 api.saveNewCard(title, listener);
 } The usual way

Slide 38

Slide 38 text

void onAddNewCard(String title) {
 NetworkListener listener = () -> {
 hideProgressDialog();
 
 database.saveCard(TrelloCard.create(title));
 reloadBoardFromDatabase();
 };
 
 showProgressDialog();
 api.saveNewCard(title, listener);
 } The usual way

Slide 39

Slide 39 text

void onAddNewCard(String title) {
 NetworkListener listener = () -> {
 hideProgressDialog();
 
 database.saveCard(TrelloCard.create(title));
 reloadBoardFromDatabase();
 };
 
 showProgressDialog();
 api.saveNewCard(title, listener);
 } The usual way

Slide 40

Slide 40 text

void onAddNewCard(String title) {
 NetworkListener listener = () -> {
 hideProgressDialog();
 
 database.saveCard(TrelloCard.create(title));
 reloadBoardFromDatabase();
 };
 
 showProgressDialog();
 api.saveNewCard(title, listener);
 } The usual way

Slide 41

Slide 41 text

The user is being blocked by a non-dismissible progress indicator
 Chances of losing user input on process death/phone restart. Problems

Slide 42

Slide 42 text

void onAddNewCard(String title) {
 NetworkListener listener = () -> {
 hideProgressDialog();
 
 database.saveCard(TrelloCard.create(title));
 reloadBoardFromDatabase();
 };
 
 showProgressDialog();
 api.saveNewCard(title, listener);
 } The usual way

Slide 43

Slide 43 text

void onAddNewCard(String title) {
 NetworkListener listener = () -> {
 hideProgressDialog();
 
 database.saveCard(TrelloCard.create(title));
 reloadBoardFromDatabase();
 };
 
 showProgressDialog();
 api.saveNewCard(title, listener);
 } The usual way

Slide 44

Slide 44 text

void onAddNewCard(String title) {
 repository.createNewCard(title);
 } PSST

Slide 45

Slide 45 text

Repository.java void createNewCard(String name) {
 database.saveCard(TrelloCard.create(name))
 .andThen(scheduleSyncWithServer())
 .subscribe();
 } PSST

Slide 46

Slide 46 text

Repository.java void createNewCard(String name) {
 database.saveCard(TrelloCard.create(name))
 .andThen(scheduleSyncWithServer())
 .subscribe();
 } PSST

Slide 47

Slide 47 text

Repository.java void createNewCard(String name) {
 database.saveCard(TrelloCard.create(name))
 .andThen(scheduleSyncWithServer())
 .subscribe();
 } PSST

Slide 48

Slide 48 text

Usecase 3, Content Details

Slide 49

Slide 49 text

void onCreate() { UserContent contentToShow = getIntent().getParcelable(KEY_CONTENT); votesCountView.setText(contentToShow.votes() + " points”); } The usual way

Slide 50

Slide 50 text

void onCreate() { UserContent contentToShow = getIntent().getParcelable(KEY_CONTENT); votesCountView.setText(contentToShow.votes() + " points”); } The usual way

Slide 51

Slide 51 text

void onCreate() { UserContent contentToShow = getIntent().getParcelable(KEY_CONTENT); votesCountView.setText(contentToShow.votes() + " points”); } The usual way

Slide 52

Slide 52 text

void onUpvoteClick() { int newVoteCount = contentToShow.votes() + 1; NetworkListener listener = () -> { hideProgressDialog(); contentToShow = contentToShow.withVotes(newVoteCount); votesCountView.setText(newVoteCount + " points"); database.updateUserContent(contentToShow); }; showProgressDialog(); api.voteOnUserContent(contentToShow.id(), newVoteCount, listener); } The usual way

Slide 53

Slide 53 text

void onUpvoteClick() { int newVoteCount = contentToShow.votes() + 1; NetworkListener listener = () -> { hideProgressDialog(); contentToShow = contentToShow.withVotes(newVoteCount); votesCountView.setText(newVoteCount + " points"); database.updateUserContent(contentToShow); }; showProgressDialog(); api.voteOnUserContent(contentToShow.id(), newVoteCount, listener); } The usual way

Slide 54

Slide 54 text

void onUpvoteClick() { int newVoteCount = contentToShow.votes() + 1; NetworkListener listener = () -> { hideProgressDialog(); contentToShow = contentToShow.withVotes(newVoteCount); votesCountView.setText(newVoteCount + " points"); database.updateUserContent(contentToShow); }; showProgressDialog(); api.voteOnUserContent(contentToShow.id(), newVoteCount, listener); } The usual way

Slide 55

Slide 55 text

void onUpvoteClick() { int newVoteCount = contentToShow.votes() + 1; NetworkListener listener = () -> { hideProgressDialog(); contentToShow = contentToShow.withVotes(newVoteCount); votesCountView.setText(newVoteCount + " points"); database.updateUserContent(contentToShow); }; showProgressDialog(); api.voteOnUserContent(contentToShow.id(), newVoteCount, listener); } The usual way

Slide 56

Slide 56 text

void onUpvoteClick() { int newVoteCount = contentToShow.votes() + 1; NetworkListener listener = () -> { hideProgressDialog(); contentToShow = contentToShow.withVotes(newVoteCount); votesCountView.setText(newVoteCount + " points"); database.updateUserContent(contentToShow); }; showProgressDialog(); api.voteOnUserContent(contentToShow.id(), newVoteCount, listener); } The usual way

Slide 57

Slide 57 text

void onUpvoteClick() { int newVoteCount = contentToShow.votes() + 1; NetworkListener listener = () -> { hideProgressDialog(); contentToShow = contentToShow.withVotes(newVoteCount); votesCountView.setText(newVoteCount + " points"); database.updateUserContent(contentToShow); }; showProgressDialog(); api.voteOnUserContent(contentToShow.id(), newVoteCount, listener); } The usual way

Slide 58

Slide 58 text

void onUpvoteClick() { int newVoteCount = contentToShow.votes() + 1; NetworkListener listener = () -> { hideProgressDialog(); contentToShow = contentToShow.withVotes(newVoteCount); votesCountView.setText(newVoteCount + " points"); database.updateUserContent(contentToShow); }; showProgressDialog(); api.voteOnUserContent(contentToShow.id(), newVoteCount, listener); } The usual way

Slide 59

Slide 59 text

Problem: UI is being driven from multiple sources void onCreate() { ... votesCountView.setText(contentToShow.votes() + " points”); } void onUpvoteClick() { ... votesCountView.setText(newVoteCount + " points"); ... }

Slide 60

Slide 60 text

String id = getIntent().getStringExtra(KEY_CONTENT_ID); contentRepository.streamUserContent(id) .takeUntil(lifecycle.onDestroy()) .subscribe(userContent -> { loadImage(userContent.imageUrl()); votesCountView.setText( userContent.votes() + " points” ); upvoteButton.setOnClickListener(o -> contentRepository.upvote(userContent) ); }); The PSST way

Slide 61

Slide 61 text

String id = getIntent().getStringExtra(KEY_CONTENT_ID); contentRepository.streamUserContent(id) .takeUntil(lifecycle.onDestroy()) .subscribe(userContent -> { loadImage(userContent.imageUrl()); votesCountView.setText( userContent.votes() + " points” ); upvoteButton.setOnClickListener(o -> contentRepository.upvote(userContent) ); }); The PSST way

Slide 62

Slide 62 text

String id = getIntent().getStringExtra(KEY_CONTENT_ID); contentRepository.streamUserContent(id) .takeUntil(lifecycle.onDestroy()) .subscribe(userContent -> { loadImage(userContent.imageUrl()); votesCountView.setText( userContent.votes() + " points” ); upvoteButton.setOnClickListener(o -> contentRepository.upvote(userContent) ); }); The PSST way

Slide 63

Slide 63 text

String id = getIntent().getStringExtra(KEY_CONTENT_ID); contentRepository.streamUserContent(id) .takeUntil(lifecycle.onDestroy()) .subscribe(userContent -> { loadImage(userContent.imageUrl()); votesCountView.setText( userContent.votes() + " points” ); upvoteButton.setOnClickListener(o -> contentRepository.upvote(userContent) ); }); The PSST way

Slide 64

Slide 64 text

String id = getIntent().getStringExtra(KEY_CONTENT_ID); contentRepository.streamUserContent(id) .takeUntil(lifecycle.onDestroy()) .subscribe(userContent -> { loadImage(userContent.imageUrl()); votesCountView.setText( userContent.votes() + " points” ); upvoteButton.setOnClickListener(o -> contentRepository.upvote(userContent) ); }); The PSST way

Slide 65

Slide 65 text

Part 2, User Experience

Slide 66

Slide 66 text

If a network call does not involve any input validation, assume that it’ll succeed. Never make the user wait

Slide 67

Slide 67 text

ContentRepository.java void onUpvoteClick(UserContent content) { // Assume success. database.upvote(content.id(), content.votes() + 1); performApiCallInBackground(); } Never make the user wait

Slide 68

Slide 68 text

ContentRepository.java void onUpvoteClick(UserContent content) { // Assume success. database.upvote(content.id(), content.votes() + 1); performApiCallInBackground(); } Never make the user wait

Slide 69

Slide 69 text

If a network call’s progress has to be communicated, consider displaying the progress state inline in UI.
 
 Totally avoid covering the screen with a progress bar. Never make the user wait

Slide 70

Slide 70 text

void postComment(UserContent parent, String comment) { api.postComment(parent.id(), comment) .map(response -> ProgressState.POSTED) .onErrorReturnItem(ProgressState.FAILED) .startWith(ProgressState.IN_FLIGHT) .subscribe(progressState -> { database.createOrUpdateComment( comment, progressState ); }); } Never make the user wait

Slide 71

Slide 71 text

void postComment(UserContent parent, String comment) { api.postComment(parent.id(), comment) .map(response -> ProgressState.POSTED) .onErrorReturnItem(ProgressState.FAILED) .startWith(ProgressState.IN_FLIGHT) .subscribe(progressState -> { database.createOrUpdateComment( comment, progressState ); }); } Never make the user wait

Slide 72

Slide 72 text

void postComment(UserContent parent, String comment) { api.postComment(parent.id(), comment) .map(response -> ProgressState.POSTED) .onErrorReturnItem(ProgressState.FAILED) .startWith(ProgressState.IN_FLIGHT) .subscribe(progressState -> { database.createOrUpdateComment( comment, progressState ); }); } Never make the user wait

Slide 73

Slide 73 text

void postComment(UserContent parent, String comment) { api.postComment(parent.id(), comment) .map(response -> ProgressState.POSTED) .onErrorReturnItem(ProgressState.FAILED) .startWith(ProgressState.IN_FLIGHT) .subscribe(progressState -> { database.createOrUpdateComment( comment, progressState ); }); } Never make the user wait

Slide 74

Slide 74 text

void postComment(UserContent parent, String comment) { api.postComment(parent.id(), comment) .map(response -> ProgressState.POSTED) .onErrorReturnItem(ProgressState.FAILED) .startWith(ProgressState.IN_FLIGHT) .subscribe(progressState -> { database.createOrUpdateComment( comment, progressState ); }); } Never make the user wait

Slide 75

Slide 75 text

contentRepository.postCommentAndStreamProgress(…) .subscribe(progressEvent -> { switch (progressEvent.state()) { case IN_FLIGHT: statusView.setText("Posting…"); break; case POSTED: statusView.setText("Posted at " + progressEvent.timestamp()); break; case FAILED: statusView.setText("Failed. Tap to retry."); break; } }); Never make the user wait

Slide 76

Slide 76 text

Instant gratification Increased perceived speed of the app Advantages

Slide 77

Slide 77 text

Notifications can also be used for communicating progress.
 Never make the user wait

Slide 78

Slide 78 text

Notifications can also be used for communicating progress.
 Never make the user wait

Slide 79

Slide 79 text

Notifications can also be used for communicating progress.
 Never make the user wait

Slide 80

Slide 80 text

Part 3, Libraries

Slide 81

Slide 81 text

Store github.com/NYTimes/Store

Slide 82

Slide 82 text

android-job github.com/evernote/android-job

Slide 83

Slide 83 text

onComplete()