-
Notifications
You must be signed in to change notification settings - Fork 5
/
BulkActionsQuery.swift
129 lines (114 loc) · 5.53 KB
/
BulkActionsQuery.swift
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//
// Created by Jeffrey Bergier on 2022/06/25.
//
// MIT License
//
// Copyright (c) 2021 Jeffrey Bergier
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import SwiftUI
import V3Model
@propertyWrapper
public struct BulkActionsQuery: DynamicProperty {
public struct Value: Hashable, Equatable {
public var websiteAdd: Bool = false
public var tagAdd: Bool = false
/// Contains items that can be deselected
public var deselectAll: Website.Selection = []
public var openInSheet: SingleMulti<Website.Selection.Element>? = nil
public var openInWindow: SingleMulti<Website.Selection.Element>? = nil
public var openExternal: SingleMulti<URL>? = nil
public var share: Website.Selection = []
public var archiveYes: Website.Selection = []
public var archiveNo: Website.Selection = []
public var tagApply: Website.Selection = []
public var websiteEdit: Website.Selection = []
public var tagsEdit: Tag.Selection = []
public var websiteDelete: Website.Selection = []
public var tagDelete: Tag.Selection = []
public var showErrors: Bool = false
public init() {}
}
@Controller private var controller
@State private var valueCache: Value = .init()
@State private var websiteSelection: Website.Selection = []
@State private var tagSelection: Tag.Selection = []
public init() {}
public var wrappedValue: Value {
get { self.valueCache }
nonmutating set { self.valueCache = newValue }
}
public func setWebsite(selection newValue: Website.Selection) {
self.websiteSelection = newValue
self.recalculate()
}
public func setTag(selection newValue: Tag.Selection) {
self.tagSelection = newValue
self.recalculate()
}
private func recalculate() {
let controller = self.controller
let selectionW = self.websiteSelection
let selectionT = self.tagSelection
// reset default state
self.valueCache.websiteAdd = false
self.valueCache.tagAdd = false
// set things that are always true
self.valueCache.share = selectionW
self.valueCache.tagApply = selectionW
self.valueCache.websiteEdit = selectionW
self.valueCache.websiteDelete = selectionW
self.valueCache.deselectAll = selectionW
// set things with custom logic
self.valueCache.openInSheet = type(of: self).openWebsite(selectionW, controller)
self.valueCache.openInWindow = type(of: self).openWebsite(selectionW, controller)
self.valueCache.openExternal = type(of: self).openURL(selectionW, controller)
self.valueCache.archiveYes = type(of: self).canArchiveYes(selectionW, controller)
self.valueCache.archiveNo = type(of: self).canArchiveNo(selectionW, controller)
self.valueCache.tagDelete = type(of: self).editTags(selectionT)
self.valueCache.tagsEdit = type(of: self).editTags(selectionT)
}
}
extension BulkActionsQuery {
// Workaround so this can be used from Menu
public static func canArchiveYes(_ selection: Website.Selection, _ controller: ControllerProtocol) -> Website.Selection {
return (controller as! CD_Controller).canArchiveYes(selection) ? selection : []
}
public static func canArchiveNo(_ selection: Website.Selection, _ controller: ControllerProtocol) -> Website.Selection {
return (controller as! CD_Controller).canArchiveNo(selection) ? selection : []
}
public static func openURL(_ selection: Website.Selection, _ controller: ControllerProtocol) -> SingleMulti<URL>? {
return (controller as! CD_Controller).openURL(selection)
}
public static func openWebsite(_ selection: Website.Selection, _ controller: ControllerProtocol) -> SingleMulti<Website.Selection.Element>? {
return (controller as! CD_Controller).openURL(selection)
}
public static func setArchive(_ newValue: Bool,
_ selection: Website.Selection,
_ controller: ControllerProtocol)
-> Result<Void, Error>
{
let controller = controller as! CD_Controller
return controller.setArchive(newValue, on: selection)
}
public static func editTags(_ selection: Tag.Selection) -> Tag.Selection {
return selection.filter { $0.isSystem == false }
}
}