Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions Sources/Oatcake/Library.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,24 @@ final class Library {
attempt { moved(from: path, to: try meetings.retitle(path, to: title)) }
}

func move(_ path: String, into target: String) {
guard path != target, !target.hasPrefix(path + "/"), parentOf(path) != target else { return }
attempt {
moved(from: path, to: try meetings.move(path, into: target))
toast("Moved to \(folderPath(target))")
/// Moves what was dropped on a folder: one meeting or folder, or every meeting picked in the list.
/// Ones already there are left alone. One that can't be moved stops the rest, and the toast says
/// how many went before it and why, as Move to Trash does.
func move(_ paths: [String], into target: String) {
let paths = paths.filter { $0 != target && !target.hasPrefix($0 + "/") && parentOf($0) != target }
guard !paths.isEmpty else { return }
var count = 0
do {
for path in paths {
moved(from: path, to: try meetings.move(path, into: target))
count += 1
}
} catch {
toast(paths.count == 1 ? error.localizedDescription
: "Moved \(count) of \(paths.count) meetings to \(folderPath(target)) — \(error.localizedDescription)", isError: true)
return
}
toast(paths.count == 1 ? "Moved to \(folderPath(target))" : "Moved \(paths.count) meetings to \(folderPath(target))")
}

@discardableResult
Expand Down
15 changes: 11 additions & 4 deletions Sources/Oatcake/SidebarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import SwiftUI
/// What a drag from the meeting list or the folder tree carries: an item's path in the Meetings folder.
let dragPrefix = "oatcake-item:"

/// The paths in a drop. Dragging several meetings picked in the list brings one item for each.
func droppedPaths(_ items: [String]) -> [String] {
items.filter { $0.hasPrefix(dragPrefix) }.map { String($0.dropFirst(dragPrefix.count)) }
}

enum SidebarItem: Hashable {
case live, todos, job(String), folder(String)
}
Expand Down Expand Up @@ -148,8 +153,9 @@ struct SidebarView: View {
}

private func drop(_ items: [String], on target: String) -> Bool {
guard let item = items.first, item.hasPrefix(dragPrefix) else { return false }
model.library.move(String(item.dropFirst(dragPrefix.count)), into: target)
let paths = droppedPaths(items)
guard !paths.isEmpty else { return false }
model.library.move(paths, into: target)
return true
}

Expand Down Expand Up @@ -350,8 +356,9 @@ private struct FolderRow: View {
.tag(SidebarItem.folder(node.path))
.draggable(dragPrefix + node.path)
.dropDestination(for: String.self) { items, _ in
guard let item = items.first, item.hasPrefix(dragPrefix) else { return false }
model.library.move(String(item.dropFirst(dragPrefix.count)), into: node.path)
let paths = droppedPaths(items)
guard !paths.isEmpty else { return false }
model.library.move(paths, into: node.path)
return true
} isTargeted: { dropTarget = $0 ? node.path : (dropTarget == node.path ? nil : dropTarget) }
.contextMenu {
Expand Down
Loading