<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Boris Ochieng</title>
    <description>The latest articles on Forem by Boris Ochieng (@slowburn404).</description>
    <link>https://forem.com/slowburn404</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1021153%2F3dd6bb0a-7088-443f-a1bd-d1456fb1106a.png</url>
      <title>Forem: Boris Ochieng</title>
      <link>https://forem.com/slowburn404</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/slowburn404"/>
    <language>en</language>
    <item>
      <title>Navigating Mobile Development Platforms and Some Common Architecture Patterns</title>
      <dc:creator>Boris Ochieng</dc:creator>
      <pubDate>Sat, 29 Jun 2024 15:48:24 +0000</pubDate>
      <link>https://forem.com/slowburn404/navigating-mobile-development-platforms-and-some-common-architecture-patterns-4p39</link>
      <guid>https://forem.com/slowburn404/navigating-mobile-development-platforms-and-some-common-architecture-patterns-4p39</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Over the years, computers have become increasingly smaller, leading to the widespread use of smartphones, which are among the smallest and most common computing devices. Smartphones run operating systems that support various applications, which need to be developed. This article discusses different mobile application development platforms, which are tools and environments used to create these applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mobile Development Platforms
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;h4&gt;
  
  
  Android
&lt;/h4&gt;

&lt;p&gt;Built by Google, Android is a mobile operating system based on a modified version of the Linux kernel and other open-source software, designed primarily for smartphones and tablets. Apps targeting Android are developed using various programming languages, including Java, a statically typed language used with XML views to render the UI. Kotlin, now the official programming language, is also statically typed and used with XML, though Jetpack Compose is the modern approach for UI development. C and C++ are used for high-performance applications like video games. Android Studio, combined with Gradle, is the primary IDE for building Android applications.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h5&gt;
  
  
  iOS
&lt;/h5&gt;

&lt;p&gt;Developed by Apple, iOS is used for iPhone and iPad devices. Apps for iOS are built using Swift, the main programming language, with SwiftUI and UIKit for UI development. Xcode is the primary IDE used for iOS development.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h5&gt;
  
  
  React Native
&lt;/h5&gt;

&lt;p&gt;React Native is an open-source UI framework based on the JavaScript library React, developed by Meta (formerly Facebook). It extends React principles to build native Android and iOS applications from a single codebase, making development cost-effective and efficient. The main programming languages used are JavaScript and TypeScript, with development tools including Visual Studio Code and Expo.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h5&gt;
  
  
  Flutter
&lt;/h5&gt;

&lt;p&gt;Created by Google, Flutter is an open-source UI software development kit for building cross-platform applications from a single codebase. It supports Android, iOS, web, and desktop applications. Dart is the main programming language, and development tools include Android Studio and Visual Studio Code.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h5&gt;
  
  
  Xamarin
&lt;/h5&gt;

&lt;p&gt;Developed by Microsoft, Xamarin is used for building cross-platform applications. The main programming languages are C# and the .NET framework. Development tools include Visual Studio Code and Xamarin Studio. Xamarin has been discontinued in favor of MAUI, which primarily uses the .NET framework for cross-platform development, with Visual Studio and Visual Studio Code as development platforms.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h5&gt;
  
  
  Ionic
&lt;/h5&gt;

&lt;p&gt;Ionic is an open-source UI toolkit for building cross-platform mobile, web, and desktop applications using various JavaScript frameworks and libraries such as React, Angular, and Vue. The main programming languages are HTML, CSS, JavaScript, and/or TypeScript, with Visual Studio Code as the development environment.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Architecture Patterns
&lt;/h3&gt;

&lt;p&gt;Architecture patterns generally split applications into loosely coupled layers, including aspects like data sources, state management, and data presentation. This separation of concerns helps create a more modular, testable, scalable, and maintainable codebase, improving the overall quality of the application.&lt;/p&gt;

&lt;h4&gt;
  
  
  Data Source
&lt;/h4&gt;

&lt;p&gt;Many apps handle data, which could involve reading or writing data from external services like REST APIs or locally on the device.&lt;/p&gt;

&lt;h5&gt;
  
  
  State Management
&lt;/h5&gt;

&lt;p&gt;State management involves handling the application's state, which describes the current status of the application at any given time. This can include user inputs, UI updates, data fetched from sources, or any information needed for the application to be responsive or dynamic.&lt;/p&gt;

&lt;h5&gt;
  
  
  Presentation
&lt;/h5&gt;

&lt;p&gt;The presentation layer is responsible for displaying the user interface (UI) and handling user interactions. It is often designed to be separate from the business logic and data layers to ensure a clear separation of concerns.&lt;/p&gt;

&lt;h4&gt;
  
  
  Model-View-ViewModel (MVVM)
&lt;/h4&gt;

&lt;p&gt;MVVM is an architecture pattern commonly used on Android and iOS with SwiftUI. It separates apps into three layers: Model (responsible for data management), View (the user interface), and ViewModel (which retrieves data from the Model and manages the state observed by the View).&lt;/p&gt;

&lt;h5&gt;
  
  
  Advantages
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Promotes clear separation between UI, data, business logic, and presentation logic.&lt;/li&gt;
&lt;li&gt;Easy to test, as business logic is independent of the View.&lt;/li&gt;
&lt;li&gt;Useful when multiple Views share the same business logic.&lt;/li&gt;
&lt;li&gt;Layers are separated, making code modifications easier.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Disadvantages
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Can introduce complexity in small applications.&lt;/li&gt;
&lt;li&gt;Not all development environments and frameworks support MVVM.&lt;/li&gt;
&lt;li&gt;Requires more setup, increasing initial development time with boilerplate code.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Model-View-Intent (MVI)
&lt;/h4&gt;

&lt;p&gt;MVI is mostly used in Android and focuses on unidirectional data flow. The Model represents the application's state. Each state change produces a new state rather than modifying the existing one. The View renders the UI based on the current state of the Model, and Intents are actions or events triggered by the user or system, processed by a central component called a "Reducer."&lt;/p&gt;

&lt;h5&gt;
  
  
  Advantages
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Unidirectional data flow simplifies debugging and reduces bugs.&lt;/li&gt;
&lt;li&gt;Immutability ensures that each state change creates a new state, avoiding issues with simultaneous state access/modification.&lt;/li&gt;
&lt;li&gt;Separation of concerns makes the application easier to test and maintain.&lt;/li&gt;
&lt;li&gt;The UI always reflects the current state consistently due to a single source of truth.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Disadvantages
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Can introduce unnecessary complexity in simple applications.&lt;/li&gt;
&lt;li&gt;Steep learning curve compared to other patterns.&lt;/li&gt;
&lt;li&gt;Potential performance issues due to the creation of new state objects.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Clean Architecture
&lt;/h4&gt;

&lt;p&gt;Clean Architecture splits the application into three layers: Presentation, Domain, and Data. The Presentation layer handles the UI and state management, the Domain layer contains entities or data models and Interactors or Use Cases that process data from the Data layer, and the Data layer is responsible for data fetching locally or remotely. It is commonly used in iOS and Android development.&lt;/p&gt;

&lt;h5&gt;
  
  
  Advantages
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Each layer has a clear responsibility, making the code "cleaner" and easier to understand and maintain.&lt;/li&gt;
&lt;li&gt;Easier to scale and maintain, designed for large projects.&lt;/li&gt;
&lt;li&gt;Simplifies testing as different layers can be tested independently.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Disadvantages
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;New developers may find it challenging to grasp its concepts compared to MVVM or MVI.&lt;/li&gt;
&lt;li&gt;Introduces complexity for small applications.&lt;/li&gt;
&lt;li&gt;Requires a lot of boilerplate code, increasing initial development time.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Redux
&lt;/h4&gt;

&lt;p&gt;Redux is popular among React Native developers and shares principles with MVI. It involves three main components: Store, Reducer, and Actions. The Store is the single source of truth for the application's state. Actions define changes to the state, and Reducers use the current state and action to create or return a new state, specifying how the state changes in response to an action.&lt;/p&gt;

&lt;h5&gt;
  
  
  Advantages
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Predictable state management due to immutability.&lt;/li&gt;
&lt;li&gt;A single store ensures consistency and simplifies state management.&lt;/li&gt;
&lt;li&gt;Middleware support provides powerful ways to handle asynchronous actions and logging.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Disadvantages
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Can lead to performance issues if implemented incorrectly in large applications.&lt;/li&gt;
&lt;li&gt;Introduces a considerable amount of boilerplate code.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Business Logic Component (BLoC)
&lt;/h4&gt;

&lt;p&gt;Used widely in Flutter, BLoC ensures a distinction between the UI layer and business logic. It involves three main components: Events (user interactions), State (the output of BLoC), and BLoC (where the business logic resides).&lt;/p&gt;

&lt;h5&gt;
  
  
  Advantages
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Highly maintainable with isolated business logic that can be modified without affecting the UI layers.&lt;/li&gt;
&lt;li&gt;Facilitates adding new features and scaling the application.&lt;/li&gt;
&lt;li&gt;Business logic components can be reused in different parts of the application.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Disadvantages
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Adds complexity to smaller projects.&lt;/li&gt;
&lt;li&gt;Has a steep learning curve for new developers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No development platform, tool, or architecture pattern is universally the best; the choice depends on project requirements.&lt;/p&gt;

&lt;p&gt;I would like to conclude by sharing my recent journey with the &lt;a href="https://hng.tech/internship"&gt;HNG Internship&lt;/a&gt;. As a new participant with a Bachelor's Degree in Information Technology and over 12 months of experience working on solo projects, I am eager to collaborate with a team and gain insight into team dynamics. This opportunity excites me not only for the chance to enhance my skills but also to connect with fellow developers at &lt;a href="https://hng.tech/hire"&gt;HNG&lt;/a&gt; and learn from their experiences. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Room Database Auto Migration</title>
      <dc:creator>Boris Ochieng</dc:creator>
      <pubDate>Tue, 19 Mar 2024 17:44:45 +0000</pubDate>
      <link>https://forem.com/slowburn404/room-database-auto-migration-15cb</link>
      <guid>https://forem.com/slowburn404/room-database-auto-migration-15cb</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Building offline-first Apps to enhance user experience during limited network connectivity is a common practice among developers. The Room Persistence Library, a part of the Android Architecture components, facilitates this by enabling local data storage. As the application scales, developers often encounter the need to fine-tune existing features or introduce new ones. When deploying these changes to end-users, it's crucial to preserve their existing on-device data. However, manual adjustments to the database schema can become cumbersome as the application evolves. This is where auto migration comes into play, automating the process and streamlining development workflows.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up auto migration
&lt;/h3&gt;

&lt;p&gt;To set up auto migration with Room, you'll first need to configure the Gradle Plugin to export the schema to a designated directory. In your project's top-level &lt;code&gt;build.gradle&lt;/code&gt; file (&lt;code&gt;/build.gradle.kts&lt;/code&gt;), declare the Room plugin and its version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;plugins&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="n"&gt;androidx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;room&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="mf"&gt;2.6.0&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt; &lt;span class="n"&gt;apply&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, in the app-level &lt;code&gt;build.gradle&lt;/code&gt; file (&lt;code&gt;/app/build.gradle.kts&lt;/code&gt;), apply the plugin and specify the directory where the schema will be exported:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;plugins&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="n"&gt;androidx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;room&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;room&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="n"&gt;schemaDirectory&lt;/span&gt; &lt;span class="s"&gt;"$projectDir/schemas"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ensure that the schema storage directory is located outside of your app directory. The &lt;code&gt;$projectDir&lt;/code&gt; variable references the root directory of your project, and &lt;code&gt;/schemas&lt;/code&gt; denotes the directory where the database file will be stored.&lt;/p&gt;

&lt;p&gt;Next, set the &lt;code&gt;exportSchema&lt;/code&gt; property to &lt;code&gt;true&lt;/code&gt; within the &lt;code&gt;@Database()&lt;/code&gt; annotation class. This step is essential as Room relies on the generated schema to execute migrations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Database&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; 
&lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SampleDatabaseClass&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LATEST_VERSION&lt;/span&gt;
&lt;span class="n"&gt;entities&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Entity&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;exportSchema&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SampleDatabaseClass&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RoomDatabase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;companion&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;LATEST_VERSION&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After completing these steps, rebuild your project to export the current database schema history into a JSON file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Performing auto migration
&lt;/h3&gt;

&lt;p&gt;To declare an automated migration between two database versions, specify the existing version and the new version. For instance, if the previous version was &lt;code&gt;1&lt;/code&gt;, and after making changes, the version is updated to &lt;code&gt;2&lt;/code&gt;, you can declare this migration in the database class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Database&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; 
&lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SampleDatabaseClass&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LATEST_VERSION&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;entities&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Entity&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="n"&gt;exportSchema&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;autoMigrations&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
&lt;span class="nc"&gt;AutoMigration&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SampleDatabaseClass&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RoomDatabase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;companion&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;LATEST_VERSION&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rebuild your project to let Room handle the migration seamlessly. If further changes are made in subsequent versions, such as migrating from version &lt;code&gt;2&lt;/code&gt; to &lt;code&gt;3&lt;/code&gt;, simply add the corresponding migration.&lt;/p&gt;

&lt;p&gt;When you move to version &lt;code&gt;3&lt;/code&gt; we will have to migrate from &lt;code&gt;2&lt;/code&gt; to &lt;code&gt;3&lt;/code&gt;. We will simply add this to the class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Database&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; 
&lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SampleDatabaseClass&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LATEST_VERSION&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;entities&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Entity&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="n"&gt;exportSchema&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;autoMigrations&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
&lt;span class="nc"&gt;AutoMigration&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="nc"&gt;AutoMigration&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SampleDatabaseClass&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RoomDatabase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;companion&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;LATEST_VERSION&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Auto Migration Specifications
&lt;/h3&gt;

&lt;p&gt;In some cases, Room may require additional information to perform migrations, particularly when changes involve deleting or renaming tables or columns. For such scenarios, you can use &lt;code&gt;AutoMigrationSpec&lt;/code&gt; to provide the necessary details.&lt;/p&gt;

&lt;p&gt;For example, if you need to rename a table, you can define an &lt;code&gt;AutoMigrationSpec&lt;/code&gt; class within your database class and annotate it accordingly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Database&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; 
&lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SampleDatabaseClass&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LATEST_VERSION&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;entities&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Entity&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="n"&gt;exportSchema&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;autoMigrations&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
&lt;span class="nc"&gt;AutoMigration&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
 &lt;span class="n"&gt;spec&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SampleDatabaseClass&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;YourAutoMigration&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;

&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SampleDatabaseClass&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RoomDatabase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;companion&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;LATEST_VERSION&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@RenameTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fromTableName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"old_name"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;toTableName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"new_name"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SampleAutoMigration&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;AutoMigrationSpec&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;The implementation of auto migration in Room significantly simplifies the process of managing database schema changes, enhancing the overall developer experience. By automating migration tasks, developers can focus more on building and refining app features without the burden of manual data management.&lt;/p&gt;

&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://medium.com/androiddevelopers/room-auto-migrations-d5370b0ca6eb"&gt;https://medium.com/androiddevelopers/room-auto-migrations-d5370b0ca6eb&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.android.com/training/data-storage/room/migrating-db-versions"&gt;https://developer.android.com/training/data-storage/room/migrating-db-versions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.android.com/reference/kotlin/androidx/room/AutoMigration"&gt;https://developer.android.com/reference/kotlin/androidx/room/AutoMigration&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  See it in action
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/slowburn-404/NoteWave"&gt;https://github.com/slowburn-404/NoteWave&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>room</category>
      <category>android</category>
      <category>androiddev</category>
    </item>
    <item>
      <title>View Binding in Android.</title>
      <dc:creator>Boris Ochieng</dc:creator>
      <pubDate>Sun, 05 Feb 2023 15:54:28 +0000</pubDate>
      <link>https://forem.com/slowburn404/how-to-implement-view-binding-in-android-45p9</link>
      <guid>https://forem.com/slowburn404/how-to-implement-view-binding-in-android-45p9</guid>
      <description>&lt;p&gt;On the 23rd of November 2020, Google &lt;a href="https://android-developers.googleblog.com/2020/11/the-future-of-kotlin-android-extensions.html" rel="noopener noreferrer"&gt;announced&lt;/a&gt; that Kotlin Synthetics had been deprecated where the &lt;code&gt;findViewById()&lt;/code&gt; method was used to find an existing view in your XML layout by it's &lt;code&gt;android:id&lt;/code&gt; attribute. Enter View Binding.&lt;/p&gt;

&lt;p&gt;If you're like me, you often have to go back to the &lt;a href="https://developer.android.com/topic/libraries/view-binding" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt; and read on how to integrate this into your project. I wrote this article as a quick reference for future me and probably you😉.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enable view binding.
&lt;/h2&gt;

&lt;p&gt;In your tool window bar, switch to Project View and under Gradle Scripts, in the app module, select the build.gradle file and set the &lt;code&gt;viewBinding&lt;/code&gt; to &lt;code&gt;true&lt;/code&gt; as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;android&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
    &lt;span class="nf"&gt;buildFeatures&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;viewBinding&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Behind the scenes, a binding class is generated for all the XML layout files. Each binding class holds references to the root view and all views that possess an ID. The name of the binding class comes from the name of the XML file. The name is changed to a special format called Pascal case and the word "Binding" is added to the end.&lt;br&gt;
Suppose you have a layout file named &lt;code&gt;activity_main.xml&lt;/code&gt;;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;RelativeLayout&lt;/span&gt; &lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;TextView&lt;/span&gt; &lt;span class="na"&gt;android:id=&lt;/span&gt;&lt;span class="s"&gt;"@+id/hello"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/RelativeLayout&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The generated class will be named &lt;code&gt;ActivityMainBinding&lt;/code&gt; with a &lt;code&gt;TextView&lt;/code&gt; named &lt;code&gt;hello&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use view binding in activities.
&lt;/h2&gt;

&lt;p&gt;In the &lt;code&gt;MainActivity.kt&lt;/code&gt; file, add the following code or adjust accordingly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;lateinit&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;binding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ActivityMainBinding&lt;/span&gt;

&lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onCreate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;savedInstanceState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bundle&lt;/span&gt;&lt;span class="p"&gt;?)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onCreate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;savedInstanceState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;binding&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ActivityMainBinding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;inflate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;layoutInflater&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;view&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;binding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;
    &lt;span class="nf"&gt;setContentView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can use the binding class to access any of the views now.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;//change the text to "Hello World"&lt;/span&gt;
&lt;span class="n"&gt;binding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello World"&lt;/span&gt;
&lt;span class="c1"&gt;//do something when clicked&lt;/span&gt;
&lt;span class="n"&gt;binding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setOnClickListener&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;//do something&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Use view binding in fragments.
&lt;/h2&gt;

&lt;p&gt;Suppose you have a fragment named &lt;code&gt;HelloWorld.kt&lt;/code&gt; with a layout file &lt;code&gt;fragment_hello_world.xml&lt;/code&gt;. Add the following code or adjust accordingly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;_binding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;FragmentHelloWorldBinding&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
&lt;span class="c1"&gt;// This property is only valid between onCreateView and&lt;/span&gt;
&lt;span class="c1"&gt;// onDestroyView.&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;binding&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_binding&lt;/span&gt;&lt;span class="o"&gt;!!&lt;/span&gt;

&lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onCreateView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;inflater&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LayoutInflater&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;container&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ViewGroup&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt;
    &lt;span class="n"&gt;savedInstanceState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bundle&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;View&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;_binding&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FragmentHelloWorldBinding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;inflate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inflater&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;container&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;view&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;binding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onDestroyView&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onDestroyView&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;_binding&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's just about it, you can get an in depth look in the &lt;a href="https://developer.android.com/topic/libraries/view-binding" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt; and a practical tutorial &lt;a href="https://www.youtube.com/watch?v=z0F2QTAKsWU" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
