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

New Android Architecture Components with MVVM

New Android Architecture Components with MVVM

This presentation highlights the new architecture libraries released by the Google during this year Google IO. It was presented during forLoopUyo maiden Edition.

Nsikak Thompson

August 05, 2017
Tweet

More Decks by Nsikak Thompson

Other Decks in Programming

Transcript

  1. <New> Android Architecture Component collection of libraries that help you

    design robust, Android Architecture Component collection of libraries that help you design robust, testable, and maintainable apps.
  2. Core Components of the Architecture Libraries - Room : An

    abstraction layer over SQLite which provided object mapping similar to OrmLite. - LiveData: data holder class that keeps a value and allows this value to be observed. - - LifeCycle: it contains information about the lifecycle state of a component such as Activities and fragments - - ViewModel: store and manage UI-related data so that the data survives configuration changes such as screen rotations..
  3. Why Use this architecture components? - This framework aims to

    reduce the amount of boilerplate and repetitive code - Helps you focus on your business Logic - Easy to Test
  4. Adding architecture components to your Android App //For LifeCycle, ViewModel

    and LiveData compile "android.arch.lifecycle:runtime:1.0.0-alpha5" compile "android.arch.lifecycle:extensions:1.0.0-alpha5" annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha5" //For Room compile "android.arch.persistence.room:runtime:1.0.0-alpha5" annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha5"
  5. What is Room? Room - An abstraction layer over SQLite

    which provided object mapping similar to OrmLite. - The core framework provides built-in support for working with raw SQL content.
  6. Create Entity @Entity(tableName = TABLE_NAME) public class Task { public

    static final String TABLE_NAME = "task"; public static final String TITLE = "title"; public static final String DESCRIPTION = "description"; @PrimaryKey(autoGenerate = true) private int id; @ColumnInfo(name = TITLE) private String title; @ColumnInfo(name = DESCRIPTION) private String description; //getter and setters
  7. Create Data Access Object (DAO) @Dao public interface TaskDao {

    @Query("SELECT * FROM " + Task.TABLE_NAME ) LiveData<List<Task>> getAllTask(); @Insert(onConflict = REPLACE) void addTask(Task task); @Delete void deleteTask(Task task); @Update(onConflict = REPLACE) void updateTask(Task task);
  8. Finally Create the Database @Database(entities = {Task.class}, version = 1)

    public abstract class TaskDatabase extends RoomDatabase { public abstract TaskDao taskDao(); } Room.databaseBuilder(context.getApplicationContext(), TaskDatabase.class, "task_db").build();
  9. What is LiveData? - LiveData allows you to observe changes

    to data across multiple components of your app without creating explicit, and rigid dependency paths between them. - data holder class that keeps a value and allows this value to be observed.
  10. Get LiveData and Observe @Dao public interface TaskDao { @Query("SELECT

    * FROM " + Task.TABLE_NAME ) LiveData<List<Task>> getAllTask(); taskDao.getTasks().observe(this, tasks ->{ //Update UI taskAdapter.setItems(tasks); recyclerView.setAdapter(taskAdapter); });
  11. ViewModel - It is part of the MVVM architecture -

    stores and manage UI-related data - survives configuration changes such as screen rotations
  12. The lifecycle of a ViewModel - The ViewModel stays in

    memory until the LifeCycle it’s scoped to goes away permanently - retain data across configuration changes - ViewModel outlives the specific Activity or Fragment instances.
  13. Returning and Observing your ViewModel @Override public View onCreateView(LayoutInflater inflater,

    ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_task_list, container, false); //Get our VM taskListViewModel = ViewModelProviders.of(this, newTaskFactory(appController)).get(TaskListViewModel.class); //Observing taskListViewModel.getTasks().observe(this, tasks ->{ taskAdapter.setItems(tasks); recyclerView.setAdapter(taskAdapter); });
  14. Summary • Room – A SQLite object mapper. Very similar

    to other libraries such as ORMlite or greenDAO. It uses SQL while still allowing compile time guarantees on the queries. • LiveData – A Lifecycle aware observable core component. • ViewModel – The communication points with the rest of the application for Activities / Fragments. They are UI code free and outlive the activity or fragment. • Lifecycle – A core part of the Architecture components, it contains information about the lifecycle state of a component (for instance an Activity).