Git Product home page Git Product logo

mmkv-ktx's Introduction

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.

mmkv-ktx's People

Contributors

dylancaicoding avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.