Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Design Patterns

7fd4ba468da56bb5330a6352c1b54f52?s=47 felipecsl
February 21, 2014

Android Design Patterns

Apresentação realizada na meetup do GDG Porto Alegre em 20/02/2014.

7fd4ba468da56bb5330a6352c1b54f52?s=128

felipecsl

February 21, 2014
Tweet

Transcript

  1. Android Design Patterns

  2. Android Design Patterns @felipecsl

  3. None
  4. 5MM+ downloads 85k reviews 4.5 ★ average rating

  5. Android app 2.5 developers

  6. Brigita Fernežir on Feb 9, 2014 at 4:56 PM It

    fucking crashes all the time fix it !!! Usuários são cruéis
  7. 1. Best practices

  8. 2. Open Source 1. Best practices

  9. 2. Open Source 3. Code design patterns 1. Best practices

  10. 1. Best practices

  11. UNIT TESTS 1. Best practices

  12. UNIT TESTS • jUnit 1. Best practices

  13. UNIT TESTS • jUnit • Robolectric 1. Best practices

  14. UNIT TESTS • jUnit • Robolectric • Mockito 1. Best

    practices
  15. INTEGRAÇÃO CONTÍNUA 1. Best practices

  16. INTEGRAÇÃO CONTÍNUA • Jenkins 1. Best practices

  17. INTEGRAÇÃO CONTÍNUA • Jenkins • TestFlight 1. Best practices

  18. INTEGRAÇÃO CONTÍNUA • Jenkins • TestFlight 1. Best practices •

    Travis
  19. TESTES FUNCIONAIS 1. Best practices

  20. TESTES FUNCIONAIS • Calabash 1. Best practices

  21. TESTES FUNCIONAIS • Calabash • Robotium 1. Best practices

  22. 1. Best practices DRY - DON’T REPEAT YOURSELF

  23. • Android Bootstrap 1. Best practices http://www.androidbootstrap.com/ DRY - DON’T

    REPEAT YOURSELF
  24. DRY - DON’T REPEAT YOURSELF • Android Action Bar Style

    Generator 1. Best practices http://jgilfelt.github.io/android-actionbarstylegenerator
  25. 1. Best practices USE STANDARD LIBRARIES

  26. USE STANDARD LIBRARIES • Support v4 1. Best practices

  27. USE STANDARD LIBRARIES • Support v4 • AppCompat 1. Best

    practices
  28. ANDROID STUDIO 1. Best practices

  29. ANDROID STUDIO • Gradle 1. Best practices

  30. EMULATOR 1. Best practices

  31. EMULATOR • Genymotion 1. Best practices

  32. 2. Open Source

  33. 2. Open Source Por que?

  34. 2. Open Source Por que? • Maior qualidade

  35. 2. Open Source Por que? • Maior qualidade • Liberdade

    para fazer alterações
  36. 2. Open Source Por que? • Maior qualidade • Liberdade

    para fazer alterações • Aprendizado
  37. 2. Open Source Por que? • Maior qualidade • Liberdade

    para fazer alterações • Aprendizado • Visibilidade
  38. 2. Open Source Jake Wharton Square

  39. 2. Open Source UI e Compatibilidade

  40. 2. Open Source UI e Compatibilidade Android Support Library v4

    • DrawerLayout, ActionBarDrawerToggle • Fragment • ViewPager • NotificationCompat http://developer.android.com/tools/support-library/features.html#v4
  41. 2. Open Source UI e Compatibilidade Android AppCompat v7 •

    ActionBar • ActionBarActivity • ShareActionProvider • GridLayout http://developer.android.com/tools/support-library/features.html#v7-appcompat
  42. 2. Open Source UI e Compatibilidade ViewPagerIndicator http://viewpagerindicator.com/

  43. 2. Open Source UI e Compatibilidade NineOldAndroids Backport da API

    de animações https://github.com/JakeWharton/NineOldAndroids/ AnimatorSet set = new AnimatorSet(); set.playTogether( ObjectAnimator.ofFloat(myView, "rotationX", 0, 360), ObjectAnimator.ofFloat(myView, "rotationY", 0, 180), ObjectAnimator.ofFloat(myView, "rotation", 0, -90), ObjectAnimator.ofFloat(myView, "translationX", 0, 90), ObjectAnimator.ofFloat(myView, "translationY", 0, 90), ObjectAnimator.ofFloat(myView, "scaleX", 1, 1.5f), ObjectAnimator.ofFloat(myView, "scaleY", 1, 0.5f), ObjectAnimator.ofFloat(myView, "alpha", 1, 0.25f, 1) ); set.setDuration(5 * 1000).start();
  44. 2. Open Source UI e Compatibilidade ActionBar-PullToRefresh https://github.com/chrisbanes/actionbar-pulltorefresh

  45. 2. Open Source Caching e Networking

  46. 2. Open Source Caching e Networking DiskLruCache Disk-based LRU cache

    https://github.com/jakewharton/disklrucache
  47. 2. Open Source Caching e Networking Picasso Image downloading and

    caching http://square.github.io/picasso/
  48. 2. Open Source Caching e Networking Android-Async-Http Asynchronous HTTP https://github.com/loopj/android-async-http

    import org.json.*; import com.loopj.android.http.*; ! class TwitterRestClientUsage { public void getPublicTimeline() throws JSONException { TwitterRestClient.get("statuses/public_timeline.json", null, new JsonHttpResponseHandler() { @Override public void onSuccess(JSONArray timeline) { // Pull out the first event on the public timeline JSONObject firstEvent = timeline.get(0); String tweetText = firstEvent.getString("text"); ! // Do something with the response System.out.println(tweetText); } }); } }
  49. 2. Open Source Caching e Networking Http-Request https://github.com/kevinsawicki/http-request String response

    = HttpRequest.get("http://google.com").body(); System.out.println("Response was: " + response);
  50. 2. Open Source Comunicação e Concorrência

  51. 2. Open Source Comunicação e Concorrência Otto Event Bus http://square.github.io/otto/

    Bus bus = new Bus(); bus.post(new AnswerAvailableEvent(42)); ! //... ! @Subscribe public void answerAvailable(AnswerAvailableEvent event) { // TODO: React to the event somehow! }
  52. 2. Open Source Dependency Injection

  53. 2. Open Source Dependency Injection Dagger http://square.github.io/dagger/ class CoffeeMaker {

    @Inject Heater heater; @Inject Pump pump; ! ... } ! class Thermosiphon implements Pump { private final Heater heater; ! @Inject Thermosiphon(Heater heater) { this.heater = heater; } ! ... }
  54. 2. Open Source Dependency Injection Butter Knife https://github.com/JakeWharton/butterknife View Injection

    class ExampleActivity extends Activity { @InjectView(R.id.user) EditText username; @InjectView(R.id.pass) EditText password; ! @OnClick(R.id.submit) void submit() { // TODO call server... } ! @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_activity); ButterKnife.inject(this); // TODO Use "injected" views... } }
  55. 3. Code design patterns Activity

  56. 3. Code design patterns • Auto-contida Activity

  57. 3. Code design patterns • Libera recursos ao ser finalizada

    • Auto-contida Activity
  58. 3. Code design patterns • Auto-contida • Libera recursos ao

    ser finalizada • Não é dependência de objetos globais Activity • Delega responsabilidades
  59. 3. Code design patterns Activity Auto-contida Inputs e outputs definidos

    via Intent/Bundle Implementa onSaveInstanceState Não possui lógica de negócio
  60. 3. Code design patterns Activity Libera recursos ao ser finalizada

    @Override public void onDestroy() { super.onDestroy(); BusProvider.getInstance().unregister(this); }
  61. 3. Code design patterns Activity Não é dependência de objetos

    globais @Override public void onCreate(Bundle savedState) { super.onCreate(savedState); LongLivedObject obj = LongLivedObject.getInstance(); obj.doSomething(this); // bad } Exemplo do que não fazer:
  62. 3. Code design patterns Fragment

  63. 3. Code design patterns Fragment Reutilize fragments para evitar repetição

  64. 3. Code design patterns Fragment Reutilize fragments para evitar repetição

    Crie custom Views para layouts complexos
  65. 3. Code design patterns Performance

  66. 3. Code design patterns Performance Utilize a UI Thread apenas

    para layout!
  67. 3. Code design patterns Performance Utilize a UI Thread apenas

    para layout! Mova tarefas paralelas para o background
  68. 3. Code design patterns Performance Utilize a UI Thread apenas

    para layout! Mova tarefas paralelas para o background Pense um pouco antes de instanciar objetos
  69. 3. Code design patterns Performance Utilize a UI Thread apenas

    para layout! Mova tarefas paralelas para o background Pense um pouco antes de instanciar objetos Cancele AsyncTasks ao finalizar a Activity
  70. 3. Code design patterns A.B.C Always be coding! :-)

  71. Obrigado! @felipecsl http://felipecsl.com