From bf3b870c8f76478ec4a6e6f44fadc1524592ada1 Mon Sep 17 00:00:00 2001 From: Ryan Zhao Date: Mon, 31 Jul 2023 17:09:48 +1000 Subject: [PATCH] WIP: show full screen media gallery in message info screen --- .../MediaGalleryNavigationController.swift | 30 ++++++++++++++ .../MediaGalleryViewModel.swift | 39 +++++++++++++++++++ .../MessageInfoView.swift | 13 +++++++ 3 files changed, 82 insertions(+) diff --git a/Session/Media Viewing & Editing/MediaGalleryNavigationController.swift b/Session/Media Viewing & Editing/MediaGalleryNavigationController.swift index 55283edec..ebe751962 100644 --- a/Session/Media Viewing & Editing/MediaGalleryNavigationController.swift +++ b/Session/Media Viewing & Editing/MediaGalleryNavigationController.swift @@ -74,3 +74,33 @@ class MediaGalleryNavigationController: UINavigationController { relayoutBackgroundView() } } + +// MARK: - SwiftUI +import SwiftUI + +struct MediaGalleryNavigationController_SwiftUI: UIViewControllerRepresentable { + typealias UIViewControllerType = MediaGalleryNavigationController + + public var viewControllers: [UIViewController] + private let transitioningDelegate: UIViewControllerTransitioningDelegate? + + public init( + viewControllers: [UIViewController], + transitioningDelegate: UIViewControllerTransitioningDelegate? = nil + ) { + self.viewControllers = viewControllers + self.transitioningDelegate = transitioningDelegate + } + + func makeUIViewController(context: Context) -> MediaGalleryNavigationController { + let mediaGalleryNavigationController = MediaGalleryNavigationController() + mediaGalleryNavigationController.modalPresentationStyle = .fullScreen + mediaGalleryNavigationController.transitioningDelegate = transitioningDelegate + + return mediaGalleryNavigationController + } + + func updateUIViewController(_ mediaGalleryNavigationController: MediaGalleryNavigationController, context: Context) { + mediaGalleryNavigationController.viewControllers = viewControllers + } +} diff --git a/Session/Media Viewing & Editing/MediaGalleryViewModel.swift b/Session/Media Viewing & Editing/MediaGalleryViewModel.swift index 10eff35e3..f63ed5264 100644 --- a/Session/Media Viewing & Editing/MediaGalleryViewModel.swift +++ b/Session/Media Viewing & Editing/MediaGalleryViewModel.swift @@ -5,6 +5,7 @@ import GRDB import DifferenceKit import SignalUtilitiesKit import SessionUtilitiesKit +import SwiftUI public class MediaGalleryViewModel { public typealias SectionModel = ArraySection @@ -565,6 +566,44 @@ public class MediaGalleryViewModel { return navController } + public static func createDetailViewSwiftUI( + for threadId: String, + threadVariant: SessionThread.Variant, + interactionId: Int64, + selectedAttachmentId: String, + options: [MediaGalleryOption] + ) -> (any UIViewControllerRepresentable)? { + // Load the data for the album immediately (needed before pushing to the screen so + // transitions work nicely) + let viewModel: MediaGalleryViewModel = MediaGalleryViewModel( + threadId: threadId, + threadVariant: threadVariant, + isPagedData: false, + mediaType: .media + ) + viewModel.loadAndCacheAlbumData(for: interactionId, in: threadId) + viewModel.replaceAlbumObservation(toObservationFor: interactionId) + + guard + !viewModel.albumData.isEmpty, + let initialItem: Item = viewModel.albumData[interactionId]?.first(where: { item -> Bool in + item.attachment.id == selectedAttachmentId + }) + else { return nil } + + let pageViewController: MediaPageViewController = MediaPageViewController( + viewModel: viewModel, + initialItem: initialItem, + options: options + ) + let navController = MediaGalleryNavigationController_SwiftUI( + viewControllers: [pageViewController], + transitioningDelegate: pageViewController + ) + + return navController + } + public static func createMediaTileViewController( threadId: String, threadVariant: SessionThread.Variant, diff --git a/Session/Media Viewing & Editing/MessageInfoView.swift b/Session/Media Viewing & Editing/MessageInfoView.swift index 0660ab70a..899db5321 100644 --- a/Session/Media Viewing & Editing/MessageInfoView.swift +++ b/Session/Media Viewing & Editing/MessageInfoView.swift @@ -406,6 +406,19 @@ struct MessageInfoView: View { } } } + + private func showMediaFullScreen(attachment: Attachment) { + let viewController: UIViewController? = MediaGalleryViewModel.createDetailViewController( + for: messageViewModel.threadId, + threadVariant: messageViewModel.threadVariant, + interactionId: messageViewModel.id, + selectedAttachmentId: attachment.id, + options: [ .sliderEnabled ] + ) + if let viewController: UIViewController = viewController { + viewController.transitioningDelegate = nil + } + } } struct InfoBlock: View where Content: View {