react-native-navigation的迁移库

OrientationOptions.java 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package com.reactnativenavigation.parse;
  2. import android.support.annotation.CheckResult;
  3. import com.reactnativenavigation.parse.params.Orientation;
  4. import org.json.JSONArray;
  5. import org.json.JSONObject;
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8. import java.util.List;
  9. public class OrientationOptions {
  10. List<Orientation> orientations = new ArrayList<>();
  11. public static OrientationOptions parse(JSONObject json) {
  12. OrientationOptions options = new OrientationOptions();
  13. if (json == null) return options;
  14. JSONArray orientations = json.optJSONArray("orientation");
  15. if (orientations == null) {
  16. String orientation = json.optString("orientation", Orientation.Default.name);
  17. options.orientations.add(Orientation.fromString(orientation));
  18. } else {
  19. List<Orientation> parsed = new ArrayList<>();
  20. for (int i = 0; i < orientations.length(); i++) {
  21. Orientation o = Orientation.fromString(orientations.optString(i, "default"));
  22. if (o != null) {
  23. parsed.add(o);
  24. }
  25. }
  26. options.orientations = parsed;
  27. }
  28. return options;
  29. }
  30. public int getValue() {
  31. if (!hasValue()) return Orientation.Default.orientationCode;
  32. switch (orientations.get(0)) {
  33. case Landscape:
  34. return orientations.contains(Orientation.Portrait) ? Orientation.PortraitLandscape.orientationCode : Orientation.Landscape.orientationCode;
  35. case Portrait:
  36. return orientations.contains(Orientation.Landscape) ? Orientation.PortraitLandscape.orientationCode : Orientation.Portrait.orientationCode;
  37. default:
  38. case Default:
  39. return Orientation.Default.orientationCode;
  40. }
  41. }
  42. public boolean hasValue() {
  43. return !orientations.isEmpty();
  44. }
  45. @CheckResult
  46. public OrientationOptions copy() {
  47. OrientationOptions result = new OrientationOptions();
  48. result.orientations = new ArrayList<>(orientations);
  49. return result;
  50. }
  51. public OrientationOptions mergeWithDefault(OrientationOptions defaultOptions) {
  52. if (!hasValue()) orientations = defaultOptions.orientations;
  53. return this;
  54. }
  55. @Override
  56. public String toString() {
  57. return hasValue() ? Arrays.toString(orientations.toArray(new Orientation[0])) : Orientation.Default.toString();
  58. }
  59. }