Skip to content

Commit 3fdcac6

Browse files
authored
Fix old iPhones issue (#15)
* Add new screen * Observable * bump version
1 parent b318e2a commit 3fdcac6

File tree

12 files changed

+170
-75
lines changed

12 files changed

+170
-75
lines changed

example/ios/Podfile.lock

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ PODS:
210210
- React-Core
211211
- react-native-usercentrics (2.0.2):
212212
- React
213+
- RxSwift (= 6.2.0)
213214
- UsercentricsUI (= 2.0.2)
214215
- React-perflogger (0.65.1)
215216
- React-RCTActionSheet (0.65.1):
@@ -278,6 +279,7 @@ PODS:
278279
- RNScreens (3.9.0):
279280
- React-Core
280281
- React-RCTImage
282+
- RxSwift (6.2.0)
281283
- Usercentrics (2.0.2)
282284
- UsercentricsUI (2.0.2):
283285
- Usercentrics (= 2.0.2)
@@ -322,6 +324,7 @@ SPEC REPOS:
322324
trunk:
323325
- boost-for-react-native
324326
- fmt
327+
- RxSwift
325328
- Usercentrics
326329
- UsercentricsUI
327330

@@ -408,7 +411,7 @@ SPEC CHECKSUMS:
408411
React-jsiexecutor: 43f2542aed3c26e42175b339f8d37fe3dd683765
409412
React-jsinspector: 41e58e5b8e3e0bf061fdf725b03f2144014a8fb0
410413
react-native-safe-area-context: 584dc04881deb49474363f3be89e4ca0e854c057
411-
react-native-usercentrics: cf25504befdc47dae93f4b731998e20e630a19a7
414+
react-native-usercentrics: 08cc61b771ed7fbd3bca91b78ec61d446c06fd87
412415
React-perflogger: fd28ee1f2b5b150b00043f0301d96bd417fdc339
413416
React-RCTActionSheet: 7f3fa0855c346aa5d7c60f9ced16e067db6d29fa
414417
React-RCTAnimation: 2119a18ee26159004b001bc56404ca5dbaae6077
@@ -422,6 +425,7 @@ SPEC CHECKSUMS:
422425
React-runtimeexecutor: 85187f19dd9c47a7c102f9994f9d14e4dc2110de
423426
ReactCommon: eafed38eec7b591c31751bfa7494801618460459
424427
RNScreens: 4d79118be80f79fa1f4aa131909a1d6e86280af3
428+
RxSwift: d356ab7bee873611322f134c5f9ef379fa183d8f
425429
Usercentrics: 0a782c069efd2cfa7836f504f418d90a7a613d83
426430
UsercentricsUI: 2c0a85d538649179afbb8cb278958bea10328bf9
427431
Yoga: aa0cb45287ebe1004c02a13f279c55a95f1572f4

example/ios/example.xcodeproj/project.pbxproj

Lines changed: 59 additions & 53 deletions
Large diffs are not rendered by default.

example/ios/exampleTests/Fake/FakeUsercentricsManager.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ enum FakeUsercentricsError: Error {
1515
}
1616

1717
final class FakeUsercentricsManager: UsercentricsManager {
18+
var alreadyConfigured: Bool = false
19+
1820
var restoreControllerId: String?
1921

2022
var getTCStringValue: String?
@@ -41,10 +43,12 @@ final class FakeUsercentricsManager: UsercentricsManager {
4143

4244
func configure(options: UsercentricsOptions) {
4345
self.configureOptions = options
46+
alreadyConfigured = true
4447
}
4548

4649
func reset() {
4750
resetCount += 1
51+
alreadyConfigured = false
4852
}
4953

5054
func getPredefinedUI(settings: UsercentricsUISettings?, dismissViewHandler: @escaping (UsercentricsConsentUserResponse) -> Void) -> UIViewController {

example/ios/exampleTests/RNUsercentricsModuleTests.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ class RNUsercentricsModuleTests: XCTestCase {
4343
XCTAssertEqual(expectedOptions.timeoutMillis, fakeUsercentrics.configureOptions!.timeoutMillis)
4444
}
4545

46-
func testConfigureWhenIsAlreadyConfigured() {
47-
fakeUsercentrics.isReadySuccessCompletion = .mock()
48-
module.configure(UsercentricsOptions.asDict())
49-
XCTAssertEqual(nil, fakeUsercentrics.configureOptions)
50-
}
51-
5246
func testConfigureWithoutCorrectValues() {
5347
module.configure([:])
5448
XCTAssertEqual(nil, fakeUsercentrics.configureOptions)

example/src/App.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from 'react';
22
import { NavigationContainer } from '@react-navigation/native';
33
import { createNativeStackNavigator } from '@react-navigation/native-stack';
44
import { CustomScreen, HomeScreen } from './screens';
5+
import Consent from './screens/Consent';
56

67
const Stack = createNativeStackNavigator();
78

@@ -15,7 +16,7 @@ const App = () => {
1516
/>
1617
<Stack.Screen
1718
name="CustomUI"
18-
component={CustomScreen} />
19+
component={Consent} />
1920
</Stack.Navigator>
2021
</NavigationContainer>
2122
)

example/src/screens/Consent.tsx

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import React, { Component } from 'react';
2+
import { Alert } from 'react-native';
3+
import {
4+
Usercentrics,
5+
UsercentricsLoggerLevel,
6+
UsercentricsOptions,
7+
UsercentricsServiceConsent,
8+
UsercentricsUIOptions,
9+
} from '../../../src/index';
10+
11+
interface Props {
12+
children: Element;
13+
}
14+
15+
interface State {
16+
consents: [UsercentricsServiceConsent] | null;
17+
ucReset: () => void;
18+
}
19+
20+
export const ConsentContext = React.createContext<[UsercentricsServiceConsent] | null>(null);
21+
export const DevelopemetConsentContext = React.createContext<null | (() => void)>(null);
22+
23+
class Consent extends Component<Props, State> {
24+
constructor(props: any) {
25+
super(props);
26+
const options = new UsercentricsOptions("Yi9N3aXia");
27+
// if (Config.EXPERIMENTAL_FEATURES) {
28+
options.loggerLevel = UsercentricsLoggerLevel.debug;
29+
// }
30+
// options.defaultLanguage = this.props.intl.locale;
31+
Usercentrics.configure(options);
32+
this.state = { consents: null, ucReset: Usercentrics.reset };
33+
}
34+
35+
async componentDidMount() {
36+
const status = await Usercentrics.status();
37+
const mapped = status.consents.map( consent => {return consent.templateId});
38+
39+
Alert.alert("Component Did Mount", `status ${status.shouldShowCMP}, consents: ${mapped}`)
40+
41+
if (status.shouldShowCMP) {
42+
this.showCMP(false).then();
43+
} else {
44+
this.applyConsents(status.consents);
45+
}
46+
}
47+
48+
async showCMP(showCloseButton: boolean) {
49+
const options = new UsercentricsUIOptions(showCloseButton);
50+
const response = await Usercentrics.showCMP(options);
51+
console.log('showCMP', response.consents);
52+
this.setState({ consents: response.consents });
53+
}
54+
55+
applyConsents(consents: [UsercentricsServiceConsent]) {
56+
console.log('applyConsents', consents);
57+
this.setState({ consents });
58+
}
59+
60+
render() {
61+
return (
62+
<ConsentContext.Provider value={this.state.consents}>
63+
{/* {Config.EXPERIMENTAL_FEATURES ? ( */}
64+
<DevelopemetConsentContext.Provider value={this.state.ucReset}>
65+
{this.props.children}
66+
</DevelopemetConsentContext.Provider>
67+
{/* ) : (
68+
this.props.children
69+
)} */}
70+
</ConsentContext.Provider>
71+
);
72+
}
73+
}
74+
75+
export default Consent;

example/src/screens/Home.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ import {
1111

1212
export const HomeScreen = ({ navigation }: { navigation: any }) => {
1313
useEffect(() => {
14-
// let options = new UsercentricsOptions("Yi9N3aXia");
15-
let options = new UsercentricsOptions("eQ6JwYNPb");
16-
// let options = new UsercentricsOptions("EA4jnNPb9");
14+
let options = new UsercentricsOptions("Yi9N3aXia");
1715
options.loggerLevel = UsercentricsLoggerLevel.debug;
1816
Usercentrics.configure(options);
1917
});

example/src/screens/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export { HomeScreen } from "./Home";
2-
export { CustomScreen } from './CustomUI';
2+
export { CustomScreen } from './CustomUI';

ios/Manager/UsercentricsManager.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import Foundation
22
import Usercentrics
33
import UsercentricsUI
4+
import RxSwift
45

56
public protocol UsercentricsManager {
7+
var alreadyConfigured: Bool { get }
68
func configure(options: UsercentricsOptions)
79

810
func isReady(onSuccess: @escaping ((UsercentricsReadyStatus) -> Void), onFailure: @escaping ((Error) -> Void))
@@ -38,16 +40,33 @@ public protocol UsercentricsManager {
3840

3941
final class UsercentricsManagerImplementation: UsercentricsManager {
4042

43+
private var bag: DisposeBag = DisposeBag()
44+
private let isConfiguredObservable: BehaviorSubject<Bool> = .init(value: false)
45+
var alreadyConfigured: Bool {
46+
try! isConfiguredObservable.value()
47+
}
48+
4149
func isReady(onSuccess: @escaping ((UsercentricsReadyStatus) -> Void), onFailure: @escaping ((Error) -> Void)) {
42-
UsercentricsCore.isReady(onSuccess: onSuccess, onFailure: onFailure)
50+
var subscription: Disposable?
51+
52+
subscription = isConfiguredObservable
53+
.subscribe(onNext: { isConfigured in
54+
guard isConfigured else { return }
55+
UsercentricsCore.isReady(onSuccess: onSuccess, onFailure: onFailure)
56+
subscription?.dispose()
57+
})
58+
59+
subscription?.disposed(by: bag)
4360
}
4461

4562
func configure(options: UsercentricsOptions) {
4663
UsercentricsCore.configure(options: options)
64+
isConfiguredObservable.onNext(true)
4765
}
4866

4967
func reset() {
5068
UsercentricsCore.reset()
69+
isConfiguredObservable.onNext(false)
5170
}
5271

5372
func getPredefinedUI(settings: UsercentricsUISettings?, dismissViewHandler: @escaping (UsercentricsConsentUserResponse) -> Void) -> UIViewController {

ios/RNUsercentricsModule.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,7 @@ class RNUsercentricsModule: NSObject, RCTBridgeModule {
1919
}
2020

2121
@objc func configure(_ dict: NSDictionary) -> Void {
22-
var alreadyInitialized = false
23-
usercentricsManager.isReady { _ in
24-
alreadyInitialized = true
25-
} onFailure: { _ in
26-
alreadyInitialized = true
27-
}
28-
29-
if !alreadyInitialized {
22+
if !usercentricsManager.alreadyConfigured {
3023
queue.async { [weak self] in
3124
guard
3225
let self = self,

0 commit comments

Comments
 (0)