Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
flocked committed Feb 3, 2024
1 parent 7db3b10 commit 35236d2
Show file tree
Hide file tree
Showing 21 changed files with 533 additions and 232 deletions.
52 changes: 17 additions & 35 deletions Example/Example/Model/CollectionItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,33 @@ import FZQuicklook

public class GalleryItem: NSObject, Identifiable {

public var title: String
public var detail: String
public var imageName: String
public var badge: String?
public var badgeColor: NSColor = .controlAccentColor
public let title: String
public let detail: String
public let imageName: String
public let badgeText: String?
public let badgeColor: NSColor
public var isFavorite: Bool = false

public init(title: String, detail: String, imageName: String, badge: String? = nil, badgeColor: NSColor = .controlAccentColor) {
public init(title: String, detail: String, imageName: String, badgeText: String? = nil, badgeColor: NSColor = .controlAccentColor) {
self.title = title
self.detail = detail
self.imageName = imageName
self.badge = badge
self.badgeText = badgeText
self.badgeColor = badgeColor
}

public static var sampleItems: [GalleryItem] {
[GalleryItem(title: "Astronaut Cat", detail: "Liquid ink", imageName: "astronaut cat"),
GalleryItem(title: "Cat", detail: "Painted by Vermeer", imageName: "cat vermeer"),
GalleryItem(title: "Cat", detail: "Vaporwave", imageName: "cat vaporwave", badge: "new"),
GalleryItem(title: "Sea Creature", detail: "Oil on canvas", imageName: "sea creature oil"),
GalleryItem(title: "Sea Creature", detail: "Science fiction", imageName: "sea creature science fiction", badge: "favorite", badgeColor: .systemPurple),
GalleryItem(title: "Techno Club", detail: "Surrealist painting", imageName: "techno club surrealist"),
GalleryItem(title: "Techno Club", detail: "Oil painting", imageName: "techno club oil"),
GalleryItem(title: "Fireworker Monkey", detail: "Japanese manga", imageName: "monkey fireworkers manga"),
GalleryItem(title: "Dystopian City", detail: "Oil painting", imageName: "dystopian city science fiction")]
}
static let sampleItems = [GalleryItem(title: "Astronaut Cat", detail: "Liquid ink", imageName: "astronaut cat"),
GalleryItem(title: "Cat", detail: "Painted by Vermeer", imageName: "cat vermeer"),
GalleryItem(title: "Cat", detail: "Vaporwave", imageName: "cat vaporwave", badgeText: "new"),
GalleryItem(title: "Sea Creature", detail: "Oil on canvas", imageName: "sea creature oil"),
GalleryItem(title: "Sea Creature", detail: "Science fiction", imageName: "sea creature science fiction", badgeText: "favorite", badgeColor: .systemPurple),
GalleryItem(title: "Techno Club", detail: "Surrealist painting", imageName: "techno club surrealist"),
GalleryItem(title: "Techno Club", detail: "Oil painting", imageName: "techno club oil"),
GalleryItem(title: "Fireworker Monkey", detail: "Japanese manga", imageName: "monkey fireworkers manga"),
GalleryItem(title: "Dystopian City", detail: "Oil painting", imageName: "dystopian city science fiction")]
}

/// By conforming to `QuicklookPreviewable` and providing `previewItemURL` the item can be quicklooked by `QuicklookPanel` (simliar to Finder's Quicklook panel).
/// By conforming to `QuicklookPreviewable` and providing `previewItemURL` the items can be previewed by pressing spacebar which opens a Quicklook panel (simliar to Finder).
extension GalleryItem: QuicklookPreviewable {
public var previewItemURL: URL? {
Bundle.main.urlForImageResource(imageName)
Expand All @@ -48,19 +46,3 @@ extension GalleryItem: QuicklookPreviewable {
title
}
}

public extension Array where Element: GalleryItem {
/// Shuffles the items by replacing the info of each item.
func shuffledItems() -> Self {
var sampleItems = GalleryItem.sampleItems
for galleryItem in self {
let newRandomItem = sampleItems.randomElement(excluding: [galleryItem])!
sampleItems.remove(newRandomItem)
galleryItem.title = newRandomItem.title
galleryItem.detail = newRandomItem.detail
galleryItem.imageName = newRandomItem.imageName
galleryItem.badge = newRandomItem.badge
}
return self
}
}
18 changes: 5 additions & 13 deletions Example/Example/Model/SidebarItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,9 @@ class SidebarItem: NSObject, Identifiable {
self.symbolName = symbolName
}

public static var sampleItems1: [SidebarItem] {
[SidebarItem(title: "Messages", symbolName: "message.fill"),
SidebarItem(title: "Photos", symbolName: "photo"),
SidebarItem(title: "Videos", symbolName: "film")]
}

public static var sampleItems2: [SidebarItem] {
[SidebarItem(title: "Archive", symbolName: "tray.full")]
}

public static var sampleItems3: [SidebarItem] {
[SidebarItem(title: "News", symbolName: "newspaper")]
}
static let sampleItems1 = [SidebarItem(title: "Messages", symbolName: "message.fill"),
SidebarItem(title: "Photos", symbolName: "photo"),
SidebarItem(title: "Videos", symbolName: "film")]
static let sampleItems2 = [SidebarItem(title: "Archive", symbolName: "tray.full")]
static let sampleItems3 = [SidebarItem(title: "News", symbolName: "newspaper")]
}
8 changes: 4 additions & 4 deletions Example/Example/SidebarViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ class SidebarViewController: NSViewController {
color: swippedItem.isFavorite ? .systemGray : .systemYellow
) { _, _ in
swippedItem.isFavorite = !swippedItem.isFavorite
self.dataSource.reloadItems([swippedItem])
self.dataSource.reconfigureItems([swippedItem])
self.tableView.rowActionsVisible = false
}]
} else {
// Right swipe
return [NSTableViewRowAction(
style: .destructive, title: "", symbolName: "trash.fill"
) { _, _ in
var currentSnapshot = self.dataSource.snapshot()
currentSnapshot.deleteItems([swippedItem])
self.dataSource.apply(currentSnapshot, .animated)
var snapshot = self.dataSource.snapshot()
snapshot.deleteItems([swippedItem])
self.dataSource.apply(snapshot)
}]
}
}
Expand Down
17 changes: 7 additions & 10 deletions Example/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ import FZSwiftUtils
import FZUIKit

class ViewController: NSViewController {
typealias ItemRegistration = NSCollectionView.ItemRegistration<NSCollectionViewItem, GalleryItem>
typealias DataSource = CollectionViewDiffableDataSource<Section, GalleryItem>

@IBOutlet var collectionView: NSCollectionView!

var galleryItems = GalleryItem.sampleItems

lazy var dataSource = DataSource(collectionView: collectionView, itemRegistration: itemRegistration)
lazy var dataSource = CollectionViewDiffableDataSource<Section, GalleryItem>(collectionView: collectionView, itemRegistration: itemRegistration)

lazy var itemRegistration = ItemRegistration { collectionViewItem, _, galleryItem in
let itemRegistration = NSCollectionView.ItemRegistration<NSCollectionViewItem, GalleryItem>() { collectionViewItem, _, galleryItem in

// Configurate the item
var configuration = NSItemContentConfiguration()
Expand All @@ -30,12 +27,12 @@ class ViewController: NSViewController {
configuration.image = NSImage(named: galleryItem.imageName)
configuration.contentProperties.shadow = .black(opacity: 0.5, radius: 5.0)

if let badgeText = galleryItem.badge {
if let badgeText = galleryItem.badgeText {
configuration.badges = [.textBadge(badgeText, color: galleryItem.badgeColor, type: .attachment)]
}

if galleryItem.isFavorite {
configuration.badges = [.textBadge("􀋃", color: .systemRed, type: .attachment, shape: .circle)]
configuration.badges = [.textBadge("􀋃", color: .systemRed, shape: .circle, type: .attachment)]
}

// Apply the configuration
Expand Down Expand Up @@ -75,22 +72,22 @@ class ViewController: NSViewController {
// Reconfigurates items without reloading them by calling the item registration
self.dataSource.reconfigureElements(selectedItems)
}

applySnapshot(using: galleryItems)
}

override func viewDidAppear() {
super.viewDidAppear()

view.window?.makeFirstResponder(collectionView)
collectionView.selectItems(at: [.zero], scrollPosition: .top)
collectionView.selectItems(at: [IndexPath(item: 0, section: 0)], scrollPosition: .top)
}

func applySnapshot(using items: [GalleryItem]) {
var snapshot = dataSource.emptySnapshot()
snapshot.appendSections([.main])
snapshot.appendItems(items, toSection: .main)
dataSource.apply(snapshot, .animated)
dataSource.apply(snapshot, .withoutAnimation)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public extension NSItemContentConfiguration {
public init() {}

/// A text badge.
public static func textBadge(_ text: String, textStyle: NSFont.TextStyle = .body, textColor: NSColor = .white, color: NSColor? = .controlAccentColor, type: BadgeType, position: Position = .topRight, shape: Shape = .roundedRect) -> Badge {
public static func textBadge(_ text: String, textStyle: NSFont.TextStyle = .body, textColor: NSColor = .white, color: NSColor? = .controlAccentColor, shape: Shape = .roundedRect, type: BadgeType, position: Position = .topRight) -> Badge {
var badge = Badge()
badge.text = text
badge.textProperties.font = .systemFont(textStyle)
Expand All @@ -172,7 +172,7 @@ public extension NSItemContentConfiguration {
}

/// A badge displaying a view.
public static func viewBadge(_ view: NSView, color: NSColor? = .controlAccentColor, type: BadgeType, position: Position = .topRight, shape: Shape = .roundedRect) -> Badge {
public static func viewBadge(_ view: NSView, color: NSColor? = .controlAccentColor, shape: Shape = .roundedRect, type: BadgeType, position: Position = .topRight) -> Badge {
var badge = Badge()
badge.view = view
badge.backgroundColor = color
Expand All @@ -186,7 +186,7 @@ public extension NSItemContentConfiguration {
}

/// A symbol image badge.
public static func symbolImageBadge(_ symbolName: String, text: String? = nil, size: NSFont.TextStyle = .body, color: NSColor = .white, backgroundColor: NSColor? = .controlAccentColor, type: BadgeType, position: Position = .topRight, shape: Shape = .roundedRect) -> Badge {
public static func symbolImageBadge(_ symbolName: String, text: String? = nil, size: NSFont.TextStyle = .body, color: NSColor = .white, backgroundColor: NSColor? = .controlAccentColor, shape: Shape = .roundedRect, type: BadgeType, position: Position = .topRight) -> Badge {
var badge = Badge()
badge.text = text
badge.image = NSImage(systemSymbolName: symbolName)
Expand All @@ -206,7 +206,7 @@ public extension NSItemContentConfiguration {
}

/// An image badge.
public static func imageBadge(_ image: NSImage, text: String? = nil, textStyle: NSFont.TextStyle = .body, color: NSColor? = .controlAccentColor, type: BadgeType, position: Position = .topRight, shape: Shape = .roundedRect) -> Badge {
public static func imageBadge(_ image: NSImage, text: String? = nil, textStyle: NSFont.TextStyle = .body, color: NSColor? = .controlAccentColor, shape: Shape = .roundedRect, type: BadgeType, position: Position = .topRight) -> Badge {
var badge = Badge()
badge.image = image
badge.text = text
Expand Down
Loading

0 comments on commit 35236d2

Please sign in to comment.