react-native-daro 2.0.8 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/README.md +2 -2
  2. package/android/build.gradle +3 -1
  3. package/android/src/main/java/com/daro/reactnative/DaroRNAdBannerViewManager.kt +5 -0
  4. package/android/src/main/java/com/daro/reactnative/DaroRNAdNativeViewManager.kt +5 -0
  5. package/android/src/main/java/com/daro/reactnative/DaroRNModule.kt +42 -0
  6. package/android/src/main/java/com/daro/reactnative/event/DaroRNEvent.kt +8 -0
  7. package/android/src/main/java/com/daro/reactnative/impl/DaroRNPreloadModuleImpl.kt +77 -0
  8. package/android/src/main/java/com/daro/reactnative/preload/DaroRNPreloadRegistry.kt +57 -0
  9. package/android/src/main/java/com/daro/reactnative/util/AdInfoUtil.kt +11 -0
  10. package/android/src/main/java/com/daro/reactnative/view/DaroMediaView.kt +3 -0
  11. package/android/src/main/java/com/daro/reactnative/view/DaroRNAdBannerViewContainer.kt +29 -8
  12. package/android/src/main/java/com/daro/reactnative/view/DaroRNAdNativeViewContainer.kt +29 -8
  13. package/android/src/oldarch/java/com/daro/reactnative/DaroRNModuleSpec.kt +14 -0
  14. package/android/src/test/java/com/daro/reactnative/preload/DaroRNPreloadRegistryTest.kt +65 -0
  15. package/android/src/test/java/com/daro/reactnative/view/DaroMediaViewTest.kt +45 -0
  16. package/android/src/test/java/com/daro/reactnative/view/DaroRNPreloadFirstLoadTest.kt +160 -0
  17. package/ios/DaroRNModule+Preload.swift +122 -0
  18. package/ios/DaroRNModule.mm +6 -0
  19. package/ios/DaroRNModule.swift +5 -0
  20. package/ios/Fabric/DaroRNAdBannerComponentView.mm +5 -0
  21. package/ios/Fabric/DaroRNNativeAdComponentView.mm +5 -0
  22. package/ios/Paper/DaroRNAdBannerViewManager.m +1 -0
  23. package/ios/Paper/DaroRNNativeAdViewManager.m +1 -0
  24. package/ios/Shared/DaroRNBannerAdView.swift +16 -2
  25. package/ios/Shared/DaroRNNativeAdView.swift +63 -32
  26. package/ios/Shared/DaroRNPreloadRegistry.swift +54 -0
  27. package/lib/commonjs/AdBannerView.js +2 -0
  28. package/lib/commonjs/AdBannerView.js.map +1 -1
  29. package/lib/commonjs/AdPreload.js +105 -0
  30. package/lib/commonjs/AdPreload.js.map +1 -0
  31. package/lib/commonjs/DaroRNAdBannerViewNativeComponent.ts +2 -0
  32. package/lib/commonjs/DaroRNNativeAdViewNativeComponent.ts +2 -0
  33. package/lib/commonjs/NativeDaroModule.js.map +1 -1
  34. package/lib/commonjs/hooks/useAdPreload.js +61 -0
  35. package/lib/commonjs/hooks/useAdPreload.js.map +1 -0
  36. package/lib/commonjs/index.js +37 -0
  37. package/lib/commonjs/index.js.map +1 -1
  38. package/lib/commonjs/nativeAd/NativeAdView.js +2 -0
  39. package/lib/commonjs/nativeAd/NativeAdView.js.map +1 -1
  40. package/lib/commonjs/types/NativeAdViewProps.js.map +1 -1
  41. package/lib/module/AdBannerView.js +2 -0
  42. package/lib/module/AdBannerView.js.map +1 -1
  43. package/lib/module/AdPreload.js +98 -0
  44. package/lib/module/AdPreload.js.map +1 -0
  45. package/lib/module/DaroRNAdBannerViewNativeComponent.ts +2 -0
  46. package/lib/module/DaroRNNativeAdViewNativeComponent.ts +2 -0
  47. package/lib/module/NativeDaroModule.js.map +1 -1
  48. package/lib/module/hooks/useAdPreload.js +56 -0
  49. package/lib/module/hooks/useAdPreload.js.map +1 -0
  50. package/lib/module/index.js +2 -0
  51. package/lib/module/index.js.map +1 -1
  52. package/lib/module/nativeAd/NativeAdView.js +2 -0
  53. package/lib/module/nativeAd/NativeAdView.js.map +1 -1
  54. package/lib/module/types/NativeAdViewProps.js.map +1 -1
  55. package/lib/typescript/commonjs/src/AdBannerView.d.ts +1 -0
  56. package/lib/typescript/commonjs/src/AdBannerView.d.ts.map +1 -1
  57. package/lib/typescript/commonjs/src/AdPreload.d.ts +62 -0
  58. package/lib/typescript/commonjs/src/AdPreload.d.ts.map +1 -0
  59. package/lib/typescript/commonjs/src/DaroRNAdBannerViewNativeComponent.d.ts +1 -0
  60. package/lib/typescript/commonjs/src/DaroRNAdBannerViewNativeComponent.d.ts.map +1 -1
  61. package/lib/typescript/commonjs/src/DaroRNNativeAdViewNativeComponent.d.ts +1 -0
  62. package/lib/typescript/commonjs/src/DaroRNNativeAdViewNativeComponent.d.ts.map +1 -1
  63. package/lib/typescript/commonjs/src/NativeDaroModule.d.ts +5 -0
  64. package/lib/typescript/commonjs/src/NativeDaroModule.d.ts.map +1 -1
  65. package/lib/typescript/commonjs/src/hooks/useAdPreload.d.ts +19 -0
  66. package/lib/typescript/commonjs/src/hooks/useAdPreload.d.ts.map +1 -0
  67. package/lib/typescript/commonjs/src/index.d.ts +4 -0
  68. package/lib/typescript/commonjs/src/index.d.ts.map +1 -1
  69. package/lib/typescript/commonjs/src/nativeAd/NativeAdView.d.ts +1 -0
  70. package/lib/typescript/commonjs/src/nativeAd/NativeAdView.d.ts.map +1 -1
  71. package/lib/typescript/commonjs/src/types/AdBannerViewProps.d.ts +6 -0
  72. package/lib/typescript/commonjs/src/types/AdBannerViewProps.d.ts.map +1 -1
  73. package/lib/typescript/commonjs/src/types/NativeAdViewProps.d.ts +6 -0
  74. package/lib/typescript/commonjs/src/types/NativeAdViewProps.d.ts.map +1 -1
  75. package/lib/typescript/module/src/AdBannerView.d.ts +1 -0
  76. package/lib/typescript/module/src/AdBannerView.d.ts.map +1 -1
  77. package/lib/typescript/module/src/AdPreload.d.ts +62 -0
  78. package/lib/typescript/module/src/AdPreload.d.ts.map +1 -0
  79. package/lib/typescript/module/src/DaroRNAdBannerViewNativeComponent.d.ts +1 -0
  80. package/lib/typescript/module/src/DaroRNAdBannerViewNativeComponent.d.ts.map +1 -1
  81. package/lib/typescript/module/src/DaroRNNativeAdViewNativeComponent.d.ts +1 -0
  82. package/lib/typescript/module/src/DaroRNNativeAdViewNativeComponent.d.ts.map +1 -1
  83. package/lib/typescript/module/src/NativeDaroModule.d.ts +5 -0
  84. package/lib/typescript/module/src/NativeDaroModule.d.ts.map +1 -1
  85. package/lib/typescript/module/src/hooks/useAdPreload.d.ts +19 -0
  86. package/lib/typescript/module/src/hooks/useAdPreload.d.ts.map +1 -0
  87. package/lib/typescript/module/src/index.d.ts +4 -0
  88. package/lib/typescript/module/src/index.d.ts.map +1 -1
  89. package/lib/typescript/module/src/nativeAd/NativeAdView.d.ts +1 -0
  90. package/lib/typescript/module/src/nativeAd/NativeAdView.d.ts.map +1 -1
  91. package/lib/typescript/module/src/types/AdBannerViewProps.d.ts +6 -0
  92. package/lib/typescript/module/src/types/AdBannerViewProps.d.ts.map +1 -1
  93. package/lib/typescript/module/src/types/NativeAdViewProps.d.ts +6 -0
  94. package/lib/typescript/module/src/types/NativeAdViewProps.d.ts.map +1 -1
  95. package/package.json +1 -1
  96. package/plugin/build/withAndroid.js +2 -2
  97. package/react-native-daro.podspec +1 -1
  98. package/src/AdBannerView.tsx +2 -0
  99. package/src/AdPreload.ts +141 -0
  100. package/src/DaroRNAdBannerViewNativeComponent.ts +2 -0
  101. package/src/DaroRNNativeAdViewNativeComponent.ts +2 -0
  102. package/src/NativeDaroModule.ts +17 -0
  103. package/src/hooks/useAdPreload.ts +82 -0
  104. package/src/index.tsx +16 -0
  105. package/src/nativeAd/NativeAdView.tsx +2 -0
  106. package/src/types/AdBannerViewProps.ts +6 -0
  107. package/src/types/NativeAdViewProps.ts +6 -0
@@ -0,0 +1,160 @@
1
+ package com.daro.reactnative.view
2
+
3
+ import android.app.Activity
4
+ import android.os.Looper
5
+ import android.view.View
6
+ import android.widget.FrameLayout
7
+ import com.daro.reactnative.preload.DaroRNPreloadRegistry
8
+ import com.facebook.react.bridge.BridgeReactContext
9
+ import com.facebook.react.views.text.ReactTextView
10
+ import droom.daro.core.model.DaroBannerSize
11
+ import droom.daro.view.DaroBannerAdController
12
+ import droom.daro.view.DaroBannerAdView
13
+ import droom.daro.view.DaroNativeAdController
14
+ import droom.daro.view.DaroNativeAdView
15
+ import org.junit.After
16
+ import org.junit.Test
17
+ import org.junit.runner.RunWith
18
+ import org.mockito.MockedConstruction
19
+ import org.mockito.Mockito.mock
20
+ import org.mockito.Mockito.mockConstruction
21
+ import org.mockito.Mockito.times
22
+ import org.mockito.Mockito.verify
23
+ import org.robolectric.Robolectric
24
+ import org.robolectric.RobolectricTestRunner
25
+ import org.robolectric.Shadows.shadowOf
26
+ import org.robolectric.annotation.Config
27
+
28
+ /**
29
+ * 받아 둔 광고를 넘겨받은 뷰의 첫 로드 ( DARO-2488 ).
30
+ *
31
+ * 컨트롤러를 가진 SDK 뷰는 화면에 붙을 때 스스로 `loadAd()` 를 불러 받아 둔 광고를 쓴다.
32
+ * 컨테이너가 그 뒤에 `loadAd()` 를 또 부르면, 그 처리가 끝난 뒤에는 새 요청이 되어 받아 둔
33
+ * 광고를 덮어쓴다. SDK 뷰는 생성자 mock 이라 스스로 로드하지 않는다 — 여기서 세는 것은
34
+ * 컨테이너가 부른 횟수다.
35
+ */
36
+ @RunWith(RobolectricTestRunner::class)
37
+ @Config(sdk = [28], manifest = Config.NONE)
38
+ class DaroRNPreloadFirstLoadTest {
39
+ private val activity = Robolectric.buildActivity(Activity::class.java).setup().get()
40
+ private val reactContext = BridgeReactContext(activity).apply { onHostResume(activity) }
41
+ private val root = FrameLayout(activity).also {
42
+ activity.setContentView(it)
43
+ // Robolectric 은 창 가시성을 갱신하지 않아 GONE 으로 남는다. 배너는 창이 보일 때만 로드한다.
44
+ val attachInfo = View::class.java.getDeclaredField("mAttachInfo").apply { isAccessible = true }.get(it)
45
+ attachInfo.javaClass.getDeclaredField("mWindowVisibility").apply { isAccessible = true }.setInt(attachInfo, View.VISIBLE)
46
+ }
47
+
48
+ @After fun tearDown() = DaroRNPreloadRegistry.shared.destroyAll()
49
+
50
+ private fun idle() = shadowOf(Looper.getMainLooper()).idle()
51
+
52
+ private fun <T> MockedConstruction<T>.only(): T = constructed().single()
53
+
54
+ // ── 배너 ──────────────────────────────────────────────────────────────
55
+
56
+ private fun preloadBanner(id: String) {
57
+ val controller = mock(DaroBannerAdController::class.java)
58
+ DaroRNPreloadRegistry.shared.preload(id) {
59
+ DaroRNPreloadRegistry.Entry("unit", DaroBannerSize.Banner.name, controller, {}, {})
60
+ }
61
+ }
62
+
63
+ /** 화면에 붙어 있고 크기가 [width] × 50 인 배너 컨테이너. */
64
+ private fun banner(preloadId: String?, width: Int = 320) = DaroRNAdBannerViewContainer(reactContext).also {
65
+ it.adUnitKey = "unit"
66
+ it.adSize = DaroBannerSize.Banner
67
+ it.preloadId = preloadId
68
+ root.addView(it, width, 50)
69
+ it.layout(0, 0, width, 50)
70
+ idle()
71
+ }
72
+
73
+ @Test fun bannerThatTookAPreloadLeavesTheFirstLoadToTheSdk() {
74
+ mockConstruction(DaroBannerAdView::class.java).use { views ->
75
+ preloadBanner("p")
76
+ val container = banner("p")
77
+
78
+ container.loadAd(); idle()
79
+ verify(views.only(), times(0)).loadAd()
80
+
81
+ // 그 뒤의 loadAd 는 재로드다.
82
+ container.loadAd(); idle()
83
+ verify(views.only(), times(1)).loadAd()
84
+ }
85
+ }
86
+
87
+ @Test fun bannerThatTookAPreloadDoesNotLoadOnceItGetsASize() {
88
+ mockConstruction(DaroBannerAdView::class.java).use { views ->
89
+ preloadBanner("p")
90
+ val container = banner("p", width = 0)
91
+
92
+ container.loadAd(); idle()
93
+ // 크기가 준비되면 미뤄 둔 로드를 다시 시도하던 경로.
94
+ container.layout(0, 0, 320, 50); idle()
95
+
96
+ verify(views.only(), times(0)).loadAd()
97
+ }
98
+ }
99
+
100
+ @Test fun bannerWithoutAPreloadLoadsAsBefore() {
101
+ mockConstruction(DaroBannerAdView::class.java).use { views ->
102
+ val container = banner(null)
103
+
104
+ container.loadAd(); idle()
105
+ verify(views.only(), times(1)).loadAd()
106
+ }
107
+ }
108
+
109
+ @Test fun bannerWithoutAPreloadStillLoadsOnceItGetsASize() {
110
+ mockConstruction(DaroBannerAdView::class.java).use { views ->
111
+ val container = banner(null, width = 0)
112
+
113
+ container.loadAd(); idle()
114
+ verify(views.only(), times(0)).loadAd()
115
+
116
+ container.layout(0, 0, 320, 50); idle()
117
+ verify(views.only(), times(1)).loadAd()
118
+ }
119
+ }
120
+
121
+ // ── 네이티브 ──────────────────────────────────────────────────────────
122
+
123
+ /** 화면에 붙어 있고 제목 뷰 하나를 등록한 네이티브 컨테이너. */
124
+ private fun native(preloadId: String?) = DaroRNAdNativeViewContainer(reactContext).also {
125
+ it.adUnitKey = "unit"
126
+ it.loadOnMount = false
127
+ it.preloadId = preloadId
128
+ it.addReactSubview(ReactTextView(reactContext).apply { id = TITLE_ID }, 0)
129
+ it.setTitleView(TITLE_ID)
130
+ root.addView(it, 320, 100)
131
+ idle()
132
+ }
133
+
134
+ @Test fun nativeThatTookAPreloadLeavesTheFirstLoadToTheSdk() {
135
+ mockConstruction(DaroNativeAdView::class.java).use { views ->
136
+ val controller = mock(DaroNativeAdController::class.java)
137
+ DaroRNPreloadRegistry.shared.preload("p") { DaroRNPreloadRegistry.Entry("unit", "", controller, {}, {}) }
138
+ val container = native("p")
139
+
140
+ container.loadAd(); idle()
141
+ verify(views.only(), times(0)).loadAd()
142
+
143
+ container.loadAd(); idle()
144
+ verify(views.only(), times(1)).loadAd()
145
+ }
146
+ }
147
+
148
+ @Test fun nativeWithoutAPreloadLoadsAsBefore() {
149
+ mockConstruction(DaroNativeAdView::class.java).use { views ->
150
+ val container = native(null)
151
+
152
+ container.loadAd(); idle()
153
+ verify(views.only(), times(1)).loadAd()
154
+ }
155
+ }
156
+
157
+ private companion object {
158
+ const val TITLE_ID = 1001
159
+ }
160
+ }
@@ -0,0 +1,122 @@
1
+ import React
2
+ import UIKit
3
+
4
+ // MARK: - View Ad Preload
5
+
6
+ extension DaroRNModule {
7
+
8
+ @objc(preloadBannerAd:adUnitKey:adFormat:resolve:reject:)
9
+ func preloadBannerAd(
10
+ preloadId: String,
11
+ adUnitKey: String,
12
+ adFormat: String,
13
+ resolve: @escaping RCTPromiseResolveBlock,
14
+ reject: @escaping RCTPromiseRejectBlock
15
+ ) {
16
+ DispatchQueue.main.async { [weak self] in
17
+ DaroRNPreloadRegistry.shared.preload(preloadId) {
18
+ let bannerSize = adFormat == Constants.mrecAdFormatLabel.value ? DaroAdBannerSize.MREC : DaroAdBannerSize.banner
19
+ let view = DaroAdBannerView(unit: DaroAdUnit(adUnitKey: adUnitKey), bannerSize: bannerSize, preload: true)
20
+ view.listener.onPreloadSuccess = { [weak self] adInfo in
21
+ self?.sendPreloadSuccess(preloadId: preloadId, adUnitKey: adUnitKey, adInfo: adInfo)
22
+ }
23
+ view.listener.onPreloadFail = { [weak self] error in
24
+ self?.sendPreloadFail(preloadId: preloadId, adUnitKey: adUnitKey, error: error)
25
+ }
26
+ return DaroRNPreloadRegistry.Entry(
27
+ adUnitKey: adUnitKey,
28
+ variant: adFormat,
29
+ view: view,
30
+ preload: { [weak view] in view?.loadAd() },
31
+ destroy: { [weak view] in
32
+ view?.listener.onPreloadSuccess = nil
33
+ view?.listener.onPreloadFail = nil
34
+ }
35
+ )
36
+ }
37
+ resolve(nil)
38
+ }
39
+ }
40
+
41
+ @objc(preloadNativeAd:adUnitKey:adChoicesPosition:resolve:reject:)
42
+ func preloadNativeAd(
43
+ preloadId: String,
44
+ adUnitKey: String,
45
+ adChoicesPosition: String,
46
+ resolve: @escaping RCTPromiseResolveBlock,
47
+ reject: @escaping RCTPromiseRejectBlock
48
+ ) {
49
+ DispatchQueue.main.async { [weak self] in
50
+ DaroRNPreloadRegistry.shared.preload(preloadId) {
51
+ let view = DaroRNBridgeNativeAdView(
52
+ unit: DaroAdUnit(adUnitKey: adUnitKey),
53
+ preload: true,
54
+ preferredAdChoicesPosition: DaroAdChoicesPosition(reactValue: adChoicesPosition)
55
+ )
56
+ view.listener.onPreloadSuccess = { [weak self] adInfo in
57
+ self?.sendPreloadSuccess(preloadId: preloadId, adUnitKey: adUnitKey, adInfo: adInfo)
58
+ }
59
+ view.listener.onPreloadFail = { [weak self] error in
60
+ self?.sendPreloadFail(preloadId: preloadId, adUnitKey: adUnitKey, error: error)
61
+ }
62
+ return DaroRNPreloadRegistry.Entry(
63
+ adUnitKey: adUnitKey,
64
+ variant: "",
65
+ view: view,
66
+ preload: { [weak view] in view?.loadAd() },
67
+ destroy: { [weak view] in
68
+ view?.listener.onPreloadSuccess = nil
69
+ view?.listener.onPreloadFail = nil
70
+ view?.clearNativeAd()
71
+ }
72
+ )
73
+ }
74
+ resolve(nil)
75
+ }
76
+ }
77
+
78
+ @objc(destroyAdPreload:)
79
+ func destroyAdPreload(preloadId: String) {
80
+ DispatchQueue.main.async {
81
+ DaroRNPreloadRegistry.shared.destroy(preloadId)
82
+ }
83
+ }
84
+
85
+ func invalidatePreloads() {
86
+ DispatchQueue.main.async {
87
+ DaroRNPreloadRegistry.shared.destroyAll()
88
+ }
89
+ }
90
+
91
+ private func sendPreloadSuccess(preloadId: String, adUnitKey: String, adInfo: DaroAdInfo?) {
92
+ var body = adInfo?.toBody ?? ["adUnitKey": adUnitKey]
93
+ body["preloadId"] = preloadId
94
+ sendEvent(with: .onAdPreloadSuccessEvent, body: body)
95
+ }
96
+
97
+ // preload 실패는 SDK 의 공통 오류 ( DaroError ) 로 온다. 기존 로드 실패와 같은 모양으로 싣는다.
98
+ private func sendPreloadFail(preloadId: String, adUnitKey: String, error: DaroError) {
99
+ sendEvent(with: .onAdPreloadFailedEvent, body: [
100
+ "preloadId": preloadId,
101
+ "adUnitKey": adUnitKey,
102
+ "code": error.code.rawValue,
103
+ "message": error.message,
104
+ "latencyMillis": 0,
105
+ ])
106
+ }
107
+ }
108
+
109
+ extension DaroAdChoicesPosition {
110
+ init(reactValue: String?) {
111
+ switch reactValue {
112
+ case "topLeft":
113
+ self = .topLeft
114
+ case "topRight":
115
+ self = .topRight
116
+ case "bottomLeft":
117
+ self = .bottomLeft
118
+ default:
119
+ self = .bottomRight
120
+ }
121
+ }
122
+ }
@@ -59,6 +59,12 @@ RCT_EXTERN_METHOD(loadLightPopup:(double)requestId adUnitKey:(NSString *)adUnitK
59
59
  RCT_EXTERN_METHOD(showLightPopup:(double)requestId adUnitKey:(NSString *)adUnitKey resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
60
60
  RCT_EXTERN_METHOD(setLightPopupAdConfiguration:(double)requestId adUnitKey:(NSString *)adUnitKey configuration:(NSDictionary *)configuration resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
61
61
 
62
+ #pragma mark - View Ad Preload
63
+
64
+ RCT_EXTERN_METHOD(preloadBannerAd:(NSString *)preloadId adUnitKey:(NSString *)adUnitKey adFormat:(NSString *)adFormat resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
65
+ RCT_EXTERN_METHOD(preloadNativeAd:(NSString *)preloadId adUnitKey:(NSString *)adUnitKey adChoicesPosition:(NSString *)adChoicesPosition resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
66
+ RCT_EXTERN_METHOD(destroyAdPreload:(NSString *)preloadId)
67
+
62
68
  + (BOOL)requiresMainQueueSetup {
63
69
  return YES;
64
70
  }
@@ -49,6 +49,10 @@ class DaroRNModule: RCTEventEmitter {
49
49
  case onLightPopupHiddenEvent = "ON_LIGHTPOPUP_HIDDEN_EVENT"
50
50
  case onLightPopupAdImpressionRecordedEvent = "ON_LIGHTPOPUP_AD_IMPRESSION_RECORDED"
51
51
 
52
+ // MARK: - View Ad Preload
53
+ case onAdPreloadSuccessEvent = "ON_AD_PRELOAD_SUCCESS_EVENT"
54
+ case onAdPreloadFailedEvent = "ON_AD_PRELOAD_FAILED_EVENT"
55
+
52
56
  static var supportedEvents: [String] {
53
57
  allCases.map(\.rawValue)
54
58
  }
@@ -175,6 +179,7 @@ class DaroRNModule: RCTEventEmitter {
175
179
  let requests = Array(lightPopups.keys)
176
180
  requests.forEach { releaseLightPopup(requestId: $0) }
177
181
  }
182
+ invalidatePreloads()
178
183
  super.invalidate()
179
184
  }
180
185
 
@@ -176,6 +176,11 @@ extern int DaroRNFabricEventInt(NSDictionary *body, NSString *key);
176
176
  [changedProps addObject:@"loadOnMount"];
177
177
  }
178
178
 
179
+ if (oldComponentProps.preloadId != newComponentProps.preloadId) {
180
+ DaroRNFabricSetValue(_bannerView, @"preloadId", DaroRNFabricNSStringFromStdString(newComponentProps.preloadId));
181
+ [changedProps addObject:@"preloadId"];
182
+ }
183
+
179
184
  if (oldComponentProps.isVisible != newComponentProps.isVisible) {
180
185
  DaroRNFabricSetValue(_bannerView, @"isVisible", @(newComponentProps.isVisible));
181
186
  [changedProps addObject:@"isVisible"];
@@ -204,6 +204,11 @@ static NSString *DaroRNFabricAdChoicesPositionString(DaroRNNativeAdViewAdChoices
204
204
  [changedProps addObject:@"loadOnMount"];
205
205
  }
206
206
 
207
+ if (oldComponentProps.preloadId != newComponentProps.preloadId) {
208
+ DaroRNFabricSetValue(_nativeAdView, @"preloadId", DaroRNFabricNSStringFromStdString(newComponentProps.preloadId));
209
+ [changedProps addObject:@"preloadId"];
210
+ }
211
+
207
212
  if (oldComponentProps.adChoicesPosition != newComponentProps.adChoicesPosition) {
208
213
  DaroRNFabricSetValue(
209
214
  _nativeAdView, @"adChoicesPosition", DaroRNFabricAdChoicesPositionString(newComponentProps.adChoicesPosition));
@@ -5,6 +5,7 @@
5
5
  RCT_EXPORT_VIEW_PROPERTY(adUnitKey, NSString)
6
6
  RCT_EXPORT_VIEW_PROPERTY(adFormat, NSString)
7
7
  RCT_EXPORT_VIEW_PROPERTY(loadOnMount, BOOL)
8
+ RCT_EXPORT_VIEW_PROPERTY(preloadId, NSString)
8
9
  RCT_EXPORT_VIEW_PROPERTY(isVisible, BOOL)
9
10
 
10
11
  RCT_EXPORT_VIEW_PROPERTY(onAdLoadedEvent, RCTDirectEventBlock)
@@ -4,6 +4,7 @@
4
4
 
5
5
  RCT_EXPORT_VIEW_PROPERTY(adUnitKey, NSString)
6
6
  RCT_EXPORT_VIEW_PROPERTY(loadOnMount, BOOL)
7
+ RCT_EXPORT_VIEW_PROPERTY(preloadId, NSString)
7
8
  RCT_EXPORT_VIEW_PROPERTY(adChoicesPosition, NSString)
8
9
 
9
10
  RCT_EXPORT_VIEW_PROPERTY(titleView, NSNumber)
@@ -17,6 +17,8 @@ final class DaroRNAdBannerView: UIView {
17
17
  @objc var adUnitKey: String?
18
18
  @objc var adFormat: String = DaroRNModule.Constants.bannerAdFormatLabel.value
19
19
  @objc var loadOnMount: Bool = true
20
+ /// preload 로 받아 둔 광고의 id. 뷰를 처음 만들 때 한 번만 본다.
21
+ @objc var preloadId: String?
20
22
  @objc var isVisible: Bool = true {
21
23
  didSet {
22
24
  setAdVisibility(isVisible)
@@ -77,6 +79,7 @@ final class DaroRNAdBannerView: UIView {
77
79
  adUnitKey = nil
78
80
  adFormat = DaroRNModule.Constants.bannerAdFormatLabel.value
79
81
  loadOnMount = true
82
+ preloadId = nil
80
83
  isVisible = true
81
84
  isHidden = false
82
85
  }
@@ -106,8 +109,17 @@ final class DaroRNAdBannerView: UIView {
106
109
  }
107
110
 
108
111
  let bannerSize = adFormat == DaroRNModule.Constants.bannerAdFormatLabel.value ? DaroAdBannerSize.banner : DaroAdBannerSize.MREC
109
- let adUnit = DaroAdUnit(adUnitKey: adUnitKey)
110
- let adBannerView = DaroAdBannerView(unit: adUnit, bannerSize: bannerSize, autoLoad: false)
112
+ let adBannerView: DaroAdBannerView
113
+ if let preloaded = DaroRNPreloadRegistry.shared.take(
114
+ preloadId, adUnitKey: adUnitKey, variant: adFormat, as: DaroAdBannerView.self
115
+ ) {
116
+ // 받아 둔 광고는 SDK 가 window 부착 때 소비한다 ( 실패 / 만료면 SDK 가 일반 로드로 폴백 ).
117
+ // 여기서 `loadAd()` 를 부르면 부착 뒤에는 새 일반 로드가 되어 요청이 겹친다.
118
+ adBannerView = preloaded
119
+ hasRequestedInitialLoad = true
120
+ } else {
121
+ adBannerView = DaroAdBannerView(unit: DaroAdUnit(adUnitKey: adUnitKey), bannerSize: bannerSize)
122
+ }
111
123
  setupBannerListener(for: adBannerView)
112
124
 
113
125
  adBannerView.isHidden = !isVisible
@@ -178,6 +190,8 @@ final class DaroRNAdBannerView: UIView {
178
190
  private func resetAdState() {
179
191
  pendingLoadRequest = nil
180
192
  hasRequestedInitialLoad = false
193
+ adBannerView?.listener.onPreloadSuccess = nil
194
+ adBannerView?.listener.onPreloadFail = nil
181
195
  adBannerView?.listener.onAdLoadSuccess = nil
182
196
  adBannerView?.listener.onAdLoadFail = nil
183
197
  adBannerView?.listener.onAdImpression = nil
@@ -8,6 +8,9 @@ final class RNDaroRNNativeAdView: UIView, DaroRNNativeAdRenderDelegate {
8
8
  private static let maxAssetLoadRetryCount = 40
9
9
 
10
10
  var _internalAdView: DaroRNBridgeNativeAdView?
11
+ /// preload 로 받아 둔 SDK 뷰. 에셋 뷰 등록이 끝날 때까지 계층 밖에 둔다 —
12
+ /// SDK 는 window 에 붙는 순간 그때 등록된 에셋 뷰로 광고를 렌더한다.
13
+ private var pendingPreloadedAdView: DaroRNBridgeNativeAdView?
11
14
  private var pinnedConstraints: [ObjectIdentifier: [NSLayoutConstraint]] = [:]
12
15
  private var pendingLoadWorkItem: DispatchWorkItem?
13
16
  private let loadCoordinator = DaroRNNativeAdLoadCoordinator()
@@ -18,6 +21,8 @@ final class RNDaroRNNativeAdView: UIView, DaroRNNativeAdRenderDelegate {
18
21
 
19
22
  @objc var adUnitKey: String?
20
23
  @objc var loadOnMount: Bool = true
24
+ /// preload 로 받아 둔 광고의 id. 뷰를 처음 만들 때 한 번만 본다.
25
+ @objc var preloadId: String?
21
26
  @objc var titleViewStyleProps: NSDictionary? {
22
27
  didSet {
23
28
  _internalTitleLabel.applyStyle(titleViewStyleProps)
@@ -221,7 +226,7 @@ final class RNDaroRNNativeAdView: UIView, DaroRNNativeAdRenderDelegate {
221
226
  }
222
227
 
223
228
  private func requestInitialLoadIfNeeded() {
224
- guard loadOnMount, !isDisposed, _internalAdView != nil, hasRegisteredAssetView else { return }
229
+ guard loadOnMount, !isDisposed, hasAdView, hasRegisteredAssetView else { return }
225
230
  loadCoordinator.requestAutomaticLoad(loadOnMount: loadOnMount)
226
231
  processPendingLoad(retryCount: 0)
227
232
  }
@@ -234,16 +239,37 @@ final class RNDaroRNNativeAdView: UIView, DaroRNNativeAdRenderDelegate {
234
239
  return
235
240
  }
236
241
 
237
- guard let internalAdView = _internalAdView,
238
- loadCoordinator.consumePendingLoad(loadOnMount: loadOnMount) != nil else {
242
+ guard hasAdView, loadCoordinator.consumePendingLoad(loadOnMount: loadOnMount) != nil else {
239
243
  return
240
244
  }
241
245
 
242
246
  pendingLoadWorkItem?.cancel()
243
247
  pendingLoadWorkItem = nil
248
+ if let preloaded = pendingPreloadedAdView {
249
+ pendingPreloadedAdView = nil
250
+ attachPreloadedAdView(preloaded)
251
+ return
252
+ }
253
+
254
+ isBindBridge = false
255
+ bindAssetViews()
256
+ _internalAdView?.loadAd()
257
+ }
258
+
259
+ /// 받아 둔 광고를 일반 로드 대신 쓴다. 에셋 뷰를 먼저 묶고 자식을 옮긴 뒤에 계층에 넣는다 —
260
+ /// 넣는 순간 SDK 가 광고를 소비해 렌더하기 때문이다. 실패 / 만료면 SDK 가 일반 로드로 폴백한다.
261
+ private func attachPreloadedAdView(_ native: DaroRNBridgeNativeAdView) {
262
+ let reactSubviews = subviews
263
+ _internalAdView = native
244
264
  isBindBridge = false
245
265
  bindAssetViews()
246
- internalAdView.loadAd()
266
+ reactSubviews.forEach { native.addSubview($0) }
267
+ super.addSubview(native)
268
+ pin(native, to: self)
269
+ }
270
+
271
+ private var hasAdView: Bool {
272
+ _internalAdView != nil || pendingPreloadedAdView != nil
247
273
  }
248
274
 
249
275
  private func retryPendingLoad(retryCount: Int) {
@@ -263,7 +289,7 @@ final class RNDaroRNNativeAdView: UIView, DaroRNNativeAdRenderDelegate {
263
289
  }
264
290
 
265
291
  private var isReadyToLoadAd: Bool {
266
- _internalAdView != nil && hasRegisteredAssetView && registeredAssetViewsReady
292
+ hasAdView && hasRegisteredAssetView && registeredAssetViewsReady
267
293
  }
268
294
 
269
295
  private var hasRegisteredAssetView: Bool {
@@ -288,9 +314,18 @@ final class RNDaroRNNativeAdView: UIView, DaroRNNativeAdRenderDelegate {
288
314
 
289
315
  private func makeAdViews(adUnitKey: String) {
290
316
  self.adUnit = DaroAdUnit(adUnitKey: adUnitKey)
317
+ if let preloaded = DaroRNPreloadRegistry.shared.take(
318
+ preloadId, adUnitKey: adUnitKey, variant: "", as: DaroRNBridgeNativeAdView.self
319
+ ) {
320
+ preloaded.delegate = self
321
+ installListeners(on: preloaded)
322
+ pendingPreloadedAdView = preloaded
323
+ return
324
+ }
325
+
291
326
  let native = DaroRNBridgeNativeAdView(
292
327
  unit: adUnit,
293
- preferredAdChoicesPosition: preferredAdChoicesPosition
328
+ preferredAdChoicesPosition: DaroAdChoicesPosition(reactValue: adChoicesPosition)
294
329
  )
295
330
  native.delegate = self
296
331
  self._internalAdView = native
@@ -302,20 +337,23 @@ final class RNDaroRNNativeAdView: UIView, DaroRNNativeAdRenderDelegate {
302
337
  insertAdSubview(subview, at: index)
303
338
  }
304
339
  pin(native, to: self)
340
+ installListeners(on: native)
341
+ }
305
342
 
306
- self._internalAdView?.listener.onNativeAdAssetLoaded = { [weak self] assetTypes in
343
+ private func installListeners(on native: DaroRNBridgeNativeAdView) {
344
+ native.listener.onNativeAdAssetLoaded = { [weak self] assetTypes in
307
345
  self?.eventSink.onNativeAdAssetLoadedEvent?(["assetTypes": assetTypes.map(\.rawValue)])
308
346
  }
309
- self._internalAdView?.listener.onAdLoadSuccess = { [weak self] _, adInfo in
347
+ native.listener.onAdLoadSuccess = { [weak self] _, adInfo in
310
348
  self?.eventSink.emitAdLoaded(adInfo?.toBody)
311
349
  }
312
- self._internalAdView?.listener.onAdLoadFail = { [weak self] error in
350
+ native.listener.onAdLoadFail = { [weak self] error in
313
351
  self?.eventSink.emitAdLoadFailed(error.toBody)
314
352
  }
315
- self._internalAdView?.listener.onAdImpression = { [weak self] adInfo in
353
+ native.listener.onAdImpression = { [weak self] adInfo in
316
354
  self?.eventSink.emitAdImpressionRecorded(adInfo?.toBody)
317
355
  }
318
- self._internalAdView?.listener.onAdClicked = { [weak self] adInfo in
356
+ native.listener.onAdClicked = { [weak self] adInfo in
319
357
  self?.eventSink.emitAdClicked(adInfo?.toBody)
320
358
  }
321
359
  }
@@ -438,19 +476,6 @@ final class RNDaroRNNativeAdView: UIView, DaroRNNativeAdRenderDelegate {
438
476
  return changedProps.contains { bridgeProps.contains($0) }
439
477
  }
440
478
 
441
- private var preferredAdChoicesPosition: DaroAdChoicesPosition {
442
- switch adChoicesPosition {
443
- case "topLeft":
444
- return .topLeft
445
- case "topRight":
446
- return .topRight
447
- case "bottomLeft":
448
- return .bottomLeft
449
- default:
450
- return .bottomRight
451
- }
452
- }
453
-
454
479
  private func pin(_ view: UIView, to targetView: UIView) {
455
480
  let identifier = ObjectIdentifier(view)
456
481
  if let constraints = pinnedConstraints[identifier] {
@@ -483,6 +508,7 @@ final class RNDaroRNNativeAdView: UIView, DaroRNNativeAdRenderDelegate {
483
508
  private func resetReactProps() {
484
509
  adUnitKey = nil
485
510
  loadOnMount = true
511
+ preloadId = nil
486
512
  titleViewStyleProps = nil
487
513
  titleView = nil
488
514
  bodyViewStyleProps = nil
@@ -511,15 +537,20 @@ final class RNDaroRNNativeAdView: UIView, DaroRNNativeAdRenderDelegate {
511
537
  pinnedConstraints.values.forEach { NSLayoutConstraint.deactivate($0) }
512
538
  pinnedConstraints.removeAll()
513
539
 
514
- _internalAdView?.listener.onNativeAdAssetLoaded = nil
515
- _internalAdView?.listener.onAdLoadSuccess = nil
516
- _internalAdView?.listener.onAdLoadFail = nil
517
- _internalAdView?.listener.onAdImpression = nil
518
- _internalAdView?.listener.onAdClicked = nil
519
- _internalAdView?.delegate = nil
520
- _internalAdView?.clearNativeAd()
521
- _internalAdView?.removeFromSuperview()
540
+ [_internalAdView, pendingPreloadedAdView].forEach { native in
541
+ native?.listener.onPreloadSuccess = nil
542
+ native?.listener.onPreloadFail = nil
543
+ native?.listener.onNativeAdAssetLoaded = nil
544
+ native?.listener.onAdLoadSuccess = nil
545
+ native?.listener.onAdLoadFail = nil
546
+ native?.listener.onAdImpression = nil
547
+ native?.listener.onAdClicked = nil
548
+ native?.delegate = nil
549
+ native?.clearNativeAd()
550
+ native?.removeFromSuperview()
551
+ }
522
552
  _internalAdView = nil
553
+ pendingPreloadedAdView = nil
523
554
 
524
555
  _internalTitleLabel.removeFromSuperview()
525
556
  _internalBodyLabel.removeFromSuperview()
@@ -0,0 +1,54 @@
1
+ import Foundation
2
+
3
+ /// `preloadId` 로 preload 뷰를 들고 있다가 RN 뷰가 마운트될 때 한 번 넘긴다.
4
+ ///
5
+ /// - 넘긴 항목은 레지스트리에서 빠진다. 그 뒤의 정리는 가져간 RN 뷰의 몫이다.
6
+ /// - 광고 유닛이나 포맷이 RN 뷰와 다르면 넘기지 않고 정리한다 — RN 뷰는 일반 로드를 한다.
7
+ /// - 안 쓴 항목은 JS 의 `destroy()` 와 모듈 invalidate 가 정리한다.
8
+ ///
9
+ /// 메인 스레드에서만 부른다.
10
+ final class DaroRNPreloadRegistry {
11
+ struct Entry {
12
+ let adUnitKey: String
13
+ /// 같은 유닛 안에서 갈리는 요청 조건 ( 배너 포맷 ). 없으면 빈 문자열.
14
+ let variant: String
15
+ let view: AnyObject
16
+ /// 들고 있는 뷰에 다시 요청한다. 중복과 TTL 판정은 SDK 가 한다.
17
+ let preload: () -> Void
18
+ let destroy: () -> Void
19
+ }
20
+
21
+ static let shared = DaroRNPreloadRegistry()
22
+
23
+ private var entries: [String: Entry] = [:]
24
+
25
+ /// 처음이면 `create` 로 만든다 ( SDK 뷰는 만들어질 때 요청을 시작한다 ). 이미 있으면 다시 요청한다.
26
+ func preload(_ preloadId: String, create: () -> Entry) {
27
+ if let entry = entries[preloadId] {
28
+ entry.preload()
29
+ } else {
30
+ entries[preloadId] = create()
31
+ }
32
+ }
33
+
34
+ func take<View>(_ preloadId: String?, adUnitKey: String, variant: String, as type: View.Type) -> View? {
35
+ guard let preloadId, !preloadId.isEmpty, let entry = entries.removeValue(forKey: preloadId) else {
36
+ return nil
37
+ }
38
+ guard entry.adUnitKey == adUnitKey, entry.variant == variant, let view = entry.view as? View else {
39
+ entry.destroy()
40
+ return nil
41
+ }
42
+ return view
43
+ }
44
+
45
+ func destroy(_ preloadId: String) {
46
+ entries.removeValue(forKey: preloadId)?.destroy()
47
+ }
48
+
49
+ func destroyAll() {
50
+ let remaining = Array(entries.values)
51
+ entries.removeAll()
52
+ remaining.forEach { $0.destroy() }
53
+ }
54
+ }
@@ -35,6 +35,7 @@ const AdBannerView = exports.AdBannerView = /*#__PURE__*/(0, _react.forwardRef)(
35
35
  adFormat,
36
36
  loadOnMount = true,
37
37
  isVisible = true,
38
+ preload,
38
39
  onAdLoaded,
39
40
  onAdLoadFailed,
40
41
  onAdClicked,
@@ -90,6 +91,7 @@ const AdBannerView = exports.AdBannerView = /*#__PURE__*/(0, _react.forwardRef)(
90
91
  ref: saveElement,
91
92
  adUnitKey: adUnitKey,
92
93
  loadOnMount: loadOnMount,
94
+ preloadId: preload?.preloadId,
93
95
  isVisible: isVisible,
94
96
  onAdLoadedEvent: onAdLoadedEvent,
95
97
  onAdLoadFailedEvent: onAdLoadFailedEvent,