HomeContentView.swift 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // LoopFollow
  2. // HomeContentView.swift
  3. import SwiftUI
  4. import UIKit
  5. /// A SwiftUI wrapper around MainViewController that displays the full Home screen.
  6. /// This can be used both in the tab bar and as a modal from the Menu.
  7. struct HomeContentView: UIViewControllerRepresentable {
  8. let isModal: Bool
  9. init(isModal: Bool = false) {
  10. self.isModal = isModal
  11. }
  12. func makeUIViewController(context _: Context) -> UIViewController {
  13. let storyboard = UIStoryboard(name: "Main", bundle: nil)
  14. // Get the MainViewController from storyboard
  15. guard let mainVC = storyboard.instantiateViewController(withIdentifier: "MainViewController") as? MainViewController else {
  16. let fallbackVC = UIViewController()
  17. fallbackVC.view.backgroundColor = .systemBackground
  18. let label = UILabel()
  19. label.text = "Unable to load Home screen"
  20. label.textAlignment = .center
  21. label.translatesAutoresizingMaskIntoConstraints = false
  22. fallbackVC.view.addSubview(label)
  23. NSLayoutConstraint.activate([
  24. label.centerXAnchor.constraint(equalTo: fallbackVC.view.centerXAnchor),
  25. label.centerYAnchor.constraint(equalTo: fallbackVC.view.centerYAnchor),
  26. ])
  27. return fallbackVC
  28. }
  29. mainVC.overrideUserInterfaceStyle = Storage.shared.appearanceMode.value.userInterfaceStyle
  30. mainVC.isPresentedAsModal = isModal
  31. return mainVC
  32. }
  33. func updateUIViewController(_ uiViewController: UIViewController, context _: Context) {
  34. uiViewController.overrideUserInterfaceStyle = Storage.shared.appearanceMode.value.userInterfaceStyle
  35. }
  36. }
  37. // MARK: - Modal wrapper with navigation bar
  38. struct HomeModalView: View {
  39. @Environment(\.dismiss) private var dismiss
  40. var body: some View {
  41. NavigationView {
  42. HomeContentView(isModal: true)
  43. .navigationTitle("Home")
  44. .navigationBarTitleDisplayMode(.inline)
  45. .toolbar {
  46. ToolbarItem(placement: .navigationBarTrailing) {
  47. Button {
  48. dismiss()
  49. } label: {
  50. Image(systemName: "checkmark")
  51. }
  52. .foregroundColor(.blue)
  53. }
  54. }
  55. }
  56. .preferredColorScheme(Storage.shared.appearanceMode.value.colorScheme)
  57. }
  58. }
  59. // MARK: - Preview
  60. #Preview {
  61. HomeModalView()
  62. }