react-native-unistyles 3.0.0-alpha.4 → 3.0.0-alpha.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -117,6 +117,7 @@ void core::UnistylesRegistry::removeStyleSheet(int tag) {
117
117
 
118
118
  DependencyMap core::UnistylesRegistry::buildDependencyMap(std::vector<UnistyleDependency>& deps) {
119
119
  DependencyMap dependencyMap;
120
+ std::set<UnistyleDependency> uniqueDependencies(deps.begin(), deps.end());
120
121
 
121
122
  for (const auto& styleSheet : this->_styleSheetRegistry) {
122
123
  for (const auto& [_, unistyle] : styleSheet->unistyles) {
@@ -125,8 +126,8 @@ DependencyMap core::UnistylesRegistry::buildDependencyMap(std::vector<UnistyleDe
125
126
  bool hasAnyOfDependencies = std::any_of(
126
127
  unistyle->dependencies.begin(),
127
128
  unistyle->dependencies.end(),
128
- [&deps](UnistyleDependency dep) {
129
- return std::find(deps.begin(), deps.end(), dep) != deps.end();
129
+ [&uniqueDependencies](UnistyleDependency dep) {
130
+ return std::find(uniqueDependencies.begin(), uniqueDependencies.end(), dep) != uniqueDependencies.end();
130
131
  }
131
132
  );
132
133
 
@@ -1,5 +1,6 @@
1
1
  #pragma once
2
2
 
3
+ #include "set"
3
4
  #include <jsi/jsi.h>
4
5
  #include <react/renderer/uimanager/UIManager.h>
5
6
  #include <unordered_map>
@@ -221,9 +221,17 @@ void HybridStyleSheet::loadExternalMethods(const jsi::Value& thisValue, jsi::Run
221
221
  void HybridStyleSheet::onPlatformDependenciesChange(std::vector<UnistyleDependency> dependencies) {
222
222
  auto& registry = core::UnistylesRegistry::get();
223
223
  auto parser = parser::Parser(this->_unistylesRuntime);
224
- auto dependencyMap = registry.buildDependencyMap(dependencies);
225
224
  auto& rt = this->_unistylesRuntime->getRuntime();
226
225
 
226
+ // check if color scheme changed and then if Unistyles state depend on it (adaptive themes)
227
+ auto colorSchemeIt = std::find(dependencies.begin(), dependencies.end(), UnistyleDependency::COLORSCHEME);
228
+
229
+ if (colorSchemeIt != dependencies.end()) {
230
+ this->_unistylesRuntime->includeDependenciesForColorSchemeChange(dependencies);
231
+ }
232
+
233
+ auto dependencyMap = registry.buildDependencyMap(dependencies);
234
+
227
235
  if (dependencyMap.size() == 0) {
228
236
  return;
229
237
  }
@@ -71,7 +71,7 @@ void HybridUnistylesRuntime::setAdaptiveThemes(bool isEnabled) {
71
71
 
72
72
  std::vector<UnistyleDependency> changedDependencies{};
73
73
 
74
- changedDependencies.reserve(5);
74
+ changedDependencies.reserve(3);
75
75
 
76
76
  bool hadAdaptiveThemes = this->getHasAdaptiveThemes();
77
77
 
@@ -92,6 +92,11 @@ void HybridUnistylesRuntime::setAdaptiveThemes(bool isEnabled) {
92
92
 
93
93
  // if user enabled adaptive themes, then we need to make sure
94
94
  // we selected theme based on color scheme
95
+ this->calculateNewThemeAndDependencies(changedDependencies);
96
+ this->_onDependenciesChange(changedDependencies);
97
+ };
98
+
99
+ void HybridUnistylesRuntime::calculateNewThemeAndDependencies(std::vector<UnistyleDependency>& changedDependencies) {
95
100
  auto& state = core::UnistylesRegistry::get().getState(*_rt);
96
101
  auto colorScheme = this->getColorScheme();
97
102
  auto currentThemeName = this->getThemeName();
@@ -105,9 +110,7 @@ void HybridUnistylesRuntime::setAdaptiveThemes(bool isEnabled) {
105
110
 
106
111
  state.setTheme(nextTheme);
107
112
  }
108
-
109
- this->_onDependenciesChange(changedDependencies);
110
- };
113
+ }
111
114
 
112
115
  jsi::Value HybridUnistylesRuntime::updateTheme(jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
113
116
  helpers::assertThat(rt, count == 2, "UnistylesRuntime.updateTheme expected to be called with 2 arguments.");
@@ -201,3 +204,15 @@ void HybridUnistylesRuntime::registerPlatformListener(const std::function<void(s
201
204
  this->_nativePlatform.registerPlatformListener(listener);
202
205
  this->_onDependenciesChange = listener;
203
206
  }
207
+
208
+ void HybridUnistylesRuntime::includeDependenciesForColorSchemeChange(std::vector<UnistyleDependency>& deps) {
209
+ auto& registry = core::UnistylesRegistry::get();
210
+ auto& state = registry.getState(*this->_rt);
211
+
212
+ // ignore color scheme changes if user has no adaptive themes
213
+ if (!state.hasAdaptiveThemes()) {
214
+ return;
215
+ }
216
+
217
+ this->calculateNewThemeAndDependencies(deps);
218
+ }
@@ -58,6 +58,8 @@ struct HybridUnistylesRuntime: public HybridUnistylesRuntimeSpec {
58
58
  UnistylesCxxMiniRuntime getMiniRuntime() override;
59
59
  jsi::Value getMiniRuntimeAsValue(jsi::Runtime& rt);
60
60
  jsi::Runtime& getRuntime();
61
+ void includeDependenciesForColorSchemeChange(std::vector<UnistyleDependency>& deps);
62
+ void calculateNewThemeAndDependencies(std::vector<UnistyleDependency>& deps);
61
63
 
62
64
  private:
63
65
  jsi::Runtime* _rt;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-unistyles",
3
- "version": "3.0.0-alpha.4",
3
+ "version": "3.0.0-alpha.5",
4
4
  "description": "Level up your React Native StyleSheet",
5
5
  "scripts": {
6
6
  "test": "jest",