forked from aheze/Setting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingSearchResult.swift
More file actions
88 lines (80 loc) · 2.74 KB
/
SettingSearchResult.swift
File metadata and controls
88 lines (80 loc) · 2.74 KB
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
//
// SettingSearchResult.swift
// Setting
//
// Created by A. Zheng (github.com/aheze) on 2/21/23.
// Copyright © 2023 A. Zheng. All rights reserved.
//
import SwiftUI
/**
A struct containing the search results.
*/
public struct SettingSearchResult {
public var sections: [Section]
public struct Section: Identifiable {
public let id = UUID()
public var icon: SettingIcon?
public var header: String?
public var paths: [SettingPath]
}
}
public struct SettingSearchResultView: View {
@Environment(\.settingBackgroundColor) var settingBackgroundColor
public var searchResult: SettingSearchResult
public var spacing = CGFloat(20)
public var verticalPadding = CGFloat(6)
public var backgroundColor: Color?
public init(
searchResult: SettingSearchResult,
spacing: CGFloat = CGFloat(20),
verticalPadding: CGFloat = CGFloat(6),
backgroundColor: Color? = nil
) {
self.searchResult = searchResult
self.spacing = spacing
self.verticalPadding = verticalPadding
self.backgroundColor = backgroundColor
}
public var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: spacing) {
ForEach(searchResult.sections) { section in
content(section: section)
}
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.vertical, verticalPadding)
}
.background(backgroundColor ?? settingBackgroundColor)
}
@ViewBuilder func content(section: SettingSearchResult.Section) -> some View {
/// If it's just a custom view on the first page, show the custom view as-is.
if
section.header == nil,
let firstPath = section.paths.first,
let firstItem = firstPath.settings.first,
let setting = firstItem as? SettingCustomView,
setting.displayIndependentlyInSearch
{
SettingView(setting: firstItem)
} else {
VStack {
SettingGroupView(
icon: section.icon,
header: section.header
) {
ForEach(section.paths) { path in
/// If it's only 1 setting, the setting is on the main page - so just show it.
if path.settings.count == 1 {
if let setting = path.settings.first {
SettingView(setting: setting)
}
} else {
SettingJumpLink(path: path)
}
}
}
}
}
}
}