nicholas gustilo "clean android: building great mobile apps"

28

Upload: mobicode

Post on 22-Jan-2017

63 views

Category:

Education


0 download

TRANSCRIPT

Nicholas Gustilo

Director of Engineering, Apalon NYC.

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

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

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

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

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.

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).

App Organization (packages) and Naming

● Demo

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

Reactive

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

Reactive - Costs

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

Reactive - Benefits

Reactive programming combines three features into a compelling technology.

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

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 -> { … });

Reactive - Life Cycle

Reactive - Thread Management

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

Reactive - Thread Management

Reactive - Thread Management

Reactive - Thread Management

● Thread management in reactive code is compacted and clear.

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.

Reactive - Operators

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.

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().

Extra - Apalon Translation Tool

Demo

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

Extra - Subjects

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

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

● paletteBehaviorSubject.onNext(paletteList);

Extra - Rx-preferences

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

{ … });