// Cotton Chat's viewer: a bubble's photos one at a time, swiped between, // pinched to look closer, and a downward drag puts it away. A video plays // in the system's player, from the file `chat.file` writes; a refusal // (`file_gone`, `file_offline`) is said in its own words. #if os(iOS) import AVKit import CottonKit import SwiftUI import UIKit struct ChatViewing: Identifiable, Hashable { let bubble: ChatBubble /// The place of the part tapped among the bubble's parts. let index: Int var id: String { "\(bubble.message)#\(index)" } } struct ChatViewer: View { let viewing: ChatViewing let book: any ChatBook let close: () -> Void @State private var page: Int @State private var drag: CGSize = .zero init(viewing: ChatViewing, book: any ChatBook, close: @escaping () -> Void) { self.viewing = viewing self.book = book self.close = close _page = State(initialValue: viewing.index) } var body: some View { // Paged by each part's place in the bubble: a grid's tiles each open through their own message, all at index 0. let parts = viewing.bubble.media.enumerated().filter { $0.element.kind == .photo || $0.element.kind == .video } ZStack(alignment: .topLeading) { Color.black.opacity(1 - min(0.6, abs(drag.height) / 600)).ignoresSafeArea() TabView(selection: $page) { ForEach(parts, id: \.offset) { at, part in ChatViewerPage(message: part.opens(in: viewing.bubble), part: part, book: book) .tag(at) } } .tabViewStyle(.page(indexDisplayMode: parts.count > 1 ? .automatic : .never)) .offset(y: drag.height) .gesture(DragGesture(minimumDistance: 20) .onChanged { value in if abs(value.translation.height) > abs(value.translation.width) { drag = value.translation } } .onEnded { value in if abs(value.translation.height) > 120 { close() } else { withAnimation(.spring(duration: 0.25)) { drag = .zero } } }) Button(action: close) { Image(systemName: "xmark") .font(.system(size: 17, weight: .semibold)) .foregroundStyle(Color.black) .frame(width: 36, height: 36) .background(Circle().fill(.thinMaterial)) } .padding(16) .accessibilityIdentifier("chat-viewer-close") } .statusBarHidden() // A container of its own, so its close mark and pictures keep their own names. .accessibilityElement(children: .contain) .accessibilityIdentifier("chat-viewer") } } private struct ChatViewerPage: View { let message: String let part: ChatMediaPart let book: any ChatBook @State private var image: UIImage? @State private var player: AVPlayer? @State private var refused: String? @State private var zoom: CGFloat = 1 @GestureState private var pinch: CGFloat = 1 var body: some View { GeometryReader { geometry in ZStack { if let player { ChatVideoPlayer(player: player) } else if let refused { let look = ChatLook.from(ChatThemesStore.last, dark: true) Text(verbatim: refused) .font(look.font(15)) .foregroundStyle(look.colours.text) .multilineTextAlignment(.center) .padding(32) .accessibilityIdentifier("chat-viewer-refused") } else if let image { Image(uiImage: image) .resizable() .scaledToFit() .scaleEffect(zoom * pinch) .gesture(MagnifyGesture() .updating($pinch) { value, state, _ in state = value.magnification } .onEnded { value in withAnimation(.spring(duration: 0.25)) { zoom = max(1, min(4, zoom * value.magnification)) } }) .onTapGesture(count: 2) { withAnimation(.spring(duration: 0.25)) { zoom = zoom > 1 ? 1 : 2.5 } } // Which picture this is, by the message it opens through and its place there. .accessibilityIdentifier("chat-viewer-picture-\(ChatPictureTries.key(message, part.index))") } else { ProgressView().tint(Color.black.opacity(0.0)) } } .frame(width: geometry.size.width, height: geometry.size.height) .task { if part.kind == .video { do { let opened = try await book.file(message: message, index: part.index) player = AVPlayer(url: opened.url) } catch let refusal as CottonCallError { refused = refusal.message } catch { refused = ShellLines.line("chat", "app.notice.file_offline") } return } guard let url = await ChatPictureTries.shared.file(message: message, index: part.index, book: book) else { return } image = await ChatPictures.shared.image(url, side: max(geometry.size.width, geometry.size.height)) } .onDisappear { player?.pause() } } } } /// The system's video player, its own controls and picture in picture. private struct ChatVideoPlayer: UIViewControllerRepresentable { let player: AVPlayer func makeUIViewController(context: Context) -> AVPlayerViewController { let controller = AVPlayerViewController() controller.player = player controller.allowsPictureInPicturePlayback = true player.play() return controller } func updateUIViewController(_ controller: AVPlayerViewController, context: Context) { if controller.player !== player { controller.player = player } } } #endif