{
14 | ObjectListMetadata(Metadata metadata) {
15 | super(metadata, JSONObject.class);
16 | }
17 |
18 | public void addObject(Object element) {
19 | if (element == null) {
20 | return;
21 | }
22 | try {
23 | add(new JSONObject(KVStorage.toJSONString(element)));
24 | } catch (JSONException e) {
25 | e.printStackTrace();
26 | }
27 | }
28 |
29 | @Nullable
30 | public List
convert(Class
clazz) {
31 | return KVStorage.parseArray(metadata.getString("[]"), clazz);
32 | }
33 |
34 | public JSONArray findByProperty(String propertyName, Object value) {
35 | JSONArray result = new JSONArray();
36 | List data = getData();
37 | for (int i = 0; i < data.size(); i++) {
38 | JSONObject item = data.get(i);
39 | try {
40 | if (value.equals(item.get(propertyName))) {
41 | result.put(item);
42 | }
43 | } catch (JSONException e) {
44 | e.printStackTrace();
45 | }
46 | }
47 | return result;
48 | }
49 |
50 | public List removeByProperty(String propertyName, Object value) {
51 | List result = new ArrayList<>();
52 | List data = getData();
53 | Iterator iterator = data.iterator();
54 | while (iterator.hasNext()) {
55 | JSONObject item = iterator.next();
56 | try {
57 | if (value.equals(item.get(propertyName))) {
58 | result.add(item);
59 | iterator.remove();
60 | }
61 | } catch (JSONException e) {
62 | e.printStackTrace();
63 | }
64 | }
65 | flush();
66 | return result;
67 | }
68 |
69 | boolean updateByProperty(JSONObject value) {
70 | return false;
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/kvstorage/src/main/java/com/taoweiji/kvstorage/ObjectMetadata.java:
--------------------------------------------------------------------------------
1 | package com.taoweiji.kvstorage;
2 |
3 | import androidx.annotation.NonNull;
4 | import androidx.annotation.Nullable;
5 |
6 |
7 | import org.json.JSONArray;
8 | import org.json.JSONException;
9 | import org.json.JSONObject;
10 |
11 |
12 | /**
13 | * 如果操作 json 修改字段,框架会自动检查内容是否发生了改变,检查Activity的变化情况自动保存
14 | */
15 | public class ObjectMetadata {
16 |
17 | private final Metadata metadata;
18 | private JSONObject json;
19 |
20 | ObjectMetadata(Metadata metadata) {
21 | this.metadata = metadata;
22 | }
23 |
24 | public boolean exists() {
25 | return getData().length() > 0;
26 | }
27 |
28 | @NonNull
29 | public JSONObject getData() {
30 | if (json == null) {
31 | String str = metadata.getString("{}");
32 | try {
33 | json = new JSONObject(str);
34 | } catch (JSONException e) {
35 | e.printStackTrace();
36 | json = new JSONObject();
37 | }
38 | }
39 | return json;
40 | }
41 |
42 | public void setData(JSONObject json) {
43 | this.json = json;
44 | flush();
45 | }
46 |
47 | public void setObject(Object object) {
48 | json = null;
49 | metadata.set(KVStorage.toJSONString(object));
50 | }
51 |
52 | public void flush() {
53 | metadata.set(getData().toString());
54 | }
55 |
56 | public void clear() {
57 | json = null;
58 | flush();
59 | }
60 |
61 |
62 | @NonNull
63 | public ObjectMetadata set(@NonNull String name, Object value) {
64 | try {
65 | getData().putOpt(name, value);
66 | } catch (JSONException e) {
67 | e.printStackTrace();
68 | }
69 | flush();
70 | return this;
71 | }
72 |
73 | @Nullable
74 | public Object remove(@Nullable String name) {
75 | return getData().remove(name);
76 | }
77 |
78 | public int getInt(@NonNull String name, int def) {
79 | return getData().optInt(name, def);
80 | }
81 |
82 | public long getLong(@NonNull String name, long def) {
83 | return getData().optLong(name, def);
84 | }
85 |
86 | public double getDouble(@NonNull String name, double def) {
87 | return getData().optDouble(name, def);
88 | }
89 |
90 | public boolean getBool(@NonNull String name, boolean def) {
91 | return getData().optBoolean(name, def);
92 | }
93 |
94 | public String getString(@NonNull String name, String def) {
95 | return getData().optString(name, def);
96 | }
97 |
98 | @Nullable
99 | public JSONArray getJSONArray(@NonNull String name) {
100 | return getData().optJSONArray(name);
101 | }
102 |
103 | @Nullable
104 | public JSONObject getJSONObject(@NonNull String name) {
105 | return getData().optJSONObject(name);
106 | }
107 |
108 | @Nullable
109 | public T convert(Class clazz) {
110 | if (!exists()) {
111 | return null;
112 | }
113 | return KVStorage.parseObject(getData().toString(), clazz);
114 | }
115 | }
--------------------------------------------------------------------------------
/kvstorage/src/main/java/com/taoweiji/kvstorage/PreferencesProvider.java:
--------------------------------------------------------------------------------
1 | package com.taoweiji.kvstorage;
2 |
3 | public interface PreferencesProvider {
4 |
5 | float getFloat(String key, float def);
6 |
7 | boolean getBoolean(String key, boolean def);
8 |
9 | long getLong(String key, long def);
10 |
11 | int getInt(String key, int def);
12 |
13 | String getString(String key, String def);
14 |
15 | void putString(String key, String value);
16 |
17 | void putBoolean(String key, boolean value);
18 |
19 | void putFloat(String key, float value);
20 |
21 | void putLong(String key, long value);
22 |
23 | void putInt(String key, int value);
24 |
25 | void remove(String key);
26 |
27 | void clear();
28 | }
29 |
--------------------------------------------------------------------------------
/kvstorage/src/main/java/com/taoweiji/kvstorage/ReadOnlyConfigGroup.java:
--------------------------------------------------------------------------------
1 | package com.taoweiji.kvstorage;
2 |
3 | public class ReadOnlyConfigGroup {
4 | private final String groupName;
5 |
6 | public ReadOnlyConfigGroup(String groupName) {
7 | this.groupName = groupName;
8 | }
9 |
10 | protected String createGroup(String name) {
11 | return groupName + "." + name;
12 | }
13 |
14 | protected ReadOnlyMetadata get(String name) {
15 | return new ReadOnlyMetadata(this.groupName, name);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/kvstorage/src/main/java/com/taoweiji/kvstorage/ReadOnlyMetadata.java:
--------------------------------------------------------------------------------
1 | package com.taoweiji.kvstorage;
2 |
3 | public class ReadOnlyMetadata {
4 | private final String groupName;
5 | private final String key;
6 |
7 | public ReadOnlyMetadata(String groupName, String key) {
8 | this.groupName = groupName;
9 | this.key = key;
10 | }
11 |
12 | public String getString(String def) {
13 | return KVStorage.getReadOnlyConfigProvider().getString(groupName, key, def);
14 | }
15 |
16 | public int getInt(int def) {
17 | return KVStorage.getReadOnlyConfigProvider().getInt(groupName, key, def);
18 | }
19 |
20 | public long getLong(long def) {
21 | return KVStorage.getReadOnlyConfigProvider().getLong(groupName, key, def);
22 | }
23 |
24 | public float getFloat(float def) {
25 | return KVStorage.getReadOnlyConfigProvider().getFloat(groupName, key, def);
26 | }
27 |
28 | public boolean getBool(boolean def) {
29 | return KVStorage.getReadOnlyConfigProvider().getBool(groupName, key, def);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/kvstorage/src/main/java/com/taoweiji/kvstorage/SetMetadata.java:
--------------------------------------------------------------------------------
1 | package com.taoweiji.kvstorage;
2 |
3 | import android.os.Build;
4 | import android.text.TextUtils;
5 |
6 | import androidx.annotation.NonNull;
7 | import androidx.annotation.RequiresApi;
8 |
9 | import org.json.JSONArray;
10 | import org.json.JSONObject;
11 |
12 | import java.util.Collection;
13 | import java.util.HashSet;
14 | import java.util.Iterator;
15 | import java.util.Set;
16 | import java.util.function.Consumer;
17 |
18 | public class SetMetadata {
19 |
20 | final Metadata metadata;
21 | private final Class type;
22 | private Set data;
23 |
24 | SetMetadata(Metadata metadata, Class type) {
25 | this.metadata = metadata;
26 | this.type = type;
27 | String text = metadata.getString(null);
28 | data = new HashSet<>();
29 | if (!TextUtils.isEmpty(text)) {
30 | try {
31 | JSONArray array = new JSONArray(text);
32 | for (int i = 0; i < array.length(); i++) {
33 | if (type == Integer.class) {
34 | data.add((T) Integer.valueOf(array.getInt(i)));
35 | } else if (type == Long.class) {
36 | data.add((T) Long.valueOf(array.getLong(i)));
37 | } else if (type == Float.class) {
38 | data.add((T) Float.valueOf((float) array.getDouble(i)));
39 | } else if (type == Boolean.class) {
40 | data.add((T) Boolean.valueOf(array.getBoolean(i)));
41 | } else if (type == String.class) {
42 | data.add((T) array.getString(i));
43 | } else if (type == JSONObject.class) {
44 | data.add((T) array.getJSONObject(i));
45 | }
46 | }
47 | } catch (Exception e) {
48 | e.printStackTrace();
49 | }
50 | }
51 | }
52 |
53 | public Set getData() {
54 | if (data == null) {
55 | data = new HashSet<>();
56 | }
57 | return data;
58 | }
59 |
60 | public void setData(Set data) {
61 | this.data = data;
62 | flush();
63 | }
64 |
65 | public void flush() {
66 | if (type == JSONObject.class) {
67 | JSONArray array = new JSONArray();
68 | Iterator it = (Iterator) getData().iterator();
69 | while (it.hasNext()) {
70 | array.put(it.next());
71 | }
72 | metadata.set(array.toString());
73 | } else {
74 | metadata.set(KVStorage.toJSONString(getData()));
75 | }
76 | }
77 |
78 | public int size() {
79 | return getData().size();
80 | }
81 |
82 | public boolean isEmpty() {
83 | return getData().isEmpty();
84 | }
85 |
86 | public boolean contains(T value) {
87 | return getData().contains(value);
88 | }
89 |
90 | public Object[] toArray() {
91 | return getData().toArray();
92 | }
93 |
94 | public boolean add(T value) {
95 | boolean res = getData().add(value);
96 | flush();
97 | return res;
98 | }
99 |
100 | public boolean remove(T value) {
101 | boolean res = getData().remove(value);
102 | flush();
103 | return res;
104 | }
105 |
106 |
107 | public void clear() {
108 | getData().clear();
109 | flush();
110 | }
111 |
112 | @NonNull
113 | public Iterator iterator() {
114 | return getData().iterator();
115 | }
116 |
117 | @NonNull
118 | public T1[] toArray(@NonNull T1[] a) {
119 | return getData().toArray(a);
120 | }
121 |
122 | public boolean containsAll(@NonNull Collection c) {
123 | return getData().containsAll(c);
124 | }
125 |
126 | public boolean addAll(@NonNull Collection extends T> c) {
127 | boolean res = getData().addAll(c);
128 | flush();
129 | return res;
130 | }
131 |
132 | public boolean removeAll(@NonNull Collection c) {
133 | boolean res = getData().removeAll(c);
134 | flush();
135 | return res;
136 | }
137 |
138 | @RequiresApi(api = Build.VERSION_CODES.N)
139 | public void forEach(@NonNull Consumer super T> action) {
140 | getData().forEach(action);
141 | }
142 | }
--------------------------------------------------------------------------------
/kvstorage/src/main/java/com/taoweiji/kvstorage/SharedPreferencesProvider.java:
--------------------------------------------------------------------------------
1 | package com.taoweiji.kvstorage;
2 |
3 | import android.content.Context;
4 | import android.content.SharedPreferences;
5 |
6 | public class SharedPreferencesProvider implements PreferencesProvider {
7 | private final SharedPreferences preferences;
8 |
9 | public SharedPreferencesProvider(String fileName) {
10 | preferences = KVStorage.getContext().getSharedPreferences(fileName, Context.MODE_PRIVATE);
11 | }
12 |
13 | @Override
14 | public float getFloat(String key, float def) {
15 | return preferences.getFloat(key, def);
16 | }
17 |
18 | @Override
19 | public boolean getBoolean(String key, boolean def) {
20 | return preferences.getBoolean(key, def);
21 | }
22 |
23 | @Override
24 | public long getLong(String key, long def) {
25 | return preferences.getLong(key, def);
26 | }
27 |
28 | @Override
29 | public int getInt(String key, int def) {
30 | return preferences.getInt(key, def);
31 | }
32 |
33 | @Override
34 | public String getString(String key, String def) {
35 | return preferences.getString(key, def);
36 | }
37 |
38 | @Override
39 | public void putString(String key, String value) {
40 | preferences.edit().putString(key, value).apply();
41 | }
42 |
43 | @Override
44 | public void putBoolean(String key, boolean value) {
45 | preferences.edit().putBoolean(key, value).apply();
46 | }
47 |
48 | @Override
49 | public void putFloat(String key, float value) {
50 | preferences.edit().putFloat(key, (float) value).apply();
51 | }
52 |
53 | @Override
54 | public void putLong(String key, long value) {
55 | preferences.edit().putLong(key, value).apply();
56 | }
57 |
58 | @Override
59 | public void putInt(String key, int value) {
60 | preferences.edit().putInt(key, value).apply();
61 | }
62 |
63 | @Override
64 | public void remove(String key) {
65 | preferences.edit().remove(key).apply();
66 | }
67 |
68 | @Override
69 | public void clear() {
70 | preferences.edit().clear().apply();
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/kvstorage/src/test/java/com/taoweiji/kvstorage/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.taoweiji.kvstorage;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/maven_public.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'maven'
2 | group = GROUP_ID
3 | version = VERSION_NAME
4 |
5 | // 发布到本地仓库,方便测试
6 | uploadArchives {
7 | repositories {
8 | mavenDeployer {
9 | repository(url: uri('../repo'))
10 | }
11 | }
12 | }
13 | return
14 |
15 | apply plugin: 'signing'
16 | signing {
17 | sign configurations.archives
18 | }
19 | boolean isAndroid = project.getPlugins().hasPlugin('com.android.library')
20 | if (isAndroid) {
21 | task androidJavadocs(type: Javadoc) {
22 | source = android.sourceSets.main.java.source
23 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
24 | excludes = ['**/*.kt']
25 | }
26 | task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
27 | classifier = 'javadoc'
28 | from androidJavadocs.destinationDir
29 | }
30 | task androidSourcesJar(type: Jar) {
31 | classifier = 'sources'
32 | from android.sourceSets.main.java.source
33 | }
34 | } else {
35 | task javadocJar(type: Jar) {
36 | classifier = 'javadoc'
37 | from javadoc
38 | }
39 | task sourcesJar(type: Jar) {
40 | classifier = 'sources'
41 | from sourceSets.main.allSource
42 | }
43 | artifacts {
44 | archives javadocJar, sourcesJar
45 | }
46 | }
47 |
48 | signing {
49 | // keyId = "0945148C"
50 | // password = "Tao408191243."
51 | // secretKeyRingFile = "/Users/wiki/.gnupg/secring.gpg"
52 | }
53 |
54 |
55 | uploadArchives {
56 | repositories {
57 | mavenDeployer {
58 | beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
59 | repository(url: "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/") {
60 | authentication(userName: OSSRH_USERNAME, password: OSSRH_PASSWORD)
61 | }
62 | snapshotRepository(url: "https://s01.oss.sonatype.org/content/repositories/snapshots/") {
63 | authentication(userName: OSSRH_USERNAME, password: OSSRH_PASSWORD)
64 | }
65 | pom.project {
66 | name POM_NAME
67 | packaging isAndroid ? 'jar' : 'aar'
68 | description POM_DESCRIPTION
69 | url POM_URL
70 | scm {
71 | connection POM_SCM_CONNECTION
72 | developerConnection POM_SCM_DEV_CONNECTION
73 | url POM_SCM_URL
74 | }
75 | licenses {
76 | license {
77 | name POM_LICENCE_NAME
78 | url POM_LICENCE_URL
79 | }
80 | }
81 | developers {
82 | developer {
83 | id POM_DEVELOPER_ID
84 | name POM_DEVELOPER_NAME
85 | email POM_DEVELOPER_EMAIL
86 | }
87 | }
88 | }
89 | }
90 | }
91 | }
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | //dependencyResolutionManagement {
2 | // repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
3 | // repositories {
4 | // google()
5 | // mavenCentral()
6 | // jcenter() // Warning: this repository is going to shut down soon
7 | // }
8 | //}
9 | rootProject.name = "KVStorage"
10 | include ':example'
11 | include ':kvstorage-gradle-plugin'
12 | include ':kvstorage'
13 | include ':kvstorage-mmkv'
14 |
--------------------------------------------------------------------------------