From e9dc64542312db41c584fe8333d5a5f228a8145b Mon Sep 17 00:00:00 2001 From: Paco Estevez Garcia Date: Mon, 19 Oct 2020 05:58:02 -0700 Subject: [PATCH] Add Android support for Timeline detail view Summary: This diff adds the Java classes that serialize to the data expected by the new timeline widget. See D23865369 for the JS counterpart. Currently the Flipper plugin uses `toString` to get the wire values. I'm not feeling like using a json library or dealing with JSONObject's nonsense, so these are rolled manually for now. Reviewed By: astreet Differential Revision: D24254377 fbshipit-source-id: b2fc00400c40b47ac29c9b83c0b66621c3677974 --- .../plugins/inspector/InspectorValue.java | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/android/src/main/java/com/facebook/flipper/plugins/inspector/InspectorValue.java b/android/src/main/java/com/facebook/flipper/plugins/inspector/InspectorValue.java index 84b801ae0..31b794641 100644 --- a/android/src/main/java/com/facebook/flipper/plugins/inspector/InspectorValue.java +++ b/android/src/main/java/com/facebook/flipper/plugins/inspector/InspectorValue.java @@ -9,7 +9,14 @@ package com.facebook.flipper.plugins.inspector; import com.facebook.flipper.core.FlipperObject; import com.facebook.flipper.core.FlipperValue; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Map; import java.util.Set; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; public class InspectorValue implements FlipperValue { @@ -30,6 +37,7 @@ public class InspectorValue implements FlipperValue { public static final Type Enum = new Type<>("enum"); public static final Type Color = new Type<>("color"); public static final Type Picker = new Type<>("picker"); + public static final Type Timeline = new Type<>("timeline"); private final String mName; @@ -108,4 +116,84 @@ public class InspectorValue implements FlipperValue { return b.toString(); } } + + /** + * A widget that represents a timeline. Each point has a moment to be placed on the timeline, and + * a key to be identified as. The current field represents the key of the point in the timeline + * that matches the current moment in time. + */ + public static final class Timeline { + public final List time; + public final String current; + + public Timeline(List time, String current) { + Collections.sort( + time, + new Comparator() { + @Override + public int compare(TimePoint stringTimePointEntry, TimePoint t1) { + return Float.compare(stringTimePointEntry.moment, t1.moment); + } + }); + this.time = time; + this.current = current; + } + + private JSONObject toJson() { + final JSONArray points = new JSONArray(); + for (TimePoint value : time) { + points.put(value.toJson()); + } + try { + return new JSONObject().put("time", points).put("current", current); + } catch (JSONException t) { + throw new RuntimeException(t); + } + } + + @Override + public String toString() { + return toJson().toString(); + } + + /** + * An entry in the timeline, identified by its key. They're sorted in Flipper by moment, and are + * rendered according to the display and color. Any additional properties attached to the point + * will be displayed when it's selected. + */ + public static final class TimePoint { + public final long moment; + public final String display; + public final String color; + public final String key; + public final Map properties; + + public TimePoint( + String key, long moment, String display, String color, Map properties) { + this.key = key; + this.moment = moment; + this.display = display; + this.color = color; + this.properties = properties; + } + + private JSONObject toJson() { + try { + return new JSONObject() + .put("moment", moment) + .put("display", display) + .put("color", color) + .put("key", key) + .put("properties", new JSONObject(properties)); + } catch (JSONException t) { + throw new RuntimeException(t); + } + } + + @Override + public String toString() { + return toJson().toString(); + } + } + } }