Skip to content

The best MMKV utils to ensure type safety. (最好用的 MMKV 工具,可以确保类型安全)

License

Notifications You must be signed in to change notification settings

DylanCaiCoding/MMKV-KTX

Repository files navigation

MMKV-KTX

English | 中文

License

Combined with the features of Kotlin property delegation, it makes MMKV more flexible and easy to use.

Features

  • Automatic initialization of MMKV;
  • Use the property name as the key name, eliminating the need to declare a large number of key name constants;
  • Can ensure type safety and avoid exceptions caused by inconsistent types or key values;

Usage

📝 >> Usage Document <<

Get started

Add the following to the build.gradle file in the root directory:

allprojects {
    repositories {
        //...
        maven { url 'https://www.jitpack.io' }
    }
}

Add the dependency in the module's build.gradle file:

dependencies {
    implementation 'com.github.DylanCaiCoding:MMKV-KTX:1.2.16'
}

By having a class inherit from the MMKVOwner class, you can use the by mmkvXXXX() function to delegate properties to MMKV. For example:

object SettingsRepository : MMKVOwner(mmapID = "settings") {
  var isNightMode by mmkvBool()
  var language by mmkvString(default = "zh")
}

If you already have a parent class that cannot be inherited from, implement IMMKVOwner by MMKVOwner(mmapID), such as:

object SettingsRepository : BaseRepository(), IMMKVOwner by MMKVOwner(mmapID = "settings") {
  // ...
}

Make sure that each mmapID is unique to ensure type safety 100%!!!

Setting or getting the value of a property will call the corresponding encode() or decode() function with the property name as the key name. For example:

if (SettingsRepository.isNightMode) {
  // do some thing
}

SettingsRepository.isNightMode = true

Support the following types:

Function Default value
mmkvInt() 0
mmkvLong() 0L
mmkvBool() false
mmkvFloat() 0f
mmkvDouble() 0.0
mmkvString() /
mmkvStringSet() /
mmkvBytes() /
mmkvParcelable() /

Support using the mmkvXXXX().asLiveData() function to delegate the property to LiveData, such as:

object SettingRepository : MMKVOwner(mmapID = "settings") {
  val isNightMode by mmkvBool().asLiveData()
}

SettingRepository.isNightMode.observe(this) {
  checkBox.isChecked = it
}

SettingRepository.isNightMode.value = true

The kv object can be used to delete values or clear the cache, for example:

kv.removeValueForKey(::language.name) // Recommend removing the key after the default value is modified to shorten the assignment operation
kv.clearAll()

For more advanced usage, please refer to the Usage Document.

Update log

Releases

Other libraries created by the author

Library Brief Introduction
Longan Perhaps the most user-friendly Kotlin tool library
LoadingStateView Deep decoupling of the default page of the title bar or loading, loading failure, no data, etc.
ViewBindingKTX Most comprehensive ViewBinding tool
Tracker Lightweight burrowing framework based on the chain of responsibility burrowing idea of Buzzvideo

License

Copyright (C) 2021. Dylan Cai

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

The best MMKV utils to ensure type safety. (最好用的 MMKV 工具,可以确保类型安全)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages