Android Database

This package provides an extensible interface for creating and managing databases in Android apps. This includes both local and remote databases.

By default, the package provides implementation for a Firebase Realtime Database, and a local SharedPreferences-based database. More database implementations can be added by extending the LocalDatabase and RemoteDatabase classes.

Installation

The compiled package is only available internally via GitHubPackages at the moment, and can be added to your project dependencies (only if you have internal access) as follows:

dependencies {
    implementation 'io.github.saifkhichi96:android-database:1.0.0'
}

In the future, the package would be made publicly available via Maven Central.

For now, please clone the repository as a submodule of your project as follows:

git submodule add https://github.com/saifkhichi96/android-database database

This will import the package as a local module in your project.

Then add the following to your build.gradle file:

dependencies {
    implementation project(path: ':database')
}

Usage

The package can be used with Hilt to automatically inject the database dependencies where required. To achieve this, create the following singleton class in your app:

@Module
@InstallIn(ViewModelComponent::class) // ApplicationComponent::class, FragmentComponent::class, etc.
object DatabaseModule {
    @Provides
    fun provideLocalDatabase(context: Context): LocalDatabase {
        return PrefsDatabase(context)  // return the implementation you want to use
    }

    @Provides
    fun provideRemoteDatabase(context: Context): RemoteDatabase {
        return FirebaseDatabase(context)  // return the implementation you want to use
    }
}

Then in your ViewModel, for example, use the @Inject annotation to inject the database dependencies:

@Inject
lateinit var localDatabase: LocalDatabase

@Inject
lateinit var remoteDatabase: RemoteDatabase

It is also possible to use the package without Hilt, in which case you can simply initialize the databases yourself where required.

To get started with the package, see the LocalDatabase and RemoteDatabase classes, or read the API documentation.