Skip to content

Commit

Permalink
Prepare for release 1.2.16
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanCaiCoding committed May 2, 2023
1 parent b3d9adc commit d5aecca
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 91 deletions.
108 changes: 66 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,96 +2,120 @@

English | [中文](README_ZH.md)

[![](https://www.jitpack.io/v/DylanCaiCoding/MMKV-KTX.svg)](https://www.jitpack.io/#DylanCaiCoding/MMKV-KTX)
[![](https://www.jitpack.io/v/DylanCaiCoding/MMKV-KTX.svg)](https://www.jitpack.io/#DylanCaiCoding/MMKV-KTX)
[![License](https://img.shields.io/badge/License-Apache--2.0-blue.svg)](https://github.com/DylanCaiCoding/MMKV-KTX/blob/master/LICENSE)

It's easier to use the [MMKV](https://github.com/Tencent/MMKV) without initializing the MMKV and defining the Key value.
Combined with the features of Kotlin property delegation, it makes [MMKV](https://github.com/Tencent/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

:pencil: **[>> Usage documentation <<](https://dylancaicoding.github.io/MMKV-KTX)**
:pencil: **[>> Usage Document <<](https://dylancaicoding.github.io/MMKV-KTX)**

## Quick start
## Get started

Add it in your root `build.gradle` at the end of repositories:
Add the following to the `build.gradle` file in the root directory:

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

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

```groovy
dependencies {
implementation 'com.github.DylanCaiCoding:MMKV-KTX:1.2.15'
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:

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

Create a class to implement the `MMKVOwner` interface and delegate properties to `MMKV` with the `by mmkvXXXX()` method, for example:
If you already have a parent class that cannot be inherited from, implement `IMMKVOwner by MMKVOwner(mmapID)`, such as:

```kotlin
object DataRepository : MMKVOwner {
var isFirstLaunch by mmkvBool(default = true)
var user by mmkvParcelable<User>()
object SettingsRepository : BaseRepository(), IMMKVOwner by MMKVOwner(mmapID = "settings") {
// ...
}
```

Setting or getting the value of the property calls the corresponding encode or decode method, and the key value is the property name.
Make sure that each `mmapID` is unique to ensure type safety 100%!!!

The following types are supported:
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:

| Method | Default value |
| ------------------ | ------------- |
| `mmkvInt()` | 0 |
| `mmkvLong()` | 0L |
| `mmkvBool()` | false |
| `mmkvFloat()` | 0f |
| `mmkvDouble()` | 0.0 |
| `mmkvString()` | / |
| `mmkvStringSet()` | / |
| `mmkvBytes()` | / |
| `mmkvParcelable()` | / |
```kotlin
if (SettingsRepository.isNightMode) {
// do some thing
}

SettingsRepository.isNightMode = true
```

In version 1.2.15, the newly added `mmkvXXXX().asLiveData()` function delegates the property to `LiveData`, for example:
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:

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

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

SettingRepository.nightMode.value = true
SettingRepository.isNightMode.value = true
```

You can get the `kv` object in the implementation class of `MMKVOwner` to delete values or clear all, for example:
The `kv` object can be used to delete values or clear the cache, for example:

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

See the [usage documentation](https://dylancaicoding.github.io/MMKV-KTX) for more advanced usage.
For more advanced usage, please refer to the [Usage Document](https://dylancaicoding.github.io/MMKV-KTX).

## Change log
## Update log

[Releases](https://github.com/DylanCaiCoding/MMKV-KTX/releases)

## Author's other libraries
## Other libraries created by the author

| Library | Description |
| Library | Brief Introduction |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| [Longan](https://github.com/DylanCaiCoding/Longan) | Probably the best Kotlin utils library for Android. |
| [LoadingStateView](https://github.com/DylanCaiCoding/LoadingStateView) | Decoupling the code of toolbar or loading status view. |
| [ViewBindingKTX](https://github.com/DylanCaiCoding/ViewBindingKTX) | The most comprehensive utils of ViewBinding. |
| [Tracker](https://github.com/DylanCaiCoding/Tracker) | A lightweight tracking framework based on the tracking idea of Buzzvideo.|
| [Longan](https://github.com/DylanCaiCoding/Longan) | Perhaps the most user-friendly Kotlin tool library |
| [LoadingStateView](https://github.com/DylanCaiCoding/LoadingStateView) | Deep decoupling of the default page of the title bar or loading, loading failure, no data, etc. |
| [ViewBindingKTX](https://github.com/DylanCaiCoding/ViewBindingKTX) | Most comprehensive ViewBinding tool |
| [Tracker](https://github.com/DylanCaiCoding/Tracker) | Lightweight burrowing framework based on the chain of responsibility burrowing idea of Buzzvideo |

## License

Expand All @@ -102,11 +126,11 @@ 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
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.
```
```
54 changes: 39 additions & 15 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

[English](README.md) | 中文

[![](https://www.jitpack.io/v/DylanCaiCoding/MMKV-KTX.svg)](https://www.jitpack.io/#DylanCaiCoding/MMKV-KTX)
[![](https://www.jitpack.io/v/DylanCaiCoding/MMKV-KTX.svg)](https://www.jitpack.io/#DylanCaiCoding/MMKV-KTX)
[![License](https://img.shields.io/badge/License-Apache--2.0-blue.svg)](https://github.com/DylanCaiCoding/MMKV-KTX/blob/master/LICENSE)

结合了 Kotlin 属性委托的特性,使得 [MMKV](https://github.com/Tencent/MMKV) 更加易用,无需初始化 MMKV,无需传 key 值。
结合了 Kotlin 属性委托的特性,使得 [MMKV](https://github.com/Tencent/MMKV) 更加灵活易用。

## Features

- 自动初始化 MMKV ;
- 用属性名作为键名,无需声明大量的键名常量;
- 可以确保类型安全,避免类型或者 key 值不一致导致的异常;

## 用法

Expand All @@ -28,20 +34,38 @@ allprojects {

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

让一个类继承 `MMKVOwner` 类,即可在该类使用 `by mmkvXXXX()` 函数将属性委托给 `MMKV`,例如:

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

让一个类实现 `MMKVOwner` 接口,即可在该类使用 `by mmkvXXXX()` 函数将属性委托给 `MMKV`,例如
如果已经有了父类继承不了,那就实现 `IMMKVOwner by MMKVOwner(mmapID)`,比如

```kotlin
object DataRepository : MMKVOwner {
var isFirstLaunch by mmkvBool(default = true)
var user by mmkvParcelable<User>()
object SettingsRepository : BaseRepository(), IMMKVOwner by MMKVOwner(mmapID = "settings") {
// ...
}
```

设置或获取属性的值会调用对应的 encode() 或 decode() 函数,**用属性名作为 key 值**
**不管哪种都要确保每个 `mmapID` 不重复,只有这样才能 100% 确保类型安全!!!**

设置或获取属性的值会调用对应的 encode() 或 decode() 函数,用属性名作为键名。比如:

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

SettingsRepository.isNightMode = true
```

支持以下类型:

Expand All @@ -57,24 +81,24 @@ object DataRepository : MMKVOwner {
| `mmkvBytes()` | / |
| `mmkvParcelable()` | / |

1.2.15 版本新增 `mmkvXXXX().asLiveData()` 函数将属性委托给 `LiveData`,例如:
支持用 `mmkvXXXX().asLiveData()` 函数将属性委托给 `LiveData`,例如:

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

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

SettingRepository.nightMode.value = true
SettingRepository.isNightMode.value = true
```

`MMKVOwner` 的实现类中可以获取 `kv` 对象进行删除值或清理缓存等操作,例如:
可以用 `kv` 对象进行删除值或清理缓存等操作,例如:

```kotlin
kv.removeValueForKey(::isFirstLaunch.name)
kv.removeValueForKey(::language.name) // 建议修改了默认值才移除 key,否则赋值操作更简洁
kv.clearAll()
```

Expand Down
Loading

0 comments on commit d5aecca

Please sign in to comment.