react-native-navigation的迁移库

TitleOptions.java 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package com.reactnativenavigation.parse;
  2. import android.graphics.Typeface;
  3. import android.support.annotation.Nullable;
  4. import com.reactnativenavigation.parse.params.Color;
  5. import com.reactnativenavigation.parse.params.Fraction;
  6. import com.reactnativenavigation.parse.params.NullColor;
  7. import com.reactnativenavigation.parse.params.NullFraction;
  8. import com.reactnativenavigation.parse.params.NullNumber;
  9. import com.reactnativenavigation.parse.params.NullText;
  10. import com.reactnativenavigation.parse.params.Number;
  11. import com.reactnativenavigation.parse.params.Text;
  12. import com.reactnativenavigation.parse.parsers.ColorParser;
  13. import com.reactnativenavigation.parse.parsers.FractionParser;
  14. import com.reactnativenavigation.parse.parsers.TextParser;
  15. import com.reactnativenavigation.utils.TypefaceLoader;
  16. import org.json.JSONObject;
  17. public class TitleOptions {
  18. public static TitleOptions parse(TypefaceLoader typefaceManager, JSONObject json) {
  19. final TitleOptions options = new TitleOptions();
  20. if (json == null) return options;
  21. options.component = Component.parse(json.optJSONObject("component"));
  22. options.text = TextParser.parse(json, "text");
  23. options.color = ColorParser.parse(json, "color");
  24. options.fontSize = FractionParser.parse(json, "fontSize");
  25. options.fontFamily = typefaceManager.getTypeFace(json.optString("fontFamily", ""));
  26. options.alignment = Alignment.fromString(TextParser.parse(json, "alignment").get(""));
  27. return options;
  28. }
  29. public Text text = new NullText();
  30. public Color color = new NullColor();
  31. public Fraction fontSize = new NullFraction();
  32. public Alignment alignment = Alignment.Default;
  33. @Nullable public Typeface fontFamily;
  34. public Component component = new Component();
  35. public Number height = new NullNumber();
  36. void mergeWith(final TitleOptions other) {
  37. if (other.text.hasValue()) text = other.text;
  38. if (other.color.hasValue()) color = other.color;
  39. if (other.fontSize.hasValue()) fontSize = other.fontSize;
  40. if (other.fontFamily != null) fontFamily = other.fontFamily;
  41. if (other.alignment != Alignment.Default) alignment = other.alignment;
  42. if (other.component.hasValue()) component = other.component;
  43. if (other.height.hasValue()) height = other.height;
  44. }
  45. void mergeWithDefault(TitleOptions defaultOptions) {
  46. if (!text.hasValue()) text = defaultOptions.text;
  47. if (!color.hasValue()) color = defaultOptions.color;
  48. if (!fontSize.hasValue()) fontSize = defaultOptions.fontSize;
  49. if (fontFamily == null) fontFamily = defaultOptions.fontFamily;
  50. if (alignment == Alignment.Default) alignment = defaultOptions.alignment;
  51. component.mergeWithDefault(defaultOptions.component);
  52. if (!height.hasValue()) height = defaultOptions.height;
  53. }
  54. }