Skip to content

KunMinX/KeyValueX

Repository files navigation

 

研发小故事:《KeyValueX 立项 & 迭代心路历程》

 

特性

1.key、value、get、put、init 缩减为一,不再 KV 爆炸

2.使用注解,但无需初始化 build,对巨型项目友好

3.支持 Java

 

项目根目录 build.gradle 添加如下依赖:

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

模块 build.gradle 添加如下依赖:

implementation 'com.github.KunMinX.KeyValueX:keyvalue:2.2.7-beta'
annotationProcessor 'com.github.KunMinX.KeyValueX:keyvalue-compiler:2.2.7-beta'

 

使用 3 步曲

1.创建 KeyValueGroup 接口,例如

@KeyValueGroup
public interface Test {
  @KeyValue KeyValueInteger days();
  @KeyValue KeyValueString accountId();
  @KeyValue KeyValueSerializable<User> user();
}

2.像往常一样,创建项目配置管理类,如 Configs

//Configs 无须定义一堆 KEY、VALUE 常量和 get、put、init 静态方法,
//只需一条 KeyValues 静态变量:

public class Configs {
  public final static Test keyValues = KeyValueCreator.create(Test.class);
}

3.在页面等处通过 get( ) set( ) 方法读写 KeyValue

public class MainActivity extends AppCompatActivity {
  ...

  //测试持久化写入
  Configs.keyValues.user().set(u);

  //测试读取
  Log.d("---title", Configs.keyValues.user().get().title);
  Log.d("---content", Configs.keyValues.user().get().content);
}

KeyValueX 默认使用 SharedPreference 读写,可根据 KeyValueTool 自行注入 MMKV 等实现(详见 MainActivity 示例)。

 

Thanks

AndroidCodeUtils - SPUtils

 

License

Copyright 2019-present KunMinX

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.