Share
Embed
Transcript
83 slides
-
Never make the user wait, • when consuming content •
when producing content -
Call call = api.login(…); void onDestroy() { call.cancel(); } What
if the Activity gets destroyed? -
Memory leaks Lifecycle issues when using Fragments No support for
orientation changes Response may come while Activity is minimized Problems -
void onSubmitClick() { LoginListener listener = (authToken) -> { database.saveAuthToken(authToken);
startActivity(ContentFeedActivity.startIntent(this)); }; authRepository.login(username, password, listener); } The PSST way -
void onSubmitClick() { authRepository.login(username, password); } @Singleton public class AuthRepository
{ api.login(username, password) .doOnSuccess(authToken -> database.saveAuthToken(authToken)) .subscribe(); } The PSST way -
void onSubmitClick() { authRepository.login(username, password); } @Singleton public class AuthRepository
{ api.login(username, password) .doOnSuccess(authToken -> database.saveAuthToken(authToken)) .subscribe(); } The PSST way -
void onStart() { authRepository.streamUserSession() .takeUntil(lifecycle.onStop()) .subscribe(userSession -> { if (userSession.isLoggedIn())
{ // Start next Activity. } else { // Show login form. } }); } The PSST way -
void onStart() { authRepository.streamUserSession() .takeUntil(lifecycle.onStop()) .subscribe(userSession -> { if (userSession.isLoggedIn())
{ // Start next Activity. } else { // Show login form. } }); } The PSST way Observable<UserSession> -
void onStart() { authRepository.streamUserSession() .takeUntil(lifecycle.onStop()) .subscribe(userSession -> { if (userSession.isLoggedIn())
{ // Start next Activity. } else { // Show login form. } }); } The PSST way -
void onStart() { authRepository.streamUserSession() .takeUntil(lifecycle.onStop()) .subscribe(userSession -> { if (userSession.isLoggedIn())
{ // Start next Activity. } else { // Show login form. } }); } The PSST way -
Flow of data is in a single place and not
scattered. Every problem can be traced to an event, action, result, or model. Advantages -
void onAddNewCard(String title) { NetworkListener listener = () -> {
hideProgressDialog(); database.saveCard(TrelloCard.create(title)); reloadBoardFromDatabase(); }; showProgressDialog(); api.saveNewCard(title, listener); } The usual way -
void onAddNewCard(String title) { NetworkListener listener = () -> {
hideProgressDialog(); database.saveCard(TrelloCard.create(title)); reloadBoardFromDatabase(); }; showProgressDialog(); api.saveNewCard(title, listener); } The usual way -
void onAddNewCard(String title) { NetworkListener listener = () -> {
hideProgressDialog(); database.saveCard(TrelloCard.create(title)); reloadBoardFromDatabase(); }; showProgressDialog(); api.saveNewCard(title, listener); } The usual way -
void onAddNewCard(String title) { NetworkListener listener = () -> {
hideProgressDialog(); database.saveCard(TrelloCard.create(title)); reloadBoardFromDatabase(); }; showProgressDialog(); api.saveNewCard(title, listener); } The usual way -
void onAddNewCard(String title) { NetworkListener listener = () -> {
hideProgressDialog(); database.saveCard(TrelloCard.create(title)); reloadBoardFromDatabase(); }; showProgressDialog(); api.saveNewCard(title, listener); } The usual way -
The user is being blocked by a non-dismissible progress indicator
Chances of losing user input on process death/phone restart. Problems -
void onAddNewCard(String title) { NetworkListener listener = () -> {
hideProgressDialog(); database.saveCard(TrelloCard.create(title)); reloadBoardFromDatabase(); }; showProgressDialog(); api.saveNewCard(title, listener); } The usual way -
void onAddNewCard(String title) { NetworkListener listener = () -> {
hideProgressDialog(); database.saveCard(TrelloCard.create(title)); reloadBoardFromDatabase(); }; showProgressDialog(); api.saveNewCard(title, listener); } The usual way -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
Problem: UI is being driven from multiple sources void onCreate()
{ ... votesCountView.setText(contentToShow.votes() + " points”); } void onUpvoteClick() { ... votesCountView.setText(newVoteCount + " points"); ... } -
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 -
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 -
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 -
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 -
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 -
If a network call does not involve any input validation,
assume that it’ll succeed. Never make the user wait -
ContentRepository.java void onUpvoteClick(UserContent content) { // Assume success. database.upvote(content.id(), content.votes()
+ 1); performApiCallInBackground(); } Never make the user wait -
ContentRepository.java void onUpvoteClick(UserContent content) { // Assume success. database.upvote(content.id(), content.votes()
+ 1); performApiCallInBackground(); } Never make the user wait -
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 -
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 -
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 -
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 -
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 -
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 -
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