-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPayViewModel.swift
60 lines (52 loc) · 1.67 KB
/
PayViewModel.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//
// PayViewModel.swift
// storeApp
//
// Created by Remi Robert on 16/04/16.
// Copyright © 2016 Remi Robert. All rights reserved.
//
import UIKit
import RxSwift
import NSObject_Rx
enum PaymentStatus {
case Received
case Canceled
case Successed
}
class PayViewModel: NSObject {
private var voiceSendRecognizer = VoiceSendRecognizer()
private var voiceListenRecognizer = VoiceListenRecognizer()
private var amount: Double!
private var completionHandler:(PaymentStatus -> Void)!
func sendBackResponse() {
self.voiceSendRecognizer.startPlay("\(Int(self.amount!) * 100)") {
self.receivePayment()
}
}
func receivePayment() {
self.voiceListenRecognizer.startRecord { (string: String?) in
if string == "WTP" {
self.completionHandler(.Received)
dispatch_async(dispatch_get_main_queue(), {
let _ = NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: #selector(PayViewModel.sendBackResponse), userInfo: nil, repeats: false)
})
}
else if string == "OKC" {
self.completionHandler(.Successed)
}
else if string == "BNE" {
self.completionHandler(.Canceled)
}
}
}
func makeTransaction(amount: Double) -> Observable<PaymentStatus> {
self.amount = amount
return Observable.create({ observer in
self.completionHandler = { success in
observer.onNext(success)
}
self.receivePayment()
return NopDisposable.instance
})
}
}