Open source CapCut alternative.
TypeScript
feat(ios): a card wears its media's shape
David Leb90e84df4c99
diff --git a/apps/ios/Donkey/Controllers/CutCloudClient.swift b/apps/ios/Donkey/Controllers/CutCloudClient.swiftindex 76366417..df29cee1 100644--- a/apps/ios/Donkey/Controllers/CutCloudClient.swift+++ b/apps/ios/Donkey/Controllers/CutCloudClient.swift@@ -291,6 +291,10 @@ extension CutCloudClient: CloudSyncServicing {var fileName: Stringvar type: Stringvar posterFile: String?+ // The worker probes the media it fetched; the card takes+ // its shape from this.+ var width: Int?+ var height: Int?}var assets: [Asset]?var text: String?@@ -316,6 +320,8 @@ extension CutCloudClient: CloudSyncServicing {fileName: asset.fileName,isVideo: asset.type == "video",posterFile: asset.posterFile,+ width: asset.width,+ height: asset.height,text: status.result?.text))diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CloudSync.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CloudSync.swiftindex 8ca1b7ae..13346e9e 100644--- a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CloudSync.swift+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CloudSync.swift@@ -168,6 +168,11 @@ nonisolated public struct ImportedLink: Equatable, Sendable {public var isVideo: Bool/// The source's cover, stored beside the media under the same prefix.public var posterFile: String?+ /// The media's pixel size, as the worker probed it. The card takes its+ /// shape from this, so a portrait reel and a landscape clip sit in the+ /// grid as themselves.+ public var width: Int?+ public var height: Int?/// The source's own words, when it had any.public var text: String?@@ -176,12 +181,16 @@ nonisolated public struct ImportedLink: Equatable, Sendable {fileName: String,isVideo: Bool,posterFile: String? = nil,+ width: Int? = nil,+ height: Int? = nil,text: String? = nil) {self.assetId = assetIdself.fileName = fileNameself.isVideo = isVideoself.posterFile = posterFile+ self.width = width+ self.height = heightself.text = text}}diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/DonkeyStore.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/DonkeyStore.swiftindex d307deee..3b5c3cd6 100644--- a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/DonkeyStore.swift+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/DonkeyStore.swift@@ -97,6 +97,10 @@ final class InspirationRecord {var noMedia: Bool?/// Why the last attempt failed. Set while the card offers another try.var importError: String?+ /// The media's pixel size: the worker's probe for a fetched link, this+ /// phone's measurement for an import. The card's shape comes from it.+ var mediaWidth: Int?+ var mediaHeight: Int?init(id: UUID, linkURL: String?, mediaFileName: String?, isVideo: Bool, createdAt: Date) {self.id = id@@ -113,7 +117,9 @@ final class InspirationRecord {assetId: remoteAssetId ?? "",fileName: $0,isVideo: cloudIsVideo ?? false,- posterFileName: posterFileName+ posterFileName: posterFileName,+ width: mediaWidth,+ height: mediaHeight)}// Only a live job is fetching. A link the cloud took whose job@@ -147,7 +153,9 @@ final class InspirationRecord {id: id,kind: .media(fileName: mediaFileName, isVideo: isVideo),createdAt: createdAt,- importState: .ready+ importState: .ready,+ localWidth: mediaWidth,+ localHeight: mediaHeight)}return nil@@ -572,6 +580,8 @@ public final class DonkeyStore: IdeasStoring, RecordingStoring, SyncJournalStorifileName: String,isVideo: Bool,posterFileName: String?,+ width: Int?,+ height: Int?,sourceText: String?,remoteAssetId: String) throws {@@ -580,6 +590,8 @@ public final class DonkeyStore: IdeasStoring, RecordingStoring, SyncJournalStorirecord.cloudFileName = fileNamerecord.cloudIsVideo = isVideorecord.posterFileName = posterFileName+ record.mediaWidth = width+ record.mediaHeight = heightrecord.sourceText = sourceText ?? record.sourceTextrecord.remoteAssetId = remoteAssetIdrecord.importJobId = nil@@ -611,6 +623,14 @@ public final class DonkeyStore: IdeasStoring, RecordingStoring, SyncJournalStoritry context.save()}+ public func setInspirationSize(id: UUID, width: Int, height: Int) throws {+ let descriptor = FetchDescriptor<InspirationRecord>(predicate: #Predicate { $0.id == id })+ guard let record = try context.fetch(descriptor).first else { return }+ record.mediaWidth = width+ record.mediaHeight = height+ try context.save()+ }+/// Back in the queue: the failure clears and the next pass hands the link/// over again.public func retryInspirationImport(id: UUID) throws {diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/Ideas.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/Ideas.swiftindex 43f0ad70..021fa3b6 100644--- a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/Ideas.swift+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/Ideas.swift@@ -114,12 +114,24 @@ nonisolated public struct InspirationCloudMedia: Equatable, Sendable {public var isVideo: Bool/// The poster this phone downloaded beside it, if the source had one.public var posterFileName: String?+ /// The pixel size the worker probed, which the card's shape comes from.+ public var width: Int?+ public var height: Int?- public init(assetId: String, fileName: String, isVideo: Bool, posterFileName: String? = nil) {+ public init(+ assetId: String,+ fileName: String,+ isVideo: Bool,+ posterFileName: String? = nil,+ width: Int? = nil,+ height: Int? = nil+ ) {self.assetId = assetIdself.fileName = fileNameself.isVideo = isVideoself.posterFileName = posterFileName+ self.width = width+ self.height = height}}@@ -150,6 +162,9 @@ nonisolated public struct InspirationItem: Identifiable, Equatable, Sendable {/// Where the fetch of a link stands. Media saved from the photo library/// has nothing to fetch and reads as `.ready`.public var importState: InspirationImport+ /// The pixel size of media held on this phone, measured when it landed.+ public var localWidth: Int?+ public var localHeight: Int?public init(id: UUID = UUID(),@@ -157,7 +172,9 @@ nonisolated public struct InspirationItem: Identifiable, Equatable, Sendable {createdAt: Date = .now,cloud: InspirationCloudMedia? = nil,sourceText: String? = nil,- importState: InspirationImport = .queued+ importState: InspirationImport = .queued,+ localWidth: Int? = nil,+ localHeight: Int? = nil) {self.id = idself.kind = kind@@ -165,6 +182,8 @@ nonisolated public struct InspirationItem: Identifiable, Equatable, Sendable {self.cloud = cloudself.sourceText = sourceTextself.importState = importState+ self.localWidth = localWidth+ self.localHeight = localHeight}/// Media on this phone: a photo-library import. A link's media is the@@ -186,6 +205,27 @@ nonisolated public struct InspirationItem: Identifiable, Equatable, Sendable {public var isVideo: Bool {localMedia?.isVideo ?? cloud?.isVideo ?? false}++ /// The media's own width over height: the cloud's probe for a fetched+ /// link, this phone's for an import. Nil until something has measured it,+ /// and clamped so one panorama or one very tall reel still sits in a grid+ /// beside the others.+ public var aspectRatio: Double? {+ guard let width = pixelSize?.width, let height = pixelSize?.height,+ width > 0, height > 0 else { return nil }+ return min(max(Double(width) / Double(height), Self.narrowest), Self.widest)+ }++ /// The widest and narrowest shapes a card takes: a 2:1 landscape, and a+ /// touch taller than a 9:16 reel.+ static let widest = 2.0+ static let narrowest = 0.5++ var pixelSize: (width: Int, height: Int)? {+ if let width = localWidth, let height = localHeight { return (width, height) }+ if let width = cloud?.width, let height = cloud?.height { return (width, height) }+ return nil+ }}/// Normalizes user-typed inspiration input into a URL, defaulting to https.@@ -214,6 +254,8 @@ public protocol IdeasStoring: AnyObject {func deleteInspiration(id: UUID) throws/// Put a link's import back in the queue after a failure.func retryInspirationImport(id: UUID) throws+ /// The pixel size of media stored on this phone, measured after it landed.+ func setInspirationSize(id: UUID, width: Int, height: Int) throws/// Absolute location of a stored media file.func mediaURL(fileName: String) -> URL}@@ -395,10 +437,12 @@ public final class IdeasModel {return true}- public func addInspiration(mediaData: Data, isVideo: Bool) {- guard let item = try? store.addMedia(data: mediaData, isVideo: isVideo) else { return }+ @discardableResult+ public func addInspiration(mediaData: Data, isVideo: Bool) -> InspirationItem? {+ guard let item = try? store.addMedia(data: mediaData, isVideo: isVideo) else { return nil }inspiration.insert(item, at: 0)onLocalChange?()+ return item}public func deleteInspiration(id: UUID) {@@ -415,6 +459,14 @@ public final class IdeasModel {onLocalChange?()}+ /// Record what an imported photo or video measures, so its card takes the+ /// media's own shape. The cloud reports this for a fetched link.+ public func recordSize(id: UUID, width: Int, height: Int) {+ guard width > 0, height > 0 else { return }+ try? store.setInspirationSize(id: id, width: width, height: height)+ reloadFromStore()+ }+/// Where this card streams from: a signed URL for the account's own copy.public func streamURL(for media: InspirationCloudMedia) async -> URL? {try? await cloud?.libraryMediaURL(fileName: media.fileName)diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/SyncEngine.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/SyncEngine.swiftindex 5746abc8..5af83c46 100644--- a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/SyncEngine.swift+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/SyncEngine.swift@@ -89,6 +89,8 @@ public protocol SyncJournalStoring: AnyObject {fileName: String,isVideo: Bool,posterFileName: String?,+ width: Int?,+ height: Int?,sourceText: String?,remoteAssetId: String) throws@@ -596,6 +598,8 @@ public final class SyncEngine {fileName: imported.fileName,isVideo: imported.isVideo,posterFileName: await poster(imported, for: item.id),+ width: imported.width,+ height: imported.height,sourceText: imported.text,remoteAssetId: imported.assetId)diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/IdeasScreen.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/IdeasScreen.swiftindex 74542982..faa7b4dd 100644--- a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/IdeasScreen.swift+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/IdeasScreen.swift@@ -81,7 +81,16 @@ struct IdeasScreen: View {for item in items {guard let data = try? await item.loadTransferable(type: Data.self) else { continue }let isVideo = item.supportedContentTypes.contains { $0.conforms(to: .movie) }- ideas.addInspiration(mediaData: data, isVideo: isVideo)+ guard let saved = ideas.addInspiration(mediaData: data, isVideo: isVideo),+ let media = saved.localMedia else { continue }+ // The card takes the media's own shape, so what landed is+ // measured here the way the cloud measures what it fetches.+ if let size = await mediaPixelSize(+ at: ideas.mediaURL(fileName: media.fileName),+ isVideo: isVideo+ ) {+ ideas.recordSize(id: saved.id, width: size.width, height: size.height)+ }}pickerItems = []app.show(toast: "Saved to Inspiration")@@ -190,23 +199,61 @@ struct IdeasScreen: View {.foregroundStyle(.secondary)}+ // Cards keep their media's shape, so the two columns run at their own+ // heights and each item is placed in whichever column is shorter.private var inspirationGrid: some View {- LazyVGrid(columns: ideaColumns, spacing: 14) {- ForEach(ideas.inspiration) { item in- InspirationCard(item: item, ideas: ideas) { viewing = item }- .contextMenu {- Button("Delete", systemImage: "trash", role: .destructive) {- ideas.deleteInspiration(id: item.id)- }- .tint(.red)+ HStack(alignment: .top, spacing: 14) {+ ForEach(Array(inspirationColumns.enumerated()), id: \.offset) { _, column in+ VStack(spacing: 14) {+ ForEach(column) { item in+ InspirationCard(item: item, ideas: ideas) { viewing = item }+ .contextMenu {+ Button("Delete", systemImage: "trash", role: .destructive) {+ ideas.deleteInspiration(id: item.id)+ }+ .tint(.red)+ }}+ Spacer(minLength: 0)+ }+ .frame(maxWidth: .infinity)}}}++ /// The items dealt into columns by the height each one will take at a+ /// column's width: the next card goes to whichever column is shorter, so+ /// the two ends stay level however the shapes fall.+ private var inspirationColumns: [[InspirationItem]] {+ var columns: [[InspirationItem]] = [[], []]+ var heights = [0.0, 0.0]+ for item in ideas.inspiration {+ let target = heights[0] <= heights[1] ? 0 : 1+ columns[target].append(item)+ heights[target] += 1 / (item.aspectRatio ?? InspirationCard.defaultRatio)+ }+ return columns+ }}let ideaColumns = [GridItem(.adaptive(minimum: 160, maximum: 260), spacing: 14)]+/// What a photo or a video imported from the photo library measures, in+/// pixels, read off the file the store just wrote.+func mediaPixelSize(at url: URL, isVideo: Bool) async -> (width: Int, height: Int)? {+ if !isVideo {+ guard let image = UIImage(contentsOfFile: url.localPath) else { return nil }+ let size = CGSize(width: image.size.width * image.scale, height: image.size.height * image.scale)+ return (Int(size.width.rounded()), Int(size.height.rounded()))+ }+ let asset = AVURLAsset(url: url)+ guard let track = try? await asset.loadTracks(withMediaType: .video).first,+ let (natural, transform) = try? await track.load(.naturalSize, .preferredTransform)+ else { return nil }+ let rect = CGRect(origin: .zero, size: natural).applying(transform)+ return (Int(abs(rect.width).rounded()), Int(abs(rect.height).rounded()))+}+/// One folder's notes, pushed from the Ideas screen.struct NoteFolderScreen: View {@Bindable var ideas: IdeasModel@@ -490,6 +537,9 @@ struct InspirationCard: View {var ideas: IdeasModellet onOpen: () -> Void+ /// The shape a card holds while nothing has measured its media yet.+ static let defaultRatio = 9.0 / 14+var body: some View {// A card is the media itself, the way a Library clip and a project// card are. What the source said rides along in the viewer.@@ -518,7 +568,7 @@ struct InspirationPoster: View {@State private var frame: UIImage?var body: some View {- MediaTile(ratio: 9 / 14) {+ MediaTile(ratio: item.aspectRatio ?? InspirationCard.defaultRatio) {if let image = localImage ?? frame {Image(uiImage: image).resizable()diff --git a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swiftindex 11bc13f5..f8b5863a 100644--- a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift+++ b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift@@ -151,6 +151,8 @@ import TestingfileName: "source.mp4",isVideo: true,posterFile: "source.mp4.poster.jpg",+ width: 1080,+ height: 1920,text: "What the source said"))@@ -449,11 +451,32 @@ import Testing#expect(item.cloud?.posterFileName != nil)#expect(item.sourceText == "What the source said")#expect(item.importState == .ready)+ // The card takes its shape from what the worker measured.+ #expect(item.aspectRatio == 1080.0 / 1920)// Nothing is asked of the job again once the media is on the shelf.await rig.engine.run()#expect(rig.cloud.downloads.count == 1)}+ @Test func aCardsShapeIsClampedToWhatAGridCanHold() async throws {+ let rig = try makeRig()+ rig.cloud.imported = .ready(+ ImportedLink(assetId: "a", fileName: "wide.mp4", isVideo: true, width: 4000, height: 500)+ )+ #expect(rig.ideas.addInspiration(urlText: "example.com/pano"))+ await rig.engine.run()+ await rig.engine.run()+ #expect(try #require(rig.ideas.inspiration.first).aspectRatio == 2)+ }++ @Test func anImportKeepsTheSizeItWasMeasuredAt() throws {+ let rig = try makeRig()+ let item = try #require(rig.ideas.addInspiration(mediaData: Data(repeating: 1, count: 8), isVideo: false))+ #expect(item.aspectRatio == nil)+ rig.ideas.recordSize(id: item.id, width: 1600, height: 1200)+ #expect(try #require(rig.ideas.inspiration.first).aspectRatio == 4.0 / 3)+ }+@Test func inspirationLinkWithNoMediaKeepsWhatTheSourceSaid() async throws {let rig = try makeRig()rig.cloud.imported = .noMedia(text: "An article, all words")diff --git a/docs/guides/ios-app.md b/docs/guides/ios-app.mdindex fa0cf04a..fdd2cd25 100644--- a/docs/guides/ios-app.md+++ b/docs/guides/ios-app.md@@ -32,7 +32,10 @@ words stays a link and keeps what it said.An inspiration card is the media itself, the way a Library clip and a projectcard are: a poster tile that opens full screen on a tap, with the source's own-words alongside it there. A card always says where its link stands: waiting to be handed over,+words alongside it there. A card wears its media's own shape — the worker+probes what it fetched and sends the pixel size along, an import is measured+as it lands — so a reel stands tall beside a landscape clip, and the grid+deals cards into whichever of its two columns is shorter. A card always says where its link stands: waiting to be handed over,fetching, or failed with the reason and a way to try again. Every attempt thatfails writes that reason on the item, so a card can never spin on a requestnothing is making any more. Notes merge both ways by last-writer-wins on the edit stamp,