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

Replace to async sequence interface #138

Merged
merged 32 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
301f114
Add `init(pollingOption:)` and `eventStream` for `NFCTagReaderSession`
treastrain Sep 15, 2024
780c17f
Add `init(vasCommandConfigurations:)` and `eventStream` for `NFCVASRe…
treastrain Sep 15, 2024
10b152c
Add `init(of:invalidateAfterFirstRead:)` and `eventStream` for `NFCND…
treastrain Sep 15, 2024
40b83ee
Create `NFCDetectedObjects`
treastrain Sep 15, 2024
6b3d060
Conform `NFCDetectedObjects` to `CustomStringConvertible`
treastrain Oct 19, 2024
604f517
Add `NFCNDEFReaderSession.messageEventStream` and `NFCNDEFReaderSessi…
treastrain Oct 19, 2024
1c882dc
Delete `AsyncNFCNDEFMessageReaderSession` and `AsyncNFCNDEFTagReaderS…
treastrain Oct 19, 2024
728f81a
Delete `AsyncNFCTagReaderSession`
treastrain Oct 19, 2024
452a13a
Delete `AsyncNFCVASReaderSession`
treastrain Oct 19, 2024
88dedd1
Delete `TRETNFCKit_Async`
treastrain Oct 19, 2024
1af4729
Replace `NFCNDEFMessageReaderViewModifier` with async sequence
treastrain Oct 19, 2024
28d0ee5
Delete `NDEFMessage` and the reader
treastrain Oct 19, 2024
9c31a51
Create `NFCReaderSessionInvalidator`
treastrain Oct 19, 2024
4af7835
Fix risks causing data races
treastrain Oct 19, 2024
ccfcfa2
Delete `NDEFTag` and the reader
treastrain Oct 19, 2024
89926d0
Delete `NFCNDEFReader`
treastrain Oct 19, 2024
711238c
Replace `NFCNativeTagReaderViewModifier ` with async sequence
treastrain Oct 19, 2024
4968adb
Delete `NFCTagReader`
treastrain Oct 19, 2024
f91b768
Replace `FeliCaTagReaderViewModifier` with async sequence
treastrain Oct 19, 2024
e2cd2d2
Replace `ISO7816TagReaderViewModifier` with async sequence
treastrain Oct 19, 2024
4c3947c
Replace `ISO15693TagReaderViewModifier` with async sequence
treastrain Oct 19, 2024
a5c3332
Replace `MiFareTagReaderViewModifier` with async sequence
treastrain Oct 19, 2024
f993e40
Delete unused Core files
treastrain Oct 19, 2024
e074e38
Delete unused NativeTag files
treastrain Oct 19, 2024
90ed32c
Delete unused NDEFMessage files
treastrain Oct 19, 2024
45aae4f
Delete unused NDEFTag files
treastrain Oct 19, 2024
3a43058
Delete unused Secondary modules' files
treastrain Oct 19, 2024
4afa317
Delete AssertServices
treastrain Oct 19, 2024
4433318
Update Package.swift Swift Language Mode
treastrain Oct 19, 2024
6a76312
Add empty test file
treastrain Oct 19, 2024
866aa75
Merge branch 'tretnfckit-main' into replace-to-async-sequence-interface
treastrain Oct 19, 2024
9ea16fa
Update README.md
treastrain Oct 20, 2024
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
89 changes: 24 additions & 65 deletions Examples/TRETNFCKitExample/NFCFeliCaTagReaderExampleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
//

import SwiftUI
import TRETNFCKit_Async
import TRETNFCKit_Core
import TRETNFCKit_FeliCa

struct NFCFeliCaTagReaderExampleView: View {
@State private var isPresented = false
@ObservedObject var viewModel = ViewModel()
@State private var readerSession: AsyncNFCTagReaderSession?
@State private var readerSession: NFCTagReaderSession?

var body: some View {
List {
Expand All @@ -21,69 +20,55 @@ struct NFCFeliCaTagReaderExampleView: View {
} label: {
Text("Read (using view modifier)")
}
.disabled(!NFCTagReaderSession.readingAvailable || isPresented)
Button {
Task {
try await viewModel.read()
}
} label: {
Text("Read (using reader)")
}
Button {
readerSession = AsyncNFCTagReaderSession(pollingOption: .iso18092)
readerSession = NFCTagReaderSession(pollingOption: .iso18092)
} label: {
Text("Read (using async stream)")
}
.disabled(readerSession != nil)
.disabled(!NFCTagReaderSession.readingAvailable || readerSession != nil)
}
.feliCaTagReader(
isPresented: $isPresented,
detectingAlertMessage: "Place the tag on a flat, non-metal surface and rest your iPhone on the tag.",
onBeginReadingError: { error in
print(error)
terminatingAlertMessage: "CANCELLED",
onDidBecomeActive: { readerSession in
print(readerSession.alertMessage)
},
didBecomeActive: { reader in
await print(reader.alertMessage)
onDidDetect: { readerSession, tags in
do {
let tag = tags.first!
let feliCaTag = try await readerSession.connectAsFeliCaTag(to: tag)
let (idm, systemCode) = try await feliCaTag.polling(systemCode: Data([0xFE, 0x00]), requestCode: .systemCode, timeSlot: .max1)
readerSession.alertMessage = "\(systemCode as NSData)\n\(idm as NSData)"
readerSession.invalidate()
} catch {
readerSession.invalidate(errorMessage: error.localizedDescription)
}
},
didInvalidate: { error in
onDidInvalidate: { error in
print(error)
},
didDetect: { reader, tags in
let tag = tags.first!
let feliCaTag = try await reader.connectAsFeliCaTag(to: tag)
let (idm, systemCode) = try await feliCaTag.polling(systemCode: Data([0xFE, 0x00]), requestCode: .systemCode, timeSlot: .max1)
await reader.set(alertMessage: "\(systemCode as NSData)\n\(idm as NSData)")
return .success
}
)
.task(id: readerSession == nil) {
defer { readerSession = nil }
guard let readerSession else { return }
guard AsyncNFCTagReaderSession.readingAvailable else { return }

guard NFCTagReaderSession.readingAvailable else { return }
readerSession.alertMessage = "Place the tag on a flat, non-metal surface and rest your iPhone on the tag."
for await event in readerSession.eventStream {
switch event {
case .sessionIsReady:
readerSession.alertMessage = "Place the tag on a flat, non-metal surface and rest your iPhone on the tag."
readerSession.start()
case .sessionStarted:
break
case .sessionBecomeActive:
break
case .sessionDetected(let tags):
do {
let tag = tags.first!
guard case .feliCa(let feliCaTag) = tag else {
throw NFCReaderError(.readerErrorInvalidParameter)
}
try await readerSession.connect(to: tag)
let feliCaTag = try await readerSession.connectAsFeliCaTag(to: tag)
let (idm, systemCode) = try await feliCaTag.polling(systemCode: Data([0xFE, 0x00]), requestCode: .systemCode, timeSlot: .max1)
readerSession.alertMessage = "\(systemCode as NSData)\n\(idm as NSData)"
readerSession.stop()
readerSession.invalidate()
} catch {
readerSession.stop(errorMessage: error.localizedDescription)
readerSession.invalidate(errorMessage: error.localizedDescription)
}
case .sessionCreationFailed(let reason):
print(reason)
case .sessionInvalidated(let reason):
print(reason)
}
Expand All @@ -92,29 +77,3 @@ struct NFCFeliCaTagReaderExampleView: View {
.navigationTitle("FeliCa")
}
}

extension NFCFeliCaTagReaderExampleView {
final class ViewModel : ObservableObject {
private var reader: FeliCaTagReader!

func read() async throws {
reader = .init()
try await reader.read(
detectingAlertMessage: "Place the tag on a flat, non-metal surface and rest your iPhone on the tag.",
didBecomeActive: { reader in
await print(reader.alertMessage)
},
didInvalidate: { error in
print(error)
},
didDetect: { reader, tags in
let tag = tags.first!
let feliCaTag = try await reader.connectAsFeliCaTag(to: tag)
let (idm, systemCode) = try await feliCaTag.polling(systemCode: Data([0xFE, 0x00]), requestCode: .systemCode, timeSlot: .max1)
await reader.set(alertMessage: "\(systemCode as NSData)\n\(idm as NSData)")
return .success
}
)
}
}
}
81 changes: 22 additions & 59 deletions Examples/TRETNFCKitExample/NFCISO15693TagReaderExampleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
//

import SwiftUI
import TRETNFCKit_Async
import TRETNFCKit_Core
import TRETNFCKit_ISO15693

struct NFCISO15693TagReaderExampleView: View {
@State private var isPresented = false
@ObservedObject var viewModel = ViewModel()
@State private var readerSession: AsyncNFCTagReaderSession?
@State private var readerSession: NFCTagReaderSession?

var body: some View {
List {
Expand All @@ -21,51 +20,42 @@ struct NFCISO15693TagReaderExampleView: View {
} label: {
Text("Read (using view modifier)")
}
.disabled(!NFCTagReaderSession.readingAvailable || isPresented)
Button {
Task {
try await viewModel.read()
}
} label: {
Text("Read (using reader)")
}
Button {
readerSession = AsyncNFCTagReaderSession(pollingOption: .iso15693)
readerSession = NFCTagReaderSession(pollingOption: .iso15693)
} label: {
Text("Read (using async stream)")
}
.disabled(readerSession != nil)
.disabled(!NFCTagReaderSession.readingAvailable || readerSession != nil)
}
.iso15693TagReader(
isPresented: $isPresented,
detectingAlertMessage: "Place the tag on a flat, non-metal surface and rest your iPhone on the tag.",
onBeginReadingError: { error in
print(error)
terminatingAlertMessage: "CANCELLED",
onDidBecomeActive: { readerSession in
print(readerSession.alertMessage)
},
didBecomeActive: { reader in
await print(reader.alertMessage)
onDidDetect: { readerSession, tags in
do {
let tag = tags.first!
let iso15693Tag = try await readerSession.connectAsISO15693Tag(to: tag)
readerSession.alertMessage = "\(iso15693Tag.identifier as NSData)"
readerSession.invalidate()
} catch {
readerSession.invalidate(errorMessage: error.localizedDescription)
}
},
didInvalidate: { error in
onDidInvalidate: { error in
print(error)
},
didDetect: { reader, tags in
let tag = tags.first!
let iso15693Tag = try await reader.connectAsISO15693Tag(to: tag)
await reader.set(alertMessage: "\(iso15693Tag.identifier as NSData)")
return .success
}
)
.task(id: readerSession == nil) {
defer { readerSession = nil }
guard let readerSession else { return }
guard AsyncNFCTagReaderSession.readingAvailable else { return }

guard NFCTagReaderSession.readingAvailable else { return }
readerSession.alertMessage = "Place the tag on a flat, non-metal surface and rest your iPhone on the tag."
for await event in readerSession.eventStream {
switch event {
case .sessionIsReady:
readerSession.alertMessage = "Place the tag on a flat, non-metal surface and rest your iPhone on the tag."
readerSession.start()
case .sessionStarted:
break
case .sessionBecomeActive:
break
case .sessionDetected(let tags):
Expand All @@ -76,12 +66,10 @@ struct NFCISO15693TagReaderExampleView: View {
}
try await readerSession.connect(to: tag)
readerSession.alertMessage = "\(iso15693Tag.identifier as NSData)"
readerSession.stop()
readerSession.invalidate()
} catch {
readerSession.stop(errorMessage: error.localizedDescription)
readerSession.invalidate(errorMessage: error.localizedDescription)
}
case .sessionCreationFailed(let reason):
print(reason)
case .sessionInvalidated(let reason):
print(reason)
}
Expand All @@ -90,28 +78,3 @@ struct NFCISO15693TagReaderExampleView: View {
.navigationTitle("ISO 15693-compatible")
}
}

extension NFCISO15693TagReaderExampleView {
final class ViewModel : ObservableObject {
private var reader: ISO15693TagReader!

func read() async throws {
reader = .init()
try await reader.read(
detectingAlertMessage: "Place the tag on a flat, non-metal surface and rest your iPhone on the tag.",
didBecomeActive: { reader in
await print(reader.alertMessage)
},
didInvalidate: { error in
print(error)
},
didDetect: { reader, tags in
let tag = tags.first!
let iso15693Tag = try await reader.connectAsISO15693Tag(to: tag)
await reader.set(alertMessage: "\(iso15693Tag.identifier as NSData)")
return .success
}
)
}
}
}
Loading