Posts

Showing posts with the label Room

App data and files in Android

Image
Save data using SQLite Saving data to a database is ideal for repeating or structured data, such as contact information. Define a schema and contact Schema is a formal declaration of how the database is organized.  You should create a companion class, known as a contract class , which explicitly specifies the layout of your schema in a systematic and self-documenting way. A contract class is a container for constants that define names for URIs, tables and columns. The contract class allows you to use the same constants across all the other classes in the same package. A good way to organize a contract class is to put definitions that are global to your whole database in the root level of the class. Then create an inner class for each table. Each inner class enumerates the corresponding table's columns. For complete implementation please click the header or check the e.g. public class DeviceContract { private DeviceContract () { } public static class DeviceEntry impl...

Offline Caching in Android Studio

Image
Overview Hello World Code plugins { id 'com.android.application' id 'kotlin-android' id 'kotlin-kapt' id 'dagger.hilt.android.plugin' } // Dagger Hilt def hilt_version = "2.38.1" implementation "com.google.dagger:hilt-android: $hilt_version " kapt "com.google.dagger:hilt-compiler: $hilt_version " // Retrofit def retrofit_version = "2.9.0" implementation "com.squareup.retrofit2:retrofit: $retrofit_version " implementation "com.squareup.retrofit2:converter-gson: $retrofit_version " implementation "com.squareup.okhttp3:logging-interceptor:4.9.1" // Coroutines def coroutines_version = "1.6.0" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core: $coroutines_version " implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android: $coroutines_version " // ViewModel and LiveData def lifecycle_version = "2.4.0...

Room Database in Android

Image
Overview Room database is a persistence library of Android Jetpack. It is used to deal with database operations efficiently. It provides and abstraction layer over SQLite. Steps to follow Dependencies def room_version = "2.4.0" implementation "androidx.room:room-runtime: $room_version " kapt "androidx.room:room-compiler: $room_version " implementation "androidx.room:room-ktx: $room_version " annotationProcessor "androidx.room:room-compiler: $room_version " def coroutine_version = "1.6.0" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core: $coroutine_version " implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android: $coroutine_version " def lifecycle_version = "2.4.0" implementation "androidx.lifecycle:lifecycle-livedata-ktx: $lifecycle_version " implementation "androidx.lifecycle:lifecycle-viewmodel-ktx: $lifecycle_version " Updated Dependencies ...

MVVM Design Pattern in Android

Image
Overview MVVM is a Model-View-ViewModel design pattern architecture that removes the tight coupling between every application components. In the below image, you can clearly see that every children components don't have a reference to the parent components. They have a reference to an observable though which they can communicate with the parent components. Components of MVVM Model It is the data and business logic of an Android application. It's a data class where you can define the properties and methods too. View It is the user interface of an Android application, activities and fragments. These views send the user requests to ViewModel and to get the response, the application components has to subscribe to the observables which the ViewModel has exposed to it. ViewModel It is a bridge between the View and the Model in MVVM design pattern. ViewModel isn't aware which view is interacting with it. ViewModel only interacts with the Model and exposes the observable that can b...