3. #gdg_kharkiv_center
But… There are Everyday Problems to Solve
App-hopping
OS may kill app at random time
App components lifecycle is not under control
Components should not depend on each other
Can’t rely on data, stored in components
5. #gdg_kharkiv_center
Common Principles for staying in the mind :)
-Separation of concerns
-Provide solid user experience
-Keep UI lean and simple
-Keep UI free of app logic
-Drive UI from model
-Use persistent model
-Assign clear responsibilities for each model class
12. #gdg_kharkiv_center
Be together. not the same
“It is impossible to have one way of writing apps that will be the best for every
scenario. That being said, this recommended architecture should be a good
starting point for most use cases. If you already have a good way of writing
Android apps, you don't need to change.”
Quote from Guide to App Architecture
14. #gdg_kharkiv_center
Views = UI Controllers = LifecycleOwners
Purpose: Display data and pass on UI events
Neither contain the UI data, nor directly manipulate
data
Examples: Activity, Fragment
23. #gdg_kharkiv_center
public class DetailActivityViewModel extends ViewModel {
private MutableLiveData<WeatherEntry> mWeather;
public DetailActivityViewModel() {}
public MutableLiveData<WeatherEntry> getWeather() {
return mWeather;
}
public void setWeather(WeatherEntry weatherEntry) {
mWeather.postValue(weatherEntry);
}
}
Live Data
LiveData
24. #gdg_kharkiv_center
public class DetailActivity extends LifecycleActivity {
DetailActivityViewModel viewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
viewModel = ViewModelProviders.of(this).get(DetailActivityViewModel.class);
viewModel.getWeather().observe(this, weatherEntry -> {
if(weatherEntry!=null) { bindWeatherToUI(weatherEntry); }
});
}
Live Data observing
Observe
35. #gdg_kharkiv_center
//Room constructor
public WeatherEntry(int id, int weatherIconId, Date date, ...) {
//Json constructor - ignored by Room
@Ignore
public WeatherEntry(int weatherIconId, Date date,
// (!) Only one constructor should be exposed to Room
...
@Entity constructors
56. #gdg_kharkiv_center
September - October updates
October 5, 2017 - Beta 2
- improved LiveDataReactiveStreams,
- FullLifecycleObserver for Java 8,
- Handling @Query methods with @NonNull annotations,
- Still requires proguard settings to keep android.arch.lifecycle.GeneratedAdapter
1.0.0 Alpha 9-1
- Support Library 26.1.0, AppCompatActivity and Support Fragment now implement the
LifecycleOwner interface
- LifecycleActivity & LifecycleFragment are now deprecated
- New Library: Paging
58. #gdg_kharkiv_center
Paging Library - Annotations on DAO
@Query("select * from users WHERE age > :age order by name DESC, id ASC")
TiledDataSource<User> usersOlderThan(int age);
@Query("SELECT * from users order WHERE age > :age order by name DESC, id ASC")
public abstract LivePagedListProvider<Integer, User> usersOlderThan(int age);
63. #gdg_kharkiv_center
What to read :)
Guide to App Architecture https://developer.android.com/topic/libraries/architecture/guide.html
Architecture Components
https://developer.android.com/topic/libraries/architecture/index.html
I/O ‘17 Architecture Components Introduction - https://youtu.be/FrteWKKVyzI
Solving the Lifecycle Problem - https://youtu.be/bEKNi1JOrNs
Persistence and Offline - https://youtu.be/MfHsPGQ6bgE
Architecture Components on GDD Europe - https://youtu.be/Ts-uxYiBEQ8
GDD Europe CodeLabs g.co/codelabs/gdd17
Google Github samples https://github.com/googlesamples/android-architecture-components
64. #gdg_kharkiv_center
Alternatives to consider
Moxy https://github.com/Arello-Mobile/Moxy
Mosby https://github.com/sockeqwe/mosby
Android MVP Helper https://github.com/Ufkoku/AndroidMVPHelper
Clean Architecture https://github.com/android10/Android-CleanArchitecture
Reark https://github.com/reark/reark
MVP + Dagger2 + Rx https://android.jlelse.eu/mvp-dagger-2-rx-clean-modern-android-app-
code-74f63c9a6f2f
Architecture the Lost Years by Uncle Bob https://youtu.be/WpkDN78P884