Skip to content

Commit

Permalink
feat: Support info and delete swipe actions in the iOS list view (#672)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbmorley authored Jun 20, 2023
1 parent 24fc4d9 commit 7443cd4
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 39 deletions.
28 changes: 19 additions & 9 deletions core/Sources/BookmarksCore/Model/SectionViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class SectionViewModel: ObservableObject, Runnable {
private let applicationModel: ApplicationModel?
private let sceneModel: SceneModel?
private let section: BookmarksSection
private let openWindow: OpenWindowAction?
private var cancellables: Set<AnyCancellable> = []

public var isPlaceholder: Bool {
Expand All @@ -60,10 +61,12 @@ public class SectionViewModel: ObservableObject, Runnable {

public init(applicationModel: ApplicationModel? = nil,
sceneModel: SceneModel? = nil,
section: BookmarksSection = .all) {
section: BookmarksSection = .all,
openWindow: OpenWindowAction? = nil) {
self.applicationModel = applicationModel
self.sceneModel = sceneModel
self.section = section
self.openWindow = openWindow
self.query = section.query
self.title = section.navigationTitle
self.layoutMode = applicationModel?.settings.layoutMode(for: section) ?? .grid
Expand Down Expand Up @@ -222,6 +225,16 @@ public class SectionViewModel: ObservableObject, Runnable {
return ids.compactMap { bookmarksLookup[$0] }
}

@MainActor public func getInfo(_ scope: SelectionScope<Bookmark.ID>) {
for bookmark in bookmarks(scope) {
#if os(iOS)
sceneModel?.edit(bookmark)
#else
openWindow?(value: bookmark.id)
#endif
}
}

@MainActor func open(_ scope: SelectionScope<Bookmark.ID>,
location: Bookmark.Location = .web,
browser: BrowserPreference = .user) {
Expand Down Expand Up @@ -304,24 +317,21 @@ public class SectionViewModel: ObservableObject, Runnable {
return Set(bookmarks.map { $0.tags }.reduce([], +))
}

@MainActor @MenuItemBuilder public func contextMenu(_ selection: Set<Bookmark.ID>,
openWindow: OpenWindowAction? = nil) -> [MenuItem] {
@MainActor @MenuItemBuilder public func contextMenu(_ selection: Set<Bookmark.ID>) -> [MenuItem] {

let bookmarks = bookmarks(.items(selection))
let containsUnreadBookmark = bookmarks.containsUnreadBookmark
let containsPublicBookmark = bookmarks.containsPublicBookmark

#if os(iOS)
if bookmarks.count == 1, let bookmark = bookmarks.first {
MenuItem(LocalizedString("BOOKMARK_MENU_TITLE_GET_INFO"), systemImage: "square.and.pencil") {
self.sceneModel?.edit(bookmark)
MenuItem(LocalizedString("BOOKMARK_MENU_TITLE_GET_INFO"), systemImage: "info") {
self.getInfo(.items([bookmark.id]))
}
}
#else
MenuItem(LocalizedString("BOOKMARK_MENU_TITLE_GET_INFO"), systemImage: "square.and.pencil") {
for id in selection {
openWindow?(value: id)
}
MenuItem(LocalizedString("BOOKMARK_MENU_TITLE_GET_INFO"), systemImage: "info") {
self.getInfo(.items(selection))
}
#endif
Divider()
Expand Down
7 changes: 6 additions & 1 deletion core/Sources/BookmarksCore/Views/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import Interact

public struct ContentView: View {

@Environment(\.openWindow) var openWindow

@ObservedObject var applicationModel: ApplicationModel

@StateObject var sceneModel: SceneModel
Expand All @@ -39,7 +41,10 @@ public struct ContentView: View {
Sidebar()
} detail: {
if let section = sceneModel.section {
SectionView(applicationModel: applicationModel, sceneModel: sceneModel, section: section)
SectionView(applicationModel: applicationModel,
sceneModel: sceneModel,
section: section,
openWindow: openWindow)
.id(section)
.environmentObject(sceneModel)
} else {
Expand Down
4 changes: 1 addition & 3 deletions core/Sources/BookmarksCore/Views/MacSectionGridView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import SelectableCollectionView

public struct MacSectionGridView: View {

@Environment(\.openWindow) var openWindow

@EnvironmentObject var applicationModel: ApplicationModel
@EnvironmentObject var sectionViewModel: SectionViewModel

Expand All @@ -51,7 +49,7 @@ public struct MacSectionGridView: View {
.shadow(color: .shadow, radius: 3.0)

} contextMenu: { selection in
sectionViewModel.contextMenu(selection, openWindow: openWindow)
sectionViewModel.contextMenu(selection)
} primaryAction: { selection in
sectionViewModel.open(.items(selection))
} keyDown: { event in
Expand Down
69 changes: 46 additions & 23 deletions core/Sources/BookmarksCore/Views/SectionTableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ struct SectionTableView: View {
private let isCompact = false
#endif

@Environment(\.openWindow) var openWindow

@EnvironmentObject var sectionViewModel: SectionViewModel

var body: some View {
Table(sectionViewModel.bookmarks, selection: $sectionViewModel.selection) {
TableColumn("") { bookmark in
if isCompact {
if isCompact {
List(selection: $sectionViewModel.selection) {
ForEach(sectionViewModel.bookmarks) { bookmark in
HStack(spacing: LayoutMetrics.horizontalSpacing) {
FaviconImage(url: bookmark.url)
VStack(alignment: .leading) {
Expand All @@ -59,30 +57,55 @@ struct SectionTableView: View {
}
.lineLimit(1)
}
} else {
FaviconImage(url: bookmark.url)
.swipeActions {
Button(role: .destructive) {
await sectionViewModel.delete(.items([bookmark.id]))
} label: {
Image(systemName: "trash")
}
}
.swipeActions(edge: .leading) {
Button {
sectionViewModel.getInfo(.items([bookmark.id]))
} label: {
Image(systemName: "info")
}
.tint(.accentColor)
}
}
}
.width(isCompact ? .none : FaviconImage.LayoutMetrics.size.width)
TableColumn("Title", value: \.title)
TableColumn("Domain") { bookmark in
Text(bookmark.url.formatted(.short))
}
TableColumn("Date") { bookmark in
Text(bookmark.date.formatted(date: .long, time: .standard))
.contextMenu(forSelectionType: Bookmark.ID.self) { selection in
sectionViewModel.contextMenu(selection)
} primaryAction: { selection in
sectionViewModel.open(.items(selection))
}
TableColumn("Notes") { bookmark in
Text(bookmark.notes)
.listStyle(.plain)
} else {
Table(sectionViewModel.bookmarks, selection: $sectionViewModel.selection) {
TableColumn("") { bookmark in
FaviconImage(url: bookmark.url)
}
.width(FaviconImage.LayoutMetrics.size.width)
TableColumn("Title", value: \.title)
TableColumn("Domain") { bookmark in
Text(bookmark.url.formatted(.short))
}
TableColumn("Date") { bookmark in
Text(bookmark.date.formatted(date: .long, time: .standard))
}
TableColumn("Notes") { bookmark in
Text(bookmark.notes)
}
TableColumn("Tags") { bookmark in
TagsView(bookmark.tags, wraps: false)
}
}
TableColumn("Tags") { bookmark in
TagsView(bookmark.tags, wraps: false)
.contextMenu(forSelectionType: Bookmark.ID.self) { selection in
sectionViewModel.contextMenu(selection)
} primaryAction: { selection in
sectionViewModel.open(.items(selection))
}
}
.contextMenu(forSelectionType: Bookmark.ID.self) { selection in
sectionViewModel.contextMenu(selection, openWindow: openWindow)
} primaryAction: { selection in
sectionViewModel.open(.items(selection))
}
}

}
8 changes: 6 additions & 2 deletions core/Sources/BookmarksCore/Views/SectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ public struct SectionView: View {

@StateObject var sectionViewModel: SectionViewModel

public init(applicationModel: ApplicationModel, sceneModel: SceneModel, section: BookmarksSection) {
public init(applicationModel: ApplicationModel,
sceneModel: SceneModel,
section: BookmarksSection,
openWindow: OpenWindowAction) {
self.applicationModel = applicationModel
_sectionViewModel = StateObject(wrappedValue: SectionViewModel(applicationModel: applicationModel,
sceneModel: sceneModel,
section: section))
section: section,
openWindow: openWindow))
}

public var body: some View {
Expand Down
2 changes: 1 addition & 1 deletion interact

0 comments on commit 7443cd4

Please sign in to comment.