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

add content view model to top of app #28

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 5 additions & 2 deletions SampleApp/SampleApp/SampleApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ import TyroTapToPaySDK
struct SampleApp: App {
@Environment(\.scenePhase) private var scenePhase: ScenePhase
@ObservedObject var tapToPaySdk: TyroTapToPay
private var contentViewModel: ContentViewModel

init() {
do {
tapToPaySdk = try TyroTapToPay(
let tapToPaySdk = try TyroTapToPay(
environment: .sandbox,
connectionProvider: DemoConnectionProvider()
)
contentViewModel = ContentViewModel(tapToPaySdk: tapToPaySdk)
self.tapToPaySdk = tapToPaySdk
} catch {
fatalError(error.localizedDescription)
}
Expand All @@ -31,7 +34,7 @@ struct SampleApp: App {
.aspectRatio(contentMode: .fit)
.frame(maxWidth: 100)
.padding()
ContentView(viewModel: ContentViewModel(tapToPaySdk: self.tapToPaySdk))
ContentView(viewModel: contentViewModel)
}
}
}
Expand Down
34 changes: 16 additions & 18 deletions SampleApp/SampleApp/ViewModels/ContentViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,36 @@ class ContentViewModel: ObservableObject {

init(tapToPaySdk: TyroTapToPay) {
self.tapToPaySdk = tapToPaySdk
}

func showError(errorMessage: String) {
state = .error(errorMessage)
Task(priority: .userInitiated) { [weak self] in
await self?.connect()
}
}

func connect() async throws {
func connect() async {
do {
DispatchQueue.main.async {
await MainActor.run {
self.state = .loading("Connecting to reader...")
}
try await self.tapToPaySdk.connect()
DispatchQueue.main.async {
await MainActor.run {
self.state = .ready
self.isConnected = true
}
} catch {
DispatchQueue.main.async {
await MainActor.run {
self.state = .error(error.localizedDescription)
}
}
}

func reset() {
DispatchQueue.main.async {
self.state = .ready
self.transactionOutcome = nil
}
state = .ready
transactionOutcome = nil
}

func startPayment(_ transactionType: TransactionType, _ amount: Decimal) async throws {
DispatchQueue.main.async {
await MainActor.run {
self.state = .loading("Processing \(transactionType.rawValue.lowercased())...")
}
let transactionDetail = TransactionDetail(
Expand All @@ -67,28 +65,28 @@ class ContentViewModel: ObservableObject {
transactionType == .payment
? try await self.tapToPaySdk.startPayment(transactionDetail: transactionDetail)
: try await self.tapToPaySdk.refundPayment(transactionDetail: transactionDetail)
DispatchQueue.main.async {
await MainActor.run {
self.state = .success(outcome)
self.transactionOutcome = outcome
}
} catch TapToPaySDKError.failedToVerifyConnection {
DispatchQueue.main.async {
await MainActor.run {
self.state = .error("failedToVerifyConnection")
}
} catch TapToPaySDKError.transactionError(let errorMessage) {
DispatchQueue.main.async {
await MainActor.run {
self.state = .error("transactionError: \(errorMessage)")
}
} catch TapToPaySDKError.unableToConnectReader {
DispatchQueue.main.async {
await MainActor.run {
self.state = .error("unableToConnectReader")
}
} catch TapToPaySDKError.invalidParameter(let errorMessage) {
DispatchQueue.main.async {
await MainActor.run {
self.state = .error("invalidParameter: \(errorMessage)")
}
} catch {
DispatchQueue.main.async {
await MainActor.run {
self.state = .error(error.localizedDescription)
}
}
Expand Down
14 changes: 1 addition & 13 deletions SampleApp/SampleApp/Views/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ enum LoadingState {
struct ContentView: View {
@Environment(\.scenePhase) var scenePhase
@StateObject private var viewModel: ContentViewModel
@State private var firstLoad = true


init(viewModel: ContentViewModel) {
_viewModel = StateObject(wrappedValue: viewModel)
}
Expand Down Expand Up @@ -72,16 +71,5 @@ struct ContentView: View {
await self.viewModel.tapToPaySdk.didChange(scenePhase: newValue)
}
}
.task {
guard firstLoad else {
return
}
firstLoad = false
do {
try await viewModel.connect()
} catch {
viewModel.showError(errorMessage: error.localizedDescription)
}
}
}
}