Last active
August 29, 2015 14:18
-
-
Save rhoadster91/8d61f3d4d8598d61c0bb to your computer and use it in GitHub Desktop.
ViewMapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import android.app.Activity; | |
| import android.support.annotation.IdRes; | |
| import android.support.annotation.NonNull; | |
| import android.view.View; | |
| import android.widget.CheckBox; | |
| import android.widget.EditText; | |
| import com.google.gson.Gson; | |
| import com.google.gson.GsonBuilder; | |
| import com.google.gson.JsonElement; | |
| import com.google.gson.JsonObject; | |
| import com.google.gson.JsonParser; | |
| import com.google.gson.reflect.TypeToken; | |
| import org.json.JSONException; | |
| import org.json.JSONObject; | |
| import java.util.HashMap; | |
| import java.util.Map; | |
| import java.util.regex.Pattern; | |
| public class ViewMapper<T> { | |
| private HashMap<String, Integer> viewMap = new HashMap<>(); | |
| private Differentiator<T> mDifferentiator; | |
| public ViewMapper() { | |
| mDifferentiator = new Differentiator<>(); | |
| } | |
| public Differentiator<T> getDifferentiator() { | |
| return mDifferentiator; | |
| } | |
| public ViewMapper<T> map(@NonNull String fieldName, @IdRes int viewId) { | |
| viewMap.put(fieldName, viewId); | |
| return this; | |
| } | |
| @SuppressWarnings("unchecked") | |
| private void insertInJson(Map<String, Object> root, String key, String value) { | |
| if (key.contains("/")) { | |
| Map<String, Object> newRoot = new HashMap<>(); | |
| if (root.containsKey(key.split(Pattern.quote("/"))[0])) { | |
| newRoot = (Map<String, Object>) root.get(key.split(Pattern.quote("/"))[0]); | |
| } | |
| insertInJson(newRoot, key.split(Pattern.quote(key.split(Pattern.quote("/"))[0] + "/"))[1], value); | |
| //root.put(key.split(Pattern.quote("/"))[0], toMap(newRoot)); | |
| root.put(key.split(Pattern.quote("/"))[0], newRoot); | |
| } else { | |
| root.put(key, value); | |
| } | |
| } | |
| private String getFromJson(JSONObject root, String key) { | |
| if (key.contains("/")) { | |
| JSONObject newRoot; | |
| try { | |
| newRoot = root.getJSONObject(key.split(Pattern.quote("/"))[0]); | |
| return getFromJson(newRoot, key.split(Pattern.quote(key.split(Pattern.quote("/"))[0] + "/"))[1]); | |
| } catch (JSONException e) { | |
| e.printStackTrace(); | |
| } | |
| } else { | |
| try { | |
| return root.getString(key); | |
| } catch (JSONException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| return ""; | |
| } | |
| public String getJSON(Activity activity) { | |
| Map<String, Object> object = new HashMap<>(); | |
| for (Map.Entry<String, Integer> entry : viewMap.entrySet()) { | |
| View view = activity.findViewById(entry.getValue()); | |
| if (view != null) { | |
| if (view instanceof EditText) { | |
| insertInJson(object, entry.getKey(), ((EditText) view).getText().toString()); | |
| } else if (view instanceof CheckBox) { | |
| insertInJson(object, entry.getKey(), ((CheckBox) view).isChecked() ? "1" : "0"); | |
| } | |
| } | |
| } | |
| return new Gson().toJson(object); | |
| } | |
| public T get(Activity activity, Class<T> modelClass) { | |
| GsonBuilder builder = new GsonBuilder(); | |
| Gson gson = builder.create(); | |
| return gson.fromJson(getJSON(activity), modelClass); | |
| } | |
| public void put(Activity activity, T src) { | |
| Gson gson = new Gson(); | |
| java.lang.reflect.Type listType = new TypeToken<T>() { | |
| }.getType(); | |
| String json = gson.toJson(src, listType); | |
| try { | |
| JSONObject object = new JSONObject(json); | |
| for (Map.Entry<String, Integer> entry : viewMap.entrySet()) { | |
| View view = activity.findViewById(entry.getValue()); | |
| if (view != null) { | |
| if (view instanceof EditText) { | |
| ((EditText) view).setText(getFromJson(object, entry.getKey())); | |
| } else if (view instanceof CheckBox) { | |
| ((CheckBox) view).setChecked("1".equals(getFromJson(object, entry.getKey()))); | |
| } | |
| } | |
| } | |
| } catch (JSONException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| public static class Differentiator<T> { | |
| public String getUpdatedFields(T original, T updated) { | |
| Gson gson = new Gson(); | |
| java.lang.reflect.Type type = new TypeToken<T>() {}.getType(); | |
| JsonParser parser = new JsonParser(); | |
| JsonObject originalJson = (JsonObject) parser.parse(gson.toJson(original, type)); | |
| JsonObject updatedJson = (JsonObject) parser.parse(gson.toJson(updated, type)); | |
| return getUpdatedFields(originalJson, updatedJson).toString(); | |
| } | |
| private JsonObject getUpdatedFields(JsonObject original, JsonObject updated) { | |
| JsonObject delta = new JsonObject(); | |
| for(Map.Entry<String, JsonElement> entry : updated.entrySet()) { | |
| JsonElement element = entry.getValue(); | |
| if(element instanceof JsonObject) { | |
| JsonObject originalElement = (JsonObject) original.get(entry.getKey()); | |
| delta.add(entry.getKey(), getUpdatedFields(originalElement, (JsonObject) element)); | |
| } else { | |
| if(!entry.getValue().toString().contentEquals(original.get(entry.getKey()).toString())) { | |
| delta.add(entry.getKey(), entry.getValue()); | |
| } | |
| } | |
| } | |
| return delta; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment