|
1 | 1 | [  ](https://bintray.com/efff/maven/RotorKotlinDatabase/_latestVersion)
|
| 2 | + |
2 | 3 | <p align="center"><img width="10%" vspace="20" src="https://github.com/rotorlab/database-kotlin/raw/master/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png"></p>
|
3 |
| - |
4 |
| -Before use this library, [Rotor Core](https://github.com/rotorlab/core-kotlin) must be initialized. Lastest version is always the same on all Rotor libs. |
5 |
| - |
6 |
| -# Rotor Database |
7 |
| -Rotor Database is a complementary module for Rotor Core (kotlin). It allows to work with shared (Java) objects between many devices offering users real time changes and better mobile data consumption. |
| 4 | +<p align="center">Rotor Database Android library</p> |
8 | 5 |
|
9 |
| -Forget things like swipe-to-refresh events, lots of server requests and object storage management. Why? |
| 6 | +----------------------------- |
| 7 | +> Before use this library, [Rotor Core](https://github.com/rotorlab/core-kotlin) must be initialized. Lastest version is always the same for all Rotor libs. |
10 | 8 |
|
11 |
| -**Rotor Database philosophy** states that the only needed requests are those that change data on remote database. That means that the rest of requests you are imaging (give me updates, give updates, give me updates) are replaced now. How? |
| 9 | +Welcome to the **Rotor Database Android library - wiki**, here you'll find all information about the implementation and use of Rotor Database library. |
12 | 10 |
|
13 |
| -Rotor Core is connected to Rotor and Redis servers. The first one controls object sharing queues, devices waiting for changes and all data edition on remote database. The second (as you probably know) gives us Pub/Sub messaging pattern for data changes replication. |
| 11 | +[Wiki](https://github.com/rotorlab/database-kotlin/wiki) |
14 | 12 |
|
15 |
| -## Implementation |
16 |
| -Import libraries: |
| 13 | +[Get Started](https://github.com/rotorlab/database-kotlin/wiki/Get-Started) |
17 | 14 |
|
18 |
| -```groovy |
19 |
| -android { |
20 |
| - defaultConfig { |
21 |
| - multiDexEnabled true |
22 |
| - } |
23 |
| -} |
24 |
| - |
25 |
| -def rotor_version = "0.3" |
| 15 | +[Real Time Database](https://github.com/rotorlab/database-kotlin/wiki/Real-time-database) |
26 | 16 |
|
27 |
| -dependencies { |
28 |
| - implementation ("com.rotor:core:$rotor_version@aar") { |
29 |
| - transitive = true |
30 |
| - } |
31 |
| - implementation ("com.rotor:database:$rotor_version@aar") { |
32 |
| - transitive = true |
33 |
| - } |
34 |
| -} |
35 |
| -``` |
36 |
| -Initialize database module after Rotor core initialization. Should be invoked on `LoadingActivity` or `SplashActivity`. : |
37 |
| -```java |
38 |
| -Rotor.initialize(getApplicationContext(), "http://10.0.2.2:1508/", "redis://10.0.2.2", new StatusListener() { |
39 |
| - @Override |
40 |
| - public void connected() { |
41 |
| - Database.initialize() |
42 |
| - |
43 |
| - // login - main UI |
44 |
| - // start listen references |
45 |
| - } |
46 |
| - |
47 |
| - @Override |
48 |
| - public void reconnecting() { |
49 |
| - |
50 |
| - } |
51 |
| -}); |
52 |
| -``` |
53 |
| -## Listen shared object changes |
54 |
| -Rotor Database allows devices to work with the same objects by listening the same `path`. When an object is listened, library says to Rotor server your device is waiting for changes on that `path`, so every time any device makes a change on that (object), the differences are calculated and replicated on all devices listening. |
55 |
| -For that we have `Database.listen(...)` method which has a simple **object lifecycle interface** for control every object state. |
| 17 | +Rotor Database is a complementary module for Rotor Core. It allows to work with shared (Java) objects between many devices offering users real time changes and better mobile data consumption. |
56 | 18 |
|
57 |
| -```kotlin |
58 |
| -// kotlin |
59 |
| -Database.listen(database: String, path: String, Reference<T>(T::class.java) { |
60 |
| - |
61 |
| - fun onCreate() |
62 |
| - |
63 |
| - fun onUpdate(): T ? |
64 |
| - |
65 |
| - fun onChanged(ref: T) |
66 |
| - |
67 |
| - fun onDestroy() |
68 |
| - |
69 |
| - fun progress(value: Int) |
70 |
| - |
71 |
| -}) |
72 |
| -``` |
| 19 | +Forget things like swipe-to-refresh events, lots of server requests and object storage management. |
73 | 20 |
|
74 |
| -### onCreate |
75 |
| -Called when object is not created in remote DB yet. Object is defined and synchronized with server here. This method won't be called if object already exists on server, `onChange` method will be called instead. |
76 |
| -```java |
77 |
| -@Override |
78 |
| -public void onCreate() { |
79 |
| - objectA = new ObjectA("foo"); |
80 |
| - Database.sync(path); |
81 |
| -} |
82 |
| -``` |
83 |
| -### onChanged |
84 |
| -Called in two situations, when some device has made changes on the same object and when `listen` method is called and the object is cached. Database library pass the object up to date as parameter. |
85 |
| -```java |
86 |
| -@Override |
87 |
| -public void onChanged(ObjectA objectA) { |
88 |
| - this.objectA = objectA; |
89 |
| - // notify change on UI |
90 |
| -} |
91 |
| -``` |
92 |
| -### onUpdate |
93 |
| -Called when `sync` method is invoked. Differences with the last "fresh" object passed by library are calculated and sent to server. |
94 |
| -```java |
95 |
| -@Override |
96 |
| -public ObjectA onUpdate() { |
97 |
| - return objectA; |
98 |
| -} |
99 |
| -``` |
100 |
| -### onDestroy |
101 |
| -Called when `remove` method is invoked. Reference is removed on database too. |
102 |
| -```java |
103 |
| -@Override |
104 |
| -public void onDestroy() { |
105 |
| - objectA = null; |
106 |
| - // UI changes for removed object |
107 |
| -} |
108 |
| -``` |
109 |
| -### progress |
110 |
| -Some object updates can become too big, so server slices updates and sends them sequentially. value parameter goes from 0 to 100 |
111 |
| -```java |
112 |
| -@Override |
113 |
| -public void progress(int value) { |
114 |
| - Log.e(TAG, "loading " + path + " : " + value + " %"); |
115 |
| -} |
116 |
| -``` |
117 |
| -After work with objects, changes must be synchronized with servers. Call `Database.sync(path: String)` method to sync it. |
118 |
| -```java |
119 |
| -Database.sync("myObjects/objectA"); |
120 |
| -``` |
121 |
| -Remove listener in server by calling `Database.unlisten(path: String)`. |
122 |
| -```java |
123 |
| -Database.unlisten("myObjects/objectA"); |
124 |
| -``` |
125 |
| -Remove reference in database by calling `Database.remove(path: String)`. `onDestroy` will be called. |
126 |
| -```java |
127 |
| -Database.remove("myObjects/objectA"); |
128 |
| -``` |
129 |
| -Samples: |
130 |
| -```java |
131 |
| - |
132 |
| -class ObjectA { |
133 |
| - @SerializedName("value") |
134 |
| - @Expose |
135 |
| - String value; |
136 |
| - public ObjectA(String value) { |
137 |
| - this.value = value; |
138 |
| - } |
139 |
| - public void setValue(String value) { |
140 |
| - this.value = vaue; |
141 |
| - } |
142 |
| - public void getValue() { |
143 |
| - return value; |
144 |
| - } |
145 |
| -} |
| 21 | +**Rotor Database philosophy** states that the only needed requests are those that change data on remote database. That means that the rest of requests you are imaging (give me updates, give updates, give me updates) are removed now. |
146 | 22 |
|
147 |
| -String database = "database"; |
148 |
| -String path = "/myObjects/objectA"; |
149 |
| -ObjectA objectA = null; |
150 |
| - |
151 |
| -Database.listen(database, path, new Reference<ObjectA>(ObjectA.class) { |
152 |
| - @Override |
153 |
| - public void onCreate() { |
154 |
| - objectA = new ObjectA("foo"); |
155 |
| - Database.sync(path); |
156 |
| - } |
157 |
| - |
158 |
| - @Override |
159 |
| - public void onChanged(ObjectA objectA) { |
160 |
| - this.objectA = objectA; |
161 |
| - // notify change on UI |
162 |
| - } |
163 |
| - |
164 |
| - @Override |
165 |
| - public ObjectA onUpdate() { |
166 |
| - return objectA; |
167 |
| - } |
168 |
| - |
169 |
| - @Override |
170 |
| - public void onDestroy() { |
171 |
| - objectA = null; |
172 |
| - // UI changes for removed object |
173 |
| - } |
| 23 | +<p align="center"><img width="40%" vspace="20" src="https://github.com/rotorlab/core-kotlin/blob/develop/core_implementation.png"></p> |
174 | 24 |
|
175 |
| - @Override |
176 |
| - public void progress(int value) { |
177 |
| - Log.e(TAG, "loading " + path + " : " + value + " %"); |
178 |
| - } |
179 |
| -}); |
180 |
| -``` |
| 25 | +Rotor Core is connected to Rotor and Redis servers. The first one controls object sharing queues, devices waiting for changes and all data edition on remote database. The second (as you probably know) gives us Pub/Sub messaging pattern for data changes replication. |
181 | 26 |
|
182 | 27 | License
|
183 | 28 | -------
|
|
0 commit comments