Skip to content

Commit 6df6d16

Browse files
committed
优化数据结构,减少内存
1 parent 8a7563f commit 6df6d16

File tree

4 files changed

+4
-86
lines changed

4 files changed

+4
-86
lines changed

SwiftPamphletApp/InfoOrganizer/Category/CategoryListView.swift

+2-76
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ struct CategoryListView: View {
1313
@Environment(\.modelContext) var modelContext
1414
@Query(IOCategory.allOrderByName) var cates: [IOCategory]
1515
@State var selectCate: IOCategory?
16-
@State var catesByCount: [IOCategory] = [IOCategory]()
17-
18-
@State var maxCount: Int = 0
19-
@State var averageCount: Int = 0
2016

2117
let linearGradient = LinearGradient(gradient: Gradient(colors: [Color.pink.opacity(0.8), Color.pink.opacity(0.01)]),
2218
startPoint: .top,
@@ -32,79 +28,9 @@ struct CategoryListView: View {
3228
}
3329
}
3430
}
35-
GroupBox("分类排序") {
36-
Chart(catesByCount) { cate in
37-
BarMark(
38-
x: .value("名字", cate.name),
39-
y: .value("数量", cate.infos?.count ?? 0)
40-
)
41-
.foregroundStyle(linearGradient)
42-
// .interpolationMethod(.catmullRom) // 设置曲线啥的
43-
.annotation(position: .overlay, alignment: .top, spacing: 3) {
44-
VStack {
45-
Text("\(cate.infos?.count ?? 0)")
46-
.font(.footnote)
47-
.foregroundColor(light: .white, dark: .white)
48-
.padding(EdgeInsets(top: 0, leading: 0, bottom: 10, trailing: 0))
49-
Text(cate.name)
50-
.font(.footnote)
51-
.foregroundColor(light: .white, dark: .white)
52-
.rotationEffect(.degrees(45))
53-
// .fixedSize(horizontal: false, vertical: true)
54-
55-
}
56-
}
57-
RuleMark(y: .value("均线", averageCount))
58-
.foregroundStyle(Color.secondary)
59-
.lineStyle(StrokeStyle(lineWidth: 0.8, dash: [10]))
60-
.annotation(alignment: .topLeading) {
61-
Text("均线:\(averageCount)")
62-
.font(.footnote).bold()
63-
.padding(.trailing, 32)
64-
.foregroundStyle(Color.secondary)
65-
}
66-
// SectorMark(angle: .value("数量", cate.infos?.count ?? 0), innerRadius: .ratio(0.5),
67-
// angularInset: 1.5)
68-
// .foregroundStyle(by: .value("名字", cate.name))
69-
// .annotation(position: .overlay, alignment: .trailing, spacing: 3) {
70-
// Text(cate.name)
71-
// .font(.footnote)
72-
// .foregroundColor(light: .white, dark: .white)
73-
// }
74-
}
75-
// .chartYScale(domain: 0...maxCount)
76-
.aspectRatio(1, contentMode: .fit)
77-
// .chartLegend(.hidden)
78-
// .chartXAxis(.hidden)
79-
.chartScrollableAxes(.horizontal)
80-
// .chartScrollPosition(initialY: 8)
81-
// .chartXVisibleDomain(length: 5) // 调整可见数据点数
82-
// .chartYAxis(.hidden)
83-
84-
}
85-
.onAppear(perform: {
86-
sortCates()
87-
})
88-
.onChange(of: cates) { oldValue, newValue in
89-
sortCates()
90-
}
31+
9132
}
9233

93-
func sortCates() {
94-
catesByCount = cates.sorted(by: { l, r in
95-
l.infos?.count ?? 0 > r.infos?.count ?? 0
96-
})
97-
98-
// 找出最大
99-
var cateCounts = [Int]()
100-
for cate in cates {
101-
if let cateCount = cate.infos?.count {
102-
cateCounts.append(cateCount)
103-
}
104-
}
105-
maxCount = cateCounts.max() ?? 0
106-
let sum = cateCounts.reduce(0, +)
107-
averageCount = Int(Double(sum) / Double(cateCounts.count))
108-
}
34+
10935
}
11036

SwiftPamphletApp/InfoOrganizer/Category/CategoryRowView.swift

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ struct CategoryRowView: View {
2222
}
2323
TextField("name", text: $cate.name)
2424
Spacer()
25-
Text("\(cate.infos?.count ?? 0)")
2625
}
2726
.swipeActions {
2827
Button {

SwiftPamphletApp/InfoOrganizer/Info/EditInfoView.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ struct EditInfoView: View {
147147
} // end form
148148
.padding(10)
149149
.inspector(isPresented: $isShowInspector) {
150-
EditCategoryView(cate: cate ?? IOCategory(name: "unavailable.com", infos: [IOInfo](), pin: 0, createDate: Date.now, updateDate: Date.now))
150+
EditCategoryView(cate: cate ?? IOCategory(name: "unavailable.com", pin: 0, createDate: Date.now, updateDate: Date.now))
151151
}
152152
.toolbar {
153153
Button("关闭", systemImage: "sidebar.right") {
@@ -166,7 +166,7 @@ struct EditInfoView: View {
166166
}
167167
}
168168
func addCate() {
169-
cate = IOCategory(name: "", infos: [IOInfo](), pin: 0, createDate: Date.now, updateDate: Date.now)
169+
cate = IOCategory(name: "", pin: 0, createDate: Date.now, updateDate: Date.now)
170170
modelContext.insert(cate!)
171171
isShowInspector = true
172172
}

SwiftPamphletApp/InfoOrganizer/Model/InfoDataModel.swift

-7
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,17 @@ final class IOInfo {
4949
@Model
5050
class IOCategory {
5151
var name: String = ""
52-
var infos: [IOInfo]? = [IOInfo]() // 关系字段,链接 IOInfo
5352
var pin: Int = 0
5453

5554
var createDate: Date = Date.now
5655
var updateDate: Date = Date.now
5756

5857
init(name: String,
59-
infos: [IOInfo]?,
6058
pin: Int,
6159
createDate: Date,
6260
updateDate: Date
6361
) {
6462
self.name = name
65-
self.infos = [IOInfo]()
6663
self.pin = pin
6764
self.createDate = createDate
6865
self.updateDate = updateDate
@@ -76,10 +73,6 @@ class IOCategory {
7673
let fd = FetchDescriptor(sortBy: [SortDescriptor(\IOCategory.pin, order: .reverse), SortDescriptor(\IOCategory.name, order: .forward)])
7774
return fd
7875
}
79-
static var allOrderByCount: FetchDescriptor<IOCategory> {
80-
let fd = FetchDescriptor(sortBy: [SortDescriptor(\IOCategory.infos?.capacity, order: .reverse)])
81-
return fd
82-
}
8376

8477
static func pin(_ cate: IOCategory) {
8578
if cate.modelContext != nil {

0 commit comments

Comments
 (0)