├── README.md
└── realmkeyvalueutils
├── build.gradle
├── proguard-rules.pro
├── realmkeyvalueutils.iml
└── src
├── androidTest
└── java
│ └── com
│ └── github
│ └── codemao
│ └── ApplicationTest.java
├── main
├── AndroidManifest.xml
├── java
│ └── com
│ │ └── github
│ │ └── codemao
│ │ └── model
│ │ ├── KeyValueRealmObject.java
│ │ └── RealmKVHelper.java
└── res
│ └── values
│ └── strings.xml
└── test
└── java
└── com
└── github
└── mao
└── ExampleUnitTest.java
/README.md:
--------------------------------------------------------------------------------
1 | # RealmKeyValueUtils
2 |
3 | public class AppAplication extends Application {
4 |
5 | @Override
6 | public void onCreate() {
7 | super.onCreate();
8 | RealmKVHelper.initialize(this);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/realmkeyvalueutils/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion "23.0.1"
6 |
7 | defaultConfig {
8 | minSdkVersion 14
9 | targetSdkVersion 23
10 | versionCode 1
11 | versionName "1.0"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | compile fileTree(dir: 'libs', include: ['*.jar'])
23 | compile 'io.realm:realm-android:0.85.0'
24 | }
25 |
--------------------------------------------------------------------------------
/realmkeyvalueutils/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/maoweiwei/DevelopTools/adt-bundle-mac/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/realmkeyvalueutils/realmkeyvalueutils.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 | generateDebugAndroidTestSources
19 | generateDebugSources
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
--------------------------------------------------------------------------------
/realmkeyvalueutils/src/androidTest/java/com/github/codemao/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.github.codemao;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/realmkeyvalueutils/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/realmkeyvalueutils/src/main/java/com/github/codemao/model/KeyValueRealmObject.java:
--------------------------------------------------------------------------------
1 | package com.github.codemao.model;
2 |
3 | import io.realm.RealmObject;
4 | import io.realm.annotations.PrimaryKey;
5 |
6 | /**
7 | * Created by maoweiwei on 15/11/26.
8 | */
9 | public class KeyValueRealmObject extends RealmObject {
10 |
11 |
12 | @PrimaryKey
13 | private String key;
14 | private String value;
15 |
16 | public String getKey() {
17 | return key;
18 | }
19 |
20 | public void setKey(String key) {
21 | this.key = key;
22 | }
23 |
24 | public String getValue() {
25 | return value;
26 | }
27 |
28 | public void setValue(String value) {
29 | this.value = value;
30 | }
31 | }
--------------------------------------------------------------------------------
/realmkeyvalueutils/src/main/java/com/github/codemao/model/RealmKVHelper.java:
--------------------------------------------------------------------------------
1 | package com.github.codemao.model;
2 |
3 | import android.content.Context;
4 |
5 | import java.util.List;
6 |
7 | import io.realm.Realm;
8 | import io.realm.RealmConfiguration;
9 | import io.realm.RealmList;
10 | import io.realm.RealmObject;
11 | import io.realm.RealmResults;
12 |
13 | /**
14 | * Created by maoweiwei on 15/11/26.
15 | */
16 | public class RealmKVHelper {
17 |
18 | private static RealmKVHelper instance;
19 | private static Realm mRealm;
20 |
21 | public RealmKVHelper(Context mContext) {
22 | RealmConfiguration config = new RealmConfiguration.Builder(mContext)
23 | .name("key_value_db")
24 | .schemaVersion(1)
25 | .deleteRealmIfMigrationNeeded()
26 | .build();
27 | mRealm = Realm.getInstance(config);
28 | }
29 |
30 | // * A static method to initialize a new instance of this
31 | public synchronized static void initialize(Context mContext) {
32 |
33 | if (instance == null)
34 | instance = new RealmKVHelper(mContext);
35 | }
36 |
37 | public static RealmKVHelper getInstance() throws NullPointerException {
38 | if (instance == null) {
39 | throw new NullPointerException(
40 | "The class has never been initialized. "
41 | + "Use initialize(context) first to create a new instance");
42 | }
43 | return instance;
44 | }
45 |
46 | public Realm getRealm() {
47 | return mRealm;
48 | }
49 |
50 | public boolean addValue(K dataKey, V value) {
51 | boolean pass = addOrUpdate(String.valueOf(dataKey),
52 | String.valueOf(value));
53 | return pass;
54 | }
55 |
56 | public boolean addValuesArray(K dataKey, V[] values) {
57 | String value = "";
58 | for (int i = 0; i < (values.length - 1); i++) {
59 | value += values[i] + ",";
60 | }
61 | value += values[(values.length - 1)];
62 |
63 | boolean pass = addOrUpdate(String.valueOf(dataKey),
64 | String.valueOf(value));
65 | return pass;
66 | }
67 |
68 | public Integer getIntValue(K dataKey, Integer defaultValue) {
69 | Integer value = defaultValue;
70 | String stringValue = getStringValue(dataKey, null);
71 | if (stringValue != null) {
72 | try {
73 | value = Integer.valueOf(stringValue);
74 | } catch (Exception e) {
75 | }
76 | }
77 | return value;
78 | }
79 |
80 | public Long getLongValue(K dataKey, Long defaultValue) {
81 | Long value = defaultValue;
82 | String stringValue = getStringValue(dataKey, null);
83 | if (stringValue != null) {
84 | try {
85 | value = Long.valueOf(stringValue);
86 | } catch (Exception e) {
87 | }
88 | }
89 | return value;
90 | }
91 |
92 | public Double getDoubleValue(K dataKey, Double defaultValue) {
93 | Double value = defaultValue;
94 | String stringValue = getStringValue(dataKey, null);
95 | if (stringValue != null) {
96 | try {
97 | value = Double.valueOf(stringValue);
98 | } catch (Exception e) {
99 | }
100 | }
101 | return value;
102 | }
103 |
104 | public Float getFloatValue(K dataKey, Float defaultValue) {
105 | Float value = defaultValue;
106 | String stringValue = getStringValue(dataKey, null);
107 | if (stringValue != null) {
108 | try {
109 | value = Float.valueOf(stringValue);
110 | } catch (Exception e) {
111 | }
112 | }
113 | return value;
114 | }
115 |
116 |
117 | public Boolean getBooleanValue(K dataKey, Boolean defaultValue) {
118 | Boolean value = defaultValue;
119 | String stringValue = getStringValue(dataKey, null);
120 | if (stringValue != null) {
121 | try {
122 | value = Boolean.valueOf(stringValue);
123 | } catch (Exception e) {
124 | }
125 | }
126 | return value;
127 | }
128 |
129 | public String[] getStringArray(K dataKey) {
130 | String[] values = null;
131 | String value = getStringValue(dataKey, null);
132 | if (value != null) {
133 | values = value.split(",");
134 | }
135 |
136 | return values;
137 | }
138 |
139 | public String getStringValue(K dataKey, String defaultValue) {
140 | RealmResults datas = mRealm.where(KeyValueRealmObject.class)
141 | .equalTo("key", String.valueOf(dataKey))
142 | .findAll();
143 |
144 | if (datas == null || datas.isEmpty())
145 | return defaultValue;
146 |
147 | return datas.get(0).getValue();
148 | }
149 |
150 | private boolean addOrUpdate(String dataKey, String value) {
151 | KeyValueRealmObject realmObject = new KeyValueRealmObject();
152 | realmObject.setKey(dataKey);
153 | realmObject.setValue(value);
154 | saveObject(realmObject);
155 | return true;
156 | }
157 |
158 |
159 | public void saveAllObject(List objects) {
160 | mRealm.beginTransaction();
161 | for (T object : objects) {
162 | mRealm.copyToRealm(object);
163 | }
164 | mRealm.commitTransaction();
165 | }
166 |
167 | public void saveObject(T object) {
168 | mRealm.beginTransaction();
169 | mRealm.copyToRealmOrUpdate(object);
170 | mRealm.commitTransaction();
171 | }
172 |
173 | public void removeObject(final RealmObject realmObject) {
174 | if (null == realmObject)
175 | return;
176 | mRealm.beginTransaction();
177 | realmObject.removeFromRealm();
178 | mRealm.commitTransaction();
179 | }
180 |
181 | public void removeObjectList(final RealmList list) {
182 |
183 | if (null == list)
184 | return;
185 |
186 | mRealm.beginTransaction();
187 | for (int i = 0; i < list.size(); i++) {
188 | list.get(i).removeFromRealm();
189 | }
190 | mRealm.commitTransaction();
191 | }
192 |
193 | public RealmResults getAllObjects(final Class clazz) {
194 | return mRealm.where(clazz).findAll();
195 | }
196 |
197 | public void removeObjectList(RealmResults> result) {
198 | if (null == result)
199 | return;
200 | mRealm.beginTransaction();
201 | for (int i = 0; i < result.size(); i++) {
202 | result.get(i).removeFromRealm();
203 | }
204 | mRealm.commitTransaction();
205 | }
206 |
207 | }
208 |
--------------------------------------------------------------------------------
/realmkeyvalueutils/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | RealmKeyValueUtils
3 |
4 |
--------------------------------------------------------------------------------
/realmkeyvalueutils/src/test/java/com/github/mao/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.github.codemao;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------