This repository has been archived by the owner on Sep 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConversationRecord.swift
136 lines (115 loc) · 4.55 KB
/
ConversationRecord.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
//
// ConversationRecord.swift
// uscfun
//
// Created by Wenzheng Li on 3/30/17.
// Copyright © 2017 Wenzheng Li. All rights reserved.
//
import Foundation
struct ConversationRecord {
var eventId: String
var latestMessage: String?
var isUnread: Bool
var lastUpdatedAt: Int64?
}
struct ConversationList {
static func parseConversationRecords() -> [String: ConversationRecord]? {
guard let plist = Plist(name: "ConversationRecord") else { return nil }
guard let conversationList = plist.getValuesInPlistFile() as? [String: [String: Any]] else { return nil }
var results = [String: ConversationRecord]()
for (conversationId, conversation) in conversationList {
let eventId = conversation[keyOfEventId] as! String
let latestMessage = conversation[keyOfLatestMessage] as! String?
let isUnread = conversation[keyOfIsUnread] as! Bool
let lastUpdatedAt = conversation[keyOfLastUpdatedAt] as! Int64?
results[conversationId] = ConversationRecord(eventId: eventId, latestMessage: latestMessage, isUnread: isUnread, lastUpdatedAt: lastUpdatedAt)
}
return results
}
static func addRecord(conversationId: String, record: ConversationRecord)throws {
guard let plist = Plist(name: "ConversationRecord") else {
print("cannot get plist file")
return
}
guard let records = plist.getMutablePlistFile() else {
print("cannot get mutable plist file")
return
}
var recordValue = [String: Any]()
recordValue[keyOfEventId] = record.eventId
recordValue[keyOfLatestMessage] = record.latestMessage
recordValue[keyOfIsUnread] = record.isUnread
recordValue[keyOfLastUpdatedAt] = record.lastUpdatedAt
records[conversationId] = recordValue
do {
try plist.addValuesToPlistFile(dictionary: records)
} catch let error {
throw error
}
}
static let keyOfConversationId = "conversationId"
static let keyOfEventId = "eventId"
static let keyOfLatestMessage = "latestMessage"
static let keyOfIsUnread = "isUnread"
static let keyOfLastUpdatedAt = "lastUpdatedAt"
}
struct Plist {
enum PlistError: Error {
case FileNotWritten
case FileDoesNotExist
}
let name:String
var sourcePath: String? {
guard let path = Bundle.main.path(forResource: name, ofType: "plist") else { return nil}
return path
}
var destPath:String? {
guard sourcePath != nil else { return nil }
let dir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
return (dir as NSString).appendingPathComponent("\(name).plist")
}
init?(name:String) {
self.name = name
let fileManager = FileManager.default
guard let source = sourcePath else { return nil }
guard let destination = destPath else { return nil }
guard fileManager.fileExists(atPath: source) else { return nil }
if !fileManager.fileExists(atPath: destination) {
do {
try fileManager.copyItem(atPath: source, toPath: destination)
} catch let error as NSError {
print("Unable to copy file. ERROR: \(error.localizedDescription)")
return nil
}
}
}
func getValuesInPlistFile() -> NSDictionary? {
let fileManager = FileManager.default
if fileManager.fileExists(atPath: destPath!) {
guard let dictionary = NSDictionary(contentsOfFile: destPath!) else { return nil }
return dictionary
} else {
return nil
}
}
func getMutablePlistFile() -> NSMutableDictionary? {
let fileManager = FileManager.default
if fileManager.fileExists(atPath: destPath!) {
guard let dictionary = NSMutableDictionary(contentsOfFile: destPath!) else { return nil }
return dictionary
} else {
return nil
}
}
func addValuesToPlistFile(dictionary: NSDictionary)throws {
let fileManager = FileManager.default
if fileManager.fileExists(atPath: destPath!) {
if !dictionary.write(toFile: destPath!, atomically: false) {
print("File not written successfully")
throw PlistError.FileNotWritten
}
} else {
throw PlistError.FileDoesNotExist
}
}
}