Skip to content

Commit

Permalink
add content view model to top of app (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanj-tyro authored May 17, 2024
1 parent a722526 commit 286dad9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 33 deletions.
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)
}
}
}
}

0 comments on commit 286dad9

Please sign in to comment.