Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown Checkbox Functionality #54

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
Text(markdown)
.padding()

if checkboxSnapshot != nil {
Image(uiImage: checkboxSnapshot!)

Check failure on line 76 in Sources/SpeziOnboarding/ConsentView/ConsentDocument+Export.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint / SwiftLint

Force Unwrapping Violation: Force unwrapping should be avoided (force_unwrapping)

Check warning on line 76 in Sources/SpeziOnboarding/ConsentView/ConsentDocument+Export.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests iOS (Debug, TestApp-iOS.xcresult, TestApp-iOS.xcresult) / Test using xcodebuild or run fastlane

Force Unwrapping Violation: Force unwrapping should be avoided (force_unwrapping)

Check warning on line 76 in Sources/SpeziOnboarding/ConsentView/ConsentDocument+Export.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests visionOS (Debug, TestApp-visionOS.xcresult, TestApp-visionOS.xcresult) / Test using xcodebuild or run fastlane

Force Unwrapping Violation: Force unwrapping should be avoided (force_unwrapping)
}

Spacer()

ZStack(alignment: .bottomLeading) {
Expand Down Expand Up @@ -101,7 +105,10 @@
/// - Returns: The exported consent form in PDF format as a PDFKit `PDFDocument`
@MainActor
func export() async -> PDFDocument? {
let markdown = await asyncMarkdown()
var markdown = await asyncMarkdown()
if cleanedMarkdownData != nil {
markdown = cleanedMarkdownData!

Check failure on line 110 in Sources/SpeziOnboarding/ConsentView/ConsentDocument+Export.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint / SwiftLint

Force Unwrapping Violation: Force unwrapping should be avoided (force_unwrapping)
}

let markdownString = (try? AttributedString(
markdown: markdown,
Expand Down
78 changes: 76 additions & 2 deletions Sources/SpeziOnboarding/ConsentView/ConsentDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@
#endif
@State var signatureSize: CGSize = .zero
@Binding private var viewState: ConsentViewState

@State private var checked: [String: Bool] = [:]
@State private var markdownStrings: [String] = []
@State public var cleanedMarkdownData: Data?
@State public var checkboxSnapshot: UIImage?

Check failure on line 63 in Sources/SpeziOnboarding/ConsentView/ConsentDocument.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package macOS (Debug, SpeziOnboarding-macOS.xcresult, SpeziOnboarding-macOS.... / Test using xcodebuild or run fastlane

cannot find type 'UIImage' in scope

Check failure on line 63 in Sources/SpeziOnboarding/ConsentView/ConsentDocument.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package macOS (Debug, SpeziOnboarding-macOS.xcresult, SpeziOnboarding-macOS.... / Test using xcodebuild or run fastlane

cannot find type 'UIImage' in scope

Check failure on line 63 in Sources/SpeziOnboarding/ConsentView/ConsentDocument.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package macOS (Debug, SpeziOnboarding-macOS.xcresult, SpeziOnboarding-macOS.... / Test using xcodebuild or run fastlane

cannot find type 'UIImage' in scope

Check failure on line 63 in Sources/SpeziOnboarding/ConsentView/ConsentDocument.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package macOS (Release, SpeziOnboarding-macOS-Release.xcresult, SpeziOnboard... / Test using xcodebuild or run fastlane

cannot find type 'UIImage' in scope

private var nameView: some View {
VStack {
Expand Down Expand Up @@ -91,6 +94,69 @@
}
}

private func extractMarkdownCB() async -> (cleanedMarkdown: String, checkboxes: [String]) {

Check failure on line 97 in Sources/SpeziOnboarding/ConsentView/ConsentDocument.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint / SwiftLint

Type Contents Order Violation: An 'other_method' should not be placed amongst the type content(s) 'instance_property' (type_contents_order)

Check warning on line 97 in Sources/SpeziOnboarding/ConsentView/ConsentDocument.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests iOS (Debug, TestApp-iOS.xcresult, TestApp-iOS.xcresult) / Test using xcodebuild or run fastlane

Type Contents Order Violation: An 'other_method' should not be placed amongst the type content(s) 'instance_property' (type_contents_order)

Check warning on line 97 in Sources/SpeziOnboarding/ConsentView/ConsentDocument.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests visionOS (Debug, TestApp-visionOS.xcresult, TestApp-visionOS.xcresult) / Test using xcodebuild or run fastlane

Type Contents Order Violation: An 'other_method' should not be placed amongst the type content(s) 'instance_property' (type_contents_order)
let data = await asyncMarkdown()
let dataString = String(data: data, encoding: .utf8)!

Check failure on line 99 in Sources/SpeziOnboarding/ConsentView/ConsentDocument.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint / SwiftLint

Force Unwrapping Violation: Force unwrapping should be avoided (force_unwrapping)

var result = [String]()
var start: String.Index? = nil

Check failure on line 102 in Sources/SpeziOnboarding/ConsentView/ConsentDocument.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint / SwiftLint

Redundant Optional Initialization Violation: Initializing an optional variable with nil is redundant (redundant_optional_initialization)
var cleanedMarkdown = ""
var isInBracket = false

for (index, char) in dataString.enumerated() {
let currentIndex = dataString.index(dataString.startIndex, offsetBy: index)

if char == "[" {
start = currentIndex
isInBracket = true
} else if char == "]", let start = start {
let end = currentIndex
let subInd = dataString.index(after: start)
let substring = String(dataString[subInd..<end])
result.append(substring)
isInBracket = false
} else if !isInBracket {
cleanedMarkdown.append(char)
}
}

return (cleanedMarkdown, result)
}

private var checkboxesView: some View {
VStack {
Grid(horizontalSpacing: 15) {
ForEach(markdownStrings, id: \.self) { markdownString in
GridRow {
Text(markdownString)
.frame(maxWidth: .infinity, alignment: .leading)
Button(action: {
withAnimation {
checked[markdownString]?.toggle()
}
}) {
Image(systemName: checked[markdownString] == true ? "checkmark.square.fill" : "xmark.square.fill")

Check failure on line 138 in Sources/SpeziOnboarding/ConsentView/ConsentDocument.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint / SwiftLint

Accessibility Label for Image Violation: Images that provide context should have an accessibility label or should be explicitly hidden from accessibility (accessibility_label_for_image)
.foregroundColor(checked[markdownString] == true ? .green : .red)
}
}
Divider()
.gridCellUnsizedAxes(.horizontal)
}
}
}
.onAppear {
Task {
let (cleanedMarkdown, checkboxes) = await extractMarkdownCB()
cleanedMarkdownData = Data(cleanedMarkdown.utf8)
markdownStrings = checkboxes
for string in markdownStrings {
checked[string] = false
}
}
}
}


private var nameInputView: some View {
Grid(horizontalSpacing: 15) {
NameFieldRow(name: $name, for: \.givenName) {
Expand Down Expand Up @@ -134,7 +200,11 @@

public var body: some View {
VStack {
MarkdownView(asyncMarkdown: asyncMarkdown, state: $viewState.base)
if let cleanedMarkdownData = cleanedMarkdownData {
MarkdownView(asyncMarkdown: {cleanedMarkdownData}, state: $viewState.base)

Check failure on line 204 in Sources/SpeziOnboarding/ConsentView/ConsentDocument.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint / SwiftLint

Closure Spacing Violation: Closure expressions should have a single space inside each brace (closure_spacing)

Check warning on line 204 in Sources/SpeziOnboarding/ConsentView/ConsentDocument.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests iOS (Debug, TestApp-iOS.xcresult, TestApp-iOS.xcresult) / Test using xcodebuild or run fastlane

Closure Spacing Violation: Closure expressions should have a single space inside each brace (closure_spacing)

Check warning on line 204 in Sources/SpeziOnboarding/ConsentView/ConsentDocument.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests visionOS (Debug, TestApp-visionOS.xcresult, TestApp-visionOS.xcresult) / Test using xcodebuild or run fastlane

Closure Spacing Violation: Closure expressions should have a single space inside each brace (closure_spacing)
.padding(.bottom, 15)
}
checkboxesView
Spacer()
Group {
nameView
Expand All @@ -151,6 +221,10 @@
.animation(.easeInOut, value: viewState == .namesEntered)
.onChange(of: viewState) {
if case .export = viewState {
let renderer = ImageRenderer(content: checkboxesView)
if let uiImage = renderer.uiImage {

Check failure on line 225 in Sources/SpeziOnboarding/ConsentView/ConsentDocument.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package macOS (Debug, SpeziOnboarding-macOS.xcresult, SpeziOnboarding-macOS.... / Test using xcodebuild or run fastlane

value of type 'ImageRenderer<some View>' has no member 'uiImage'

Check failure on line 225 in Sources/SpeziOnboarding/ConsentView/ConsentDocument.swift

View workflow job for this annotation

GitHub Actions / Build and Test Swift Package macOS (Release, SpeziOnboarding-macOS-Release.xcresult, SpeziOnboard... / Test using xcodebuild or run fastlane

value of type 'ImageRenderer<some View>' has no member 'uiImage'
checkboxSnapshot = uiImage
}
Task {
guard let exportedConsent = await export() else {
viewState = .base(.error(Error.memoryAllocationError))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
import SpeziOnboarding
import SpeziViews
import SwiftUI

import QuickLook

Check failure on line 13 in Tests/UITests/TestApp/Views/OnboardingConsentMarkdownRenderingView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint / SwiftLint

Sorted Imports Violation: Imports should be sorted (sorted_imports)

struct OnboardingConsentMarkdownRenderingView: View {
@Environment(OnboardingNavigationPath.self) private var path
@Environment(ExampleStandard.self) private var standard
@State var exportedConsent: PDFDocument?

@State var pdfURL: URL?

var body: some View {
VStack {

Check failure on line 22 in Tests/UITests/TestApp/Views/OnboardingConsentMarkdownRenderingView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint / SwiftLint

Closure Body Length Violation: Closure body should span 35 lines or less excluding comments and whitespace: currently spans 36 lines (closure_body_length)

Check failure on line 22 in Tests/UITests/TestApp/Views/OnboardingConsentMarkdownRenderingView.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests iOS (Debug, TestApp-iOS.xcresult, TestApp-iOS.xcresult) / Test using xcodebuild or run fastlane

Closure Body Length Violation: Closure body should span 35 lines or less excluding comments and whitespace: currently spans 36 lines (closure_body_length)

Check failure on line 22 in Tests/UITests/TestApp/Views/OnboardingConsentMarkdownRenderingView.swift

View workflow job for this annotation

GitHub Actions / Build and Test UI Tests visionOS (Debug, TestApp-visionOS.xcresult, TestApp-visionOS.xcresult) / Test using xcodebuild or run fastlane

Closure Body Length Violation: Closure body should span 35 lines or less excluding comments and whitespace: currently spans 36 lines (closure_body_length)
if (exportedConsent?.pageCount ?? 0) == 0 {
Circle()
.fill(Color.red)
Expand All @@ -41,6 +41,15 @@
.padding()
)
}
Button("PDF preview") {
if let document = exportedConsent, let data = document.dataRepresentation() {
let tempURL = FileManager.default.temporaryDirectory.appendingPathComponent("consent.pdf")
try? data.write(to: tempURL)
pdfURL = tempURL
}
}
.quickLookPreview($pdfURL)
.buttonStyle(.borderedProminent)

Button {
path.nextStep()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct OnboardingConsentMarkdownTestView: View {
var body: some View {
OnboardingConsentView(
markdown: {
Data("This is a *markdown* **example**".utf8)
Data("This is a *markdown* **example** [May we contact you for future studies?]".utf8)
},
action: {
path.nextStep()
Expand Down
Loading