Skip to content

Commit

Permalink
优化数据结构,减少内存
Browse files Browse the repository at this point in the history
  • Loading branch information
ming1016 committed Mar 31, 2024
1 parent 8a7563f commit 6df6d16
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 86 deletions.
78 changes: 2 additions & 76 deletions SwiftPamphletApp/InfoOrganizer/Category/CategoryListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ struct CategoryListView: View {
@Environment(\.modelContext) var modelContext
@Query(IOCategory.allOrderByName) var cates: [IOCategory]
@State var selectCate: IOCategory?
@State var catesByCount: [IOCategory] = [IOCategory]()

@State var maxCount: Int = 0
@State var averageCount: Int = 0

let linearGradient = LinearGradient(gradient: Gradient(colors: [Color.pink.opacity(0.8), Color.pink.opacity(0.01)]),
startPoint: .top,
Expand All @@ -32,79 +28,9 @@ struct CategoryListView: View {
}
}
}
GroupBox("分类排序") {
Chart(catesByCount) { cate in
BarMark(
x: .value("名字", cate.name),
y: .value("数量", cate.infos?.count ?? 0)
)
.foregroundStyle(linearGradient)
// .interpolationMethod(.catmullRom) // 设置曲线啥的
.annotation(position: .overlay, alignment: .top, spacing: 3) {
VStack {
Text("\(cate.infos?.count ?? 0)")
.font(.footnote)
.foregroundColor(light: .white, dark: .white)
.padding(EdgeInsets(top: 0, leading: 0, bottom: 10, trailing: 0))
Text(cate.name)
.font(.footnote)
.foregroundColor(light: .white, dark: .white)
.rotationEffect(.degrees(45))
// .fixedSize(horizontal: false, vertical: true)

}
}
RuleMark(y: .value("均线", averageCount))
.foregroundStyle(Color.secondary)
.lineStyle(StrokeStyle(lineWidth: 0.8, dash: [10]))
.annotation(alignment: .topLeading) {
Text("均线:\(averageCount)")
.font(.footnote).bold()
.padding(.trailing, 32)
.foregroundStyle(Color.secondary)
}
// SectorMark(angle: .value("数量", cate.infos?.count ?? 0), innerRadius: .ratio(0.5),
// angularInset: 1.5)
// .foregroundStyle(by: .value("名字", cate.name))
// .annotation(position: .overlay, alignment: .trailing, spacing: 3) {
// Text(cate.name)
// .font(.footnote)
// .foregroundColor(light: .white, dark: .white)
// }
}
// .chartYScale(domain: 0...maxCount)
.aspectRatio(1, contentMode: .fit)
// .chartLegend(.hidden)
// .chartXAxis(.hidden)
.chartScrollableAxes(.horizontal)
// .chartScrollPosition(initialY: 8)
// .chartXVisibleDomain(length: 5) // 调整可见数据点数
// .chartYAxis(.hidden)

}
.onAppear(perform: {
sortCates()
})
.onChange(of: cates) { oldValue, newValue in
sortCates()
}

}

func sortCates() {
catesByCount = cates.sorted(by: { l, r in
l.infos?.count ?? 0 > r.infos?.count ?? 0
})

// 找出最大
var cateCounts = [Int]()
for cate in cates {
if let cateCount = cate.infos?.count {
cateCounts.append(cateCount)
}
}
maxCount = cateCounts.max() ?? 0
let sum = cateCounts.reduce(0, +)
averageCount = Int(Double(sum) / Double(cateCounts.count))
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ struct CategoryRowView: View {
}
TextField("name", text: $cate.name)
Spacer()
Text("\(cate.infos?.count ?? 0)")
}
.swipeActions {
Button {
Expand Down
4 changes: 2 additions & 2 deletions SwiftPamphletApp/InfoOrganizer/Info/EditInfoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ struct EditInfoView: View {
} // end form
.padding(10)
.inspector(isPresented: $isShowInspector) {
EditCategoryView(cate: cate ?? IOCategory(name: "unavailable.com", infos: [IOInfo](), pin: 0, createDate: Date.now, updateDate: Date.now))
EditCategoryView(cate: cate ?? IOCategory(name: "unavailable.com", pin: 0, createDate: Date.now, updateDate: Date.now))
}
.toolbar {
Button("关闭", systemImage: "sidebar.right") {
Expand All @@ -166,7 +166,7 @@ struct EditInfoView: View {
}
}
func addCate() {
cate = IOCategory(name: "", infos: [IOInfo](), pin: 0, createDate: Date.now, updateDate: Date.now)
cate = IOCategory(name: "", pin: 0, createDate: Date.now, updateDate: Date.now)
modelContext.insert(cate!)
isShowInspector = true
}
Expand Down
7 changes: 0 additions & 7 deletions SwiftPamphletApp/InfoOrganizer/Model/InfoDataModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,17 @@ final class IOInfo {
@Model
class IOCategory {
var name: String = ""
var infos: [IOInfo]? = [IOInfo]() // 关系字段,链接 IOInfo
var pin: Int = 0

var createDate: Date = Date.now
var updateDate: Date = Date.now

init(name: String,
infos: [IOInfo]?,
pin: Int,
createDate: Date,
updateDate: Date
) {
self.name = name
self.infos = [IOInfo]()
self.pin = pin
self.createDate = createDate
self.updateDate = updateDate
Expand All @@ -76,10 +73,6 @@ class IOCategory {
let fd = FetchDescriptor(sortBy: [SortDescriptor(\IOCategory.pin, order: .reverse), SortDescriptor(\IOCategory.name, order: .forward)])
return fd
}
static var allOrderByCount: FetchDescriptor<IOCategory> {
let fd = FetchDescriptor(sortBy: [SortDescriptor(\IOCategory.infos?.capacity, order: .reverse)])
return fd
}

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

0 comments on commit 6df6d16

Please sign in to comment.