-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathDefacs.swift
119 lines (110 loc) · 3.32 KB
/
Defacs.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
//
// Defacs.swift
// ios
//
// Copyright © 2019 Tinode. All rights reserved.
//
import Foundation
public class Defacs: Codable, Equatable {
public var auth: AcsHelper?
public var anon: AcsHelper?
init(auth: String?, anon: String?) {
if let auth = auth {
setAuth(a: auth)
}
if let anon = anon {
setAnon(a: anon)
}
}
init(from acs: Defacs) {
if let auth = acs.auth {
self.auth = AcsHelper(ah: auth)
}
if let anon = acs.anon {
self.anon = AcsHelper(ah: anon)
}
}
private enum CodingKeys: String, CodingKey {
case auth, anon
}
required public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
if let authStr = try? container.decode(String.self, forKey: .auth) {
setAuth(a: authStr)
}
if let anonStr = try? container.decode(String.self, forKey: .anon) {
setAnon(a: anonStr)
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
if let auth = auth {
try container.encode(auth.description, forKey: .auth)
}
if let anon = anon {
try container.encode(anon.description, forKey: .anon)
}
}
public func getAuth() -> String? {
return auth != nil ? auth!.description : nil
}
func setAuth(a: String) {
auth = AcsHelper(str: a)
}
public func getAnon() -> String? {
return anon != nil ? anon!.description : nil
}
func setAnon(a: String) {
anon = AcsHelper(str: a)
}
func merge(defacs: Defacs) -> Bool {
var changed = 0
if defacs.auth != nil {
if auth == nil {
auth = defacs.auth
changed += 1
} else {
changed += (auth!.merge(with: defacs.auth) ? 1 : 0)
}
}
if defacs.anon != nil {
if anon == nil {
anon = defacs.anon
changed += 1
} else {
changed += (anon!.merge(with: defacs.anon) ? 1 : 0)
}
}
return changed > 0
}
@discardableResult
func update(auth: String?, anon: String?) -> Bool {
var changed: Bool = false
if let auth = auth {
if self.auth == nil {
self.auth = AcsHelper(a: AcsHelper.kModeNone)
}
changed = self.auth!.update(from: auth)
}
if let anon = anon {
if self.anon == nil {
self.anon = AcsHelper(a: AcsHelper.kModeNone)
}
changed = changed || self.anon!.update(from: anon)
}
return changed
}
public static func == (lhs: Defacs, rhs: Defacs) -> Bool {
return lhs.anon == rhs.anon && lhs.auth == rhs.auth
}
public func serialize() -> String {
return [self.auth?.description ?? "",
self.anon?.description ?? ""].joined(separator: ",")
}
static public func deserialize(from data: String?) -> Defacs? {
guard let parts = data?.components(separatedBy: ","), parts.count == 2 else {
return nil
}
return Defacs(auth: parts[0], anon: parts[1])
}
}