Skip to content

Commit d0aadb5

Browse files
committed
Add activityEnvironmentLog
1 parent 7a0d0df commit d0aadb5

File tree

1 file changed

+92
-134
lines changed

1 file changed

+92
-134
lines changed

Sources/OpenSwiftUI/App/Scene/SceneBridge.swift

Lines changed: 92 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Audited for 6.5.4
66
// Status: Complete
77
// ID: A9714FE7FB47B9EE521B92A735A59E38 (SwiftUI)
8+
// TODO: Add test case and verify [Q]
89

910
#if canImport(Darwin)
1011
#if os(iOS) || os(visionOS)
@@ -25,6 +26,13 @@ import OpenSwiftUICore
2526
@available(OpenSwiftUI_v2_0, *)
2627
public var _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled = false
2728

29+
@_transparent
30+
private func activityEnvironmentLog(_ message: @autoclosure () -> String) {
31+
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
32+
Log.log(message())
33+
}
34+
}
35+
2836
// MARK: - UserActivityTrackingInfo
2937

3038
class UserActivityTrackingInfo: NSObject, NSUserActivityDelegate {
@@ -44,9 +52,7 @@ class UserActivityTrackingInfo: NSObject, NSUserActivityDelegate {
4452
}
4553

4654
func userActivityWillSave(_ userActivity: NSUserActivity) {
47-
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
48-
Log.log("userActivityWillSave called for \(description)")
49-
}
55+
activityEnvironmentLog("userActivityWillSave called for \(description)")
5056
if Thread.isMainThread {
5157
updateUserActivity(userActivity)
5258
} else {
@@ -59,16 +65,12 @@ class UserActivityTrackingInfo: NSObject, NSUserActivityDelegate {
5965
func updateUserActivity(_ userActivity: NSUserActivity) {
6066
guard let currentActivity = self.userActivity,
6167
currentActivity == userActivity else {
62-
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
63-
Log.log("Mismatched UserActivity in tracking info, skipping update.")
64-
}
68+
activityEnvironmentLog("Mismatched UserActivity in tracking info, skipping update.")
6569
return
6670
}
6771
guard let sceneBridge else { return }
6872
let failedIDs = handlers.compactMap { identity, handler in
69-
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
70-
Log.log("Invoking handler for \(identity)")
71-
}
73+
activityEnvironmentLog("Invoking handler for \(identity)")
7274
return handler(userActivity) ? nil : identity
7375
}
7476
for id in failedIDs {
@@ -95,12 +97,10 @@ class UserActivityTrackingInfo: NSObject, NSUserActivityDelegate {
9597
sceneBridge.userActivityTrackingInfo = self
9698
}
9799
userActivity.needsSave = false
98-
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
99-
Log.log(
100-
"updated user activity \(String(describing: userActivity.title)) "
101-
+ "with userInfo \(String(describing: userActivity.userInfo))"
102-
)
103-
}
100+
activityEnvironmentLog(
101+
"updated user activity \(String(describing: userActivity.title)) "
102+
+ "with userInfo \(String(describing: userActivity.userInfo))"
103+
)
104104
}
105105
}
106106

@@ -215,20 +215,16 @@ final class SceneBridge: ObservableObject, CustomStringConvertible {
215215
static var defaultValue: Value { nil }
216216

217217
static func reduce(value: inout Value, nextValue: () -> Value) {
218-
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
219-
Log.log(
220-
"Reducing UserActivityPreference " +
221-
"\(String(describing: value)) " +
222-
"with \(String(describing: nextValue()))"
223-
)
224-
}
218+
activityEnvironmentLog(
219+
"Reducing UserActivityPreference " +
220+
"\(String(describing: value)) " +
221+
"with \(String(describing: nextValue()))"
222+
)
225223
defer {
226-
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
227-
Log.log(
228-
"Reduced UserActivityPreference to " +
229-
"\(String(describing: value))"
230-
)
231-
}
224+
activityEnvironmentLog(
225+
"Reduced UserActivityPreference to " +
226+
"\(String(describing: value))"
227+
)
232228
}
233229
guard let current = value else {
234230
value = nextValue()
@@ -253,19 +249,15 @@ final class SceneBridge: ObservableObject, CustomStringConvertible {
253249
let preferenceValue = preferences[UserActivityPreferenceKey.self]
254250
if let userActivityPreferenceSeed,
255251
preferenceValue.seed.matches(userActivityPreferenceSeed) {
256-
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
257-
Log.log(
258-
"UserActivity Preferences hasn't changed, skipping update for advertised NSUserActivities. " +
259-
"Seed is \(preferenceValue.seed)"
260-
)
261-
}
252+
activityEnvironmentLog(
253+
"UserActivity Preferences hasn't changed, skipping update for advertised NSUserActivities. " +
254+
"Seed is \(preferenceValue.seed)"
255+
)
262256
} else {
263-
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
264-
Log.log(
265-
"UserActivityPreferences changed: " +
266-
"\(preferenceValue)"
267-
)
268-
}
257+
activityEnvironmentLog(
258+
"UserActivityPreferences changed: " +
259+
"\(preferenceValue)"
260+
)
269261
userActivityPreferenceSeed = preferenceValue.seed
270262
guard let value = preferenceValue.value, !value.handlers.isEmpty else {
271263
userActivityTrackingInfo = nil
@@ -282,9 +274,7 @@ final class SceneBridge: ObservableObject, CustomStringConvertible {
282274
initialUserActivity = nil
283275
}
284276
#endif
285-
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
286-
Log.log("Cleared AdvertiseUserActivity tracking info since UserActivity preferences are empty")
287-
}
277+
activityEnvironmentLog("Cleared AdvertiseUserActivity tracking info since UserActivity preferences are empty")
288278
return
289279
}
290280
let trackingInfo = userActivityTrackingInfo ?? UserActivityTrackingInfo(
@@ -302,12 +292,10 @@ final class SceneBridge: ObservableObject, CustomStringConvertible {
302292
if activity !== oldActivity {
303293
activity.delegate = trackingInfo
304294
}
305-
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
306-
Log.log(
307-
"Initializing advertised user activity: " +
308-
"\(String(describing: trackingInfo.userActivity))"
309-
)
310-
}
295+
activityEnvironmentLog(
296+
"Initializing advertised user activity: " +
297+
"\(String(describing: trackingInfo.userActivity))"
298+
)
311299
userActivityTrackingInfo = trackingInfo
312300
#if os(iOS) || os(visionOS)
313301
if let rootViewController {
@@ -322,20 +310,16 @@ final class SceneBridge: ObservableObject, CustomStringConvertible {
322310
initialUserActivity = trackingInfo.userActivity
323311
}
324312
#endif
325-
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
326-
Log.log(
327-
"View Advertising UserActivity, set rootViewController activity to " +
328-
"\(String(describing: trackingInfo.userActivity))"
329-
)
330-
}
331-
}
332-
trackingInfo.handlers = value.handlers
333-
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
334-
Log.log(
335-
"Set up AdvertiseUserActivity tracking info from " +
336-
"value in UserActivityPreferenceKey: \(trackingInfo.description)"
313+
activityEnvironmentLog(
314+
"View Advertising UserActivity, set rootViewController activity to " +
315+
"\(String(describing: trackingInfo.userActivity))"
337316
)
338317
}
318+
trackingInfo.handlers = value.handlers
319+
activityEnvironmentLog(
320+
"Set up AdvertiseUserActivity tracking info from " +
321+
"value in UserActivityPreferenceKey: \(trackingInfo.description)"
322+
)
339323
}
340324
}
341325

@@ -363,27 +347,21 @@ final class SceneBridge: ObservableObject, CustomStringConvertible {
363347
let preferenceValue = preferences[ActivationConditionsPreferenceKey.self]
364348
if let activationConditionsPreferenceSeed,
365349
preferenceValue.seed.matches(activationConditionsPreferenceSeed) {
366-
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
367-
Log.log(
368-
"ActivationConditions Preferences hasn't changed, skipping update for Scene ActivationConditions. " +
369-
"Seed is \(preferenceValue.seed)"
370-
)
371-
}
350+
activityEnvironmentLog(
351+
"ActivationConditions Preferences hasn't changed, skipping update for Scene ActivationConditions. " +
352+
"Seed is \(preferenceValue.seed)"
353+
)
372354
} else {
373-
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
374-
Log.log(
375-
"ActivationConditionPreferences changed: " +
376-
"\(preferenceValue)"
377-
)
378-
}
355+
activityEnvironmentLog(
356+
"ActivationConditionPreferences changed: " +
357+
"\(preferenceValue)"
358+
)
379359
activationConditionsPreferenceSeed = preferenceValue.seed
380360
setActivationConditions(preferenceValue.value)
381-
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
382-
Log.log(
383-
"Set Scene ActivationConditions to " +
384-
"\(String(describing: sceneActivationConditions))"
385-
)
386-
}
361+
activityEnvironmentLog(
362+
"Set Scene ActivationConditions to " +
363+
"\(String(describing: sceneActivationConditions))"
364+
)
387365
}
388366
}
389367

@@ -411,12 +389,10 @@ final class SceneBridge: ObservableObject, CustomStringConvertible {
411389
newConditions.prefersToActivateForTargetContentIdentifierPredicate = existingConditions.prefersToActivateForTargetContentIdentifierPredicate
412390
newConditions.canActivateForTargetContentIdentifierPredicate = existingConditions.canActivateForTargetContentIdentifierPredicate
413391
windowScene.activationConditions = newConditions
414-
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
415-
Log.log(
416-
"Changed Scene ActivationConditions to " +
417-
"\(windowScene.activationConditions.description)"
418-
)
419-
}
392+
activityEnvironmentLog(
393+
"Changed Scene ActivationConditions to " +
394+
"\(windowScene.activationConditions.description)"
395+
)
420396
sceneActivationConditions = conditions
421397
}
422398
#elseif os(macOS)
@@ -518,9 +494,7 @@ struct UserActivityModifier: ViewModifier {
518494
content
519495
.advertiseUserActivity(activityType, isActive: isActive, sceneBridge: bridge) { activity in
520496
guard isActive else {
521-
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
522-
Log.log("Skipping inactive advertiseUserActivity handler")
523-
}
497+
activityEnvironmentLog("Skipping inactive advertiseUserActivity handler")
524498
return false
525499
}
526500
update(activity)
@@ -552,16 +526,12 @@ extension View {
552526
handler: @escaping (NSUserActivity) -> Bool
553527
) -> some View {
554528
transformIdentifiedPreference(SceneBridge.UserActivityPreferenceKey.self) { value, identity in
555-
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
556-
Log.log(
557-
"TransformIdentifiedPreference closure for UserActivity " +
558-
"called with value \(String(describing: value))"
559-
)
560-
}
529+
activityEnvironmentLog(
530+
"TransformIdentifiedPreference closure for UserActivity " +
531+
"called with value \(String(describing: value))"
532+
)
561533
guard isActive, let sceneBridge else {
562-
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
563-
Log.log("TransformIdentifiedPreference closure for UserActivity: inactive, leaving value alone")
564-
}
534+
activityEnvironmentLog("TransformIdentifiedPreference closure for UserActivity: inactive, leaving value alone")
565535
return
566536
}
567537
var handlers: [ViewIdentity: (NSUserActivity) -> Bool]
@@ -572,12 +542,10 @@ extension View {
572542
handlers = [identity: handler]
573543
}
574544
value = .init((activityType: activityType, handlers: handlers))
575-
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
576-
Log.log(
577-
"TransformIdentifiedPreference for UserActivity setting value " +
578-
"to \(String(describing: value))"
579-
)
580-
}
545+
activityEnvironmentLog(
546+
"TransformIdentifiedPreference for UserActivity setting value " +
547+
"to \(String(describing: value))"
548+
)
581549
}
582550
}
583551

@@ -676,13 +644,11 @@ extension View {
676644
)
677645
return self.onReceive(publisher) { output in
678646
guard let activity = output as? NSUserActivity else {
679-
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
680-
Log.log(
681-
"onUserActivity skipping event with " +
682-
"identifier \(activityType), published object is not " +
683-
"a NSUserActivity: \(output)"
684-
)
685-
}
647+
activityEnvironmentLog(
648+
"onUserActivity skipping event with " +
649+
"identifier \(activityType), published object is not " +
650+
"a NSUserActivity: \(output)"
651+
)
686652
return
687653
}
688654
action(activity)
@@ -723,12 +689,10 @@ extension View {
723689
)
724690
return self.onReceive(publisher) { output in
725691
guard let context = output as? OpenURLContext else {
726-
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
727-
Log.log(
728-
"onURL skipping event for OpenURLContext, " +
729-
"published object is not a OpenURLContext: \(output)"
730-
)
731-
}
692+
activityEnvironmentLog(
693+
"onURL skipping event for OpenURLContext, " +
694+
"published object is not a OpenURLContext: \(output)"
695+
)
732696
return
733697
}
734698
action(context.url)
@@ -750,12 +714,10 @@ extension View {
750714
)
751715
return self.onReceive(publisher) { output in
752716
guard let context = output as? OpenURLContext else {
753-
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
754-
Log.log(
755-
"onURL skipping event for OpenURLContext, " +
756-
"published object is not a OpenURLContext: \(output)"
757-
)
758-
}
717+
activityEnvironmentLog(
718+
"onURL skipping event for OpenURLContext, " +
719+
"published object is not a OpenURLContext: \(output)"
720+
)
759721
return
760722
}
761723
action(context.url, context.options)
@@ -898,22 +860,18 @@ extension View {
898860
transformPreference(
899861
SceneBridge.ActivationConditionsPreferenceKey.self
900862
) { value in
901-
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
902-
Log.log(
903-
"TransformPreference closure for activation conditions called " +
904-
"\(String(describing: value))"
905-
)
906-
}
863+
activityEnvironmentLog(
864+
"TransformPreference closure for activation conditions called " +
865+
"\(String(describing: value))"
866+
)
907867
value = .init((
908868
preferring: value?.preferring.union(preferring) ?? preferring,
909869
allowing: value?.allowing.union(allowing) ?? allowing,
910870
))
911-
if _defaultOpenSwiftUIActivityEnvironmentLoggingEnabled {
912-
Log.log(
913-
"TransformPreference setting value for activation conditions" +
914-
"\(String(describing: value))"
915-
)
916-
}
871+
activityEnvironmentLog(
872+
"TransformPreference setting value for activation conditions" +
873+
"\(String(describing: value))"
874+
)
917875
}
918876
}
919877
}

0 commit comments

Comments
 (0)