-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewModel.swift
150 lines (125 loc) · 3.66 KB
/
ViewModel.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
//
// File.swift
//
//
// Created by 戴藏龙 on 2023/4/13.
//
import Foundation
import SwiftUI
var accountDataFileURL: URL {
let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
.appendingPathComponent("account", conformingTo: .json)
if !FileManager.default.fileExists(atPath: url.absoluteString) {
try! FileManager.default.createFile(atPath: url.absoluteString, contents: JSONEncoder().encode([Account]()))
}
return url
}
var recordDataFileURL: URL {
let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
.appendingPathComponent("record", conformingTo: .json)
if !FileManager.default.fileExists(atPath: url.absoluteString) {
try! FileManager.default.createFile(atPath: url.absoluteString, contents: JSONEncoder().encode([Record]()))
}
return url
}
private var debugDataHasApplied: Bool {
get {
UserDefaults.standard.bool(forKey: UserDefaultKeys.debugDataHasApplied)
}
set {
UserDefaults.standard.set(newValue, forKey: UserDefaultKeys.debugDataHasApplied)
}
}
// MARK: - ViewModel
class ViewModel: ObservableObject {
// MARK: Lifecycle
private init() {
load()
if accounts.isEmpty, records.isEmpty {
if !debugDataHasApplied {
replacingUsingDemoData()
debugDataHasApplied = true
}
}
}
// MARK: Internal
static let shared: ViewModel = .init()
let queue: DispatchQueue = .init(label: "SaveDatas", qos: .background)
@Published
var accounts: [Account] = [] {
didSet {
saveAccounts()
}
}
var records: [Record] {
_records.sorted(by: { $0.date < $1.date })
}
func add(_ newAccount: Account) {
accounts.append(newAccount)
}
func add(_ newRecord: Record) {
_records.append(newRecord)
}
func delete(_ record: Record) {
let index = _records.firstIndex(of: record)!
_records.remove(at: index)
}
func delete(_ account: Account) {
let index = accounts.firstIndex(of: account)!
accounts.remove(at: index)
records.filter { record in
(record.creditAccountId == account.id) || (record.debitAccountId == account.id)
}.forEach { record in
delete(record)
}
}
func replacingUsingDemoData() {
_records = DemoData.records
accounts = DemoData.accounts
}
func deleteAll() {
_records = []
accounts = []
}
// MARK: Private
private let encoder = JSONEncoder()
private let decoder = JSONDecoder()
@Published
private var _records: [Record] = [] {
didSet {
saveRecords()
}
}
private func save() {
saveAccounts()
saveRecords()
}
private func saveAccounts() {
queue.async {
do {
let data = try self.encoder.encode(self.accounts)
try data.write(to: accountDataFileURL)
} catch {
print(error)
}
}
}
private func saveRecords() {
queue.async {
do {
let data = try self.encoder.encode(self.records)
try data.write(to: recordDataFileURL)
} catch {
print(error)
}
}
}
private func load() {
do {
accounts = try decoder.decode([Account].self, from: Data(contentsOf: accountDataFileURL))
_records = try decoder.decode([Record].self, from: Data(contentsOf: recordDataFileURL))
} catch {
print(error)
}
}
}