Skip to content

Commit b3df79f

Browse files
authored
Add core support with interop for Segmentation SDK. (#3430)
* Add core support with interop for Segmentation SDK. Also update headers to be under sources folder. * Review fixes. * Minor changes. * Fix style. * Fix style. * Style changes. * Fix whitespace in travis.yml * Fix style. * Travis CI is stuck..try updating the travis.yml * Undo travis.yml change.
1 parent d7d3200 commit b3df79f

File tree

9 files changed

+249
-18
lines changed

9 files changed

+249
-18
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ jobs:
8787
- travis_retry ./scripts/if_changed.sh ./scripts/pod_lib_lint.rb FirebaseDynamicLinks.podspec
8888
- travis_retry ./scripts/if_changed.sh ./scripts/pod_lib_lint.rb FirebaseDynamicLinks.podspec --use-libraries
8989
- travis_retry ./scripts/if_changed.sh ./scripts/pod_lib_lint.rb FirebaseDynamicLinks.podspec --use-modular-headers
90-
90+
9191
env:
9292
- PROJECT=Segmentation PLATFORM=all METHOD=pod-lib-lint
9393
before_install:

FirebaseSegmentation.podspec

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ Firebase Segmentation enables you to associate your custom application instance
1515
}
1616

1717
s.ios.deployment_target = '8.0'
18-
1918
s.cocoapods_version = '>= 1.4.0'
2019
s.static_framework = true
2120
s.prefix_header_file = false
2221

2322
s.source_files = 'FirebaseSegmentation/Sources/**/*'
24-
s.public_header_files = 'FirebaseSegmentation/Public/*.h'
23+
s.public_header_files = 'FirebaseSegmentation/Sources/Public/*.h'
2524

26-
s.dependency 'FirebaseCore', '~> 6.0'
25+
s.dependency 'FirebaseCore', '~> 6.1'
2726
s.dependency 'FirebaseInstanceID', '~> 4.2'
2827

2928
header_search_paths = {
@@ -35,11 +34,11 @@ s.user_target_xcconfig = { 'FRAMEWORK_SEARCH_PATHS' => '$(PLATFORM_DIR)/Develope
3534
s.pod_target_xcconfig = {
3635
'GCC_C_LANGUAGE_STANDARD' => 'c99',
3736
'GCC_PREPROCESSOR_DEFINITIONS' => 'FIRSegmentation_VERSION=' + s.version.to_s
38-
}
37+
}.merge(header_search_paths)
3938

4039
s.test_spec 'unit' do |unit_tests|
4140
unit_tests.source_files = 'FirebaseSegmentation/Tests/Unit/*.[mh]'
4241
unit_tests.dependency 'OCMock'
42+
unit_tests.requires_app_host = true
4343
end
44-
45-
end
44+
end

FirebaseSegmentation/Sources/FIRSegmentation.m

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,47 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#import "FIRSegmentation.h"
15+
#import "FirebaseSegmentation/Sources/Public/FIRSegmentation.h"
16+
17+
#import <FirebaseCore/FIRAppInternal.h>
18+
#import <FirebaseCore/FIRComponentContainer.h>
19+
#import <FirebaseCore/FIRLogger.h>
20+
#import <FirebaseCore/FIROptionsInternal.h>
21+
#import <FirebaseSegmentation/Sources/Private/FIRSegmentationComponent.h>
22+
23+
FIRLoggerService kFIRLoggerSegmentation = @"[Firebase/Segmentation]";
24+
25+
@implementation FIRSegmentation {
26+
NSString *_appName;
27+
}
28+
29+
+ (nonnull FIRSegmentation *)segmentation {
30+
if (![FIRApp isDefaultAppConfigured]) {
31+
FIRLogError(kFIRLoggerSegmentation, @"I-SEG000001",
32+
@"FIRApp not configured. Please make sure you have called [FIRApp configure]");
33+
}
34+
35+
return [FIRSegmentation segmentationWithApp:[FIRApp defaultApp]];
36+
}
37+
38+
+ (nonnull FIRSegmentation *)segmentationWithApp:(nonnull FIRApp *)firebaseApp {
39+
// Use the provider to generate and return instances of FIRRemoteConfig for this specific app and
40+
// namespace. This will ensure the app is configured before Remote Config can return an instance.
41+
id<FIRSegmentationProvider> provider =
42+
FIR_COMPONENT(FIRSegmentationProvider, firebaseApp.container);
43+
return [provider segmentation];
44+
}
45+
46+
- (void)setCustomInstallationID:(NSString *)customInstallationID
47+
completion:(void (^)(NSError *))completionHandler {
48+
}
49+
50+
/// Designated initializer
51+
- (instancetype)initWithAppName:(NSString *)appName FIROptions:(FIROptions *)options {
52+
self = [super init];
53+
if (self) {
54+
_appName = appName;
55+
}
56+
return self;
57+
}
58+
@end
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2019 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import "FIRSegmentationComponent.h"
18+
19+
#import <FirebaseCore/FIRAppInternal.h>
20+
#import <FirebaseCore/FIRComponentContainer.h>
21+
#import <FirebaseCore/FIROptionsInternal.h>
22+
#import "FirebaseSegmentation/Sources/Private/FIRSegmentationInternal.h"
23+
24+
#ifndef FIRSegmentation_VERSION
25+
#error "FIRSegmentation_VERSION is not defined: \
26+
add -DFIRSegmentation_VERSION=... to the build invocation"
27+
#endif
28+
29+
#define STR(x) STR_EXPAND(x)
30+
#define STR_EXPAND(x) #x
31+
32+
NSString *const kFirebaseSegmentationErrorDomain = @"com.firebase.segmentation";
33+
34+
@implementation FIRSegmentationComponent
35+
36+
/// Default method for retrieving a Segmentation instance, or creating one if it doesn't exist.
37+
- (FIRSegmentation *)segmentation {
38+
// Validate the required information is available.
39+
FIROptions *options = self.app.options;
40+
NSString *errorPropertyName;
41+
if (options.googleAppID.length == 0) {
42+
errorPropertyName = @"googleAppID";
43+
} else if (options.GCMSenderID.length == 0) {
44+
errorPropertyName = @"GCMSenderID";
45+
}
46+
47+
if (errorPropertyName) {
48+
[NSException
49+
raise:kFirebaseSegmentationErrorDomain
50+
format:@"%@",
51+
[NSString
52+
stringWithFormat:
53+
@"Firebase Segmentation is missing the required %@ property from the "
54+
@"configured FirebaseApp and will not be able to function properly. Please "
55+
@"fix this issue to ensure that Firebase is correctly configured.",
56+
errorPropertyName]];
57+
}
58+
59+
FIRSegmentation *instance = self.segmentationInstance;
60+
if (!instance) {
61+
instance = [[FIRSegmentation alloc] initWithAppName:self.app.name FIROptions:self.app.options];
62+
self.segmentationInstance = instance;
63+
}
64+
65+
return instance;
66+
}
67+
68+
/// Default initializer.
69+
- (instancetype)initWithApp:(FIRApp *)app {
70+
self = [super init];
71+
if (self) {
72+
_app = app;
73+
_segmentationInstance = nil;
74+
}
75+
return self;
76+
}
77+
78+
#pragma mark - Lifecycle
79+
80+
+ (void)load {
81+
// Register as an internal library to be part of the initialization process. The name comes from
82+
// go/firebase-sdk-platform-info.
83+
[FIRApp registerInternalLibrary:self
84+
withName:@"fire-seg"
85+
withVersion:[NSString stringWithUTF8String:STR(FIRSegmentation_VERSION)]];
86+
}
87+
88+
#pragma mark - Interoperability
89+
90+
+ (NSArray<FIRComponent *> *)componentsToRegister {
91+
FIRComponent *segProvider = [FIRComponent
92+
componentWithProtocol:@protocol(FIRSegmentationProvider)
93+
instantiationTiming:FIRInstantiationTimingAlwaysEager
94+
dependencies:@[]
95+
creationBlock:^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
96+
// Cache the component so instances of Segmentation are cached.
97+
*isCacheable = YES;
98+
return [[FIRSegmentationComponent alloc] initWithApp:container.app];
99+
}];
100+
return @[ segProvider ];
101+
}
102+
103+
@synthesize instances;
104+
105+
@end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2019 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import <Foundation/Foundation.h>
18+
19+
#import "FIRLibrary.h"
20+
21+
@class FIRApp;
22+
@class FIRSegmentation;
23+
24+
NS_ASSUME_NONNULL_BEGIN
25+
26+
/// Provides and creates instances of Segmentation. Used in the
27+
/// interop registration process to keep track of Segmentation instances for each `FIRApp` instance.
28+
@protocol FIRSegmentationProvider
29+
30+
/// Cached instances of Segmentation objects.
31+
@property(nonatomic, strong) NSMutableDictionary<NSString *, FIRSegmentation *> *instances;
32+
33+
/// Default method for retrieving a Segmentation instance, or creating one if it doesn't exist.
34+
- (FIRSegmentation *)segmentation;
35+
36+
@end
37+
38+
/// A concrete implementation for FIRSegmentationInterop to create Segmentation instances and
39+
/// register with Core's component system.
40+
@interface FIRSegmentationComponent : NSObject <FIRSegmentationProvider, FIRLibrary>
41+
42+
/// The FIRApp that instances will be set up with.
43+
@property(nonatomic, weak, readonly) FIRApp *app;
44+
45+
/// Cached instances of Segmentation objects.
46+
@property(nonatomic, strong) FIRSegmentation *segmentationInstance;
47+
48+
/// Default method for retrieving a Segmentation instance, or creating one if it doesn't exist.
49+
- (FIRSegmentation *)segmentation;
50+
51+
/// Default initializer.
52+
- (instancetype)initWithApp:(FIRApp *)app NS_DESIGNATED_INITIALIZER;
53+
54+
- (instancetype)init __attribute__((unavailable("Use `initWithApp:`.")));
55+
56+
@end
57+
58+
NS_ASSUME_NONNULL_END
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2019 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import <Foundation/Foundation.h>
18+
19+
NS_ASSUME_NONNULL_BEGIN
20+
21+
@class FIRApp;
22+
23+
NS_SWIFT_NAME(Segmentation)
24+
@interface FIRSegmentation : NSObject
25+
26+
/// Initialize a Segmentation instance with all the required parameters directly.
27+
- (instancetype)initWithAppName:(NSString *)appName FIROptions:(FIROptions *)options;
28+
@end
29+
30+
NS_ASSUME_NONNULL_END
File renamed without changes.
File renamed without changes.

FirebaseSegmentation/Tests/Unit/SEGInitializationTests.m

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
#import <XCTest/XCTest.h>
1616

17+
#import "FIRApp.h"
18+
#import "FirebaseSegmentation/Sources/Public/FIRSegmentation.h"
19+
1720
@interface SEGInitializationTests : XCTestCase
1821

1922
@end
@@ -23,6 +26,7 @@ @implementation SEGInitializationTests
2326
- (void)setUp {
2427
// Put setup code here. This method is called before the invocation of each test method in the
2528
// class.
29+
[FIRApp configure];
2630
}
2731

2832
- (void)tearDown {
@@ -31,16 +35,8 @@ - (void)tearDown {
3135
}
3236

3337
- (void)testExample {
34-
NSLog(@"this is a test example");
35-
// This is an example of a functional test case.
36-
// Use XCTAssert and related functions to verify your tests produce the correct results.
37-
}
38-
39-
- (void)testPerformanceExample {
40-
// This is an example of a performance test case.
41-
[self measureBlock:^{
42-
// Put the code you want to measure the time of here.
43-
}];
38+
FIRSegmentation *segmentation = [FIRSegmentation segmentation];
39+
XCTAssertNotNil(segmentation);
4440
}
4541

4642
@end

0 commit comments

Comments
 (0)