Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

forLoopUyo Maiden Edition New Android Architecture Component with MVVM

Slide 3

Slide 3 text

+Nsikak Thompson Lead Android Developer @StartHubTech Passionate about good UI designs @thompson_nsikak NsikakTopdown

Slide 4

Slide 4 text

Scope ● Introduction ● Room Library ● LiveData ● LifeCycle ● ViewModel

Slide 5

Slide 5 text

What Troubles Us MVP MVC MVVM Clean

Slide 6

Slide 6 text

Iyamiiii !!!

Slide 7

Slide 7 text

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.

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Adding architecture components to your Android App

Slide 11

Slide 11 text

Demo App

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

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"

Slide 14

Slide 14 text

Database Libraries for Android ROOM Our Focus

Slide 15

Slide 15 text

Room

Slide 16

Slide 16 text

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.

Slide 17

Slide 17 text

Getting Started with Room

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

Create Data Access Object (DAO) @Dao public interface TaskDao { @Query("SELECT * FROM " + Task.TABLE_NAME ) LiveData> getAllTask(); @Insert(onConflict = REPLACE) void addTask(Task task); @Delete void deleteTask(Task task); @Update(onConflict = REPLACE) void updateTask(Task task);

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

LiveData

Slide 22

Slide 22 text

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.

Slide 23

Slide 23 text

Get LiveData and Observe @Dao public interface TaskDao { @Query("SELECT * FROM " + Task.TABLE_NAME ) LiveData> getAllTask(); taskDao.getTasks().observe(this, tasks ->{ //Update UI taskAdapter.setItems(tasks); recyclerView.setAdapter(taskAdapter); });

Slide 24

Slide 24 text

ViewModel

Slide 25

Slide 25 text

ViewModel - It is part of the MVVM architecture - stores and manage UI-related data - survives configuration changes such as screen rotations

Slide 26

Slide 26 text

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.

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

Questions??? goo.gl/slides/vxgqgb Github : https://goo.gl/TiX7tQ

Slide 30

Slide 30 text

Thank You @thompson_nsikak NsikakTopdown