-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetValues.swift
153 lines (125 loc) · 4.84 KB
/
GetValues.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// GetValues.swift
// BitcoinWidget
//
// Created by Anıl T. on 28.11.2017.
// Copyright © 2017 Anıl T. All rights reserved.
//
import Foundation
import NotificationCenter
import UserNotifications
import AudioToolbox
import UIKit
struct Ticker:Codable {
var buy: Double
var sell: Double
var high: Double
var low: Double
init() {
buy = 0.0
sell = 0.0
high = 0.0
low = 0.0
}
init( mbuy: Double, msell: Double, mhigh: Double, mlow: Double) {
buy = mbuy
sell = mbuy
high = mhigh
low = mlow
}
}
typealias CompletionHandler = () -> Void
class GetValues: NSObject {
var values = Ticker(mbuy: 0.0, msell: 0.0, mhigh: 0.0, mlow: 0.0)
var lastDate: String!
var totalBitcoinValue: Double!
static let sharedInstance = GetValues()
override init() {
super.init()
}
func getValues() -> Ticker {
if let existingValues = UserDefaults.standard.value(forKey: "Ticker") {
return existingValues as! Ticker
} else {
return values
}
}
func getTotalBitcoin() -> Double {
let userDefaults = UserDefaults(suiteName: "group.com.bitcoinwidget")
if let existingTotalBitcoin = userDefaults?.object(forKey: "totalBitcoinValue") as? Double {
print("Existing Bitcoin: \(existingTotalBitcoin)")
return existingTotalBitcoin
} else {
return 0.08055213
}
}
func setTotalBitcoin(bitcoin: Double){
totalBitcoinValue = bitcoin
let x = UserDefaults(suiteName: "group.com.bitcoinwidget")
x?.set(bitcoin, forKey: "totalBitcoinValue")
x?.synchronize()
}
func getLastDate()->String{
if let existingLastDate = UserDefaults.standard.value(forKey: "lastDate") {
return existingLastDate as! String
} else {
return "0"
}
}
func setLastDate(date: String){
lastDate = date
UserDefaults.standard.set(lastDate, forKey: "lastDate")
}
func downloadBitcoinValeus(completion: @escaping CompletionHandler){
let url = URL(string: "https://koinim.com/ticker")!
//create the session object
let session = URLSession.shared
//now create the URLRequest object using the url object
let request = URLRequest(url: url)
//create dataTask using the session object to send data to the server
let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
guard error == nil else {
return
}
guard let data = data else {
return
}
do {
//create json object from data
if var json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
self.values = Ticker(mbuy:json["buy"] as! Double, msell: json["sell"] as! Double, mhigh: json["high"] as! Double, mlow: json["low"] as! Double)
UserDefaults.standard.set(json, forKey: "jsonValues")
self.createNotification()
completion()
}
} catch let error {
print(error.localizedDescription)
}
})
task.resume()
}
func createNotification(){
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyyMMddHHmmss"
dateFormatter.locale = Locale(identifier: "tr_TR")
var dateString = ""
dateString = dateFormatter.string(from:Date())
print(dateString)
if let x = Int64(dateString) {
if let y = Int64(getLastDate()){
if (x-y>5){
setLastDate(date: dateString)
let str = String(format: "%.2f",self.values.sell * getTotalBitcoin())
let notification = UNMutableNotificationContent()
notification.title = "Bitcoin"
notification.body = "Buy: \(self.values.buy) Sell: \(self.values.sell) Total: \(str)"
notification.badge = 1
notification.sound = UNNotificationSound.default()
let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let request = UNNotificationRequest(identifier: "notification1", content: notification, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
}
}
}
}