Quick look support for media preview

This commit is contained in:
Thomas Ricouard 2022-12-22 10:53:36 +01:00
parent 1b4bef1459
commit 1a85fa19f8
23 changed files with 117 additions and 82 deletions

View File

@ -8,7 +8,6 @@
/* Begin PBXBuildFile section */
9F24EEB829360C330042359D /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9F24EEB729360C330042359D /* Preview Assets.xcassets */; };
9F24EEBB293619210042359D /* Routeur in Frameworks */ = {isa = PBXBuildFile; productRef = 9F24EEBA293619210042359D /* Routeur */; };
9F295540292B6C3400E0E81B /* Timeline in Frameworks */ = {isa = PBXBuildFile; productRef = 9F29553F292B6C3400E0E81B /* Timeline */; };
9F35DB44294F9A7D00B3281A /* Status in Frameworks */ = {isa = PBXBuildFile; productRef = 9F35DB43294F9A7D00B3281A /* Status */; };
9F35DB4729506F6600B3281A /* NotificationTab.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F35DB4629506F6600B3281A /* NotificationTab.swift */; };
@ -18,6 +17,7 @@
9F398AA92935FFDB00A889F2 /* Account in Frameworks */ = {isa = PBXBuildFile; productRef = 9F398AA82935FFDB00A889F2 /* Account */; };
9F398AAB2935FFDB00A889F2 /* Models in Frameworks */ = {isa = PBXBuildFile; productRef = 9F398AAA2935FFDB00A889F2 /* Models */; };
9F398AB329360A4C00A889F2 /* TimelineTab.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F398AB229360A4C00A889F2 /* TimelineTab.swift */; };
9F5E581929545BE700A53960 /* Env in Frameworks */ = {isa = PBXBuildFile; productRef = 9F5E581829545BE700A53960 /* Env */; };
9FAE4ACB293783B000772766 /* SettingsTab.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FAE4ACA293783B000772766 /* SettingsTab.swift */; };
9FAE4ACE29379A5A00772766 /* KeychainSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 9FAE4ACD29379A5A00772766 /* KeychainSwift */; };
9FAE4AD129379AD600772766 /* AppAccount.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FAE4AD029379AD600772766 /* AppAccount.swift */; };
@ -30,7 +30,6 @@
/* Begin PBXFileReference section */
9F24EEB729360C330042359D /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
9F24EEB92936185B0042359D /* Routeur */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = Routeur; path = Packages/Routeur; sourceTree = "<group>"; };
9F29553D292B67B600E0E81B /* Network */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = Network; path = Packages/Network; sourceTree = "<group>"; };
9F29553E292B6AF600E0E81B /* Timeline */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = Timeline; path = Packages/Timeline; sourceTree = "<group>"; };
9F35DB42294F9A2900B3281A /* Status */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = Status; path = Packages/Status; sourceTree = "<group>"; };
@ -42,6 +41,7 @@
9F398AA52935FE8A00A889F2 /* AppRouteur.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppRouteur.swift; sourceTree = "<group>"; };
9F398AAC2936005300A889F2 /* Account */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = Account; path = Packages/Account; sourceTree = "<group>"; };
9F398AB229360A4C00A889F2 /* TimelineTab.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimelineTab.swift; sourceTree = "<group>"; };
9F5E581729545B5500A53960 /* Env */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = Env; path = Packages/Env; sourceTree = "<group>"; };
9FAE4AC8293774FF00772766 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
9FAE4ACA293783B000772766 /* SettingsTab.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsTab.swift; sourceTree = "<group>"; };
9FAE4AD029379AD600772766 /* AppAccount.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppAccount.swift; sourceTree = "<group>"; };
@ -62,8 +62,8 @@
9F398AA92935FFDB00A889F2 /* Account in Frameworks */,
9FBFE64E292A72BD00C250E9 /* Network in Frameworks */,
9F398AAB2935FFDB00A889F2 /* Models in Frameworks */,
9F5E581929545BE700A53960 /* Env in Frameworks */,
9F35DB44294F9A7D00B3281A /* Status in Frameworks */,
9F24EEBB293619210042359D /* Routeur in Frameworks */,
9F295540292B6C3400E0E81B /* Timeline in Frameworks */,
9F35DB4A29506FA100B3281A /* Notifications in Frameworks */,
);
@ -119,11 +119,11 @@
9FBFE64C292A72BD00C250E9 /* Frameworks */,
9F398AAC2936005300A889F2 /* Account */,
9F35DB45294FA04C00B3281A /* DesignSystem */,
9F5E581729545B5500A53960 /* Env */,
9F398AA32935F90100A889F2 /* Models */,
9F29553D292B67B600E0E81B /* Network */,
9F35DB4829506F7F00B3281A /* Notifications */,
9F29553E292B6AF600E0E81B /* Timeline */,
9F24EEB92936185B0042359D /* Routeur */,
9F35DB42294F9A2900B3281A /* Status */,
);
sourceTree = "<group>";
@ -185,10 +185,10 @@
9F29553F292B6C3400E0E81B /* Timeline */,
9F398AA82935FFDB00A889F2 /* Account */,
9F398AAA2935FFDB00A889F2 /* Models */,
9F24EEBA293619210042359D /* Routeur */,
9FAE4ACD29379A5A00772766 /* KeychainSwift */,
9F35DB43294F9A7D00B3281A /* Status */,
9F35DB4929506FA100B3281A /* Notifications */,
9F5E581829545BE700A53960 /* Env */,
);
productName = IceCubesApp;
productReference = 9FBFE639292A715500C250E9 /* IceCubesApp.app */;
@ -496,10 +496,6 @@
/* End XCRemoteSwiftPackageReference section */
/* Begin XCSwiftPackageProductDependency section */
9F24EEBA293619210042359D /* Routeur */ = {
isa = XCSwiftPackageProductDependency;
productName = Routeur;
};
9F29553F292B6C3400E0E81B /* Timeline */ = {
isa = XCSwiftPackageProductDependency;
productName = Timeline;
@ -520,6 +516,10 @@
isa = XCSwiftPackageProductDependency;
productName = Models;
};
9F5E581829545BE700A53960 /* Env */ = {
isa = XCSwiftPackageProductDependency;
productName = Env;
};
9FAE4ACD29379A5A00772766 /* KeychainSwift */ = {
isa = XCSwiftPackageProductDependency;
package = 9FAE4ACC29379A5A00772766 /* XCRemoteSwiftPackageReference "keychain-swift" */;

View File

@ -1,7 +1,7 @@
import SwiftUI
import Timeline
import Account
import Routeur
import Env
import Status
import DesignSystem

View File

@ -2,12 +2,14 @@ import SwiftUI
import Timeline
import Network
import KeychainSwift
import Env
@main
struct IceCubesApp: App {
public static let defaultServer = "mastodon.social"
@StateObject private var appAccountsManager = AppAccountsManager()
@StateObject private var quickLook = QuickLook()
var body: some Scene {
WindowGroup {
@ -32,8 +34,10 @@ struct IceCubesApp: App {
}
}
.tint(.brand)
.quickLookPreview($quickLook.url, in: quickLook.urls)
.environmentObject(appAccountsManager)
.environmentObject(appAccountsManager.currentClient)
.environmentObject(quickLook)
}
}
}

View File

@ -1,5 +1,5 @@
import SwiftUI
import Routeur
import Env
import Network
import Account
import Models

View File

@ -1,6 +1,6 @@
import SwiftUI
import Timeline
import Routeur
import Env
import Network
import Notifications

View File

@ -1,6 +1,6 @@
import SwiftUI
import Timeline
import Routeur
import Env
import Network
import Account
import Models

View File

@ -1,6 +1,6 @@
import SwiftUI
import Timeline
import Routeur
import Env
import Network
struct TimelineTab: View {

View File

@ -1,7 +1,7 @@
import SwiftUI
import Models
import DesignSystem
import Routeur
import Env
struct AccountDetailHeaderView: View {
@EnvironmentObject private var routeurPath: RouterPath

View File

@ -4,7 +4,7 @@ import Network
import Status
import Shimmer
import DesignSystem
import Routeur
import Env
public struct AccountDetailView: View {
@Environment(\.redactionReasons) private var reasons

View File

@ -4,26 +4,23 @@
import PackageDescription
let package = Package(
name: "Routeur",
name: "Env",
platforms: [
.iOS(.v16),
],
products: [
.library(
name: "Routeur",
targets: ["Routeur"]),
name: "Env",
targets: ["Env"]),
],
dependencies: [
.package(name: "Models", path: "../Models")
],
targets: [
.target(
name: "Routeur",
name: "Env",
dependencies: [
.product(name: "Models", package: "Models"),
]),
.testTarget(
name: "RouteurTests",
dependencies: ["Routeur"]),
]
)

View File

@ -1,3 +1,3 @@
# Routeur
# Env
A description of this package.

View File

@ -0,0 +1,54 @@
import QuickLook
import SwiftUI
@MainActor
public class QuickLook: ObservableObject {
@Published public var url: URL?
@Published public private(set) var urls: [URL] = []
@Published public private(set) var isPreparing: Bool = false
@Published public private(set) var latestError: Error?
public init() {
}
public func prepareFor(urls: [URL], selectedURL: URL) async {
withAnimation {
isPreparing = true
}
do {
let paths: [URL] = try await withThrowingTaskGroup(of: URL.self, body: { group in
var paths: [URL] = []
for url in urls {
group.addTask {
try await self.localPathFor(url: url)
}
}
for try await path in group {
paths.append(path)
}
return paths
})
self.urls = paths
url = paths.first(where: { $0.lastPathComponent == selectedURL.lastPathComponent })
withAnimation {
isPreparing = false
}
} catch {
withAnimation {
isPreparing = false
}
self.urls = []
url = nil
latestError = error
}
}
private func localPathFor(url: URL) async throws -> URL {
let tempDir = URL(fileURLWithPath: NSTemporaryDirectory())
let path = tempDir.appendingPathComponent(url.lastPathComponent)
let data = try await URLSession.shared.data(from: url).0
try data.write(to: path)
return path
}
}

View File

@ -16,7 +16,7 @@ let package = Package(
dependencies: [
.package(name: "Network", path: "../Network"),
.package(name: "Models", path: "../Models"),
.package(name: "Routeur", path: "../Routeur"),
.package(name: "Env", path: "../Env"),
.package(name: "Status", path: "../Status"),
.package(url: "https://github.com/markiv/SwiftUI-Shimmer", exact: "1.1.0")
],
@ -26,7 +26,7 @@ let package = Package(
dependencies: [
.product(name: "Network", package: "Network"),
.product(name: "Models", package: "Models"),
.product(name: "Routeur", package: "Routeur"),
.product(name: "Env", package: "Env"),
.product(name: "Status", package: "Status"),
.product(name: "Shimmer", package: "SwiftUI-Shimmer")
]),

View File

@ -2,7 +2,7 @@ import SwiftUI
import Models
import DesignSystem
import Status
import Routeur
import Env
struct NotificationRowView: View {
@EnvironmentObject private var routeurPath: RouterPath

View File

@ -1,11 +0,0 @@
import XCTest
@testable import Routeur
final class RouteurTests: XCTestCase {
func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct
// results.
XCTAssertEqual(Routeur().text, "Hello, World!")
}
}

View File

@ -16,7 +16,7 @@ let package = Package(
dependencies: [
.package(name: "Models", path: "../Models"),
.package(name: "Network", path: "../Network"),
.package(name: "Routeur", path: "../Routeur"),
.package(name: "Env", path: "../Env"),
.package(name: "DesignSystem", path: "../DesignSystem"),
.package(url: "https://github.com/markiv/SwiftUI-Shimmer", exact: "1.1.0")
],
@ -26,7 +26,7 @@ let package = Package(
dependencies: [
.product(name: "Models", package: "Models"),
.product(name: "Network", package: "Network"),
.product(name: "Routeur", package: "Routeur"),
.product(name: "Env", package: "Env"),
.product(name: "DesignSystem", package: "DesignSystem"),
.product(name: "Shimmer", package: "SwiftUI-Shimmer")
]),

View File

@ -1,7 +1,7 @@
import SwiftUI
import Models
import Shimmer
import Routeur
import Env
import Network
import DesignSystem

View File

@ -1,6 +1,6 @@
import SwiftUI
import Models
import Routeur
import Env
import Network
struct StatusActionsView: View {

View File

@ -1,6 +1,7 @@
import SwiftUI
import Models
import AVKit
import Env
private class VideoPlayerViewModel: ObservableObject {
@Published var player: AVPlayer?
@ -33,9 +34,13 @@ private class SelectedMediaSheetManager: ObservableObject {
}
public struct StatusMediaPreviewView: View {
@EnvironmentObject private var quickLook: QuickLook
public let attachements: [MediaAttachement]
@StateObject private var selectedMediaSheetManager = SelectedMediaSheetManager()
@State private var isQuickLookLoading: Bool = false
public var body: some View {
VStack {
@ -56,9 +61,13 @@ public struct StatusMediaPreviewView: View {
}
}
}
.sheet(item: $selectedMediaSheetManager.selectedAttachement) { selectedAttachement in
makeSelectedAttachementsSheet(selectedAttachement: selectedAttachement)
.overlay {
if quickLook.isPreparing {
quickLookLoadingView
.transition(.opacity)
}
}
}
@ViewBuilder
@ -93,43 +102,25 @@ public struct StatusMediaPreviewView: View {
.cornerRadius(4)
.contentShape(Rectangle())
.onTapGesture {
selectedMediaSheetManager.selectedAttachement = attachement
}
}
}
private func makeSelectedAttachementsSheet(selectedAttachement: MediaAttachement) -> some View {
var attachements = attachements
attachements.removeAll(where: { $0.id == selectedAttachement.id })
attachements.insert(selectedAttachement, at: 0)
return TabView {
ForEach(attachements) { attachement in
if let type = attachement.supportedType {
VStack {
Spacer()
switch type {
case .image:
AsyncImage(
url: attachement.url,
content: { image in
image
.resizable()
.aspectRatio(contentMode: .fit)
},
placeholder: {
ProgressView()
.frame(maxWidth: 80, maxHeight: 80)
}
)
case .gifv:
VideoPlayerView(viewModel: .init(url: attachement.url))
}
Spacer()
}
Task {
await quickLook.prepareFor(urls: attachements.map{ $0.url }, selectedURL: attachement.url)
}
}
}
.tabViewStyle(.page(indexDisplayMode: .always))
}
private var quickLookLoadingView: some View {
ZStack(alignment: .center) {
VStack {
Spacer()
HStack {
Spacer()
ProgressView()
Spacer()
}
Spacer()
}
}
.background(.ultraThinMaterial)
}
}

View File

@ -1,6 +1,6 @@
import SwiftUI
import Models
import Routeur
import Env
import DesignSystem
import Network

View File

@ -16,7 +16,7 @@ let package = Package(
dependencies: [
.package(name: "Network", path: "../Network"),
.package(name: "Models", path: "../Models"),
.package(name: "Routeur", path: "../Routeur"),
.package(name: "Env", path: "../Env"),
.package(name: "Status", path: "../Status"),
.package(url: "https://github.com/markiv/SwiftUI-Shimmer", exact: "1.1.0")
],
@ -26,7 +26,7 @@ let package = Package(
dependencies: [
.product(name: "Network", package: "Network"),
.product(name: "Models", package: "Models"),
.product(name: "Routeur", package: "Routeur"),
.product(name: "Env", package: "Env"),
.product(name: "Status", package: "Status"),
.product(name: "Shimmer", package: "SwiftUI-Shimmer")
]),