Skip to content

KotlinFarsi/kf01-BasicSample

Repository files navigation

BasicSample

This project is based on this Sample From google. This Tutorial is provided by this Sample.

This sample showcases the following Architecture Components:

Introduction

Features

This sample contains two screens: a list of products and a detail view, that shows product reviews.

Presentation layer

The presentation layer consists of the following components:

  • A main activity that handles navigation.
  • A fragment to display the list of products.
  • A fragment to display a product review.

The app uses a Model-View-ViewModel (MVVM) architecture for the presentation layer. Each of the fragments corresponds to a MVVM View. The View and ViewModel communicate using LiveData and the following design principles:

  • ViewModel objects don't have references to activities, fragments, or Android views. That would cause leaks on configuration changes, such as a screen rotation, because the system retains a ViewModel across the entire lifecycle of the corresponding view.

ViewModel Diagram

  • ViewModel objects expose data using LiveData objects. LiveData allows you to observe changes to data across multiple components of your app without creating explicit and rigid dependency paths between them.

  • Views, including the fragments used in this sample, subscribe to corresponding LiveData objects. Because LiveData is lifecycle-aware, it doesn’t push changes to the underlying data if the observer is not in an active state, and this helps to avoid many common bugs. This is an example of a subscription:

        // Update the list of products when the underlying data changes.
        viewModel.getProducts().observe(this, new Observer<List<ProductEntity>>() {
            @Override
            public void onChanged(@Nullable List<ProductEntity> myProducts) {
                if (myProducts != null) {
                    mBinding.setIsLoading(false);
                    mProductAdapter.setProductList(myProducts);
                } else {
                    mBinding.setIsLoading(true);
                }
            }
        });

Data layer

The database is created using Room and it has two entities: a ProductEntity and a CommentEntity that generate corresponding SQLite tables at runtime.

Room populates the database asynchronously when it's created, via the RoomDatabase#Callback. To simulate low-performance, an artificial delay is added. To let other components know when the data has finished populating, the AppDatabase exposes a LiveData object..

To access the data and execute queries, you use a Data Access Object (DAO). For example, a product is loaded with the following query:

    @Query("select * from products where id = :productId")
    LiveData<ProductEntity> loadProduct(int productId);

Queries that return a LiveData object can be observed, so when a change in one of the affected tables is detected, LiveData delivers a notification of that change to the registered observers.

The DataRepository exposes the data to the UI layer. To ensure that the UI uses the list of products only after the database has been pre-populated, a MediatorLiveData object is used. This observes the changes of the list of products and only forwards it when the database is ready to be used.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages