Skip to content

Commit

Permalink
Cherry-pick 5.8.1 changes to github release branch
Browse files Browse the repository at this point in the history
commit_hash:56b8e1456f73211ac0d196e8249be701f52c6cc3
  • Loading branch information
Sergey70 committed Oct 2, 2024
1 parent 16aeaea commit 32783c9
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 34 deletions.
12 changes: 7 additions & 5 deletions AppMetricaCore/Sources/AMAAppMetricaImpl.m
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ @interface AMAAppMetricaImpl () <AMADispatcherDelegate,
@property (atomic, strong) AMADeepLinkController *deeplinkController;
@property (atomic, strong) AMAExternalAttributionController *externalAttributionController;
@property (nonatomic, strong, readonly) AMAAutoPurchasesWatcher *autoPurchasesWatcher;
@property (nonatomic, strong, readonly) AMAFirstActivationDetector *firstActivationDetector;

@property (nonatomic, strong) NSHashTable *startupCompletionObservers;

Expand Down Expand Up @@ -124,9 +125,10 @@ - (instancetype)initWithHostStateProvider:(id<AMAHostStateProviding>)hostStatePr
_dispatchingController = [[AMADispatchingController alloc] initWithTimeoutConfiguration:configuration];
_dispatchingController.proxyDelegate = self;

_configurationManager =
[[AMAAppMetricaConfigurationManager alloc] initWithExecutor:executor
strategiesContainer:_strategiesContainer];
_firstActivationDetector = [[AMAFirstActivationDetector alloc] init];
_configurationManager = [[AMAAppMetricaConfigurationManager alloc] initWithExecutor:executor
strategiesContainer:_strategiesContainer
firstActivationDetector:_firstActivationDetector];

[[AMASKAdNetworkRequestor sharedInstance] registerForAdNetworkAttribution];

Expand Down Expand Up @@ -179,8 +181,8 @@ - (void)activateWithConfiguration:(AMAAppMetricaConfiguration *)configuration

- (void)scheduleAnonymousActivationIfNeeded
{
if ([AMAFirstActivationDetector isFirstLibraryReporterActivation] == NO &&
[AMAFirstActivationDetector isFirstMainReporterActivation] == YES) {
if ([self.firstActivationDetector isFirstLibraryReporterActivation] == NO &&
[self.firstActivationDetector isFirstMainReporterActivation] == YES) {
AMADelayedExecutor *delayedExecutor = [[AMADelayedExecutor alloc] init];

__weak typeof(self) weakSelf = self;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@protocol AMAAsyncExecuting;
@protocol AMASyncExecuting;
@class AppMetricaConfigForAnonymousActivationProvider;
@class AMAFirstActivationDetector;

@interface AMAAppMetricaConfigurationManager : NSObject

Expand All @@ -20,7 +21,8 @@
+ (instancetype)new NS_UNAVAILABLE;

- (instancetype)initWithExecutor:(id<AMAAsyncExecuting, AMASyncExecuting>)executor
strategiesContainer:(AMADispatchStrategiesContainer *)strategiesContainer;
strategiesContainer:(AMADispatchStrategiesContainer *)strategiesContainer
firstActivationDetector:(AMAFirstActivationDetector *)firstActivationDetector;

- (instancetype)initWithExecutor:(id<AMAAsyncExecuting, AMASyncExecuting>)executor
strategiesContainer:(AMADispatchStrategiesContainer *)strategiesContainer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#import "AMAErrorLogger.h"
#import "AMADispatchStrategiesContainer.h"
#import "AMADatabaseQueueProvider.h"

#import "AppMetricaDefaultAnonymousConfigProvider.h"

@interface AMAAppMetricaConfigurationManager ()

Expand All @@ -31,14 +31,19 @@ @implementation AMAAppMetricaConfigurationManager

- (instancetype)initWithExecutor:(id<AMAAsyncExecuting,AMASyncExecuting>)executor
strategiesContainer:(AMADispatchStrategiesContainer *)strategiesContainer
firstActivationDetector:(AMAFirstActivationDetector *)firstActivationDetector
{
AMAMetricaConfiguration *metricaConfiguration = [AMAMetricaConfiguration sharedInstance];
AppMetricaConfigForAnonymousActivationProvider *anonymousConfigProvider =
[[AppMetricaConfigForAnonymousActivationProvider alloc] initWithStorage:metricaConfiguration.persistent
defaultProvider:[[AppMetricaDefaultAnonymousConfigProvider alloc] init]
firstActivationDetector:firstActivationDetector];
return [self initWithExecutor:executor
strategiesContainer:strategiesContainer
metricaConfiguration:metricaConfiguration
locationManager:[AMALocationManager sharedManager]
restrictionController:[AMADataSendingRestrictionController sharedInstance]
anonymousConfigProvider:[[AppMetricaConfigForAnonymousActivationProvider alloc] initWithStorage:metricaConfiguration.persistent]];
anonymousConfigProvider:anonymousConfigProvider];
}

- (instancetype)initWithExecutor:(id<AMAAsyncExecuting,AMASyncExecuting>)executor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

@interface AMAFirstActivationDetector : NSObject

+ (BOOL)isFirstLibraryReporterActivation;
+ (BOOL)isFirstMainReporterActivation;
- (BOOL)isFirstLibraryReporterActivation;
- (BOOL)isFirstMainReporterActivation;

@end
23 changes: 15 additions & 8 deletions AppMetricaCore/Sources/Configuration/AMAFirstActivationDetector.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@
#import "AMAMetricaInMemoryConfiguration.h"
#import "AMADatabaseFactory.h"

@implementation AMAFirstActivationDetector
@interface AMAFirstActivationDetector ()

+ (BOOL)isFirstLibraryReporterActivation
{
return [self isReporterUnavailable:kAMAMetricaLibraryApiKey];
}
@property (nonatomic, assign, readonly) BOOL isFirstLibraryReporterActivation;
@property (nonatomic, assign, readonly) BOOL isFirstMainReporterActivation;

@end

@implementation AMAFirstActivationDetector

+ (BOOL)isFirstMainReporterActivation
- (instancetype)init
{
return [self isReporterUnavailable:kAMAMainReporterDBPath];
self = [super init];
if (self != nil) {
_isFirstLibraryReporterActivation = [self isReporterUnavailable:kAMAMetricaLibraryApiKey];
_isFirstMainReporterActivation = [self isReporterUnavailable:kAMAMainReporterDBPath];
}
return self;
}

+ (BOOL)isReporterUnavailable:(NSString *)path
- (BOOL)isReporterUnavailable:(NSString *)path
{
NSString *libraryReporterMigrationPath = [[AMAMigrationTo500Utils migrationPath] stringByAppendingPathComponent:path];
NSString *libraryReporterPath = [AMAFileUtility persistentPathForApiKey:path];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
@class AppMetricaDefaultAnonymousConfigProvider;
@class AMAMetricaPersistentConfiguration;
@class AMAAppMetricaConfiguration;
@class AMAFirstActivationDetector;

@interface AppMetricaConfigForAnonymousActivationProvider : NSObject

Expand All @@ -12,7 +13,8 @@

- (instancetype)initWithStorage:(AMAMetricaPersistentConfiguration *)persistent;
- (instancetype)initWithStorage:(AMAMetricaPersistentConfiguration *)persistent
defaultProvider:(AppMetricaDefaultAnonymousConfigProvider *)defaultProvider;
defaultProvider:(AppMetricaDefaultAnonymousConfigProvider *)defaultProvider
firstActivationDetector:(AMAFirstActivationDetector *)firstActivationDetector;

- (AMAAppMetricaConfiguration *)configuration;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
#import "AppMetricaDefaultAnonymousConfigProvider.h"
#import "AMAMetricaPersistentConfiguration.h"
#import "AMAFirstActivationDetector.h"
#import "AMAFirstActivationDetector.h"

@interface AppMetricaConfigForAnonymousActivationProvider ()

@property (nonatomic, strong, readwrite) AppMetricaDefaultAnonymousConfigProvider *defaultProvider;
@property (nonatomic, strong, readwrite) AMAMetricaPersistentConfiguration *persistent;
@property (nonatomic, strong, readwrite) AMAFirstActivationDetector *firstActivationDetector;

@end

Expand All @@ -16,16 +18,19 @@ @implementation AppMetricaConfigForAnonymousActivationProvider
- (instancetype)initWithStorage:(AMAMetricaPersistentConfiguration *)persistent
{
return [self initWithStorage:persistent
defaultProvider:[[AppMetricaDefaultAnonymousConfigProvider alloc] init]];
defaultProvider:[[AppMetricaDefaultAnonymousConfigProvider alloc] init]
firstActivationDetector:[[AMAFirstActivationDetector alloc] init]];
}

- (instancetype)initWithStorage:(AMAMetricaPersistentConfiguration *)persistent
defaultProvider:(AppMetricaDefaultAnonymousConfigProvider *)defaultProvider
firstActivationDetector:(AMAFirstActivationDetector *)firstActivationDetector
{
self = [super init];
if (self != nil) {
_defaultProvider = defaultProvider;
_persistent = persistent;
_firstActivationDetector = firstActivationDetector;
}
return self;
}
Expand All @@ -36,7 +41,7 @@ - (AMAAppMetricaConfiguration *)configuration

if (configuration == nil) {
configuration = [self.defaultProvider configuration];
if ([AMAFirstActivationDetector isFirstLibraryReporterActivation] == NO) {
if ([self.firstActivationDetector isFirstLibraryReporterActivation] == NO) {
configuration.handleFirstActivationAsUpdate = true;
}
}
Expand Down
14 changes: 8 additions & 6 deletions AppMetricaCore/Tests/AMAAppMetricaImplTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ @interface AMAAppMetricaImpl () <AMAExtendedStartupObservingDelegate>
AMAInternalEventsReporter *__block internalEventsReporter = nil;
AMAStartupItemsChangedNotifier *__block startupNotifier = nil;
AMAExternalAttributionController *__block externalAttributionController = nil;
AMAFirstActivationDetector *__block firstActivationDetector = nil;

beforeEach(^{
[AMALocationManager stub:@selector(sharedManager)];
Expand All @@ -109,6 +110,7 @@ @interface AMAAppMetricaImpl () <AMAExtendedStartupObservingDelegate>
reporterStateStorage:)];
dispatchingController = [AMADispatchingController stubbedNullMockForInit:@selector(initWithTimeoutConfiguration:)];
internalEventsReporter = [AMAInternalEventsReporter nullMock];
firstActivationDetector = [AMAFirstActivationDetector stubbedNullMockForDefaultInit];

hostStateProvider = [AMAStubHostAppStateProvider new];
hostStateProvider.hostState = AMAHostAppStateBackground;
Expand Down Expand Up @@ -1288,25 +1290,25 @@ @interface AMAAppMetricaImpl () <AMAExtendedStartupObservingDelegate>
context(@"Anonymous activation", ^{
context(@"Scheduling activation", ^{
it(@"Should schedule anonymous activation if both conditions are met", ^{
[AMAFirstActivationDetector stub:@selector(isFirstLibraryReporterActivation) andReturn:theValue(NO)];
[AMAFirstActivationDetector stub:@selector(isFirstMainReporterActivation) andReturn:theValue(YES)];
[firstActivationDetector stub:@selector(isFirstLibraryReporterActivation) andReturn:theValue(NO)];
[firstActivationDetector stub:@selector(isFirstMainReporterActivation) andReturn:theValue(YES)];

[[appMetricaImpl shouldNot] receive:@selector(activateAnonymously)];
[[appMetricaImpl shouldEventuallyBeforeTimingOutAfter(0.2)] receive:@selector(activateAnonymously)];

[appMetricaImpl scheduleAnonymousActivationIfNeeded];
});
it(@"Should activate anonymously immediately if main activation has occurred", ^{
[AMAFirstActivationDetector stub:@selector(isFirstLibraryReporterActivation) andReturn:theValue(NO)];
[AMAFirstActivationDetector stub:@selector(isFirstMainReporterActivation) andReturn:theValue(NO)];
[firstActivationDetector stub:@selector(isFirstLibraryReporterActivation) andReturn:theValue(NO)];
[firstActivationDetector stub:@selector(isFirstMainReporterActivation) andReturn:theValue(NO)];

[[appMetricaImpl should] receive:@selector(activateAnonymously)];

[appMetricaImpl scheduleAnonymousActivationIfNeeded];
});
it(@"Should activate anonymously immediately if no any activation occured before", ^{
[AMAFirstActivationDetector stub:@selector(isFirstLibraryReporterActivation) andReturn:theValue(YES)];
[AMAFirstActivationDetector stub:@selector(isFirstMainReporterActivation) andReturn:theValue(YES)];
[firstActivationDetector stub:@selector(isFirstLibraryReporterActivation) andReturn:theValue(YES)];
[firstActivationDetector stub:@selector(isFirstMainReporterActivation) andReturn:theValue(YES)];

[[appMetricaImpl should] receive:@selector(activateAnonymously)];

Expand Down
12 changes: 8 additions & 4 deletions AppMetricaCore/Tests/AMAFirstActivationDetectorTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
return [basePath stringByAppendingPathComponent:@"data.sqlite"];
};

AMAFirstActivationDetector *(^buildActivationDetector)(void) = ^AMAFirstActivationDetector *(void) {
return [[AMAFirstActivationDetector alloc] init];
};

context(@"isFirstLibraryReporterActivation", ^{
NSString *const persistentPathForApiKey = [persistentPath stringByAppendingPathComponent:kAMAMetricaLibraryApiKey];
NSString *const migrationPathForApiKey = [migrationPath stringByAppendingPathComponent:kAMAMetricaLibraryApiKey];
Expand All @@ -33,7 +37,7 @@
andReturn:theValue(NO)
withArguments:dbFilePath(persistentPathForApiKey)];

BOOL result = [AMAFirstActivationDetector isFirstLibraryReporterActivation];
BOOL result = [buildActivationDetector() isFirstLibraryReporterActivation];
[[theValue(result) should] beYes];
});

Expand All @@ -49,7 +53,7 @@
andReturn:theValue(YES)
withArguments:dbFilePath(persistentPathForApiKey)];

BOOL result = [AMAFirstActivationDetector isFirstLibraryReporterActivation];
BOOL result = [buildActivationDetector() isFirstLibraryReporterActivation];
[[theValue(result) should] beNo];
});
});
Expand All @@ -70,7 +74,7 @@
andReturn:theValue(NO)
withArguments:dbFilePath(persistentPathForApiKey)];

BOOL result = [AMAFirstActivationDetector isFirstMainReporterActivation];
BOOL result = [buildActivationDetector() isFirstMainReporterActivation];
[[theValue(result) should] beYes];
});

Expand All @@ -86,7 +90,7 @@
andReturn:theValue(YES)
withArguments:dbFilePath(persistentPathForApiKey)];

BOOL result = [AMAFirstActivationDetector isFirstMainReporterActivation];
BOOL result = [buildActivationDetector() isFirstMainReporterActivation];
[[theValue(result) should] beNo];
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
AppMetricaConfigForAnonymousActivationProvider *__block provider = nil;
AMAMetricaPersistentConfiguration *__block persistentMock = nil;
AppMetricaDefaultAnonymousConfigProvider *__block defaultProvider = nil;
AMAFirstActivationDetector *__block firstActivationDetector = nil;

beforeEach(^{
persistentMock = [AMAMetricaPersistentConfiguration nullMock];
defaultProvider = [[AppMetricaDefaultAnonymousConfigProvider alloc] init];
firstActivationDetector = [[AMAFirstActivationDetector alloc] init];

provider = [[AppMetricaConfigForAnonymousActivationProvider alloc] initWithStorage:persistentMock
defaultProvider:defaultProvider];
defaultProvider:defaultProvider
firstActivationDetector:firstActivationDetector];
});

context(@"With stored configuration", ^{
Expand All @@ -38,15 +41,15 @@

context(@"with first activation", ^{
it(@"should set handleFirstActivationAsUpdate to YES", ^{
[AMAFirstActivationDetector stub:@selector(isFirstLibraryReporterActivation) andReturn:theValue(NO)];
[firstActivationDetector stub:@selector(isFirstLibraryReporterActivation) andReturn:theValue(NO)];

[[theValue([provider configuration].handleFirstActivationAsUpdate) should] beYes];
});
});

context(@"with next activation", ^{
it(@"should not change handleFirstActivationAsUpdate", ^{
[AMAFirstActivationDetector stub:@selector(isFirstLibraryReporterActivation) andReturn:theValue(YES)];
[firstActivationDetector stub:@selector(isFirstLibraryReporterActivation) andReturn:theValue(YES)];

[[theValue([provider configuration].handleFirstActivationAsUpdate) should] beNo];
});
Expand Down

0 comments on commit 32783c9

Please sign in to comment.