Skip to content

Commit dafa256

Browse files
authored
Merge pull request #429 from Countly/disable_autostop
feat: disable auto restart manual views
2 parents cb19d01 + 917af80 commit dafa256

File tree

7 files changed

+58
-19
lines changed

7 files changed

+58
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## XX.XX.XX
2+
* Added a new config option disableViewRestartForManualRecording to disable auto close/restart behavior of manual views on app background/foreground actions.
23
* Added a new config option "setWebviewDisplayOption: WebViewDisplayOption" to control how Content and Feedback Widgets are presented.
34
* IMMERSIVE mode (default): Full-screen display (except cutouts).
45
* SAFE_AREA mode: Omits status bar, navigation bar and cutouts when displaying WebView.

Countly.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ - (void)startWithConfig:(CountlyConfig *)config
248248
}
249249
#endif
250250

251+
if(config.disableViewRestartForManualRecording){
252+
CountlyViewTrackingInternal.sharedInstance.isManualViewRestartActive = NO;
253+
}
254+
251255
if(config.experimental.enablePreviousNameRecording) {
252256
CountlyViewTrackingInternal.sharedInstance.enablePreviousNameRecording = YES;
253257
}

CountlyConfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,11 @@ typedef enum : NSUInteger
693693
*/
694694
@property (nonatomic) BOOL disableBackoffMechanism;
695695

696+
/**
697+
* Disable view restarting for manual view recording
698+
*/
699+
@property(nonatomic) BOOL disableViewRestartForManualRecording;
700+
696701
#if (TARGET_OS_IOS)
697702
/**
698703
* Variable to access content configurations.

CountlyTests/CountlyBaseTestCase.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ class CountlyBaseTestCase: XCTestCase {
3434
}
3535

3636
func cleanupState() {
37-
Countly.sharedInstance().halt(true)
37+
// TODO: This also nils the instances which makes testing bad
38+
// Shared instances are static they must not be nilled for multi instance cases.
39+
//Countly.sharedInstance().halt(true)
3840
}
3941
}
4042

CountlyTests/CountlyViewTests.swift

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -701,9 +701,43 @@ class CountlyViewForegroundBackgroundTests: CountlyViewBaseTest {
701701
func testBackgroundAndForegroundTriggers() throws {
702702
let config = createBaseConfig()
703703
Countly.sharedInstance().start(with: config)
704+
705+
Countly.sharedInstance().views().startView("View1")
706+
707+
goBackgroundForeground()
704708

709+
let startedQueuedEventsCount = ["View1": 1]
710+
711+
let endedQueuedEventsDurations = ["View1": [3]]
712+
713+
// Call validateRecordedEvents to check if the events match expectations
714+
validateQueuedViews(startedEventsCount: startedQueuedEventsCount, endedEventsDurations: endedQueuedEventsDurations)
715+
716+
let startedEventsCount = ["View1": 1]
717+
718+
let endedEventsDurations: [String: [Int]] = [:]
719+
720+
// Call validateRecordedEvents to check if the events match expectations
721+
validateRecordedViews(startedEventsCount: startedEventsCount, endedEventsDurations: endedEventsDurations)
722+
}
723+
724+
func testBackgroundAndForegroundTriggers_manualViewRestartDisabled() throws {
725+
let config = createBaseConfig()
726+
config.disableViewRestartForManualRecording = true
727+
Countly.sharedInstance().start(with: config)
728+
705729
Countly.sharedInstance().views().startView("View1")
706730

731+
goBackgroundForeground()
732+
733+
// Call validateRecordedEvents to check if the events match expectations
734+
validateQueuedViews(startedEventsCount: ["View1": 1], endedEventsDurations: [:]) // no end view now
735+
736+
// Call validateRecordedEvents to check if the events match expectations
737+
validateRecordedViews(startedEventsCount: [:], endedEventsDurations: [:])
738+
}
739+
740+
private func goBackgroundForeground() {
707741
// Create expectations for various events
708742
let waitForStart = XCTestExpectation(description: "Wait for 3 seconds before backgrounding app.")
709743
let waitForBackground = XCTestExpectation(description: "Wait for 4 seconds in background.")
@@ -730,20 +764,6 @@ class CountlyViewForegroundBackgroundTests: CountlyViewBaseTest {
730764

731765
// Wait for all expectations to be fulfilled
732766
wait(for: [waitForStart, waitForBackground, waitForForeground], timeout: 15.0)
733-
734-
let startedQueuedEventsCount = ["View1": 1]
735-
736-
let endedQueuedEventsDurations = ["View1": [3]]
737-
738-
// Call validateRecordedEvents to check if the events match expectations
739-
validateQueuedViews(startedEventsCount: startedQueuedEventsCount, endedEventsDurations: endedQueuedEventsDurations)
740-
741-
let startedEventsCount = ["View1": 1]
742-
743-
let endedEventsDurations: [String: [Int]] = [:]
744-
745-
// Call validateRecordedEvents to check if the events match expectations
746-
validateRecordedViews(startedEventsCount: startedEventsCount, endedEventsDurations: endedEventsDurations)
747767
}
748768
}
749769

CountlyViewTrackingInternal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ extern NSString* const kCountlyVTKeyVisit;
2222
@property (nonatomic) NSString* currentViewName;
2323
@property (nonatomic) NSString* previousViewName;
2424

25+
@property (nonatomic) BOOL isManualViewRestartActive;
26+
2527
+ (instancetype)sharedInstance;
2628

2729
#if (TARGET_OS_IOS || TARGET_OS_VISION || TARGET_OS_TV)

CountlyViewTrackingInternal.m

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ - (instancetype)init
104104
self.viewDataDictionary = NSMutableDictionary.new;
105105
self.viewSegmentation = nil;
106106
self.isFirstView = YES;
107+
self.isManualViewRestartActive = YES;
107108
}
108109

109110
return self;
@@ -753,23 +754,27 @@ - (NSString*)titleForViewController:(UIViewController *)viewController
753754

754755
- (void)applicationWillEnterForeground {
755756
#if (TARGET_OS_IOS || TARGET_OS_VISION || TARGET_OS_TV)
756-
if (!self.isAutoViewTrackingActive) {
757+
if (!self.isAutoViewTrackingActive && self.isManualViewRestartActive) {
757758
[self startStoppedViewsInternal];
758759
}
759760
#else
760-
[self startStoppedViewsInternal];
761+
if (self.isManualViewRestartActive) {
762+
[self startStoppedViewsInternal];
763+
}
761764
#endif
762765
}
763766
- (void)applicationDidEnterBackground {
764767
#if (TARGET_OS_IOS || TARGET_OS_VISION || TARGET_OS_TV)
765768
if (self.isAutoViewTrackingActive) {
766769
[self stopCurrentView];
767770
}
768-
else {
771+
else if (self.isManualViewRestartActive) {
769772
[self stopRunningViewsInternal];
770773
}
771774
#else
772-
[self stopRunningViewsInternal];
775+
if (self.isManualViewRestartActive) {
776+
[self stopRunningViewsInternal];
777+
}
773778
#endif
774779
}
775780

0 commit comments

Comments
 (0)