Skip to content

Commit

Permalink
Update Preference Change Main Actor Usage
Browse files Browse the repository at this point in the history
  • Loading branch information
PSchmiedmayer committed Jan 12, 2025
1 parent 6ead6dc commit 87de80c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
7 changes: 5 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ let package = Package(
.library(name: "ResearchKitSwiftUI", targets: ["ResearchKitSwiftUI"]),
.library(name: "ResearchKitSwiftUIBridge", targets: ["ResearchKitSwiftUIBridge"])
],
dependencies: [] + swiftLintPackage(),
dependencies: [
.package(url: "https://github.com/StanfordSpezi/SpeziFoundation.git", from: "2.0.1"),
] + swiftLintPackage(),
targets: [
.binaryTarget(
name: "ResearchKit",
Expand All @@ -42,7 +44,8 @@ let package = Package(
.target(
name: "ResearchKitSwiftUI",
dependencies: [
.target(name: "ResearchKit")
.target(name: "ResearchKit"),
.product(name: "SpeziFoundation", package: "SpeziFoundation")
],
path: "ResearchKitSwiftUI"
),
Expand Down
28 changes: 17 additions & 11 deletions ResearchKitSwiftUI/ResearchForm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import SpeziFoundation
import SwiftUI

/// Manages the navigation between steps in a survey.
Expand Down Expand Up @@ -169,16 +170,20 @@ public struct ResearchFormStep<Header: View, Content: View>: View {
question
}
}
.onPreferenceChange(QuestionRequiredPreferenceKey.self) {
if $0 == true {
requiredQuestions.insert(question.id)
.onPreferenceChange(QuestionRequiredPreferenceKey.self) { value in
runOrScheduleOnMainActor {
if value {
requiredQuestions.insert(question.id)
}
}
}
.onPreferenceChange(QuestionAnsweredPreferenceKey.self) {
if $0 == true {
answeredQuestions.insert(question.id)
} else {
answeredQuestions.remove(question.id)
.onPreferenceChange(QuestionAnsweredPreferenceKey.self) { value in
runOrScheduleOnMainActor {
if value {
answeredQuestions.insert(question.id)
} else {
answeredQuestions.remove(question.id)
}
}
}
.onAppear {
Expand All @@ -197,9 +202,10 @@ public struct ResearchFormStep<Header: View, Content: View>: View {
#if os(iOS)
.frame(maxWidth: .infinity, alignment: .leading)
#endif
.onPreferenceChange(QuestionCardPreferenceKey.self) {
shouldWrapInQuestionCard in
self.shouldWrapInQuestionCard = shouldWrapInQuestionCard
.onPreferenceChange(QuestionCardPreferenceKey.self) { shouldWrapInQuestionCard in
runOrScheduleOnMainActor {
self.shouldWrapInQuestionCard = shouldWrapInQuestionCard
}
}
}

Expand Down
7 changes: 5 additions & 2 deletions ResearchKitSwiftUI/ResearchFormStepContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import SpeziFoundation
import SwiftUI

struct ResearchFormStepContentView<Content: View>: View {
Expand Down Expand Up @@ -55,8 +56,10 @@ struct ResearchFormStepContentView<Content: View>: View {
var body: some View {
StickyScrollView {
content
.onPreferenceChange(StepCompletedPreferenceKey.self) {
doneButtonEnabled = $0
.onPreferenceChange(StepCompletedPreferenceKey.self) { value in
runOrScheduleOnMainActor {
doneButtonEnabled = value
}
}
.padding()
#if !os(watchOS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import SpeziFoundation
import SwiftUI

/// A vertical ScrollView with a sticky footer, that returns to flow inline if
Expand Down Expand Up @@ -224,19 +225,25 @@ struct StickyScrollView<BodyContent: View, FooterContent: View>: View {
}
.coordinateSpace(name: scrollCoordinateSpace)
.onPreferenceChange(FrameSizeKey.self) { value in
self.frameSize = value
runOrScheduleOnMainActor {
self.frameSize = value
}
}
.onPreferenceChange(SafeAreaInsetsKey.self) { value in
self.safeAreaInsets = value
runOrScheduleOnMainActor {
self.safeAreaInsets = value
}
}
.onPreferenceChange(KeyboardIgnoringSafeAreaInsets.self) { value in
self.keyboardIgnoringSafeAreaInsets = value
runOrScheduleOnMainActor {
self.keyboardIgnoringSafeAreaInsets = value
}
}
.onPreferenceChange(
BodySizeKey.self,
perform: { value in
.onPreferenceChange(BodySizeKey.self) { value in
runOrScheduleOnMainActor {
self.bodySize = value
})
}
}
}
}
}
Expand Down

0 comments on commit 87de80c

Please sign in to comment.