Skip to content

Commit 1015984

Browse files
authored
Merge pull request #6 from jannikwojnar-diconium/patch-1
Add @published to relevant values SwiftNFC.swift
2 parents b94009c + 05c7355 commit 1015984

5 files changed

Lines changed: 51 additions & 13 deletions

File tree

Demo/NFC Read-Write/Localizable.xcstrings

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"sourceLanguage" : "en",
33
"strings" : {
44
"Close Keyboard" : {
5+
"extractionState" : "stale",
56
"localizations" : {
67
"zh-HK" : {
78
"stringUnit" : {
@@ -30,6 +31,9 @@
3031
}
3132
}
3233
}
34+
},
35+
"SwiftNFC" : {
36+
3337
},
3438
"Text" : {
3539
"localizations" : {

Demo/NFC Read-Write/NFC_Read_WriteApp.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct NFC_Read_WriteApp: App {
1313
var body: some Scene {
1414
WindowGroup {
1515
ContentView()
16+
.statusBarHidden(true)
1617
}
1718
}
1819
}

Demo/NFC Read-Write/Views/DraggableInterfaceView.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ struct DraggableInterfaceView<TopContent: View, BottomContent: View, DragContent
1111
let topContent: TopContent
1212
let bottomContent: BottomContent
1313
let dragContent: DragContent
14-
14+
1515
@State private var topHeight: CGFloat = 0
1616
@State private var isDragging = false
17+
@State private var topSafeArea: CGFloat = 0
1718

1819
init(
1920
@ViewBuilder topContent: () -> TopContent,
@@ -28,11 +29,12 @@ struct DraggableInterfaceView<TopContent: View, BottomContent: View, DragContent
2829
var body: some View {
2930
GeometryReader { geometry in
3031
let initialHeight = geometry.size.height / 2
31-
32+
3233
VStack(spacing: 0) {
3334
// Top section
3435
VStack {
3536
topContent
37+
.padding(.top, topSafeArea)
3638
}
3739
.frame(height: topHeight)
3840
.frame(maxWidth: .infinity)
@@ -85,7 +87,13 @@ struct DraggableInterfaceView<TopContent: View, BottomContent: View, DragContent
8587
}
8688
.onAppear {
8789
if topHeight == 0 {
88-
topHeight = initialHeight - 100
90+
topHeight = initialHeight
91+
}
92+
if topSafeArea == 0 {
93+
topSafeArea = UIApplication.shared.connectedScenes
94+
.compactMap { $0 as? UIWindowScene }
95+
.first?.windows
96+
.first?.safeAreaInsets.top ?? 0
8997
}
9098
}
9199
}

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,32 @@ func write() {
7171
}
7272
```
7373

74+
## Reactive Properties
75+
76+
Both `NFCReader` and `NFCWriter` classes use `@Published` properties for automatic SwiftUI view updates:
77+
78+
### NFCReader Properties
79+
- `startAlert` - Alert message shown when scanning starts
80+
- `endAlert` - Custom alert message after scanning (optional)
81+
- `msg` - Decoded NFC message content (updates after scan)
82+
- `raw` - Raw NFC data with type, identifier, and payload information
83+
84+
### NFCWriter Properties
85+
- `startAlert` - Alert message shown when writing starts
86+
- `endAlert` - Custom alert message after writing (optional)
87+
- `msg` - Content to write to the NFC tag
88+
- `type` - Record type: "T" for text (default) or "U" for URI
89+
90+
These properties automatically trigger view updates when changed, enabling seamless SwiftUI integration with two-way bindings:
91+
92+
```swift
93+
// Example: Bind to a TextField
94+
TextField("Message", text: $NFCW.msg)
95+
96+
// Example: Display scan results
97+
Text(NFCR.msg)
98+
```
99+
74100
## Demo
75101
Path: `./Demo` (Xcode Project in SwiftUI)
76102

Sources/SwiftNFC/SwiftNFC.swift

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import CoreNFC
44
@available(iOS 15.0, *)
55
public class NFCReader: NSObject, ObservableObject, NFCNDEFReaderSessionDelegate {
66

7-
public var startAlert = String(localized: "Hold your iPhone near the tag.", bundle: .module)
8-
public var endAlert = ""
9-
public var msg = String(localized: "Scan to read or Edit here to write...", bundle: .module)
10-
public var raw = String(localized: "Raw Data available after scan.", bundle: .module)
7+
@Published public var startAlert = String(localized: "Hold your iPhone near the tag.", bundle: .module)
8+
@Published public var endAlert = ""
9+
@Published public var msg = String(localized: "Scan to read or Edit here to write...", bundle: .module)
10+
@Published public var raw = String(localized: "Raw Data available after scan.", bundle: .module)
1111

1212
public var session: NFCNDEFReaderSession?
1313

@@ -50,12 +50,11 @@ public class NFCReader: NSObject, ObservableObject, NFCNDEFReaderSessionDelegate
5050
}
5151

5252
public class NFCWriter: NSObject, ObservableObject, NFCNDEFReaderSessionDelegate {
53-
54-
public var startAlert = String(localized: "Hold your iPhone near the tag.", bundle: .module)
55-
public var endAlert = ""
56-
public var msg = ""
57-
public var type = "T"
58-
53+
@Published public var startAlert = String(localized: "Hold your iPhone near the tag.", bundle: .module)
54+
@Published public var endAlert = ""
55+
@Published public var msg = ""
56+
@Published public var type = "T"
57+
5958
public var session: NFCNDEFReaderSession?
6059

6160
public func write() {

0 commit comments

Comments
 (0)