You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-ios/Session/Shared/SessionHostingViewControlle...

195 lines
7.5 KiB
Swift

// Copyright © 2023 Rangeproof Pty Ltd. All rights reserved.
import SwiftUI
import SessionUIKit
import SessionUtilitiesKit
public class HostWrapper: ObservableObject {
public weak var controller: UIViewController?
}
public enum NavigationItemPosition {
case left
case right
}
public class SessionHostingViewController<Content>: UIHostingController<ModifiedContent<Content, _EnvironmentKeyWritingModifier<HostWrapper?>>>, ThemedNavigation where Content : View {
public override var preferredStatusBarStyle: UIStatusBarStyle {
return ThemeManager.currentTheme.statusBarStyle
}
public var navigationBackground: ThemeValue? { customizedNavigationBackground }
private let customizedNavigationBackground: ThemeValue?
private let shouldHideNavigationBar: Bool
lazy var navBarTitleLabel: UILabel = {
let result = UILabel()
result.font = .boldSystemFont(ofSize: Values.veryLargeFontSize)
result.themeTextColor = .textPrimary
result.textAlignment = .center
result.alpha = 1
return result
}()
lazy var crossfadeLabel: UILabel = {
let result = UILabel()
result.font = .boldSystemFont(ofSize: Values.veryLargeFontSize)
result.themeTextColor = .textPrimary
result.textAlignment = .center
result.alpha = 0
return result
}()
public init(rootView:Content, customizedNavigationBackground: ThemeValue? = nil, shouldHideNavigationBar: Bool = false) {
self.customizedNavigationBackground = customizedNavigationBackground
self.shouldHideNavigationBar = shouldHideNavigationBar
let container = HostWrapper()
let modified = rootView.environmentObject(container) as! ModifiedContent<Content, _EnvironmentKeyWritingModifier<HostWrapper?>>
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()
}
public override func viewWillAppear(_ animated: Bool) {
if shouldHideNavigationBar {
self.navigationController?.setNavigationBarHidden(true, animated: animated)
}
super.viewWillAppear(animated)
}
public override func viewWillDisappear(_ animated: Bool) {
if shouldHideNavigationBar {
self.navigationController?.setNavigationBarHidden(false, animated: animated)
}
super.viewWillDisappear(animated)
}
internal func setNavBarTitle(_ title: String, customFontSize: CGFloat? = nil) {
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(using dependencies: Dependencies) {
let logoImageView = UIImageView()
logoImageView.image = #imageLiteral(resourceName: "SessionGreen32")
logoImageView.contentMode = .scaleAspectFit
logoImageView.set(.width, to: 32)
logoImageView.set(.height, to: 32)
switch (dependencies[feature: .serviceNetwork], dependencies[feature: .forceOffline]) {
case (.mainnet, false): navigationItem.titleView = logoImageView
case (.testnet, _), (.mainnet, true):
let containerView: UIView = UIView()
containerView.clipsToBounds = false
containerView.addSubview(logoImageView)
logoImageView.pin(to: containerView)
let labelStackView: UIStackView = UIStackView()
labelStackView.axis = .vertical
containerView.addSubview(labelStackView)
labelStackView.center(in: containerView)
labelStackView.transform = CGAffineTransform.identity.rotated(by: -(CGFloat.pi / 6))
let testnetLabel: UILabel = UILabel()
testnetLabel.font = Fonts.boldSpaceMono(ofSize: 14)
testnetLabel.textAlignment = .center
if dependencies[feature: .serviceNetwork] != .mainnet {
labelStackView.addArrangedSubview(testnetLabel)
}
let offlineLabel: UILabel = UILabel()
offlineLabel.font = Fonts.boldSpaceMono(ofSize: 14)
offlineLabel.textAlignment = .center
labelStackView.addArrangedSubview(offlineLabel)
ThemeManager.onThemeChange(observer: testnetLabel) { [weak testnetLabel, weak offlineLabel] theme, primaryColor in
guard
let textColor: UIColor = theme.color(for: .textPrimary),
let strokeColor: UIColor = theme.color(for: .backgroundPrimary)
else { return }
if dependencies[feature: .serviceNetwork] != .mainnet {
testnetLabel?.attributedText = NSAttributedString(
string: dependencies[feature: .serviceNetwork].title,
attributes: [
.foregroundColor: textColor,
.strokeColor: strokeColor,
.strokeWidth: -3
]
)
}
offlineLabel?.attributedText = NSAttributedString(
Merge remote-tracking branch 'upstream/dev' into feature/groups-rebuild # Conflicts: # Scripts/LintLocalizableStrings.swift # Session.xcodeproj/project.pbxproj # Session/Calls/Call Management/SessionCall.swift # Session/Calls/Call Management/SessionCallManager.swift # Session/Calls/WebRTC/WebRTCSession+DataChannel.swift # Session/Conversations/ConversationVC+Interaction.swift # Session/Conversations/Message Cells/Content Views/OpenGroupInvitationView.swift # Session/Conversations/Message Cells/Content Views/QuoteView.swift # Session/Conversations/Message Cells/Content Views/SwiftUI/OpenGroupInvitationView_SwiftUI.swift # Session/Conversations/Message Cells/Content Views/SwiftUI/VoiceMessageView_SwiftUI.swift # Session/Conversations/Settings/ThreadSettingsViewModel.swift # Session/Database/Migrations/_001_ThemePreferences.swift # Session/Home/GlobalSearch/GlobalSearchViewController.swift # Session/Home/HomeViewModel.swift # Session/Home/Message Requests/MessageRequestsViewModel.swift # Session/Home/New Conversation/NewMessageScreen.swift # Session/Media Viewing & Editing/GIFs/GiphyAPI.swift # Session/Media Viewing & Editing/GIFs/GiphyDownloader.swift # Session/Media Viewing & Editing/PhotoLibrary.swift # Session/Meta/MainAppContext.swift # Session/Meta/SessionApp.swift # Session/Meta/Translations/remove_unused_strings.swift # Session/Notifications/PushRegistrationManager.swift # Session/Onboarding/Onboarding.swift # Session/Settings/HelpViewModel.swift # Session/Settings/SettingsViewModel.swift # Session/Settings/Views/VersionFooterView.swift # Session/Shared/FullConversationCell.swift # Session/Shared/SessionHostingViewController.swift # Session/Utilities/BackgroundPoller.swift # Session/Utilities/UIContextualAction+Utilities.swift # SessionMessagingKit/Configuration.swift # SessionMessagingKit/Database/Models/Attachment.swift # SessionMessagingKit/Database/Models/ClosedGroup.swift # SessionMessagingKit/Database/Models/Interaction.swift # SessionMessagingKit/Database/Models/RecipientState.swift # SessionMessagingKit/Database/Models/SessionThread.swift # SessionMessagingKit/Jobs/FailedMessageSendsJob.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+Contacts.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+UserGroups.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+UserProfile.swift # SessionMessagingKit/LibSession/LibSession+SessionMessagingKit.swift # SessionMessagingKit/Messages/Message.swift # SessionMessagingKit/Messages/Visible Messages/VisibleMessage.swift # SessionMessagingKit/Sending & Receiving/Attachments/ThumbnailService.swift # SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+MessageRequests.swift # SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+VisibleMessages.swift # SessionMessagingKit/Sending & Receiving/Message Handling/MessageSender+LegacyClosedGroups.swift # SessionMessagingKit/Sending & Receiving/MessageReceiver.swift # SessionMessagingKit/Sending & Receiving/MessageSender.swift # SessionMessagingKit/Sending & Receiving/Notifications/Models/SubscribeRequest.swift # SessionMessagingKit/Sending & Receiving/Notifications/Models/UnsubscribeRequest.swift # SessionMessagingKit/Sending & Receiving/Notifications/PushNotificationAPI.swift # SessionMessagingKit/Sending & Receiving/Pollers/CurrentUserPoller.swift # SessionMessagingKit/Sending & Receiving/Pollers/OpenGroupPoller.swift # SessionMessagingKit/Utilities/OWSAudioPlayer.m # SessionMessagingKit/Utilities/Preferences.swift # SessionMessagingKit/Utilities/ProfileManager.swift # SessionMessagingKitTests/Jobs/MessageSendJobSpec.swift # SessionMessagingKitTests/Open Groups/OpenGroupManagerSpec.swift # SessionNotificationServiceExtension/NSENotificationPresenter.swift # SessionShareExtension/ShareNavController.swift # SessionSnodeKit/LibSession/LibSession+Networking.swift # SessionSnodeKit/Networking/SnodeAPI.swift # SessionSnodeKit/Types/ProxiedContentDownloader.swift # SessionUIKit/Components/ConfirmationModal.swift # SessionUtilitiesKit/Database/Models/Identity.swift # SessionUtilitiesKit/General/AppContext.swift # SessionUtilitiesKit/General/FileSystem.swift # SessionUtilitiesKit/General/NSTimer+Proxying.m # SessionUtilitiesKit/General/SNUserDefaults.swift # SessionUtilitiesKit/General/UIDevice+featureSupport.swift # SessionUtilitiesKit/Media/MediaUtils.swift # SignalUtilitiesKit/Media Viewing & Editing/Image Editing/ImageEditorCropViewController.swift # SignalUtilitiesKit/Media Viewing & Editing/MediaMessageView.swift # SignalUtilitiesKit/Media Viewing & Editing/OWSVideoPlayer.swift # SignalUtilitiesKit/Screen Lock/ScreenLockViewController.swift
6 months ago
string: "Offline", // stringlint:ignore
attributes: [
.foregroundColor: textColor,
.strokeColor: strokeColor,
.strokeWidth: -3
]
)
}
navigationItem.titleView = containerView
}
}
internal func setUpDismissingButton(on postion: NavigationItemPosition) {
let closeButton = UIBarButtonItem(image: #imageLiteral(resourceName: "X"), style: .plain, target: self, action: #selector(close))
closeButton.themeTintColor = .textPrimary
switch postion {
case .left:
navigationItem.leftBarButtonItem = closeButton
case .right:
navigationItem.rightBarButtonItem = closeButton
}
}
@objc private func close() {
dismiss(animated: true, completion: nil)
}
}