Top Banner
28

Nicholas Gustilo "Clean Android: building great mobile apps"

Jan 22, 2017

Download

Education

Mobicode
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Nicholas Gustilo "Clean Android: building great mobile apps"
Page 2: Nicholas Gustilo "Clean Android: building great mobile apps"

Nicholas Gustilo

Director of Engineering, Apalon NYC.

[email protected]● https://www.linkedin.com/in/nicholas-gustilo-b893a561

Page 3: Nicholas Gustilo "Clean Android: building great mobile apps"
Page 4: Nicholas Gustilo "Clean Android: building great mobile apps"

Apalon Translation Tool

I am pleased to announce, that today we are releasing an open source translation tool that dramatically simplifies the management of strings of Android apps. The tool consists of:

● Uses a google spreadsheet to manage all translated strings.● A gradle plugin● A library (jar file)● Call ./gradlew updateTranslations● https://github.com/Apalon/translation-tool

Page 5: Nicholas Gustilo "Clean Android: building great mobile apps"

Agenda

1. What is clean architecture and why should you care?2. App organization and naming.3. Process and libraries.4. Reactive programming (costs / benefits)5. Questions

Page 6: Nicholas Gustilo "Clean Android: building great mobile apps"

What is Clean Architecture?

● What do I mean by architecture?○ Organization of the code, including, naming conventions and package organization, libraries

chosen, the design patterns used and structure of the source code.

● What do we mean by “clean”? ○ Tidy (not messy)? Organized? Not dirty? Clear? Understandable? Simple? Modular!

● Useful links○ http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod○ http://www.codingthearchitecture.com/2011/11/22/re_clean_architecture.html

Page 7: Nicholas Gustilo "Clean Android: building great mobile apps"

What are the benefits of clean architecture?

● Easy to understand - for example, easily map app to code.● Easier to refactor. Easier to debug and fix. ● Easy for multiple engineers to work on at the same time.

Page 8: Nicholas Gustilo "Clean Android: building great mobile apps"

Practical Clean Architecture Tips

1. Organize your code and use good naming conventions2. Make code modular (how?)3. Less is more. Use tools that minimize the code that needs to be maintained.4. Use consistent design patterns (Reactive/MVC/MVP).

Page 9: Nicholas Gustilo "Clean Android: building great mobile apps"

App Organization (packages) and Naming

● Demo

Page 10: Nicholas Gustilo "Clean Android: building great mobile apps"

Process and Libraries

● Process: Agile (2 week sprints), Android Studio, gradle, git, GitLab, JIRA● Libraries: butterknife, okhttp3, retrofit, picasso● Libraries (reactive): rxjava, rx-bindings, rx-preferences, retrofit● Libraries (other): stetho, leak-canary, apalon-translation-tool

Page 11: Nicholas Gustilo "Clean Android: building great mobile apps"

Reactive

What is Reactive? Why should you care? Is it worth the effort?

Page 12: Nicholas Gustilo "Clean Android: building great mobile apps"

Reactive - Costs

● Substantial learning curve.● Clash of APIs (very different from much of android).● MUST unsubscribe or you have memory leaks.

Page 13: Nicholas Gustilo "Clean Android: building great mobile apps"

Reactive - Benefits

Reactive programming combines three features into a compelling technology.

1. Observable/Subscriber design pattern.2. Thread management3. Operators to “transform” data

Page 14: Nicholas Gustilo "Clean Android: building great mobile apps"

Reactive - Observable/Subscriber

● Observable has a one-to-many relationship with subscribers (or subscriber objects) so that when the observables state changes all the subscribers are notified automatically.

● For example, public Observable<List<Item>> getImages() ● Subscriber is an object that “subscribes” to an observable and when notified

is able to process changes using the data provided by the observable.● For example, this.model.getImages().subscribe(images -> { … });

Page 15: Nicholas Gustilo "Clean Android: building great mobile apps"

Reactive - Life Cycle

Page 16: Nicholas Gustilo "Clean Android: building great mobile apps"

Reactive - Thread Management

● Compared to standard Android threading technologies reactive handles threading in a very simple and elegant way.

Page 17: Nicholas Gustilo "Clean Android: building great mobile apps"

Reactive - Thread Management

Page 18: Nicholas Gustilo "Clean Android: building great mobile apps"

Reactive - Thread Management

Page 19: Nicholas Gustilo "Clean Android: building great mobile apps"

Reactive - Thread Management

● Thread management in reactive code is compacted and clear.

Page 20: Nicholas Gustilo "Clean Android: building great mobile apps"

Reactive - Operators

● Rxjava supports many operators. Most operators take an Observable as input and return an Observable. This allows “chaining” of many operators together. For example, to merge and filter the output of an Observable or control how an Observable emits data.

Page 21: Nicholas Gustilo "Clean Android: building great mobile apps"

Reactive - Operators

Page 22: Nicholas Gustilo "Clean Android: building great mobile apps"

Reactive is Awesome! Use it.

Reactive programming combines three features into a compelling technology.

1. Observable/Subscriber design pattern → More modular code.2. Thread management → Cleaner, easier thread management → more

responsive applications.3. Operators to “transform” data → Very powerful and (usually) clear way of

managing and controlling data.

Page 23: Nicholas Gustilo "Clean Android: building great mobile apps"

Reactive - Architecture Tips

1. In Call Recorder For Me and Coloring Book For Me almost all data is managed using reactive programming.

2. Activities/Fragments. Subscribe in onResume. Unsubscribe in onPause().a. Tip: use CompositeSubscription objects.

3. Presenters. Subscribe in UI (view) onAttachedToWindow(). Unsubscribe in UI (view) onDetachFromWindow().

Page 25: Nicholas Gustilo "Clean Android: building great mobile apps"

Extra - Apalon Translation Tool

Demo

Page 26: Nicholas Gustilo "Clean Android: building great mobile apps"

Extra - Subjects

● Subjects are like Observable you can call from anywhere at anytime.● Easy way to create a “hot” Observable.● Helps create more modular code.● Is NOT always thread safe and has gotchas.

○ http://davesexton.com/blog/post/To-Use-Subject-Or-Not-To-Use-Subject.aspx○ http://tomstechnicalblog.blogspot.com/2016/03/rxjava-problem-with-subjects.html

Page 27: Nicholas Gustilo "Clean Android: building great mobile apps"

Extra - Subjects

● private final BehaviorSubject<List<Palette>> paletteBehaviorSubject = BehaviorSubject.create();

● public BehaviorSubject<List<Category>> getCategoriesObservable() { return categoriesBehaviorSubject; }

● paletteBehaviorSubject.onNext(paletteList);

Page 28: Nicholas Gustilo "Clean Android: building great mobile apps"

Extra - Rx-preferences

● if( MyPreferences.get().isPremium().get() ) { … }● MyPreferences.get().isPremium().set(true);● MyPreferences.get().isPremium().asObservable().subscribe (premiumFlag →

{ … });