3 - Mobile Architecture Components

Learning Outcomes

  • Understand the role of architecture components in robust app development.

  • Learn how to integrate architecture components for practical and usable scenarios.


Contents & Structure

  • Avoid Short-Term Hacks:

    • Risks from tight deadlines affecting app design and structure.

    • Examples of short-term hacks:

      • Tailoring apps for specific devices.

      • Copy-pasting code without proper checks.

      • Placing business logic in UI files.

      • Hardcoding user-facing strings in the code.

    • Importance of balancing timely delivery and long-term maintainability.


Why You Need Good App Architecture

  • Good architecture:

    • Defines clear roles for business logic placement.

    • Simplifies developer collaboration.

    • Makes code easier to test.

    • Reduces technical debt and saves time during app extension.

    • Allows reuse of already-solved problems.


Separation of Concerns

  • Role of Architecture Design Patterns:

    • Provides a loose template for structuring apps.

    • MVVM pattern supports:

      • Robust, testable, and maintainable apps.


Architecture Diagram - ViewModel

  • Key Features of ViewModel:

    • Prepares data for UI without referencing UI components.

    • Scoped to lifecycle-aware screens/widgets.

    • Data persists through configuration changes.

    • Survives as long as the screen/widget remains active.


Flutter and Floor Database Overview

  • Flutter Specifics:

    • Uses Widgets for UI.

    • Adopts Floor instead of Room for SQLite mapping.

  • Floor Features:

    • Robust SQL object mapping library.

    • Generates SQLite code for Android.

    • Simple API for database access.


Components of Floor

  • Entity: Defines database schema (e.g., table structure).

  • DAO: Accesses and manipulates data (read/write).

  • Database: Connects to and manages the schema.


Entity Example

  • Define database entities as POJO (Plain Old Java Objects) classes.

  • Example for defining a database entity in Java:

    
    @Entity
    public class Person {
     
    @PrimaryKey (autoGenerate=true)
     
    private int uid;
     
    @ColumnInfo(name = "first_name")
     
    private String firstName;
     
    @ColumnInfo(name = "last_name")
     
    private String lastName;
     
    // + getters and setters if variables are private.
    public String getLastName() { return lastName; }

    public void setLastName(String lastName) { this.lastName = lastName; }


DAO Example

  • DAO (Data Access Object):

    • Handles database queries and updates.

    • Example methods:

      @Dao
      public interface WordDao {
       @Insert
       
      void insert(Word word);
       @Update
       
      public void updateWords(Word...
      words);
      


Floor Database


• Floor works with DAO andEntities

• Entities define the databaseschema
• DAO provides methods toaccess database

  • Steps to create a Floor database:

    • Define an abstract class extending FloorDatabase.

    • Annotate with @Database.

    • Declare entities and version number.

    • Example:

      
      @Database(entities = {Word.class}, version
      = 1)
      public abstract class WordFloorDatabase
       
      extends FloorDatabase
      {
       
      public abstract WordDao wordDao();
       
      private static WordFloorDatabase
      INSTANCE;
       
      // ... create instance here
      


Floor Caveats

  • Compile-time checks for SQLite statements.

  • Avoid database operations on the main thread.

  • Use LiveData for automatic background queries.

  • Singleton pattern recommended for database management.


ViewModel Role

  • ViewModel:

    • Retrieves data from Floor or other sources.

    • Serves as the interface between app UI and data sources.

    • Avoids directly managing or generating data.


Repository Best Practices

  • Repository:

    • Acts as an intermediary to fetch and manage data.

    • Keeps ViewModel clean and focused.

    • Abstracts data sources for better modularity.

  • Analogy:

    • UI ViewModel Repository Data Sources.


LiveData Overview

  • LiveData:

    • Lifecycle-aware observable data holder.

    • Automatically updates the UI when data changes.

    • Adaptable to configuration changes like device rotations.


Passing LiveData Through Layers

  • Data flow across layers (DAO, Repository, ViewModel) must use LiveData.

    • Example:

      • DAO: @Query("SELECT * FROM word_table") LiveData<List<Word>> getAllWords();

      • Repository: LiveData<List<Word>> mAllWords = mWordDao.getAllWords();

      • ViewModel: LiveData<List<Word>> mAllWords = mRepository.getAllWords();