Skip to content

Commit

Permalink
NavigationSplit 新三栏结构
Browse files Browse the repository at this point in the history
  • Loading branch information
ming1016 committed Mar 12, 2024
1 parent 93e853d commit 50cf898
Show file tree
Hide file tree
Showing 14 changed files with 225 additions and 87 deletions.
20 changes: 16 additions & 4 deletions SwiftPamphletApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
083554E327572BB60095E0EE /* AppVM.swift in Sources */ = {isa = PBXBuildFile; fileRef = 083554E227572BB60095E0EE /* AppVM.swift */; };
08397E232B9EE8F400DFDD02 /* InfoDataModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08397E222B9EE8F400DFDD02 /* InfoDataModel.swift */; };
08397E252B9EEE1300DFDD02 /* InfoListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08397E242B9EEE1300DFDD02 /* InfoListView.swift */; };
08397E272B9EF37600DFDD02 /* InfosView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08397E262B9EF37600DFDD02 /* InfosView.swift */; };
08397E292B9F0A9100DFDD02 /* EditInfoView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08397E282B9F0A9100DFDD02 /* EditInfoView.swift */; };
08397E2D2B9F10AD00DFDD02 /* EditCategoryView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08397E2C2B9F10AD00DFDD02 /* EditCategoryView.swift */; };
08397E2F2B9F353B00DFDD02 /* CategoryListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08397E2E2B9F353B00DFDD02 /* CategoryListView.swift */; };
08397E322B9F39C100DFDD02 /* SwiftSoup in Frameworks */ = {isa = PBXBuildFile; productRef = 08397E312B9F39C100DFDD02 /* SwiftSoup */; };
08397E342BA0178500DFDD02 /* DetailLink.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08397E332BA0178500DFDD02 /* DetailLink.swift */; };
084417752B99B9060049297D /* HomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 084417742B99B9060049297D /* HomeView.swift */; };
084417772B99BA3F0049297D /* SidebarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 084417762B99BA3F0049297D /* SidebarView.swift */; };
084417792B99BE720049297D /* DataLink.swift in Sources */ = {isa = PBXBuildFile; fileRef = 084417782B99BE720049297D /* DataLink.swift */; };
Expand Down Expand Up @@ -191,6 +191,9 @@
086A5F642754C14F00FECE02 /* PlayTextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 086A5F632754C14F00FECE02 /* PlayTextView.swift */; };
086A5F672754C27900FECE02 /* DBHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 086A5F662754C27900FECE02 /* DBHandler.swift */; };
086A5F6A2755F3BE00FECE02 /* repos.json in Resources */ = {isa = PBXBuildFile; fileRef = 086A5F692755F3BE00FECE02 /* repos.json */; };
0871C6192BA040E5000B620D /* InfoRowView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0871C6182BA040E5000B620D /* InfoRowView.swift */; };
0871C61B2BA04D23000B620D /* CategoryRowView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0871C61A2BA04D23000B620D /* CategoryRowView.swift */; };
0871C61D2BA05F44000B620D /* InfosView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0871C61C2BA05F44000B620D /* InfosView.swift */; };
088EE8F527BD24E000764525 /* PlayLinkView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 088EE8F427BD24E000764525 /* PlayLinkView.swift */; };
0896FB9027BA39B100676B7F /* PlayButtonView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0896FB8F27BA39B100676B7F /* PlayButtonView.swift */; };
0896FB9227BA486900676B7F /* 145.md in Resources */ = {isa = PBXBuildFile; fileRef = 0896FB9127BA486900676B7F /* 145.md */; };
Expand Down Expand Up @@ -289,10 +292,10 @@
083554E227572BB60095E0EE /* AppVM.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppVM.swift; sourceTree = "<group>"; };
08397E222B9EE8F400DFDD02 /* InfoDataModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoDataModel.swift; sourceTree = "<group>"; };
08397E242B9EEE1300DFDD02 /* InfoListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoListView.swift; sourceTree = "<group>"; };
08397E262B9EF37600DFDD02 /* InfosView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfosView.swift; sourceTree = "<group>"; };
08397E282B9F0A9100DFDD02 /* EditInfoView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditInfoView.swift; sourceTree = "<group>"; };
08397E2C2B9F10AD00DFDD02 /* EditCategoryView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditCategoryView.swift; sourceTree = "<group>"; };
08397E2E2B9F353B00DFDD02 /* CategoryListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CategoryListView.swift; sourceTree = "<group>"; };
08397E332BA0178500DFDD02 /* DetailLink.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailLink.swift; sourceTree = "<group>"; };
084417742B99B9060049297D /* HomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeView.swift; sourceTree = "<group>"; };
084417762B99BA3F0049297D /* SidebarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SidebarView.swift; sourceTree = "<group>"; };
084417782B99BE720049297D /* DataLink.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataLink.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -450,6 +453,9 @@
086A5F632754C14F00FECE02 /* PlayTextView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayTextView.swift; sourceTree = "<group>"; };
086A5F662754C27900FECE02 /* DBHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DBHandler.swift; sourceTree = "<group>"; };
086A5F692755F3BE00FECE02 /* repos.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = repos.json; path = SwiftPamphletApp/Resource/News/repos.json; sourceTree = SOURCE_ROOT; };
0871C6182BA040E5000B620D /* InfoRowView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoRowView.swift; sourceTree = "<group>"; };
0871C61A2BA04D23000B620D /* CategoryRowView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CategoryRowView.swift; sourceTree = "<group>"; };
0871C61C2BA05F44000B620D /* InfosView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfosView.swift; sourceTree = "<group>"; };
0886702B27F804010001EA81 /* SwiftPamphletApp-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "SwiftPamphletApp-Bridging-Header.h"; sourceTree = "<group>"; };
088EE8F427BD24E000764525 /* PlayLinkView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayLinkView.swift; sourceTree = "<group>"; };
0896FB8F27BA39B100676B7F /* PlayButtonView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayButtonView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -571,8 +577,10 @@
children = (
08397E222B9EE8F400DFDD02 /* InfoDataModel.swift */,
08397E242B9EEE1300DFDD02 /* InfoListView.swift */,
0871C61C2BA05F44000B620D /* InfosView.swift */,
0871C6182BA040E5000B620D /* InfoRowView.swift */,
08397E2E2B9F353B00DFDD02 /* CategoryListView.swift */,
08397E262B9EF37600DFDD02 /* InfosView.swift */,
0871C61A2BA04D23000B620D /* CategoryRowView.swift */,
08397E282B9F0A9100DFDD02 /* EditInfoView.swift */,
08397E2C2B9F10AD00DFDD02 /* EditCategoryView.swift */,
);
Expand All @@ -594,6 +602,7 @@
084417742B99B9060049297D /* HomeView.swift */,
084417762B99BA3F0049297D /* SidebarView.swift */,
084417782B99BE720049297D /* DataLink.swift */,
08397E332BA0178500DFDD02 /* DetailLink.swift */,
);
path = HomeUI;
sourceTree = "<group>";
Expand Down Expand Up @@ -1593,6 +1602,7 @@
buildActionMask = 2147483647;
files = (
08BE633727BF6CBC002BC6A8 /* PlayTextFieldView.swift in Sources */,
0871C61B2BA04D23000B620D /* CategoryRowView.swift in Sources */,
08522BF227D1D49B005FF059 /* PlayCanvas.swift in Sources */,
08BE634827C4B6A2002BC6A8 /* PlayStackView.swift in Sources */,
08BE636227C878EC002BC6A8 /* PlayLazyVStackAndLazyHStackView.swift in Sources */,
Expand All @@ -1606,6 +1616,7 @@
084417792B99BE720049297D /* DataLink.swift in Sources */,
08522BEB27CF7744005FF059 /* PlayKeyboard.swift in Sources */,
08C3BBA427CF1B7B00ACF0FE /* PlayPickerView.swift in Sources */,
0871C61D2BA05F44000B620D /* InfosView.swift in Sources */,
08448F4B279E872B00B61353 /* GuideView.swift in Sources */,
08397E2D2B9F10AD00DFDD02 /* EditCategoryView.swift in Sources */,
08BE635027C4C0F2002BC6A8 /* PlayFormView.swift in Sources */,
Expand All @@ -1623,9 +1634,11 @@
08AEAEF3277F09FA00B969E2 /* NavView.swift in Sources */,
086A5F352744ED9600FECE02 /* IssueView.swift in Sources */,
08522BF027CF9FFA005FF059 /* PlayAnimation.swift in Sources */,
08397E342BA0178500DFDD02 /* DetailLink.swift in Sources */,
08AEAEDD277EA64900B969E2 /* DBDevNoti.swift in Sources */,
08BE635E27C72F50002BC6A8 /* PlayNavigationView.swift in Sources */,
08448F0D2799035600B61353 /* PlaySecurity.swift in Sources */,
0871C6192BA040E5000B620D /* InfoRowView.swift in Sources */,
086A5F442744EE2800FECE02 /* SwiftPamphletAppConfig.swift in Sources */,
08C411F427951181006FC340 /* PlaySyntax.swift in Sources */,
083554E12756503B0095E0EE /* AnimateLayout.swift in Sources */,
Expand All @@ -1636,7 +1649,6 @@
08522BD827CF344B005FF059 /* PlaySliderView.swift in Sources */,
086A5F362744ED9600FECE02 /* RepoView.swift in Sources */,
086A5F302744ED8600FECE02 /* IssuesListFromCustomView.swift in Sources */,
08397E272B9EF37600DFDD02 /* InfosView.swift in Sources */,
08C3BB7E27CE3EBD00ACF0FE /* PlayTabView.swift in Sources */,
084417752B99B9060049297D /* HomeView.swift in Sources */,
08449034279F6D0D00B61353 /* RepoWebView.swift in Sources */,
Expand Down
12 changes: 6 additions & 6 deletions SwiftPamphletApp/HomeUI/DataLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ struct DataLink: Identifiable {
var children: [DataLink]?

@ViewBuilder
static func viewToShow(for title: String?) -> some View {
static func viewToShow(for title: String?, selectInfo:Binding<IOInfo?>) -> some View {
switch title {
case "资料整理":
InfoListView()
InfoListView(selectInfo: selectInfo)
case "库动态":
ExploreRepoListView(showAsGroup: false)
case "开发者":
Expand Down Expand Up @@ -52,12 +52,12 @@ extension DataLink {
DataLink(title: "动态", imageName: "", children: [
DataLink(title: "资料整理", imageName: "p11")
]),
DataLink(title: "Github", imageName: "", children: [
// DataLink(title: "Github", imageName: "", children: [
// DataLink(title: "库动态", imageName: "p6"),
// DataLink(title: "开发者", imageName: "p5"),
DataLink(title: "探索库", imageName: "p24"),
DataLink(title: "库存档", imageName: "p25")
]),
// DataLink(title: "探索库", imageName: "p24"),
// DataLink(title: "库存档", imageName: "p25")
// ]),
DataLink(title: "Swift指南", imageName: "", children: [
DataLink(title: "语法速查", imageName: "p23"),
DataLink(title: "特性", imageName: "p10"),
Expand Down
21 changes: 21 additions & 0 deletions SwiftPamphletApp/HomeUI/DetailLink.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// DetailLink.swift
// SwiftPamphletApp
//
// Created by Ming Dai on 2024/3/12.
//

import SwiftUI

struct DetailLink {

@ViewBuilder
static func viewToShow(for title: String?, selectInfo:IOInfo) -> some View {
switch title {
case "资料":
EditInfoView(info: selectInfo)
default:
Text("请选择")
}
}
}
17 changes: 15 additions & 2 deletions SwiftPamphletApp/HomeUI/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,25 @@ import SwiftUI
struct HomeView: View {
@State var appVM = AppVM()
@State private var selectedDataLinkString: String?
@State private var selectInfo: IOInfo? = nil

var body: some View {
NavigationSplitView {
SidebarView(selectedDataLinkString: $selectedDataLinkString)
SidebarView(selectedDataLinkString: $selectedDataLinkString, selectInfo: $selectInfo)
} content: {
if let link = selectedDataLinkString {
DataLink.viewToShow(for: link, selectInfo: $selectInfo)
} else {
Text("请选择")
}
} detail: {
DataLink.viewToShow(for: selectedDataLinkString)
if let info = selectInfo {
DetailLink.viewToShow(for: "资料", selectInfo: info)
} else {
Text("请选择")
}
// Text("请选择")
// DataLink.viewToShow(for: selectedDataLinkString)
}
.environment(appVM)
}
Expand Down
6 changes: 2 additions & 4 deletions SwiftPamphletApp/HomeUI/SidebarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ import SwiftUI
struct SidebarView: View {
@Binding var selectedDataLinkString: String?

@Binding var selectInfo: IOInfo?

var body: some View {
// List(DataLink.dataLinks, children: \.children, selection: $selectedDataLinkString) { link in
// SideBarLabel(title: link.title, imageName: link.imageName)
// .tag(link.title)
// }
List(selection: $selectedDataLinkString, content: {
ForEach(DataLink.dataLinks) { link in
Section {
Expand Down
12 changes: 3 additions & 9 deletions SwiftPamphletApp/InfoOrganizer/CategoryListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,15 @@ import SwiftData
struct CategoryListView: View {
@Environment(\.modelContext) var modelContext
@Query var cates: [IOCategory]
@State var selectCate: IOCategory?

var body: some View {
List {
List(selection: $selectCate) {
ForEach(cates) { cate in
NavigationLink(value: cate) {
Text(cate.name)
CategoryRowView(cate: cate, selectedCate: selectCate)
}
}
.onDelete(perform: deleteCates(at:))
}
}
func deleteCates(at offsets: IndexSet) {
for offset in offsets {
let cate = cates[offset]
modelContext.delete(cate)
}
}
}
Expand Down
39 changes: 39 additions & 0 deletions SwiftPamphletApp/InfoOrganizer/CategoryRowView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// CategoryRowView.swift
// SwiftPamphletApp
//
// Created by Ming Dai on 2024/3/12.
//

import SwiftUI

struct CategoryRowView: View {
@State var cate: IOCategory
let selectedCate: IOCategory?

var cateColor: Color {
selectedCate == cate ? Color.white : Color.accentColor
}

var body: some View {
HStack {
TextField("name", text: $cate.name)
Spacer()
Text("\(cate.infos?.count ?? 0)")
}
.swipeActions {
Button(role: .destructive) {
IOCategory.delete(cate)
} label: {
Label("删除", systemImage: "trash")
}
}
.contextMenu {
Button("删除") {
IOCategory.delete(cate)
}
}

}
}

10 changes: 6 additions & 4 deletions SwiftPamphletApp/InfoOrganizer/EditCategoryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ struct EditCategoryView: View {

var body: some View {
VStack {
Form {
TextField("分类名", text: $cate.name)
if cate.name != "unavailable.com" {
Form {
TextField("分类名", text: $cate.name)
}
.navigationTitle("编辑分类")
.padding(30)
}
.navigationTitle("编辑分类")
.padding(30)
CategoryListView()
Spacer()
}
Expand Down
37 changes: 27 additions & 10 deletions SwiftPamphletApp/InfoOrganizer/EditInfoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import SwiftData
import SwiftSoup

struct EditInfoView: View {
@Environment(\.modelContext) var modelContext
@Bindable var info: IOInfo
@Binding var navigationPath: NavigationPath

@Query(sort: [
SortDescriptor(\IOCategory.updateDate)
SortDescriptor(\IOCategory.updateDate, order: .reverse)
]) var categories: [IOCategory]

@Environment(\.modelContext) var modelContext
@State var isShowInspector = false
@State var cate:IOCategory? = nil

var body: some View {
VStack {
Expand All @@ -44,26 +45,42 @@ struct EditInfoView: View {
}
}
}
Button("添加和管理分类", action: addCate)
.onChange(of: info.category) { oldValue, newValue in
info.category?.updateDate = Date.now
}
HStack {
Button("添加分类", action: addCate)
Button("管理分类", action: manageCate)
}
}

Section("描述") {
TextField("详细描述", text: $info.des, axis: .vertical)
}
}
.navigationTitle("编辑资料")
.navigationDestination(for: IOCategory.self) { cate in
EditCategoryView(cate: cate)
}
.padding(30)
.inspector(isPresented: $isShowInspector) {
EditCategoryView(cate: cate ?? IOCategory(name: "unavailable.com", createDate: Date.now, updateDate: Date.now))
}
.toolbar {
Button("关闭", systemImage: "sidebar.right") {
isShowInspector.toggle()
}

}
Spacer()
}
}
func addCate() {
let cate = IOCategory(name: "", createDate: Date.now, updateDate: Date.now)
modelContext.insert(cate)
navigationPath.append(cate)
cate = IOCategory(name: "", createDate: Date.now, updateDate: Date.now)
modelContext.insert(cate!)
isShowInspector = true
}
func manageCate() {
isShowInspector.toggle()
}

func fetchTitleFromUrl(urlString: String) async {
guard let url = URL(string: urlString) else {
return
Expand Down
22 changes: 17 additions & 5 deletions SwiftPamphletApp/InfoOrganizer/InfoDataModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import Foundation
import SwiftData

@Model
class IOInfo {
final class IOInfo {
var name: String = ""
var url: String = ""
var des: String = ""
var category: IOCategory?

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

init(name: String,
url: String,
Expand All @@ -32,15 +32,21 @@ class IOInfo {
self.createDate = createDate
self.updateDate = updateDate
}

static func delete(_ info: IOInfo) {
if let context = info.modelContext {
context.delete(info)
}
}
}

@Model
class IOCategory {
var name: String = ""
var infos: [IOInfo]? = [IOInfo]()

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

init(name: String,
createDate: Date,
Expand All @@ -50,4 +56,10 @@ class IOCategory {
self.createDate = createDate
self.updateDate = updateDate
}

static func delete(_ cate: IOCategory) {
if let context = cate.modelContext {
context.delete(cate)
}
}
}
Loading

0 comments on commit 50cf898

Please sign in to comment.