Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
In 2016, how do you implement
"Web API client Activity"?
Tsuyoshi Yoshioka (@tsuyogoro)
Mercari, Inc.
About me
Tsuyoshi Yoshioka (@tsuyogoro)
Android engineer
Mercari, Inc
How do you implement this app?
Using a web API - http://zipcloud.ibsnet.co.jp/doc/api
Today’s focus on
How to support “orientation change” ?
Base of my idea
Activity
(Portrait)
Activity
(Landscape)
Data
Model
API task
Web API
(Search address)
share share
“Wait…” ...
We have 3 weapons
● Databinding
● RxJava
● Lifecycle of Activity & Fragment
1.Sharing model with databinding
public class ZipSearchActivityModel implements Serializable {
private static final long s...
1.Sharing model with databinding
Can implement model sharing,
Quite simple & safely
public class ZipSearchActivityModel im...
2. Task sharing with RxJava
ZipSearchTask.buildRequest(model.zipCode.get())
.flatMap(ZipSearchTask::submitRequest)
.flatMa...
Base of my idea
Activity
(Portrait)
Activity
(Landscape)
Data
Model
Observable
Web API
(Search address)
share shareSubscri...
2. Task sharing with RxJava
public class RxZipSearchActivity extends AppCompatActivity {
private MySubscriber mySubscriber...
3. Progress dialog
private void showProgressDialog() {
if (getSupportFragmentManager().findFragmentByTag(TAG_PROGRESS_DIAL...
Summary
Introduced my idea to implement simple WebAPI client app
We have efficient way in 2016 (databinding, RxJava)
Let’s...
2. RxJava Can implement task sharing, Quite simple & safely
public class RxZipSearchActivity extends AppCompatActivity {
p...
Upcoming SlideShare
Loading in …5
×

160714 roppongi aar3_how_to_implement_activity

669 views

Published on

Slide presented in Roppongi aar #3

Published in: Technology
  • Be the first to comment

  • Be the first to like this

160714 roppongi aar3_how_to_implement_activity

  1. 1. In 2016, how do you implement "Web API client Activity"? Tsuyoshi Yoshioka (@tsuyogoro) Mercari, Inc.
  2. 2. About me Tsuyoshi Yoshioka (@tsuyogoro) Android engineer Mercari, Inc
  3. 3. How do you implement this app? Using a web API - http://zipcloud.ibsnet.co.jp/doc/api
  4. 4. Today’s focus on How to support “orientation change” ?
  5. 5. Base of my idea Activity (Portrait) Activity (Landscape) Data Model API task Web API (Search address) share share “Wait…” dialog 1. How to share “Data model” ? 2. How to share “API task” ? 3. How to show ProgressDialog in correct period ?
  6. 6. We have 3 weapons ● Databinding ● RxJava ● Lifecycle of Activity & Fragment
  7. 7. 1.Sharing model with databinding public class ZipSearchActivityModel implements Serializable { private static final long serialVersionUID = 123L; /** * ユーザの入力した郵便番号 * */ public ObservableField<String> zipCode = new ObservableField<>(); /** * 検索した結果の住所 * (場合によってはエラー文言を表示するのに使う) * */ public ObservableField<String> address = new ObservableField<>(); <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@={model.zipCode}"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_marginBottom="16dp" android:text="@{model.address}"/> ☆ Two-way binding https://halfthought.wordpress.com/2016/03/23/2-way-data-binding-on-android/
  8. 8. 1.Sharing model with databinding Can implement model sharing, Quite simple & safely public class ZipSearchActivityModel implements Serializable { private static final long serialVersionUID = 123L; /** * ユーザの入力した郵便番号 * */ public ObservableField<String> zipCode = new ObservableField<>(); /** * 検索した結果の住所 * (場合によってはエラー文言を表示するのに使う) * */ public ObservableField<String> address = new ObservableField<>(); } Serializable Serializable Can use android studio plug-in @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putSerializable(BUNDLE_KEY_MODEL, model); }
  9. 9. 2. Task sharing with RxJava ZipSearchTask.buildRequest(model.zipCode.get()) .flatMap(ZipSearchTask::submitRequest) .flatMap(ZipSearchTask::parseResponse) .delay(5, TimeUnit.SECONDS) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(mySubscriber); Task Listener
  10. 10. Base of my idea Activity (Portrait) Activity (Landscape) Data Model Observable Web API (Search address) share shareSubscriber
  11. 11. 2. Task sharing with RxJava public class RxZipSearchActivity extends AppCompatActivity { private MySubscriber mySubscriber; @Override protected void onCreate(Bundle savedInstanceState) { ... mySubscriber = (MySubscriber) getLastCustomNonConfigurationInstance(); if (mySubscriber != null) { mySubscriber.activeActivity = this; } ... @Override public Object onRetainCustomNonConfigurationInstance() { return mySubscriber; } Can implement task sharing, Quite simple & safely
  12. 12. 3. Progress dialog private void showProgressDialog() { if (getSupportFragmentManager().findFragmentByTag(TAG_PROGRESS_DIALOG) == null) { new ProgressDialogFragment().show(getSupportFragmentManager(), TAG_PROGRESS_DIALOG); } } public static class ProgressDialogFragment extends AppCompatDialogFragment { @Override protected void onResume() { super.onResume(); // まだ通信の途中だったらdialogを出す if (mySubscriber != null && !mySubscriber.isUnsubscribed()) { showProgressDialog(); } ● Avoid multiple dialog showing ● Subscriber can be a clue if to show progress dialog
  13. 13. Summary Introduced my idea to implement simple WebAPI client app We have efficient way in 2016 (databinding, RxJava) Let’s discuss how you design / implement such application now? (Source code intoduced in my presentation) https://github.com/tsuyoyo/roppongi-aar3-address-search-app
  14. 14. 2. RxJava Can implement task sharing, Quite simple & safely public class RxZipSearchActivity extends AppCompatActivity { private static class MySubscriber extends Subscriber<List<String>> { private RxZipSearchActivity activeActivity; @Override public void onCompleted() { Log.d("TestTestTest", "onCompleted"); } @Override public void onError(Throwable e) { notifyResult(e.getMessage()); } @Override public void onNext(List<String> result) { activeActivity.notifyResult(result); onCompleted();

×