Add versioning to the serialized cards.
This commit is contained in:
parent
0171a56d6e
commit
3f8267e5ef
@ -34,6 +34,7 @@ import java.io.File;
|
|||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectStreamException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -86,6 +87,7 @@ class CanvasView extends View implements PartGrid {
|
|||||||
this.setBackgroundColor(Color.rgb(4, 69, 99));
|
this.setBackgroundColor(Color.rgb(4, 69, 99));
|
||||||
|
|
||||||
if (!loadState()){
|
if (!loadState()){
|
||||||
|
// Sprinkle some elements around if no state is found
|
||||||
parts.add(new Placeholder(this, 50, 50, 750, 500));
|
parts.add(new Placeholder(this, 50, 50, 750, 500));
|
||||||
parts.add(new ColorBox(this, 600, 1000, 700, 1100));
|
parts.add(new ColorBox(this, 600, 1000, 700, 1100));
|
||||||
parts.add(new RoundButton(this, 300, 1200, 80, 100));
|
parts.add(new RoundButton(this, 300, 1200, 80, 100));
|
||||||
@ -102,7 +104,16 @@ class CanvasView extends View implements PartGrid {
|
|||||||
fileIn = new FileReader(file);
|
fileIn = new FileReader(file);
|
||||||
char[] data = new char[(int) file.length()];
|
char[] data = new char[(int) file.length()];
|
||||||
fileIn.read(data);
|
fileIn.read(data);
|
||||||
JSONArray jsonParts = new JSONArray(new String(data ));
|
JSONObject state = new JSONObject(new String(data));
|
||||||
|
JSONObject metadata = state.getJSONObject("metadata");
|
||||||
|
int version = metadata.getInt("version");
|
||||||
|
|
||||||
|
if (version != 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If version 0 -> known structure
|
||||||
|
JSONArray jsonParts = state.getJSONArray("parts");
|
||||||
for (int i = 0; i < jsonParts.length(); i++){
|
for (int i = 0; i < jsonParts.length(); i++){
|
||||||
connections.addAll(deserializeObject(jsonParts.getJSONObject(i)));
|
connections.addAll(deserializeObject(jsonParts.getJSONObject(i)));
|
||||||
}
|
}
|
||||||
@ -349,7 +360,7 @@ class CanvasView extends View implements PartGrid {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private byte[] serializeState() throws IOException {
|
private byte[] serializeState() throws IOException {
|
||||||
JSONArray partArray = new JSONArray();
|
final JSONArray partArray = new JSONArray();
|
||||||
for (Part part : parts) {
|
for (Part part : parts) {
|
||||||
JSONObject serializedPart = new JSONObject();
|
JSONObject serializedPart = new JSONObject();
|
||||||
try {
|
try {
|
||||||
@ -362,7 +373,15 @@ class CanvasView extends View implements PartGrid {
|
|||||||
partArray.put(serializedPart);
|
partArray.put(serializedPart);
|
||||||
}
|
}
|
||||||
|
|
||||||
return partArray.toString().getBytes("UTF-8");
|
final JSONObject metadataObject = new JSONObject(new HashMap<String, Object>(){{
|
||||||
|
put("version", 0);
|
||||||
|
}});
|
||||||
|
|
||||||
|
final JSONObject state = new JSONObject(new HashMap<String, Object>(){{
|
||||||
|
put("metadata", metadataObject);
|
||||||
|
put("parts", partArray);
|
||||||
|
}});
|
||||||
|
return state.toString().getBytes("UTF-8");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
Loading…
Reference in New Issue
Block a user