-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppServices.swift
234 lines (179 loc) · 5.58 KB
/
AppServices.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//
// AppService.swift
// SayanInAppPayment
//
// Created by Amir on 8/21/1397 AP.
// Copyright © 1397 Sayan. All rights reserved.
//
import Foundation
protocol Repository{
init()
associatedtype T
func FindByKe(key : AnyObject) -> T?
func First(key : AnyObject?) -> T?
func List(key : AnyObject?) -> [T]?
func Save (key: AnyObject, entity : AnyObject)
func Delete(key: AnyObject) -> Bool?
func GetAll() -> [T]?
func Store (key : AnyObject , entity : [T])
func Store (key : AnyObject , entity : T)
func Locate(key : AnyObject?) -> T?
func LocateList(key : AnyObject?) -> [T]?
}
class NSUserDefaultsRepositorySuper<Entity> : Repository {
func Store(key: AnyObject, entity : [Entity]) {
}
func Store(key: AnyObject, entity: Entity) {
}
func Locate(key: AnyObject?) -> Entity? {
return nil
}
func LocateList(key: AnyObject?) -> [Entity]? {
return nil
}
required init() {
}
typealias T = Entity
func FindByKe(key: AnyObject) -> Entity? {
return self.First(key: key)
}
func First(key: AnyObject?) -> Entity? {
let arrayOfEvents = UserDefaults.standard.object(forKey: String(describing: key!))
if(arrayOfEvents == nil)
{
return nil
}
if let ar = arrayOfEvents as? NSData
{
let ua = NSKeyedUnarchiver.unarchiveObject(with: ar as Data)
return (ua as? Entity)!
}
return (arrayOfEvents as? Entity)!
}
func List(key: AnyObject?) -> [Entity]? {
var r : [Entity] = Array()
let el = UserDefaults.standard.array(forKey: key as? String ?? "default")
if(el == nil)
{
return Array<Entity>()
}
for it in el! {
let ua = NSKeyedUnarchiver.unarchiveObject(with: (it as! NSData) as Data)
r.append(ua as! Entity)
}
return r;
}
func Save(key: AnyObject, entity: AnyObject) {
if let t = entity as? Array<Entity>
{
self.Save(key : key, entity: t)
return
}
let encoded = NSKeyedArchiver.archivedData( withRootObject: entity)
UserDefaults.standard.set(encoded, forKey: String(describing: key))
UserDefaults.standard.synchronize()
}
func Save(key: AnyObject, entity: [Entity]) {
var e : [AnyObject] = Array()
for el in entity {
let encoded = NSKeyedArchiver.archivedData( withRootObject: el)
e.append(encoded as AnyObject)
}
UserDefaults.standard.set(e, forKey: key as! String)
UserDefaults.standard.synchronize()
}
func Delete(key: AnyObject) -> Bool? {
UserDefaults.standard.removeObject(forKey: String(describing: key))
return true;
}
func GetAll() -> [T]? {
let res : [T] = Array()
return res
}
}
class StringRepository: NSUserDefaultsRepositorySuper<String> {
required init()
{
super.init()
}
}
protocol Service
{
associatedtype T
func FindByKey(key : AnyObject) -> T?
func First(key : AnyObject?) -> T?
func List(key : AnyObject?) -> [T]?
func Save (key: AnyObject, entity : AnyObject)
func Delete(key: AnyObject) -> Bool?
func Store (key : AnyObject , entity : [T])
func Store (key : AnyObject , entity : T)
func Locate(key : AnyObject?) -> T?
func LocateList(key : AnyObject?) -> [T]?
}
class SuperService<Entity,TRepository:Repository> : Service where TRepository.T == Entity
{
typealias T = Entity
var reposity : TRepository
init()
{
reposity = TRepository()
}
func FindByKey(key : AnyObject) -> Entity?
{
return self.reposity.FindByKe(key: key)
}
func First(key: AnyObject?) -> Entity? {
return self.reposity.First(key: key)
}
func List(key: AnyObject?) -> [Entity]? {
return self.reposity.List(key: key)
}
func Save(key: AnyObject, entity: AnyObject) {
self.reposity.Save(key: key, entity: entity)
}
func Delete(key: AnyObject) -> Bool? {
return self.reposity.Delete(key: key)
}
func GetAll() -> [Entity]?
{
return self.reposity.GetAll()
}
func Store(key: AnyObject, entity : [Entity]) {
self.reposity.Store(key: key, entity: entity)
}
func Store(key: AnyObject, entity: Entity) {
self.reposity.Store(key: key, entity: entity)
}
func Locate(key: AnyObject?) -> Entity? {
return self.reposity.Locate(key: key)
}
func LocateList(key: AnyObject?) -> [Entity]? {
return self.reposity.LocateList(key: key)
}
}
class StringService: SuperService<String,StringRepository> {
func setPhoneNo(value : String)
{
self.Save(key: "PhoneNo" as AnyObject, entity: value as AnyObject)
}
func getPhoneNo() -> String
{
return self.First(key: "PhoneNo" as AnyObject) ?? ""
}
func setWorkingKey(value : String)
{
self.Save(key: "WorkingKey" as AnyObject, entity: value as AnyObject)
}
func getWorkingKey() -> String
{
return self.First(key: "WorkingKey" as AnyObject) ?? ""
}
func getMasterKey() -> String
{
return "oMnPEUM+wJmzApS7Zh41MCbijU6JiGL5";
}
func getSdkInfo() -> String
{
return "a1cd3709-1afb-41e9-a953-fc606362767e";
}
}