react-native-unistyles 2.7.2 → 2.8.0-beta.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. package/android/CMakeLists.txt +8 -1
  2. package/android/build.gradle +4 -1
  3. package/android/src/main/cxx/cpp-adapter.cpp +10 -100
  4. package/android/src/main/cxx/helpers.h +2 -0
  5. package/android/src/main/cxx/platform.cpp +126 -0
  6. package/android/src/main/cxx/platform.h +15 -0
  7. package/android/src/main/java/com/unistyles/Config.kt +31 -5
  8. package/android/src/main/java/com/unistyles/Platform.kt +24 -0
  9. package/android/src/main/java/com/unistyles/UnistylesModule.kt +24 -87
  10. package/cxx/Macros.h +11 -0
  11. package/cxx/UnistylesImpl.cpp +241 -0
  12. package/cxx/UnistylesModel.cpp +230 -0
  13. package/cxx/UnistylesModel.h +112 -0
  14. package/cxx/UnistylesRuntime.cpp +17 -388
  15. package/cxx/UnistylesRuntime.h +56 -95
  16. package/ios/UnistylesModule.h +8 -0
  17. package/ios/UnistylesModule.mm +12 -89
  18. package/ios/platform/Platform_Shared.h +5 -0
  19. package/ios/platform/Platform_Shared.mm +69 -0
  20. package/ios/platform/Platform_iOS.h +3 -10
  21. package/ios/platform/Platform_iOS.mm +53 -96
  22. package/ios/platform/Platform_macOS.h +2 -7
  23. package/ios/platform/Platform_macOS.mm +36 -36
  24. package/ios/platform/Platform_tvOS.h +2 -9
  25. package/ios/platform/Platform_tvOS.mm +30 -92
  26. package/ios/platform/Platform_visionOS.h +3 -9
  27. package/ios/platform/Platform_visionOS.mm +29 -84
  28. package/lib/commonjs/common.js.map +1 -1
  29. package/lib/commonjs/normalizer/normalizeStyle.js.map +1 -1
  30. package/lib/commonjs/normalizer/normalizer.js +1 -1
  31. package/lib/commonjs/normalizer/normalizer.js.map +1 -1
  32. package/lib/commonjs/utils/cssMediaQuery.js.map +1 -1
  33. package/lib/commonjs/utils/generateId.js.map +1 -1
  34. package/lib/commonjs/utils/mq.js.map +1 -1
  35. package/lib/module/common.js.map +1 -1
  36. package/lib/module/normalizer/normalizeStyle.js.map +1 -1
  37. package/lib/module/normalizer/normalizer.js.map +1 -1
  38. package/lib/module/utils/cssMediaQuery.js.map +1 -1
  39. package/lib/module/utils/generateId.js.map +1 -1
  40. package/lib/module/utils/mq.js.map +1 -1
  41. package/package.json +2 -1
  42. package/react-native-unistyles.podspec +3 -0
  43. package/ios/UnistylesHelpers.h +0 -3
  44. package/ios/UnistylesHelpers.mm +0 -5
@@ -5,8 +5,11 @@ project(unistyles)
5
5
  add_library(unistyles
6
6
  SHARED
7
7
  ../cxx/UnistylesRuntime.cpp
8
+ ../cxx/UnistylesModel.cpp
9
+ ../cxx/UnistylesImpl.cpp
8
10
  ./src/main/cxx/cpp-adapter.cpp
9
11
  ./src/main/cxx/helpers.cpp
12
+ ./src/main/cxx/platform.cpp
10
13
  )
11
14
 
12
15
  include_directories(
@@ -14,15 +17,19 @@ include_directories(
14
17
  )
15
18
 
16
19
  set_target_properties(unistyles PROPERTIES
17
- CXX_STANDARD 17
20
+ CXX_STANDARD 20
18
21
  CXX_STANDARD_REQUIRED ON
19
22
  CXX_EXTENSIONS OFF
20
23
  POSITION_INDEPENDENT_CODE ON
21
24
  )
22
25
 
23
26
  find_package(ReactAndroid REQUIRED CONFIG)
27
+ find_package(fbjni REQUIRED CONFIG)
24
28
 
25
29
  target_link_libraries(unistyles
26
30
  ReactAndroid::jsi
31
+ ReactAndroid::turbomodulejsijni
32
+ ReactAndroid::react_nativemodule_core
27
33
  android
34
+ fbjni::fbjni
28
35
  )
@@ -60,7 +60,10 @@ android {
60
60
  "META-INF",
61
61
  "META-INF/**",
62
62
  "**/libjsi.so",
63
- "**/libc++_shared.so"
63
+ "**/libc++_shared.so",
64
+ "**/libreact_nativemodule_core.so",
65
+ "**/libturbomodulejsijni.so",
66
+ "**/libfbjni.so"
64
67
  ]
65
68
  }
66
69
  }
@@ -1,8 +1,10 @@
1
1
  #include <jni.h>
2
2
  #include <jsi/jsi.h>
3
3
  #include <map>
4
+ #include <ReactCommon/CallInvokerHolder.h>
4
5
  #include "UnistylesRuntime.h"
5
6
  #include "helpers.h"
7
+ #include "platform.h"
6
8
 
7
9
  using namespace facebook;
8
10
 
@@ -11,116 +13,24 @@ std::shared_ptr<UnistylesRuntime> unistylesRuntime = nullptr;
11
13
 
12
14
  extern "C"
13
15
  JNIEXPORT void JNICALL
14
- Java_com_unistyles_UnistylesModule_nativeInstall(
15
- JNIEnv *env,
16
- jobject thiz,
17
- jlong jsi,
18
- jobject screen,
19
- jstring colorScheme,
20
- jstring contentSizeCategory,
21
- jobject insets,
22
- jobject statusBar,
23
- jobject navigationBar
24
- ) {
25
- auto runtime = reinterpret_cast<facebook::jsi::Runtime *>(jsi);
16
+ Java_com_unistyles_UnistylesModule_nativeInstall(JNIEnv *env, jobject thiz, jlong jsi, jobject callInvokerHolder) {
17
+ auto runtime = reinterpret_cast<jsi::Runtime *>(jsi);
18
+ auto callInvoker{
19
+ jni::alias_ref<react::CallInvokerHolder::javaobject>{ reinterpret_cast<react::CallInvokerHolder::javaobject>(callInvokerHolder)} -> cthis() ->getCallInvoker()
20
+ };
26
21
 
27
22
  if (unistylesModule == nullptr) {
28
23
  unistylesModule = env->NewGlobalRef(thiz);
29
24
  }
30
25
 
31
- if (runtime == nullptr || unistylesModule == nullptr) {
26
+ if (unistylesModule == nullptr) {
32
27
  return throwKotlinException(env, "Something went wrong while initializing UnistylesModule");
33
28
  }
34
29
 
35
- const char *colorSchemeChars = env->GetStringUTFChars(colorScheme, nullptr);
36
- std::string colorSchemeStr(colorSchemeChars);
37
- env->ReleaseStringUTFChars(colorScheme, colorSchemeChars);
38
-
39
- const char *contentSizeCategoryChars = env->GetStringUTFChars(contentSizeCategory, nullptr);
40
- std::string contentSizeCategoryStr(contentSizeCategoryChars);
41
- env->ReleaseStringUTFChars(contentSizeCategory, contentSizeCategoryChars);
42
-
43
- unistylesRuntime = std::make_shared<UnistylesRuntime>(
44
- jobjectToDimensions(env, screen),
45
- colorSchemeStr,
46
- contentSizeCategoryStr,
47
- jobjectToInsets(env, insets),
48
- jobjectToDimensions(env, statusBar),
49
- jobjectToDimensions(env, navigationBar)
50
- );
51
-
52
- unistylesRuntime->onThemeChange([=](const std::string &theme) {
53
- jstring themeStr = env->NewStringUTF(theme.c_str());
54
- jclass cls = env->GetObjectClass(unistylesModule);
55
- jmethodID methodId = env->GetMethodID(cls, "onThemeChange", "(Ljava/lang/String;)V");
56
-
57
- env->CallVoidMethod(unistylesModule, methodId, themeStr);
58
- env->DeleteLocalRef(themeStr);
59
- env->DeleteLocalRef(cls);
60
- });
61
-
62
- unistylesRuntime->onLayoutChange([=](const std::string &breakpoint, const std::string &orientation, Dimensions& screen, Dimensions& statusBar, Insets& insets, Dimensions& navigationBar) {
63
- jstring breakpointStr = env->NewStringUTF(breakpoint.c_str());
64
- jstring orientationStr = env->NewStringUTF(orientation.c_str());
65
- jclass cls = env->GetObjectClass(unistylesModule);
66
- jclass dimensionsClass = env->FindClass("com/unistyles/Dimensions");
67
- jmethodID dimensionsConstructor = env->GetMethodID(dimensionsClass, "<init>", "(II)V");
68
- jobject screenObj = env->NewObject(dimensionsClass, dimensionsConstructor, screen.width, screen.height);
69
- jobject statusBarObj = env->NewObject(dimensionsClass, dimensionsConstructor, statusBar.width, statusBar.height);
70
- jobject navigationBarObj = env->NewObject(dimensionsClass, dimensionsConstructor, navigationBar.width, navigationBar.height);
71
-
72
- jclass insetsClass = env->FindClass("com/unistyles/Insets");
73
- jmethodID insetsConstructor = env->GetMethodID(insetsClass, "<init>", "(IIII)V");
74
- jobject insetsObj = env->NewObject(insetsClass, insetsConstructor, insets.left, insets.top, insets.right, insets.bottom);
75
- jmethodID methodId = env->GetMethodID(cls, "onLayoutChange",
76
- "(Ljava/lang/String;Ljava/lang/String;Lcom/unistyles/Dimensions;Lcom/unistyles/Dimensions;Lcom/unistyles/Insets;Lcom/unistyles/Dimensions;)V");
77
-
78
- env->CallVoidMethod(unistylesModule, methodId, breakpointStr, orientationStr, screenObj, statusBarObj, insetsObj, navigationBarObj);
79
- env->DeleteLocalRef(breakpointStr);
80
- env->DeleteLocalRef(orientationStr);
81
- env->DeleteLocalRef(cls);
82
- });
83
-
84
- unistylesRuntime->onPluginChange([=]() {
85
- jclass cls = env->GetObjectClass(unistylesModule);
86
- jmethodID methodId = env->GetMethodID(cls, "onPluginChange", "()V");
87
-
88
- env->CallVoidMethod(unistylesModule, methodId);
89
- env->DeleteLocalRef(cls);
90
- });
91
-
92
- unistylesRuntime->onContentSizeCategoryChange([=](const std::string &contentSizeCategory) {
93
- jstring contentSizeCategoryStr = env->NewStringUTF(contentSizeCategory.c_str());
94
- jclass cls = env->GetObjectClass(unistylesModule);
95
- jmethodID methodId = env->GetMethodID(cls, "onContentSizeCategoryChange", "(Ljava/lang/String;)V");
96
-
97
- env->CallVoidMethod(unistylesModule, methodId, contentSizeCategoryStr);
98
- env->DeleteLocalRef(contentSizeCategoryStr);
99
- env->DeleteLocalRef(cls);
100
- });
101
-
102
- unistylesRuntime->onSetNavigationBarColor([=](const std::string &color) {
103
- jstring colorStr = env->NewStringUTF(color.c_str());
104
- jclass cls = env->GetObjectClass(unistylesModule);
105
- jmethodID methodId = env->GetMethodID(cls, "onSetNavigationBarColor", "(Ljava/lang/String;)V");
106
-
107
- env->CallVoidMethod(unistylesModule, methodId, colorStr);
108
- env->DeleteLocalRef(colorStr);
109
- env->DeleteLocalRef(cls);
110
- });
111
-
112
- unistylesRuntime->onSetStatusBarColor([=](const std::string &color) {
113
- jstring colorStr = env->NewStringUTF(color.c_str());
114
- jclass cls = env->GetObjectClass(unistylesModule);
115
- jmethodID methodId = env->GetMethodID(cls, "onSetStatusBarColor", "(Ljava/lang/String;)V");
116
-
117
- env->CallVoidMethod(unistylesModule, methodId, colorStr);
118
- env->DeleteLocalRef(colorStr);
119
- env->DeleteLocalRef(cls);
120
- });
30
+ unistylesRuntime = std::make_shared<UnistylesRuntime>(*runtime, callInvoker);
31
+ makeShared(env, unistylesModule, unistylesRuntime);
121
32
 
122
33
  jsi::Object hostObject = jsi::Object::createFromHostObject(*runtime, unistylesRuntime);
123
-
124
34
  runtime->global().setProperty(*runtime, "__UNISTYLES__", std::move(hostObject));
125
35
  }
126
36
 
@@ -1,3 +1,5 @@
1
+ #pragma once
2
+
1
3
  #include <jni.h>
2
4
  #include <string>
3
5
  #include <map>
@@ -0,0 +1,126 @@
1
+ #include "platform.h"
2
+
3
+ void makeShared(JNIEnv *env, jobject unistylesModule, std::shared_ptr<UnistylesRuntime> unistylesRuntime) {
4
+ unistylesRuntime->setScreenDimensionsCallback([&](){
5
+ return getScreenDimensions(env, unistylesModule);
6
+ });
7
+
8
+ unistylesRuntime->setColorSchemeCallback([&](){
9
+ return getColorScheme(env, unistylesModule);
10
+ });
11
+
12
+ unistylesRuntime->setStatusBarDimensionsCallback([&](){
13
+ return getStatusBarDimensions(env, unistylesModule);
14
+ });
15
+
16
+ unistylesRuntime->setNavigationBarDimensionsCallback([&](){
17
+ return getNavigationBarDimensions(env, unistylesModule);
18
+ });
19
+
20
+ unistylesRuntime->setInsetsCallback([&](){
21
+ return getInsets(env, unistylesModule);
22
+ });
23
+
24
+ unistylesRuntime->setContentSizeCategoryCallback([&](){
25
+ return getContentSizeCategory(env, unistylesModule);
26
+ });
27
+
28
+ unistylesRuntime->setNavigationBarColorCallback([&](const std::string &color) {
29
+ setNavigationBarColor(env, unistylesModule, color);
30
+ });
31
+
32
+ unistylesRuntime->setStatusBarColorCallback([&](const std::string &color) {
33
+ setStatusBarColor(env, unistylesModule, color);
34
+ });
35
+
36
+ unistylesRuntime->screen = getScreenDimensions(env, unistylesModule);
37
+ unistylesRuntime->contentSizeCategory = getContentSizeCategory(env, unistylesModule);
38
+ unistylesRuntime->colorScheme = getColorScheme(env, unistylesModule);
39
+ unistylesRuntime->statusBar = getStatusBarDimensions(env, unistylesModule);
40
+ unistylesRuntime->insets = getInsets(env, unistylesModule);
41
+ unistylesRuntime->navigationBar = getNavigationBarDimensions(env, unistylesModule);
42
+ }
43
+
44
+ Dimensions getScreenDimensions(JNIEnv *env, jobject unistylesModule) {
45
+ jclass cls = env->GetObjectClass(unistylesModule);
46
+ jmethodID methodId = env->GetMethodID(cls, "getScreenDimensions", "()Lcom/unistyles/Dimensions;");
47
+ jobject dimensionsObj = env->CallObjectMethod(unistylesModule, methodId);
48
+ Dimensions screenDimensions = jobjectToDimensions(env, dimensionsObj);
49
+
50
+ return screenDimensions;
51
+ }
52
+
53
+ std::string getColorScheme(JNIEnv *env, jobject unistylesModule) {
54
+ jclass cls = env->GetObjectClass(unistylesModule);
55
+ jmethodID methodId = env->GetMethodID(cls, "getColorScheme", "()Ljava/lang/String;");
56
+ jstring colorScheme = (jstring) env->CallObjectMethod(unistylesModule, methodId);
57
+ const char *colorSchemeChars = env->GetStringUTFChars(colorScheme, nullptr);
58
+ std::string colorSchemeStr = std::string(colorSchemeChars);
59
+
60
+ env->ReleaseStringUTFChars(colorScheme, colorSchemeChars);
61
+ env->DeleteLocalRef(colorScheme);
62
+ env->DeleteLocalRef(cls);
63
+
64
+ return colorSchemeStr;
65
+ }
66
+
67
+ Dimensions getStatusBarDimensions(JNIEnv *env, jobject unistylesModule) {
68
+ jclass cls = env->GetObjectClass(unistylesModule);
69
+ jmethodID methodId = env->GetMethodID(cls, "getStatusBarDimensions", "()Lcom/unistyles/Dimensions;");
70
+ jobject dimensionsObj = env->CallObjectMethod(unistylesModule, methodId);
71
+ Dimensions statusBarDimensions = jobjectToDimensions(env, dimensionsObj);
72
+
73
+ return statusBarDimensions;
74
+ }
75
+
76
+ Dimensions getNavigationBarDimensions(JNIEnv *env, jobject unistylesModule) {
77
+ jclass cls = env->GetObjectClass(unistylesModule);
78
+ jmethodID methodId = env->GetMethodID(cls, "getNavigationBarDimensions", "()Lcom/unistyles/Dimensions;");
79
+ jobject dimensionsObj = env->CallObjectMethod(unistylesModule, methodId);
80
+ Dimensions navigationBarDimensions = jobjectToDimensions(env, dimensionsObj);
81
+
82
+ return navigationBarDimensions;
83
+ }
84
+
85
+ Insets getInsets(JNIEnv *env, jobject unistylesModule) {
86
+ jclass cls = env->GetObjectClass(unistylesModule);
87
+ jmethodID methodId = env->GetMethodID(cls, "getInsets", "()Lcom/unistyles/Insets;");
88
+ jobject insetsObj = env->CallObjectMethod(unistylesModule, methodId);
89
+ Insets insets = jobjectToInsets(env, insetsObj);
90
+
91
+ return insets;
92
+ }
93
+
94
+ std::string getContentSizeCategory(JNIEnv *env, jobject unistylesModule) {
95
+ jclass cls = env->GetObjectClass(unistylesModule);
96
+ jmethodID methodId = env->GetMethodID(cls, "getContentSizeCategory", "()Ljava/lang/String;");
97
+ jstring contentSizeCategory = (jstring) env->CallObjectMethod(unistylesModule, methodId);
98
+ const char *contentSizeCategoryChars = env->GetStringUTFChars(contentSizeCategory, nullptr);
99
+ std::string contentSizeCategoryStr = std::string(contentSizeCategoryChars);
100
+
101
+ env->ReleaseStringUTFChars(contentSizeCategory, contentSizeCategoryChars);
102
+ env->DeleteLocalRef(contentSizeCategory);
103
+ env->DeleteLocalRef(cls);
104
+
105
+ return contentSizeCategoryStr;
106
+ }
107
+
108
+ void setStatusBarColor(JNIEnv *env, jobject unistylesModule, std::string color) {
109
+ jstring colorStr = env->NewStringUTF(color.c_str());
110
+ jclass cls = env->GetObjectClass(unistylesModule);
111
+ jmethodID methodId = env->GetMethodID(cls, "onSetStatusBarColor", "(Ljava/lang/String;)V");
112
+
113
+ env->CallVoidMethod(unistylesModule, methodId, colorStr);
114
+ env->DeleteLocalRef(colorStr);
115
+ env->DeleteLocalRef(cls);
116
+ }
117
+
118
+ void setNavigationBarColor(JNIEnv *env, jobject unistylesModule, std::string color) {
119
+ jstring colorStr = env->NewStringUTF(color.c_str());
120
+ jclass cls = env->GetObjectClass(unistylesModule);
121
+ jmethodID methodId = env->GetMethodID(cls, "onSetNavigationBarColor", "(Ljava/lang/String;)V");
122
+
123
+ env->CallVoidMethod(unistylesModule, methodId, colorStr);
124
+ env->DeleteLocalRef(colorStr);
125
+ env->DeleteLocalRef(cls);
126
+ }
@@ -0,0 +1,15 @@
1
+ #pragma once
2
+
3
+ #include "UnistylesRuntime.h"
4
+ #include "helpers.h"
5
+
6
+ void makeShared(JNIEnv *env, jobject unistylesModule, std::shared_ptr<UnistylesRuntime> unistylesRuntime);
7
+ Dimensions getScreenDimensions(JNIEnv *env, jobject unistylesModule);
8
+ std::string getColorScheme(JNIEnv *env, jobject unistylesModule);
9
+ Dimensions getStatusBarDimensions(JNIEnv *env, jobject unistylesModule);
10
+ Dimensions getNavigationBarDimensions(JNIEnv *env, jobject unistylesModule);
11
+ Insets getInsets(JNIEnv *env, jobject unistylesModule);
12
+ std::string getContentSizeCategory(JNIEnv *env, jobject unistylesModule);
13
+
14
+ void setNavigationBarColor(JNIEnv *env, jobject unistylesModule, std::string color);
15
+ void setStatusBarColor(JNIEnv *env, jobject unistylesModule, std::string color);
@@ -47,11 +47,9 @@ class UnistylesConfig(private val reactApplicationContext: ReactApplicationConte
47
47
  }
48
48
 
49
49
  private fun getAppConfig(): Config {
50
- val fontScale = reactApplicationContext.resources.configuration.fontScale
51
-
52
50
  return Config(
53
51
  this.getColorScheme(),
54
- this.getContentSizeCategory(fontScale),
52
+ this.getContentSizeCategory(),
55
53
  )
56
54
  }
57
55
 
@@ -68,7 +66,17 @@ class UnistylesConfig(private val reactApplicationContext: ReactApplicationConte
68
66
  )
69
67
  }
70
68
 
71
- private fun getContentSizeCategory(fontScale: Float): String {
69
+ fun getScreenDimensions(): Dimensions {
70
+ val displayMetrics = reactApplicationContext.resources.displayMetrics
71
+ val screenWidth = (displayMetrics.widthPixels / density).toInt()
72
+ val screenHeight = (displayMetrics.heightPixels / density).toInt()
73
+
74
+ return Dimensions(screenWidth, screenHeight)
75
+ }
76
+
77
+ fun getContentSizeCategory(): String {
78
+ val fontScale = reactApplicationContext.resources.configuration.fontScale
79
+
72
80
  val contentSizeCategory = when {
73
81
  fontScale <= 0.85f -> "Small"
74
82
  fontScale <= 1.0f -> "Default"
@@ -82,7 +90,7 @@ class UnistylesConfig(private val reactApplicationContext: ReactApplicationConte
82
90
  return contentSizeCategory
83
91
  }
84
92
 
85
- private fun getColorScheme(): String {
93
+ fun getColorScheme(): String {
86
94
  val colorScheme = when (reactApplicationContext.resources.configuration.uiMode.and(Configuration.UI_MODE_NIGHT_MASK)) {
87
95
  Configuration.UI_MODE_NIGHT_YES -> "dark"
88
96
  Configuration.UI_MODE_NIGHT_NO -> "light"
@@ -92,6 +100,24 @@ class UnistylesConfig(private val reactApplicationContext: ReactApplicationConte
92
100
  return colorScheme
93
101
  }
94
102
 
103
+ fun getStatusBarDimensions(): Dimensions {
104
+ val displayMetrics = reactApplicationContext.resources.displayMetrics
105
+ val screenWidth = (displayMetrics.widthPixels / density).toInt()
106
+
107
+ return Dimensions(screenWidth, getStatusBarHeight())
108
+ }
109
+
110
+ fun getNavigationBarDimensions(): Dimensions {
111
+ val displayMetrics = reactApplicationContext.resources.displayMetrics
112
+ val screenWidth = (displayMetrics.widthPixels / density).toInt()
113
+
114
+ return Dimensions(screenWidth, getNavigationBarHeight())
115
+ }
116
+
117
+ fun getInsets(): Insets {
118
+ return this.insets.get()
119
+ }
120
+
95
121
  @SuppressLint("InternalInsetResource", "DiscouragedApi")
96
122
  private fun getStatusBarHeight(): Int {
97
123
  val heightResId = reactApplicationContext.resources.getIdentifier("status_bar_height", "dimen", "android")
@@ -23,4 +23,28 @@ class Platform(reactApplicationContext: ReactApplicationContext) {
23
23
  fun getLayoutConfig(): LayoutConfig {
24
24
  return this.config.getLayoutConfig()
25
25
  }
26
+
27
+ fun getScreenDimensions(): Dimensions {
28
+ return this.config.getScreenDimensions()
29
+ }
30
+
31
+ fun getColorScheme(): String {
32
+ return this.config.getColorScheme()
33
+ }
34
+
35
+ fun getStatusBarDimensions(): Dimensions {
36
+ return this.config.getStatusBarDimensions()
37
+ }
38
+
39
+ fun getNavigationBarDimensions(): Dimensions {
40
+ return this.config.getNavigationBarDimensions()
41
+ }
42
+
43
+ fun getContentSizeCategory(): String {
44
+ return this.config.getContentSizeCategory()
45
+ }
46
+
47
+ fun getInsets(): Insets {
48
+ return this.config.getInsets()
49
+ }
26
50
  }
@@ -9,12 +9,11 @@ import android.os.Handler
9
9
  import android.os.Looper
10
10
  import android.util.Log
11
11
  import android.view.ViewTreeObserver
12
- import com.facebook.react.bridge.Arguments
13
12
  import com.facebook.react.bridge.LifecycleEventListener
14
13
  import com.facebook.react.bridge.ReactApplicationContext
15
14
  import com.facebook.react.bridge.ReactContextBaseJavaModule
16
15
  import com.facebook.react.bridge.ReactMethod
17
- import com.facebook.react.modules.core.DeviceEventManagerModule
16
+ import com.facebook.react.turbomodule.core.interfaces.CallInvokerHolder
18
17
 
19
18
  class UnistylesModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext), LifecycleEventListener {
20
19
  private val drawHandler = Handler(Looper.getMainLooper())
@@ -66,8 +65,7 @@ class UnistylesModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
66
65
  activity.window.decorView.rootView.viewTreeObserver.removeOnGlobalLayoutListener(layoutListener)
67
66
  }
68
67
 
69
- @Deprecated("Deprecated in Java")
70
- override fun onCatalystInstanceDestroy() {
68
+ override fun invalidate() {
71
69
  this.stopLayoutListener()
72
70
  reactApplicationContext.unregisterReceiver(configurationChangeReceiver)
73
71
  runnable?.let { drawHandler.removeCallbacks(it) }
@@ -124,24 +122,15 @@ class UnistylesModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
124
122
 
125
123
  this.platform = Platform(reactApplicationContext)
126
124
 
127
- val config = platform.getConfig()
128
- val layoutConfig = platform.getLayoutConfig()
125
+ this.reactApplicationContext.javaScriptContextHolder?.let { contextHolder ->
126
+ this.reactApplicationContext.catalystInstance.jsCallInvokerHolder?.let { callInvokerHolder: CallInvokerHolder ->
127
+ this.nativeInstall(contextHolder.get(), callInvokerHolder)
128
+ this.isCxxReady = true
129
129
 
130
- this.reactApplicationContext.javaScriptContextHolder?.let {
131
- this.nativeInstall(
132
- it.get(),
133
- layoutConfig.screen,
134
- config.colorScheme,
135
- config.contentSizeCategory,
136
- layoutConfig.insets,
137
- layoutConfig.statusBar,
138
- layoutConfig.navigationBar
139
- )
140
- this.isCxxReady = true
130
+ Log.i(NAME, "Installed Unistyles \uD83E\uDD84!")
141
131
 
142
- Log.i(NAME, "Installed Unistyles \uD83E\uDD84!")
143
-
144
- return true
132
+ return true
133
+ }
145
134
  }
146
135
 
147
136
  false
@@ -152,88 +141,36 @@ class UnistylesModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
152
141
  }
153
142
  }
154
143
 
155
- private external fun nativeInstall(
156
- jsi: Long,
157
- screen: Dimensions,
158
- colorScheme: String,
159
- contentSizeCategory: String,
160
- insets: Insets,
161
- statusBar: Dimensions,
162
- navigationBar: Dimensions
163
- )
144
+ private external fun nativeInstall(jsi: Long, callInvoker: CallInvokerHolder)
164
145
  private external fun nativeDestroy()
165
146
  private external fun nativeOnOrientationChange(screen: Dimensions, insets: Insets, statusBar: Dimensions, navigationBar: Dimensions)
166
147
  private external fun nativeOnAppearanceChange(colorScheme: String)
167
148
  private external fun nativeOnContentSizeCategoryChange(contentSizeCategory: String)
168
149
 
169
150
  //endregion
170
- //region Event emitter
171
- private fun onLayoutChange(breakpoint: String, orientation: String, screen: Dimensions, statusBar: Dimensions, insets: Insets, navigationBar: Dimensions) {
172
- val body = Arguments.createMap().apply {
173
- putString("type", "layout")
174
- putMap("payload", Arguments.createMap().apply {
175
- putString("breakpoint", breakpoint)
176
- putString("orientation", orientation)
177
- putMap("screen", Arguments.createMap().apply {
178
- putInt("width", screen.width)
179
- putInt("height", screen.height)
180
- })
181
- putMap("statusBar", Arguments.createMap().apply {
182
- putInt("width", statusBar.width)
183
- putInt("height", statusBar.height)
184
- })
185
- putMap("insets", Arguments.createMap().apply {
186
- putInt("top", insets.top)
187
- putInt("bottom", insets.bottom)
188
- putInt("left", insets.left)
189
- putInt("right", insets.right)
190
- })
191
- putMap("navigationBar", Arguments.createMap().apply {
192
- putInt("width", navigationBar.width)
193
- putInt("height", navigationBar.height)
194
- })
195
- })
196
- }
197
151
 
198
- reactApplicationContext
199
- .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
200
- .emit("__unistylesOnChange", body)
152
+ private fun getScreenDimensions(): Dimensions {
153
+ return platform.getScreenDimensions()
201
154
  }
202
155
 
203
- private fun onThemeChange(themeName: String) {
204
- val body = Arguments.createMap().apply {
205
- putString("type", "theme")
206
- putMap("payload", Arguments.createMap().apply {
207
- putString("themeName", themeName)
208
- })
209
- }
210
-
211
- reactApplicationContext
212
- .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
213
- .emit("__unistylesOnChange", body)
156
+ private fun getColorScheme(): String {
157
+ return platform.getColorScheme()
214
158
  }
215
159
 
216
- private fun onPluginChange() {
217
- val body = Arguments.createMap().apply {
218
- putString("type", "plugin")
219
- }
160
+ private fun getStatusBarDimensions(): Dimensions {
161
+ return platform.getStatusBarDimensions()
162
+ }
220
163
 
221
- reactApplicationContext
222
- .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
223
- .emit("__unistylesOnChange", body)
164
+ private fun getNavigationBarDimensions(): Dimensions {
165
+ return platform.getNavigationBarDimensions()
224
166
  }
225
167
 
226
- private fun onContentSizeCategoryChange(contentSizeCategory: String) {
227
- val body = Arguments.createMap().apply {
228
- putString("type", "dynamicTypeSize")
229
- putMap("payload", Arguments.createMap().apply {
230
- putString("contentSizeCategory", contentSizeCategory)
231
- })
232
- }
168
+ private fun getContentSizeCategory(): String {
169
+ return platform.getContentSizeCategory()
170
+ }
233
171
 
234
- reactApplicationContext
235
- .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
236
- .emit("__unistylesOnChange", body)
172
+ private fun getInsets(): Insets {
173
+ return platform.getInsets()
237
174
  }
238
175
 
239
176
  private fun onSetNavigationBarColor(color: String) {
package/cxx/Macros.h ADDED
@@ -0,0 +1,11 @@
1
+ #pragma once
2
+
3
+ #define HOST_FN(name, args, fn_body) \
4
+ jsi::Function::createFromHostFunction(rt, \
5
+ jsi::PropNameID::forAscii(rt, name), \
6
+ args, \
7
+ [this, &fnName](jsi::Runtime &rt, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count) -> jsi::Value \
8
+ fn_body \
9
+ ); \
10
+
11
+ #define BIND_FN(fn) std::bind(&UnistylesRuntime::fn, this, std::placeholders::_1, std::placeholders::_2)