Skip to content

Commit

Permalink
Cleanup shared_background_delivery_publishers (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanCooper9 authored Sep 7, 2024
1 parent 6e5bb7d commit bfdedda
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 78 deletions.
1 change: 0 additions & 1 deletion FCKit/Sources/FCKit/FeatureFlag/FeatureFlag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public enum FeatureFlagBool: String, CaseIterable, FeatureFlag {

case adsEnabled = "ads_enabled"
case newResultsBannerEnabled = "new_results_banner_enabled"
case sharedBackgroundDeliveryPublishers = "shared_background_delivery_publishers"
case ignoreManuallyEnteredHealthKitData = "ignore_manually_entered_health_kit_data"

public var defaultValue: Data { false }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,23 @@ final class ActivitySummaryManager: ActivitySummaryManaging {
]

permissionTypes.forEach { permission in
if featureFlagManager.value(forBool: .sharedBackgroundDeliveryPublishers) {
healthKitManager.registerBackgroundDeliveryTask(for: permission) { [weak self] in
guard let self else { return .just(()) }
if let fetchAndUploadPublisher = self.fetchAndUploadPublisher {
return fetchAndUploadPublisher
} else {
let publisher = fetchAndUpload()
.first()
.handleEvents(receiveCompletion: { _ in
self.fetchAndUploadPublisher = nil
}, receiveCancel: {
self.fetchAndUploadPublisher = nil
})
.share()
.eraseToAnyPublisher()
self.fetchAndUploadPublisher = publisher
return publisher
}
healthKitManager.registerBackgroundDeliveryTask(for: permission) { [weak self] in
guard let self else { return .just(()) }
if let fetchAndUploadPublisher = self.fetchAndUploadPublisher {
return fetchAndUploadPublisher
} else {
let publisher = fetchAndUpload()
.first()
.handleEvents(receiveCompletion: { _ in
self.fetchAndUploadPublisher = nil
}, receiveCancel: {
self.fetchAndUploadPublisher = nil
})
.share()
.eraseToAnyPublisher()
self.fetchAndUploadPublisher = publisher
return publisher
}
} else {
healthKitManager.registerBackgroundDeliveryPublisher(for: permission, publisher: fetchAndUpload())
}
}
}
Expand Down
13 changes: 1 addition & 12 deletions FriendlyCompetitions/Managers/HealthKit/HealthKitManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ protocol HealthKitManaging {

func execute(_ query: AnyHealthKitQuery)
func registerBackgroundDeliveryTask(for permission: HealthKitPermissionType, task: @escaping HealthKitBackgroundDeliveryTask)
func registerBackgroundDeliveryPublisher(for permission: HealthKitPermissionType, publisher: AnyPublisher<Void, Never>)
func registerForBackgroundDelivery()

func shouldRequest(_ permissions: [HealthKitPermissionType]) -> AnyPublisher<Bool, Error>
Expand Down Expand Up @@ -56,11 +55,6 @@ final class HealthKitManager: HealthKitManaging {
backgroundDeliveryTasks[permission] = tasks.appending(task)
}

func registerBackgroundDeliveryPublisher(for permission: HealthKitPermissionType, publisher: AnyPublisher<Void, Never>) {
let publishers = backgroundDeliveryPublishers[permission] ?? []
backgroundDeliveryPublishers[permission] = publishers.appending(publisher)
}

func shouldRequest(_ permissions: [HealthKitPermissionType]) -> AnyPublisher<Bool, Error> {
healthStore
.shouldRequest(permissions)
Expand Down Expand Up @@ -118,7 +112,6 @@ final class HealthKitManager: HealthKitManaging {

if error.isHealthKitAuthorizationError {
backgroundDeliveryTasks[permission]?.removeAll()
backgroundDeliveryPublishers[permission]?.removeAll()
healthStore.disableBackgroundDelivery(for: permission)
.sink()
.store(in: &cancellables)
Expand All @@ -128,11 +121,7 @@ final class HealthKitManager: HealthKitManaging {
case .success(let backgroundDeliveryCompletion):
let publishers: [AnyPublisher<Void, Never>]

if featureFlagManager.value(forBool: .sharedBackgroundDeliveryPublishers) {
publishers = backgroundDeliveryTasks[permission]?.map { $0() } ?? []
} else {
publishers = backgroundDeliveryPublishers[permission] ?? []
}
publishers = backgroundDeliveryTasks[permission]?.map { $0() } ?? []

guard publishers.isNotEmpty else {
analyticsManager.log(event: .healthKitBGDelieveryMissingPublisher(permission: permission.rawValue))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ final class StepCountManager: StepCountManaging {
// MARK: - Lifecycle

init() {
if featureFlagManager.value(forBool: .sharedBackgroundDeliveryPublishers) {
healthKitManager.registerBackgroundDeliveryTask(for: .stepCount, task: fetchAndUpload)
} else {
healthKitManager.registerBackgroundDeliveryPublisher(for: .stepCount, publisher: fetchAndUpload())
}
healthKitManager.registerBackgroundDeliveryTask(for: .stepCount, task: fetchAndUpload)

fetchAndUpload()
.sink()
Expand Down
36 changes: 16 additions & 20 deletions FriendlyCompetitions/Managers/Workout/WorkoutManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,27 +72,23 @@ final class WorkoutManager: WorkoutManaging {
]

permissionTypes.forEach { permission in
if featureFlagManager.value(forBool: .sharedBackgroundDeliveryPublishers) {
healthKitManager.registerBackgroundDeliveryTask(for: permission) { [weak self] in
guard let self else { return .just(()) }
if let fetchAndUploadPublisher {
return fetchAndUploadPublisher
} else {
let publisher = fetchAndUpload()
.first()
.handleEvents(receiveCompletion: { _ in
self.fetchAndUploadPublisher = nil
}, receiveCancel: {
self.fetchAndUploadPublisher = nil
})
.share()
.eraseToAnyPublisher()
self.fetchAndUploadPublisher = publisher
return publisher
}
healthKitManager.registerBackgroundDeliveryTask(for: permission) { [weak self] in
guard let self else { return .just(()) }
if let fetchAndUploadPublisher {
return fetchAndUploadPublisher
} else {
let publisher = fetchAndUpload()
.first()
.handleEvents(receiveCompletion: { _ in
self.fetchAndUploadPublisher = nil
}, receiveCancel: {
self.fetchAndUploadPublisher = nil
})
.share()
.eraseToAnyPublisher()
self.fetchAndUploadPublisher = publisher
return publisher
}
} else {
healthKitManager.registerBackgroundDeliveryPublisher(for: permission, publisher: fetchAndUpload())
}
}
}
Expand Down
17 changes: 0 additions & 17 deletions FriendlyCompetitions/Sourcery/AutoMockable.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -998,23 +998,6 @@ class HealthKitManagingMock: HealthKitManaging {
registerBackgroundDeliveryTaskForTaskClosure?(permission, task)
}

//MARK: - registerBackgroundDeliveryPublisher

var registerBackgroundDeliveryPublisherForPublisherCallsCount = 0
var registerBackgroundDeliveryPublisherForPublisherCalled: Bool {
return registerBackgroundDeliveryPublisherForPublisherCallsCount > 0
}
var registerBackgroundDeliveryPublisherForPublisherReceivedArguments: (permission: HealthKitPermissionType, publisher: AnyPublisher<Void, Never>)?
var registerBackgroundDeliveryPublisherForPublisherReceivedInvocations: [(permission: HealthKitPermissionType, publisher: AnyPublisher<Void, Never>)] = []
var registerBackgroundDeliveryPublisherForPublisherClosure: ((HealthKitPermissionType, AnyPublisher<Void, Never>) -> Void)?

func registerBackgroundDeliveryPublisher(for permission: HealthKitPermissionType, publisher: AnyPublisher<Void, Never>) {
registerBackgroundDeliveryPublisherForPublisherCallsCount += 1
registerBackgroundDeliveryPublisherForPublisherReceivedArguments = (permission: permission, publisher: publisher)
registerBackgroundDeliveryPublisherForPublisherReceivedInvocations.append((permission: permission, publisher: publisher))
registerBackgroundDeliveryPublisherForPublisherClosure?(permission, publisher)
}

//MARK: - registerForBackgroundDelivery

var registerForBackgroundDeliveryCallsCount = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ final class ActivitySummaryManagerTests: FCTestCase {
override func setUp() {
super.setUp()
userManager.user = .evan
featureFlagManager.valueForBoolFeatureFlagFeatureFlagBoolBoolClosure = { $0 == .sharedBackgroundDeliveryPublishers ? true : false }
featureFlagManager.valueForDoubleFeatureFlagFeatureFlagDoubleDoubleClosure = { flag in
switch flag {
case .dataUploadGracePeriodHours: return 12.0
default: return 0.0
}
}
featureFlagManager.valueForBoolFeatureFlagFeatureFlagBoolBoolClosure = { flag in
switch flag {
case .ignoreManuallyEnteredHealthKitData: return true
case .adsEnabled: return true
case .newResultsBannerEnabled: return true
}
}
}

func testThatItFetchesActivitySummariesAndSetsCurrentOnSuccess() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ final class StepCountManagerTests: FCTestCase {
override func setUp() {
super.setUp()
userManager.user = .evan
featureFlagManager.valueForBoolFeatureFlagFeatureFlagBoolBoolClosure = { $0 == .sharedBackgroundDeliveryPublishers ? true : false }
featureFlagManager.valueForDoubleFeatureFlagFeatureFlagDoubleDoubleClosure = { flag in
switch flag {
case .dataUploadGracePeriodHours: return 12.0
default: return 0.0
}
}
featureFlagManager.valueForBoolFeatureFlagFeatureFlagBoolBoolClosure = { flag in
switch flag {
case .ignoreManuallyEnteredHealthKitData: return true
case .adsEnabled: return true
case .newResultsBannerEnabled: return true
}
}
}

func testThatItRefetchesWhenCompetitionsChange() {
Expand Down
8 changes: 7 additions & 1 deletion FriendlyCompetitionsTests/Managers/WorkoutManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ final class WorkoutManagerTests: FCTestCase {
override func setUp() {
super.setUp()
userManager.user = .evan
featureFlagManager.valueForBoolFeatureFlagFeatureFlagBoolBoolClosure = { $0 == .sharedBackgroundDeliveryPublishers ? true : false }
featureFlagManager.valueForDoubleFeatureFlagFeatureFlagDoubleDoubleClosure = { flag in
switch flag {
case .dataUploadGracePeriodHours: return 12.0
default: return 0.0
}
}
featureFlagManager.valueForBoolFeatureFlagFeatureFlagBoolBoolClosure = { flag in
switch flag {
case .ignoreManuallyEnteredHealthKitData: return true
case .adsEnabled: return true
case .newResultsBannerEnabled: return true
}
}
}

func testThatItRefetchesWhenCompetitionsChange() {
Expand Down

0 comments on commit bfdedda

Please sign in to comment.