From 96c6965f44a14621fe6477737d6040d5d1192ba6 Mon Sep 17 00:00:00 2001 From: Ryan ZHAO <> Date: Thu, 9 Nov 2023 13:14:16 +1100 Subject: [PATCH] fix an issue where media gallery detail view was not displayed properly --- .../MediaGalleryViewModel.swift | 51 +++------------- .../MessageInfoView.swift | 39 +++++++------ .../Shared/SessionHostingViewController.swift | 58 +++++++++++++++---- 3 files changed, 75 insertions(+), 73 deletions(-) diff --git a/Session/Media Viewing & Editing/MediaGalleryViewModel.swift b/Session/Media Viewing & Editing/MediaGalleryViewModel.swift index 46abba713..75a7fdabd 100644 --- a/Session/Media Viewing & Editing/MediaGalleryViewModel.swift +++ b/Session/Media Viewing & Editing/MediaGalleryViewModel.swift @@ -535,7 +535,8 @@ public class MediaGalleryViewModel { threadVariant: SessionThread.Variant, interactionId: Int64, selectedAttachmentId: String, - options: [MediaGalleryOption] + options: [MediaGalleryOption], + useTransitioningDelegate: Bool = true ) -> UIViewController? { // Load the data for the album immediately (needed before pushing to the screen so // transitions work nicely) @@ -563,52 +564,14 @@ public class MediaGalleryViewModel { let navController: MediaGalleryNavigationController = MediaGalleryNavigationController() navController.viewControllers = [pageViewController] navController.modalPresentationStyle = .fullScreen - navController.transitioningDelegate = pageViewController - return navController - } - - @ViewBuilder - public static func createDetailViewSwiftUI( - for threadId: String, - threadVariant: SessionThread.Variant, - interactionId: Int64, - selectedAttachmentId: String, - options: [MediaGalleryOption] - ) -> some View { - // 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 - ) - let _ = viewModel.loadAndCacheAlbumData(for: interactionId, in: threadId) - let _ = viewModel.replaceAlbumObservation(toObservationFor: interactionId) - - if - !viewModel.albumData.isEmpty, - let initialItem: Item = viewModel.albumData[interactionId]?.first(where: { item -> Bool in - item.attachment.id == selectedAttachmentId - }) - { - let pageViewController: MediaPageViewController = MediaPageViewController( - viewModel: viewModel, - initialItem: initialItem, - options: options - ) - let navController = MediaGalleryNavigationController_SwiftUI( - viewControllers: [pageViewController], - transitioningDelegate: pageViewController - ) - navController - } - else { - EmptyView() + if useTransitioningDelegate { + navController.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 855bbf4f4..be20a9252 100644 --- a/Session/Media Viewing & Editing/MessageInfoView.swift +++ b/Session/Media Viewing & Editing/MessageInfoView.swift @@ -7,10 +7,9 @@ import SessionUtilitiesKit import SessionMessagingKit struct MessageInfoView: View { - @Environment(\.viewController) private var viewControllerHolder: UIViewController? + @EnvironmentObject var host: HostWrapper @State var index = 1 - @State var showingAttachmentFullScreen = false static private let cornerRadius: CGFloat = 17 @@ -20,8 +19,6 @@ struct MessageInfoView: View { return [.failed, .failedToSync].contains(messageViewModel.state) } - var dismiss: (() -> Void)? - var body: some View { NavigationView { ZStack (alignment: .topLeading) { @@ -120,15 +117,7 @@ struct MessageInfoView: View { } Button { - self.viewControllerHolder?.present(style: .fullScreen) { - MediaGalleryViewModel.createDetailViewSwiftUI( - for: messageViewModel.threadId, - threadVariant: messageViewModel.threadVariant, - interactionId: messageViewModel.id, - selectedAttachmentId: attachment.id, - options: [ .sliderEnabled ] - ) - } + self.showMediaFullScreen(attachment: attachment) } label: { ZStack { Circle() @@ -324,7 +313,7 @@ struct MessageInfoView: View { Button( action: { actions[index].work() - dismiss?() + dismiss() }, label: { HStack(spacing: Values.largeSpacing) { @@ -366,6 +355,23 @@ struct MessageInfoView: View { } } } + + private func showMediaFullScreen(attachment: Attachment) { + if let mediaGalleryView = MediaGalleryViewModel.createDetailViewController( + for: messageViewModel.threadId, + threadVariant: messageViewModel.threadVariant, + interactionId: messageViewModel.id, + selectedAttachmentId: attachment.id, + options: [ .sliderEnabled ], + useTransitioningDelegate: false + ) { + self.host.controller?.present(mediaGalleryView, animated: true) + } + } + + func dismiss() { + self.host.controller?.navigationController?.popViewController(animated: true) + } } struct MessageBubble: View { @@ -519,7 +525,6 @@ final class MessageInfoViewController: SessionHostingViewController: UIHostingController where Content : View { +public class HostWrapper: ObservableObject { + public weak var controller: UIViewController? +} + +public class SessionHostingViewController: UIHostingController>> where Content : View { public override var preferredStatusBarStyle: UIStatusBarStyle { return ThemeManager.currentTheme.statusBarStyle } @@ -14,7 +18,7 @@ public class SessionHostingViewController: UIHostingController result.themeTextColor = .textPrimary result.textAlignment = .center result.alpha = 1 - + return result }() @@ -24,17 +28,28 @@ public class SessionHostingViewController: UIHostingController result.themeTextColor = .textPrimary result.textAlignment = .center result.alpha = 0 - + return result }() - + + public init(rootView:Content) { + let container = HostWrapper() + let modified = rootView.environmentObject(container) as! ModifiedContent> + super.init(rootView: modified) + container.controller = self + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + public override func viewDidLoad() { super.viewDidLoad() - + navigationItem.backButtonTitle = "" view.themeBackgroundColor = .backgroundPrimary ThemeManager.applyNavigationStylingIfNeeded(to: self) - + setNeedsStatusBarAppearanceUpdate() } @@ -42,18 +57,41 @@ public class SessionHostingViewController: UIHostingController let container = UIView() navBarTitleLabel.text = title crossfadeLabel.text = title - + if let customFontSize = customFontSize { navBarTitleLabel.font = .boldSystemFont(ofSize: customFontSize) crossfadeLabel.font = .boldSystemFont(ofSize: customFontSize) } - + container.addSubview(navBarTitleLabel) container.addSubview(crossfadeLabel) - + navBarTitleLabel.pin(to: container) crossfadeLabel.pin(to: container) - + navigationItem.titleView = container } + + internal func setUpNavBarSessionHeading() { + let headingImageView = UIImageView( + image: UIImage(named: "SessionHeading")? + .withRenderingMode(.alwaysTemplate) + ) + headingImageView.themeTintColor = .textPrimary + headingImageView.contentMode = .scaleAspectFit + headingImageView.set(.width, to: 150) + headingImageView.set(.height, to: Values.mediumFontSize) + + navigationItem.titleView = headingImageView + } + + internal func setUpNavBarSessionIcon() { + let logoImageView = UIImageView() + logoImageView.image = #imageLiteral(resourceName: "SessionGreen32") + logoImageView.contentMode = .scaleAspectFit + logoImageView.set(.width, to: 32) + logoImageView.set(.height, to: 32) + + navigationItem.titleView = logoImageView + } }