react-native-navigation的迁移库

StyleParamsParser.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package com.reactnativenavigation.params.parsers;
  2. import android.graphics.Color;
  3. import android.os.Bundle;
  4. import com.reactnativenavigation.params.AppStyle;
  5. import com.reactnativenavigation.params.StyleParams;
  6. public class StyleParamsParser {
  7. private Bundle params;
  8. public StyleParamsParser(Bundle params) {
  9. this.params = params;
  10. }
  11. public StyleParams parse() {
  12. if (params == null) {
  13. return createDefaultStyleParams();
  14. }
  15. StyleParams result = new StyleParams();
  16. result.statusBarColor = getColor("statusBarColor", getDefaultStatusBarColor());
  17. result.contextualMenuStatusBarColor = getColor("contextualMenuStatusBarColor", getDefaultContextualMenuStatusBarColor());
  18. result.contextualMenuButtonsColor = getColor("contextualMenuButtonsColor", getDefaultContextualMenuButtonsColor());
  19. result.contextualMenuBackgroundColor = getColor("contextualMenuBackgroundColor", getDefaultContextualMenuBackgroundColor());
  20. result.topBarColor = getColor("topBarColor", getDefaultTopBarColor());
  21. result.titleBarHideOnScroll = getBoolean("titleBarHideOnScroll", getDefaultTitleBarHideOnScroll());
  22. result.topBarTransparent = getBoolean("topBarTransparent", getDefaultTopBarHidden());
  23. result.drawScreenBelowTopBar = params.getBoolean("drawBelowTopBar", getDefaultScreenBelowTopBar());
  24. if (result.topBarTransparent) {
  25. result.drawScreenBelowTopBar = false;
  26. }
  27. result.collapsingTopBarParams = new CollapsingTopBarParamsParser(params, result.titleBarHideOnScroll, result.drawScreenBelowTopBar).parse();
  28. result.titleBarHidden = getBoolean("titleBarHidden", getDefaultTopBarHidden());
  29. result.topBarElevationShadowEnabled = getBoolean("topBarElevationShadowEnabled", getDefaultTopBarElevationShadowEnabled());
  30. result.titleBarTitleColor = getColor("titleBarTitleColor", getDefaultTitleBarColor());
  31. result.topBarTranslucent = getBoolean("topBarTranslucent", getDefaultTopBarTranslucent());
  32. result.titleBarSubtitleColor = getColor("titleBarSubtitleColor", getDefaultSubtitleBarColor());
  33. result.titleBarButtonColor = getColor("titleBarButtonColor", getTitleBarButtonColor());
  34. result.titleBarDisabledButtonColor = getColor("titleBarDisabledButtonColor", getTitleBarDisabledButtonColor());
  35. result.backButtonHidden = getBoolean("backButtonHidden", getDefaultBackButtonHidden());
  36. result.topTabsHidden = getBoolean("topTabsHidden", getDefaultTopTabsHidden());
  37. result.topTabTextColor = getColor("topTabTextColor", getDefaultTopTabTextColor());
  38. result.selectedTopTabTextColor = getColor("selectedTopTabTextColor", getDefaultSelectedTopTabTextColor());
  39. result.selectedTopTabIndicatorHeight = getInt("selectedTopTabIndicatorHeight", getDefaultSelectedTopTabIndicatorHeight());
  40. result.selectedTopTabIndicatorColor = getColor("selectedTopTabIndicatorColor", getDefaultSelectedTopTabIndicatorColor());
  41. result.screenBackgroundColor = getColor("screenBackgroundColor", getDefaultScreenBackgroundColor());
  42. result.bottomTabsHidden = getBoolean("bottomTabsHidden", getDefaultBottomTabsHidden());
  43. result.drawScreenAboveBottomTabs = !result.bottomTabsHidden &&
  44. params.getBoolean("drawScreenAboveBottomTabs", getDefaultDrawScreenAboveBottomTabs());
  45. if (result.titleBarHideOnScroll) {
  46. result.drawScreenAboveBottomTabs = false;
  47. }
  48. result.bottomTabsHiddenOnScroll = getBoolean("bottomTabsHiddenOnScroll", getDefaultBottomTabsHiddenOnScroll());
  49. result.bottomTabsColor = getColor("bottomTabsColor", getDefaultBottomTabsColor());
  50. result.bottomTabsButtonColor = getColor("bottomTabsButtonColor", getDefaultBottomTabsButtonColor());
  51. result.selectedBottomTabsButtonColor =
  52. getColor("bottomTabsSelectedButtonColor", getDefaultSelectedBottomTabsButtonColor());
  53. result.bottomTabBadgeTextColor = getColor("bottomTabBadgeTextColor", getBottomTabBadgeTextColor());
  54. result.bottomTabBadgeBackgroundColor = getColor("bottomTabBadgeBackgroundColor", getBottomTabBadgeBackgroundColor());
  55. result.navigationBarColor = getColor("navigationBarColor", getDefaultNavigationColor());
  56. result.forceTitlesDisplay = getBoolean("forceTitlesDisplay", getDefaultForceTitlesDisplay());
  57. return result;
  58. }
  59. private StyleParams createDefaultStyleParams() {
  60. StyleParams result = new StyleParams();
  61. result.titleBarDisabledButtonColor = getTitleBarDisabledButtonColor();
  62. result.topBarElevationShadowEnabled = true;
  63. result.titleBarHideOnScroll = false;
  64. return result;
  65. }
  66. private StyleParams.Color getDefaultContextualMenuStatusBarColor() {
  67. return new StyleParams.Color(Color.parseColor("#7c7c7c"));
  68. }
  69. private StyleParams.Color getDefaultContextualMenuBackgroundColor() {
  70. return new StyleParams.Color(Color.WHITE);
  71. }
  72. private StyleParams.Color getDefaultContextualMenuButtonsColor() {
  73. return new StyleParams.Color(Color.parseColor("#757575"));
  74. }
  75. private boolean getDefaultDrawScreenAboveBottomTabs() {
  76. return AppStyle.appStyle == null || AppStyle.appStyle.drawScreenAboveBottomTabs;
  77. }
  78. private StyleParams.Color getDefaultSelectedTopTabIndicatorColor() {
  79. return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.selectedTopTabIndicatorColor;
  80. }
  81. private int getDefaultSelectedTopTabIndicatorHeight() {
  82. return AppStyle.appStyle == null ? -1 : AppStyle.appStyle.selectedTopTabIndicatorHeight;
  83. }
  84. private StyleParams.Color getDefaultSelectedTopTabTextColor() {
  85. return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.selectedTopTabTextColor;
  86. }
  87. private StyleParams.Color getDefaultNavigationColor() {
  88. return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.navigationBarColor;
  89. }
  90. private boolean getDefaultForceTitlesDisplay() {
  91. return AppStyle.appStyle != null && AppStyle.appStyle.forceTitlesDisplay;
  92. }
  93. private StyleParams.Color getDefaultSelectedBottomTabsButtonColor() {
  94. return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.selectedBottomTabsButtonColor;
  95. }
  96. private StyleParams.Color getBottomTabBadgeTextColor() {
  97. return new StyleParams.Color();
  98. }
  99. private StyleParams.Color getBottomTabBadgeBackgroundColor() {
  100. return new StyleParams.Color();
  101. }
  102. private StyleParams.Color getDefaultBottomTabsButtonColor() {
  103. return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.bottomTabsButtonColor;
  104. }
  105. private StyleParams.Color getDefaultBottomTabsColor() {
  106. return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.bottomTabsColor;
  107. }
  108. private boolean getDefaultBottomTabsHiddenOnScroll() {
  109. return AppStyle.appStyle != null && AppStyle.appStyle.bottomTabsHiddenOnScroll;
  110. }
  111. private boolean getDefaultBottomTabsHidden() {
  112. return AppStyle.appStyle != null && AppStyle.appStyle.bottomTabsHidden;
  113. }
  114. private boolean getDefaultScreenBelowTopBar() {
  115. return AppStyle.appStyle != null && AppStyle.appStyle.drawScreenBelowTopBar;
  116. }
  117. private StyleParams.Color getDefaultScreenBackgroundColor() {
  118. return AppStyle.appStyle != null ? AppStyle.appStyle.screenBackgroundColor : getColor("screenBackgroundColor", new StyleParams.Color());
  119. }
  120. private boolean getDefaultTopTabsHidden() {
  121. return AppStyle.appStyle != null && AppStyle.appStyle.topTabsHidden;
  122. }
  123. private StyleParams.Color getDefaultTopTabTextColor() {
  124. return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.topTabTextColor;
  125. }
  126. private boolean getDefaultBackButtonHidden() {
  127. return AppStyle.appStyle != null && AppStyle.appStyle.backButtonHidden;
  128. }
  129. private StyleParams.Color getDefaultTitleBarColor() {
  130. return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.titleBarTitleColor;
  131. }
  132. private StyleParams.Color getDefaultSubtitleBarColor() {
  133. return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.titleBarSubtitleColor;
  134. }
  135. private StyleParams.Color getTitleBarButtonColor() {
  136. return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.titleBarButtonColor;
  137. }
  138. private StyleParams.Color getTitleBarDisabledButtonColor() {
  139. return AppStyle.appStyle == null ? new StyleParams.Color(Color.LTGRAY) : AppStyle.appStyle.titleBarDisabledButtonColor;
  140. }
  141. private boolean getDefaultTopBarHidden() {
  142. return AppStyle.appStyle != null && AppStyle.appStyle.topBarTransparent;
  143. }
  144. private boolean getDefaultTopBarElevationShadowEnabled() {
  145. return AppStyle.appStyle == null || AppStyle.appStyle.topBarElevationShadowEnabled;
  146. }
  147. private boolean getDefaultTopBarTranslucent() {
  148. return AppStyle.appStyle != null && AppStyle.appStyle.topBarTranslucent;
  149. }
  150. private boolean getDefaultTitleBarHideOnScroll() {
  151. return AppStyle.appStyle != null && AppStyle.appStyle.titleBarHideOnScroll;
  152. }
  153. private StyleParams.Color getDefaultTopBarColor() {
  154. return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.topBarColor;
  155. }
  156. private StyleParams.Color getDefaultStatusBarColor() {
  157. return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.statusBarColor;
  158. }
  159. private boolean getBoolean(String key, boolean defaultValue) {
  160. return params.containsKey(key) ? params.getBoolean(key) : defaultValue;
  161. }
  162. private StyleParams.Color getColor(String key, StyleParams.Color defaultColor) {
  163. StyleParams.Color color = StyleParams.Color.parse(params, key);
  164. if (color.hasColor()) {
  165. return color;
  166. } else {
  167. return defaultColor != null && defaultColor.hasColor() ? defaultColor : color;
  168. }
  169. }
  170. private int getInt(String key, int defaultValue) {
  171. return params.containsKey(key) ? params.getInt(key) : defaultValue;
  172. }
  173. }