Gemini/Gemini-iOS/ToolbarPrefView.swift

339 lines
11 KiB
Swift

//
// ToolbarPrefView.swift
// Gemini-iOS
//
// Created by Shadowfacts on 10/9/21.
//
import SwiftUI
import Combine
let toolbarItemType = "space.vaccor.Gemini.toolbar-item"
struct ToolbarPrefView: View {
// todo: this should really be a @StateObject and shouldn't be passed in from the outside, but that requires iOS 14
@ObservedObject private var model: CustomizeToolbarViewModel
init(model: CustomizeToolbarViewModel) {
self.model = model
}
var body: some View {
VStack(spacing: 0) {
Spacer()
Text("Drag and drop items to change your toolbar")
Spacer()
separator
HStack(spacing: 8) {
Spacer()
ForEach(model.items) { (toolbarItem) in
Image(systemName: toolbarItem.imageName)
.font(.system(size: 24))
.foregroundColor(toolbarItem.isPlaceholder ? .gray : .blue)
.hoverEffect()
.overlay(GeometryReader { (proxy) in
Color.clear
.preference(key: FloatPrefKey.self, value: proxy.size.width)
.onPreferenceChange(FloatPrefKey.self) { newValue in
model.itemWidths[toolbarItem] = newValue
}
})
.onDrag {
guard !toolbarItem.isPlaceholder else {
return NSItemProvider()
}
model.beginDragging(item: toolbarItem.item)
return NSItemProvider(item: nil, typeIdentifier: toolbarItemType)
}
Spacer()
}
.animation(.default, value: model.items)
}
.padding(.vertical, 8)
.overlay(GeometryReader { proxy in
Color.clear
.preference(key: FloatPrefKey.self, value: proxy.size.width)
.onPreferenceChange(FloatPrefKey.self) { newValue in
model.totalWidth = newValue
}
})
.frame(maxWidth: .infinity)
.padding(.vertical, 4)
// this onDrop is needed because the one on the .background doesn't fire when the drag item is held above one of the items themselves
.onDrop(of: [toolbarItemType], delegate: model)
.background(
Color(UIColor.systemBackground)
// this onDrop is needed because the one on the ForEach doesn't fire when is the drag item held in between two of the ForEach items (i.e., above one of the spacers)
.onDrop(of: [toolbarItemType], delegate: model)
)
separator
Spacer(minLength: 16)
VStack(alignment: .textLeading, spacing: 8) {
ForEach(ToolbarItem.allCases, id: \.rawValue) { (item) in
HStack(spacing: 4) {
Image(systemName: item.imageName)
.font(.system(size: 24))
.foregroundColor(canAdd(item: item) ? .blue : .gray)
.disabled(!canAdd(item: item))
.hoverEffect()
.animation(.default, value: canAdd(item: item))
.onDrag {
model.beginDragging(item: item)
return NSItemProvider(item: nil, typeIdentifier: toolbarItemType)
}
Text(item.displayName)
.alignmentGuide(.textLeading) { d in d[HorizontalAlignment.leading] }
}
}
}
Spacer()
}
.background(
Color(.secondarySystemBackground)
.edgesIgnoringSafeArea(.all)
// this stupid onDrop is necessary because sometimes when quickly dragging an item out of the toolbar, dropExited is never called on the main delegate
// so the BackgroundDropDelegate ensures that any placeholders are removed
.onDrop(of: [toolbarItemType], delegate: BackgroundDropDelegate(toolbarDropDelegate: model))
)
}
private var separator: some View {
Rectangle()
.frame(height: 0.5)
.foregroundColor(.gray)
}
private func canAdd(item: ToolbarItem) -> Bool {
return !model.items.contains(.item(item))
}
}
private extension HorizontalAlignment {
struct TextLeadingAlignment: AlignmentID {
static func defaultValue(in context: ViewDimensions) -> CGFloat {
return context[HorizontalAlignment.center]
}
}
/// The alignment used for aligning the leading edges of all the text in the toolbar item list.
static let textLeading = HorizontalAlignment(TextLeadingAlignment.self)
}
private enum CustomizeToolbarItem: Identifiable, Equatable, Hashable {
case item(ToolbarItem)
case placeholder(ToolbarItem)
var id: String {
switch self {
case let .item(item):
return item.rawValue
case let .placeholder(item):
return "placeholder_\(item.rawValue)"
}
}
var imageName: String {
switch self {
case .item(let item), .placeholder(let item):
return item.imageName
}
}
var isPlaceholder: Bool {
switch self {
case .item(_):
return false
case .placeholder(_):
return true
}
}
var item: ToolbarItem {
switch self {
case .item(let item), .placeholder(let item):
return item
}
}
}
class CustomizeToolbarViewModel: ObservableObject, DropDelegate {
@Published fileprivate var items: [CustomizeToolbarItem]
fileprivate var totalWidth: CGFloat = 0
fileprivate var itemWidths: [CustomizeToolbarItem: CGFloat] = [:]
fileprivate var draggedItem: ToolbarItem?
private var isDraggingExisting = false
private var cancellables = Set<AnyCancellable>()
init() {
items = Preferences.shared.toolbar.map { .item($0) }
Preferences.shared.$toolbar
.sink { [unowned self] (newValue) in
self.items = newValue.map { .item($0) }
}
.store(in: &cancellables)
}
fileprivate func beginDragging(item: ToolbarItem) {
draggedItem = item
if let index = items.firstIndex(where: { $0.item == item }) {
items[index] = .placeholder(item)
isDraggingExisting = true
} else {
isDraggingExisting = false
}
}
fileprivate func cleanupCustomizeItems() {
items = items.compactMap {
switch $0 {
case .item(_):
return $0
case .placeholder(let item):
if canRemove(item: item) {
return nil
} else {
return .item(item)
}
}
}
updatePreferences()
}
private func canRemove(item: ToolbarItem) -> Bool {
// preferences can't be removed because the user would lose access to the toolbar preferences
return item != .preferences
}
private func updatePreferences() {
Preferences.shared.toolbar = self.items.compactMap {
switch $0 {
case let .item(item):
return item
case .placeholder:
return nil
}
}
}
private func proposedDropIndex(info: DropInfo) -> Int {
let totalItemWidth = itemWidths.filter { items.contains($0.key) }.reduce(0, { $0 + $1.value })
let remainingWidth = totalWidth - totalItemWidth
let spacerWidth = remainingWidth / CGFloat(items.count + 1)
var accumulatedWidth = spacerWidth
for (index, item) in items.enumerated() {
let itemWidth = itemWidths[item]!
if info.location.x < accumulatedWidth + itemWidth / 2 {
return index
} else {
accumulatedWidth += itemWidth + spacerWidth
}
}
return items.count
}
// MARK: DropDelegate
func validateDrop(info: DropInfo) -> Bool {
return draggedItem != nil && (isDraggingExisting || UIDevice.current.userInterfaceIdiom == .pad || items.filter { !$0.isPlaceholder }.count < 6)
}
func dropEntered(info: DropInfo) {
items.removeAll(where: \.isPlaceholder)
// if we just exited the other onDrop handler, we need to re-add the placeholder
if !items.contains(where: { $0.item == draggedItem! }) {
items.insert(.placeholder(draggedItem!), at: proposedDropIndex(info: info))
}
}
func dropUpdated(info: DropInfo) -> DropProposal? {
if isDraggingExisting {
let index = items.firstIndex { $0.item == draggedItem! }!
items[index] = .placeholder(draggedItem!)
items.move(fromOffsets: IndexSet(integer: index), toOffset: proposedDropIndex(info: info))
return DropProposal(operation: .move)
} else {
items.removeAll(where: \.isPlaceholder)
let index = proposedDropIndex(info: info)
items.insert(.placeholder(draggedItem!), at: index)
return DropProposal(operation: .copy)
}
}
func dropExited(info: DropInfo) {
cleanupCustomizeItems()
}
func performDrop(info: DropInfo) -> Bool {
let placeholderIndex = items.firstIndex(where: \.isPlaceholder)!
items[placeholderIndex] = .item(draggedItem!)
self.updatePreferences()
return true
}
}
private struct BackgroundDropDelegate: DropDelegate {
let toolbarDropDelegate: CustomizeToolbarViewModel
func dropUpdated(info: DropInfo) -> DropProposal? {
if toolbarDropDelegate.draggedItem != nil {
toolbarDropDelegate.cleanupCustomizeItems()
}
return DropProposal(operation: .move)
}
func performDrop(info: DropInfo) -> Bool {
return false
}
}
private struct FloatPrefKey: PreferenceKey {
static var defaultValue: CGFloat = 0
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
value = nextValue()
}
}
private extension View {
@ViewBuilder
func onDragWithPreviewIfPossible<V>(_ data: @escaping () -> NSItemProvider, preview: () -> V) -> some View where V : View {
if #available(iOS 15.0, *) {
self.onDrag(data, preview: preview)
} else {
self.onDrag(data)
}
}
@ViewBuilder
func onDragIf(condition: () -> Bool, data: @escaping () -> NSItemProvider) -> some View {
if condition() {
self.onDrag(data)
} else {
self
}
}
}
struct ToolbarPrefView_Previews: PreviewProvider {
static var previews: some View {
ToolbarPrefView(model: CustomizeToolbarViewModel())
}
}