{"id":21239252,"url":"https://github.com/tinacious/simpledatabase","last_synced_at":"2025-06-24T07:34:42.006Z","repository":{"id":148671192,"uuid":"265154963","full_name":"tinacious/simpledatabase","owner":"tinacious","description":"A simple class for Android written in Kotlin that allows you to use the Android SharedPreferences as a database for primitive and Gson-serializable types","archived":false,"fork":false,"pushed_at":"2022-12-28T18:04:47.000Z","size":130,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-15T03:41:13.639Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tinacious.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-19T05:35:19.000Z","updated_at":"2022-12-28T19:05:51.000Z","dependencies_parsed_at":"2023-05-20T19:45:44.700Z","dependency_job_id":null,"html_url":"https://github.com/tinacious/simpledatabase","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/tinacious/simpledatabase","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinacious%2Fsimpledatabase","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinacious%2Fsimpledatabase/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinacious%2Fsimpledatabase/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinacious%2Fsimpledatabase/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tinacious","download_url":"https://codeload.github.com/tinacious/simpledatabase/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinacious%2Fsimpledatabase/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261629480,"owners_count":23187049,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-21T00:42:36.102Z","updated_at":"2025-06-24T07:34:36.993Z","avatar_url":"https://github.com/tinacious.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SimpleDatabase\n\n[![](https://jitpack.io/v/tinacious/simpledatabase.svg)](https://jitpack.io/#tinacious/simpledatabase)\n\n\nA simple database class for Android written in Kotlin.\n\nSimpleDatabase allows you to store the following data types in the Android SharedPreferences:\n\n- `String`\n- `Boolean`\n- `Int`\n- `ArrayList\u003cString\u003e`\n- any `Gson`-serializable and deserializable class using `getObject\u003cT\u003e()` and `putObject\u003cT\u003e()`\n- a list of any `Gson`-serializable and deserializable class using `getListObject\u003cT\u003e()` and `putListObject\u003cT\u003e()`\n\nThe project internally has a dependency on [Gson](https://github.com/google/gson).\n\n\n## Installation\n\nYou can add the library to your project using Jitpack.\n\nFirst, add Jitpack to your root `build.gradle` if it isn't there already:\n\n```groovy\nallprojects {\n    repositories {\n        maven { url 'https://jitpack.io' }\n    }\n}\n\n```\n\nThen add SimpleDatabase to your app's `build.gradle`:\n\n```groovy\ndependencies {\n    implementation 'com.github.tinacious:simpledatabase:1.1.0'\n}\n```\n\nReplace `1.1.0` with the latest version.\n\n\n## Usage\n\n### Basic Implementation\n\nYou can pass any instance of `android.content.SharedPreferences` to the `SimpleDatabase` class.\n\nHere is an example where we get an instance of a private `SharedPreferences`:\n\n```kotlin\nval sharedPreferences = context\n    .getSharedPreferences(\n        \"${context.packageName}_preferences\",\n        Context.MODE_PRIVATE\n    )\n\nval database = SimpleDatabase(sharedPreferences)\n```\n\n\n### KeyValueStorage\n\nBelow is a suggested approach to using it. We create a class `KeyValueStorage` that implements `SimpleDatabase`:\n\n```kotlin\nimport android.content.Context\nimport com.tinaciousdesign.simpledatabase.SimpleDatabase\nimport com.tinaciousdesign.simpledatabase.SimpleDatabaseImpl\n\n\n/**\n * Simple key value storage using SimpleDatabase\n */\nclass KeyValueStorage private constructor(private val delegate: SimpleDatabase) :\n    SimpleDatabase by delegate {\n    companion object {\n        @JvmStatic\n        fun getInstance(context: Context): KeyValueStorage {\n            val sharedPreferences = context.getSharedPreferences(\n                \"${context.packageName}_preferences\",\n                Context.MODE_PRIVATE\n            )\n            val simpleDatabase = SimpleDatabaseImpl(sharedPreferences)\n\n            return KeyValueStorage(simpleDatabase)\n        }\n    }\n}\n```\n\nThen we can access this instance by calling `val keyValueStorage = KeyValueStorage.getInstance(context)`.\n\nThe sample project in `app` has an example implementation that uses the [dependencies container pattern](https://developer.android.com/training/dependency-injection/manual#dependencies-container) for dependency injection.\n\n\n#### Usage with Koin\n\nYou can create the above-mentioned class and provide it with Koin in one of your modules:\n\n```kotlin\nval servicesModule = module {\n    single { KeyValueStorage.getInstance(androidContext()) }\n}\n```\n\nThen you can use it in any Koin component as follows:\n\n```kotlin\nclass MyActivity : AppCompatActivity() {\n    private val keyValueStorage by inject\u003cKeyValueStorage\u003e()\n}\n```\n\n\n## Inspiration\n\nI was inspired by [TinyDB](https://github.com/kcochibili/TinyDB--Android-Shared-Preferences-Turbo) to make a simpler Kotlin version.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftinacious%2Fsimpledatabase","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftinacious%2Fsimpledatabase","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftinacious%2Fsimpledatabase/lists"}