Created
May 7, 2024 19:56
-
-
Save peterkos/e12c14a0ebfa89f28fecea636358dc35 to your computer and use it in GitHub Desktop.
Until I Write A Blog Post, This Gist Will Do, by Fall Out Boy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TransferablePersistentID.swift | |
// Cabinette | |
// | |
// Created by Peter Kos on 3/1/24. | |
// | |
import CoreTransferable | |
import SwiftData | |
import UniformTypeIdentifiers | |
/// Wrapper around SwiftData PersistentID that conforms to Codable, | |
/// so we can drag/drop a model using its ID as the transfer. | |
/// | |
/// Note: was unable to get this working with generics. Creating a | |
/// phantom protocol `DragContentType` was not enough to | |
/// make `.dropDestination` distinguish between them. | |
/// | |
class TransferablePersistentAlbumID: Codable { | |
var persistentID: PersistentIdentifier | |
init(_ id: PersistentIdentifier) { | |
persistentID = id | |
} | |
} | |
class TransferablePersistentTrackID: Codable { | |
var persistentID: PersistentIdentifier | |
init(_ id: PersistentIdentifier) { | |
persistentID = id | |
} | |
} | |
extension TransferablePersistentAlbumID: Transferable { | |
static var transferRepresentation: some TransferRepresentation { | |
CodableRepresentation(for: TransferablePersistentAlbumID.self, contentType: .persistentAlbumID) | |
} | |
} | |
extension TransferablePersistentTrackID: Transferable { | |
static var transferRepresentation: some TransferRepresentation { | |
CodableRepresentation(for: TransferablePersistentTrackID.self, contentType: .persistentTrackID) | |
} | |
} | |
extension UTType { | |
static var persistentAlbumID: UTType { | |
UTType(exportedAs: "me.peterkos.cabinette.persistentalbumid") | |
} | |
static var persistentTrackID: UTType { | |
UTType(exportedAs: "me.peterkos.cabinette.persistenttrackid") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment