{"id":13428772,"url":"https://github.com/chibatching/Kotpref","last_synced_at":"2025-03-16T01:33:37.759Z","repository":{"id":35800254,"uuid":"40081857","full_name":"chibatching/Kotpref","owner":"chibatching","description":"Kotpref - Android SharedPreferences delegation library for Kotlin","archived":false,"fork":false,"pushed_at":"2022-08-17T06:54:12.000Z","size":2154,"stargazers_count":697,"open_issues_count":6,"forks_count":50,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-10-27T06:39:04.744Z","etag":null,"topics":["android","kotlin","sharedpreferences"],"latest_commit_sha":null,"homepage":"https://chibatching.github.io/Kotpref/","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chibatching.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-08-02T14:27:33.000Z","updated_at":"2024-10-26T00:42:47.000Z","dependencies_parsed_at":"2022-08-18T22:23:27.668Z","dependency_job_id":null,"html_url":"https://github.com/chibatching/Kotpref","commit_stats":null,"previous_names":[],"tags_count":42,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chibatching%2FKotpref","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chibatching%2FKotpref/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chibatching%2FKotpref/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chibatching%2FKotpref/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chibatching","download_url":"https://codeload.github.com/chibatching/Kotpref/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243814905,"owners_count":20352037,"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":["android","kotlin","sharedpreferences"],"created_at":"2024-07-31T01:01:04.776Z","updated_at":"2025-03-16T01:33:36.518Z","avatar_url":"https://github.com/chibatching.png","language":"Kotlin","funding_links":[],"categories":["Libraries","开源库","开源库和框架","\u003ca name=\"utility\"\u003e\u003c/a\u003eUtility \u003csup\u003e[Back ⇈](#contents)\u003c/sup\u003e"],"sub_categories":["代理","Android 开发","Android"],"readme":"# Kotpref\n\nAndroid SharedPreference delegation for Kotlin.\n\n[![kotlin](https://img.shields.io/badge/kotlin-1.4.10-blue.svg)]() [![codecov](https://codecov.io/gh/chibatching/Kotpref/branch/master/graph/badge.svg)](https://codecov.io/gh/chibatching/Kotpref) [![license](https://img.shields.io/github/license/chibatching/Kotpref.svg?maxAge=2592000)]()\n\n## Install\n\n```groovy\nrepositories {\n    mavenCentral()\n}\n\ndependencies {\n    // core\n    implementation 'com.chibatching.kotpref:kotpref:2.13.1'\n  \n    // optional, auto initialization module\n    implementation 'com.chibatching.kotpref:initializer:2.13.1'\n  \n    // optional, support saving enum value and ordinal\n    implementation 'com.chibatching.kotpref:enum-support:2.13.1'\n  \n    // optional, support saving json string through Gson\n    implementation 'com.chibatching.kotpref:gson-support:2.13.1'\n    implementation 'com.google.code.gson:gson:2.8.6'\n  \n    // optional, support LiveData observable preference\n    implementation 'com.chibatching.kotpref:livedata-support:2.13.1'\n    implementation 'androidx.lifecycle:lifecycle-livedata:2.2.0'\n\n    // experimental, preference screen build dsl\n    implementation 'com.chibatching.kotpref:preference-screen-dsl:2.13.1'\n}\n```\n\n## How to use\n\n### Declare preference model\n\n```kotlin\nobject UserInfo : KotprefModel() {\n    var name by stringPref()\n    var code by nullableStringPref()\n    var age by intPref(default = 14)\n    var highScore by longPref()\n    var rate by floatPref()\n    val prizes by stringSetPref {\n        val set = TreeSet\u003cString\u003e()\n        set.add(\"Beginner\")\n        return@stringSetPref set\n    }\n}\n\nenum class GameLevel {\n    EASY,\n    NORMAL,\n    HARD\n}\n```\n\n### Set up\n\nPass the application context to Kotpref\n\n```kotlin\nKotpref.init(context)\n```\n\nor use auto initializer module.\n\n### Injectable Context\n\nIf you don't want to use singleton context because of unit test or etc.., you can use secondary \nconstructor of KotprefModel to inject context.  \n\n```kotlin\nclass InjectableContextSamplePref(context: Context) : KotprefModel(context) {\n    var sampleData by stringPref()\n}\n```\n\nIf you set context to all your model, you don't need call `Kotpref.init(context)` and don't use auto initializer module.\n\n### Read and Write\n\n```kotlin\nUserInfo.gameLevel = GameLevel.HARD\nUserInfo.name = \"chibatching\"\nUserInfo.code = \"DAEF2599-7FC9-49C5-9A11-3C12B14A6898\"\nUserInfo.age = 30\nUserInfo.highScore = 49219902\nUserInfo.rate = 49.21F\nUserInfo.prizes.add(\"Bronze\")\nUserInfo.prizes.add(\"Silver\")\nUserInfo.prizes.add(\"Gold\")\n\nLog.d(TAG, \"Game level: ${UserInfo.gameLevel}\")\nLog.d(TAG, \"User name: ${UserInfo.name}\")\nLog.d(TAG, \"User code: ${UserInfo.code}\")\nLog.d(TAG, \"User age: ${UserInfo.age}\")\nLog.d(TAG, \"User high score: ${UserInfo.highScore}\")\nLog.d(TAG, \"User rate: ${UserInfo.rate}\")\nUserInfo.prizes.forEachIndexed { i, s -\u003e Log.d(TAG, \"prize[$i]: $s\") }\n```\n\n### Bulk edit\n\n```kotlin\nUserInfo.bulk {\n    gameLevel = GameLevel.EASY\n    name = \"chibatching Jr\"\n    code = \"451B65F6-EF95-4C2C-AE76-D34535F51B3B\"\n    age = 2\n    highScore = 3901\n    rate = 0.4F\n    prizes.clear()\n    prizes.add(\"New Born\")\n}\n\n// Bulk edit uses Editor#apply() method internally.\n// If you want to apply immediately, you can use blockingBulk instead.\nUserInfo.blockingBulk {\n    gameLevel = GameLevel.EASY\n}\n```\n\n### Result shared preference xml\n\nXML file name equals model class name. If model class named `UserInfo`, XML file name is `UserInfo.xml`.\n\n```xml\n\u003c?xml version='1.0' encoding='utf-8' standalone='yes' ?\u003e\n\u003cmap\u003e\n    \u003clong name=\"highScore\" value=\"49219902\" /\u003e\n    \u003cset name=\"prizes\"\u003e\n        \u003cstring\u003eBeginner\u003c/string\u003e\n        \u003cstring\u003eBronze\u003c/string\u003e\n        \u003cstring\u003eGold\u003c/string\u003e\n        \u003cstring\u003eSilver\u003c/string\u003e\n    \u003c/set\u003e\n    \u003cstring name=\"name\"\u003echibatching\u003c/string\u003e\n    \u003cstring name=\"code\"\u003eDAEF2599-7FC9-49C5-9A11-3C12B14A6898\u003c/string\u003e\n    \u003cint name=\"age\" value=\"30\" /\u003e\n    \u003cfloat name=\"rate\" value=\"49.21\" /\u003e\n\u003c/map\u003e\n```\n\n### Options\n\n#### Change default value\n\n```kotlin\nvar age: Int by intPref(18)\n```\n\nor\n\n```kotlin\nvar age: Int by intPref(default = 18)\n```\n\n#### Change preference key\n\nYou can custom preference key or use from string resources.\n\n```kotlin\nvar useFunc1: Boolean by booleanPref(key = \"use_func1\")\nvar mode: Int by intPref(default = 1, key = R.string.pref_mode)\n```\n\n#### Change default save mode\n\nKotpref save all preference property by `apply` method.\nYou can change method to `commit` for each property.\n\n```kotlin\nvar age: Int by intPref(default = 18, commitByDefault = true)\n```\n\nOr change default for each KotprefModel.\n\n```kotpref\nobject UserInfo : KotprefModel() {\n    override val commitAllPropertiesByDefault: Boolean = true\n```\n\n#### Change XML file name\n\nOverride `kotprefName` property.\n\n```kotlin\nobject UserInfo : KotprefModel() {\n    override val kotprefName: String = \"user_info\"\n```\n\n#### Change SharedPreference mode\n\nOverride `kotprefMode` property. Default is `Context.MODE_PRIVATE`.\n\n```kotlin\nobject UserInfo : KotprefModel() {\n    override val kotprefMode: Int = Context.MODE_MULTI_PROCESS\n```\n\n## API Docs\n\n[https://chibatching.github.io/Kotpref/docs/api/-modules.html](https://chibatching.github.io/Kotpref/docs/api/-modules.html)\n\n## License\n\n```\nCopyright 2015-2021 Takao Chiba\n\n   Licensed under the Apache License, Version 2.0 (the \"License\");\n   you may not use this file except in compliance with the License.\n   You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n   Unless required by applicable law or agreed to in writing, software\n   distributed under the License is distributed on an \"AS IS\" BASIS,\n   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n   See the License for the specific language governing permissions and\n   limitations under the License.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchibatching%2FKotpref","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchibatching%2FKotpref","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchibatching%2FKotpref/lists"}