Skip to content

Commit

Permalink
开发者列表排序和显示优化
Browse files Browse the repository at this point in the history
  • Loading branch information
ming1016 committed Mar 23, 2024
1 parent 4c7ef70 commit cee1e37
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 53 deletions.
12 changes: 12 additions & 0 deletions SwiftPamphletApp/Core/FundationFunction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func wrapperHtmlContent(content: String, codeStyle: String = "lioshi.min") -> St
}

// MARK: - 时间
func howLongAgo(date: Date) -> String {
let simplifiedChinese = Locale(identifier: "zh_Hans")
return date.formatted(.relative(presentation: .named,
unitsStyle: .spellOut).locale(simplifiedChinese))
}
func howLongFromNow(timeStr: String) -> String {
let cn = Region(zone: Zones.asiaShanghai, locale: Locales.chineseChina)
SwiftDate.defaultRegion = cn
Expand Down Expand Up @@ -178,6 +183,13 @@ extension String {
return String(data: data, encoding: .utf8)
}
}
// 用于 SwiftData,让布尔值可排序
extension Bool: Comparable {
public static func <(lhs: Self, rhs: Self) -> Bool {
// the only true inequality is false < true
!lhs && rhs
}
}

// MARK: - 调试
func showSwiftDataStoreFileLocation() {
Expand Down
44 changes: 31 additions & 13 deletions SwiftPamphletApp/GitHubAPI/Developer/DeveloperListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,39 @@ struct DeveloperListView: View {
var body: some View {
List(selection: $selectDev) {
ForEach(devs) { dev in
Text(dev.name)
.badge(dev.unread)
.tag(dev)
.swipeActions {
Button(role: .destructive) {
DeveloperModel.delete(dev)
} label: {
Label("删除", systemImage: "trash")
HStack {
AsyncImageWithPlaceholder(size: .smallSize, url: dev.avatar)
VStack {
HStack {
Text(dev.name)
Spacer()
Text(howLongAgo(date: dev.updateDate))
.font(.footnote)
.foregroundColor(light: .secondary, dark: .secondary)
}
}
.contextMenu {
Button("删除") {
DeveloperModel.delete(dev)
HStack {
Text(dev.des)
.font(.footnote)
.foregroundColor(light: .secondary, dark: .secondary)
Spacer()
}
Spacer()
}
}
.tag(dev)
.swipeActions {
Button(role: .destructive) {
DeveloperModel.delete(dev)
} label: {
Label("删除", systemImage: "trash")
}
}
.contextMenu {
Button("删除") {
DeveloperModel.delete(dev)
}
}


}
}
Expand All @@ -44,7 +62,7 @@ struct DeveloperListView: View {
}

func addDev() {
let dev = DeveloperModel(name: "", unread: 0, lastEventIdStr: "", createDate: Date.now, updateDate: Date.now)
let dev = DeveloperModel(name: "", des: "", avatar: "", createDate: Date.now, updateDate: Date.now)
modelContext.insert(dev)
selectDev = dev
}
Expand Down
10 changes: 5 additions & 5 deletions SwiftPamphletApp/GitHubAPI/Developer/DeveloperModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import SwiftData
@Model
final class DeveloperModel {
var name: String = ""
var unread: Int = 0
var lastEventIdStr: String = ""
var des: String = ""
var avatar: String = ""

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

init(name: String, unread: Int, lastEventIdStr: String, createDate: Date, updateDate: Date) {
init(name: String, des: String, avatar: String, createDate: Date, updateDate: Date) {
self.name = name
self.unread = unread
self.lastEventIdStr = lastEventIdStr
self.des = des
self.avatar = avatar
self.createDate = createDate
self.updateDate = updateDate
}
Expand Down
24 changes: 19 additions & 5 deletions SwiftPamphletApp/GitHubAPI/Developer/EditDeveloper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ struct EditDeveloper: View {
@State private var tabSelct = 1

var body: some View {
TextField("请输入账号:", text: $dev.name)
.onSubmit {
vm = UserVM(userName: dev.name)
vm.doing(.updateAll)
Form {
HStack {
TextField("用户名:", text: $dev.name, prompt: Text("输入 Github 用户名"))
.onSubmit {
vm = UserVM(userName: dev.name)
vm.doing(.updateAll)
}
TextField("描述:", text: $dev.des)
}
.padding(EdgeInsets(top: 10, leading: 10, bottom: 0, trailing: 10))
}
.padding(EdgeInsets(top: 10, leading: 10, bottom: 0, trailing: 10))

HStack {
VStack(alignment: .leading, spacing: 10) {
Expand Down Expand Up @@ -66,6 +71,15 @@ struct EditDeveloper: View {
vm = UserVM(userName: newValue)
vm.doing(.updateAll)
})
.onChange(of: vm.events, { oldValue, newValue in
if ((newValue.first?.createdAt.isEmpty) != nil) {
let iso8601String = newValue.first?.createdAt ?? ""
let formatter = ISO8601DateFormatter()
dev.updateDate = formatter.date(from: iso8601String) ?? Date.now
dev.avatar = vm.user.avatarUrl
}

})
.frame(minWidth: SPC.detailMinWidth)

TabView(selection: $tabSelct) {
Expand Down
21 changes: 16 additions & 5 deletions SwiftPamphletApp/InfoOrganizer/Info/EditInfoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ struct EditInfoView: View {
Image(systemName: info.star ? "star.fill" : "star")
}
.toggleStyle(.button)
Button(action: {
info.updateDate = Date.now
}, label: {
Image(systemName: "arrow.up.square")
})
}
HStack {
TextField("地址:", text: $info.url)
Expand Down Expand Up @@ -119,11 +124,10 @@ struct EditInfoView: View {
}
}
.onChange(of: info.url) { oldValue, newValue in
if newValue.isEmpty {
selectedTab = 0
} else {
selectedTab = 3
}
tabSwitch()
}
.onAppear {
tabSwitch()
}
}
} // end form
Expand All @@ -140,6 +144,13 @@ struct EditInfoView: View {
Spacer()
} // end VStack
}
func tabSwitch() {
if info.url.isEmpty {
selectedTab = 1
} else {
selectedTab = 3
}
}
func addCate() {
cate = IOCategory(name: "", infos: [IOInfo](), pin: 0, createDate: Date.now, updateDate: Date.now)
modelContext.insert(cate!)
Expand Down
23 changes: 12 additions & 11 deletions SwiftPamphletApp/InfoOrganizer/Info/InfoListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
//
// Created by Ming Dai on 2024/3/11.
//

//import Foundation
import SwiftUI
import SwiftData

struct InfoListView: View {
@Environment(\.modelContext) var modelContext
@State private var searchText = ""
@Binding var selectInfo:IOInfo?
@State private var sortOrder = [SortDescriptor(\IOInfo.updateDate, order: .reverse)]
@State private var sortOrder = [SortDescriptor(\IOInfo.star, order: .reverse),SortDescriptor(\IOInfo.updateDate, order: .reverse)]

@Query(IOCategory.allOrderByName) var cates: [IOCategory]
@State private var filterCate = ""
Expand Down Expand Up @@ -45,10 +45,12 @@ struct InfoListView: View {
}
}
}
ToolbarItem(placement: .navigation) {
Toggle(isOn: $filterStar) {
if filterCate.isEmpty {
ToolbarItem(placement: .navigation) {
Toggle(isOn: $filterStar) {
}
.toggleStyle(SymbolToggleStyle(systemImage: "star.fill", activeColor: .yellow))
}
.toggleStyle(SymbolToggleStyle(systemImage: "star.fill", activeColor: .yellow))
}
ToolbarItem(placement: .navigation) {
Menu("Sort", systemImage: "arrow.up.arrow.down.square") {
Expand Down Expand Up @@ -83,12 +85,7 @@ struct InfoListView: View {
}

func addInfo() {
let info = IOInfo(name: "简单记录", url: "", des: "\n", star: false, createDate: Date.now, updateDate: Date.now)
modelContext.insert(info)
selectInfo = info
}
func addInfoWithCate() {
let info = IOInfo(name: "\(filterCate) - 简单记录", url: "", des: "\n", star: false, createDate: Date.now, updateDate: Date.now)
let info = IOInfo(name: "简单记录 - \(nowDateString())", url: "", des: "\n", star: false, createDate: Date.now, updateDate: Date.now)
for cate in cates {
if cate.name == filterCate {
info.category = cate
Expand All @@ -97,6 +94,10 @@ struct InfoListView: View {
modelContext.insert(info)
selectInfo = info
}
func nowDateString() -> String {
let locale = Locale(identifier: "zh_Hans")
return Date.now.formatted(.dateTime.locale(locale))
}

}

Expand Down
25 changes: 12 additions & 13 deletions SwiftPamphletApp/InfoOrganizer/Info/InfoRowView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,26 @@ struct InfoRowView: View {
var body: some View {
VStack(alignment:.leading) {
HStack {
if info.star == true {
Image(systemName: "star.fill")
}

if info.category != nil {
Text(info.category?.name ?? "")
}
if info.des == "\n" || info.des.isEmpty {

} else {
Image(systemName: "pencil.and.list.clipboard")
}
if info.star == true {
Image(systemName: "star.fill")
}
Spacer()

}
.font(.footnote)
.foregroundColor(light: .secondary, dark: .secondary)

Text(info.name)
HStack {
Text(dateString(date: info.updateDate))
Text(howLongAgo(date: info.updateDate))
.font(.footnote)
Spacer()
}
Expand All @@ -52,14 +59,6 @@ struct InfoRowView: View {
}
}

func dateString(date: Date) -> String {
var re = ""
if date.year != Date.now.year {
re += date.year.description + "."
}
re += date.month.description + "." + date.day.description
return re
}
}


2 changes: 1 addition & 1 deletion SwiftPamphletApp/InfoOrganizer/Info/InfosView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct InfosView: View {
|| info.url.localizedStandardContains(searchString)
|| info.des.localizedStandardContains(searchString)) && info.category?.name == filterCateName && info.star == filterStar
} else if !filterCateName.isEmpty {
info.category?.name == filterCateName && info.star == filterStar
info.category?.name == filterCateName
} else if searchString.isEmpty {
info.star == filterStar
} else {
Expand Down

0 comments on commit cee1e37

Please sign in to comment.