import SwiftUI
import VisionKit
import UIKit
struct ContentView: View {
@State private var image: UIImage?
@State private var selectedTab = 0
@State private var showingCameraPicker = false
@State private var showingCameraRollPicker = false
@State private var groups: [String: [UIImage]] = [:]
@State private var selectedGroup: String?
@State private var groupName = ""
@State private var showingCreateGroup = false
@State private var showingGroupSelection = false
@State private var showAlert = false
@State private var alertMessage = ""
@State private var complementaryColor: Color?
@State private var showMatches = false // Control to show matched images with white borders
@State private var isShowingImageView = false // State variable to control the image display view
@State private var editedImage: UIImage? // State variable to hold the edited image
var body: some View {
TabView(selection: $selectedTab) {
NavigationView {
VStack {
Button("Open Camera", action: openCamera)
.padding()
Button("Open Camera Roll", action: openCameraRoll)
.padding()
// Display the edited image above the navigation bar
if let editedImage = editedImage {
Image(uiImage: editedImage)
.resizable()
.scaledToFit()
.padding()
}
}
.padding()
.navigationBarTitle("Home") // Set the navigation title
}
.tabItem {
Label("Home", systemImage: "house")
}
.tag(0)
NavigationView {
GroupList(groups: $groups, selectedGroup: $selectedGroup)
.navigationBarTitle("Groups")
.navigationBarItems(trailing:
Button("Create Group", action: {
showingCreateGroup = true
})
)
}
.tabItem {
Label("Groups", systemImage: "folder")
}
.tag(1)
}
.sheet(isPresented: $showingCameraPicker, onDismiss: {
if let _ = image {
print("Camera picker dismissed with selected image. Image variable after selecting:", image)
isShowingImageView = true
} else {
print("Camera picker dismissed without selecting an image.")
}
}) {
ImagePicker(image: self.$image, sourceType: .camera)
}
.sheet(isPresented: $showingCameraRollPicker, onDismiss: {
if let _ = image {
print("Camera roll picker dismissed with selected image. Image variable after selecting:", image)
isShowingImageView = true
} else {
print("Camera roll picker dismissed without selecting an image.")
}
}) {
ImagePicker(image: self.$image, sourceType: .photoLibrary)
}
.sheet(isPresented: $showingCreateGroup) {
CreateGroupView(groupName: $groupName, groups: $groups, showingCreateGroup: $showingCreateGroup)
}
.sheet(isPresented: $showingGroupSelection) {
GroupSelectionView(groups: $groups, selectedGroup: $selectedGroup, image: image, showingGroupSelection: $showingGroupSelection)
}
.alert(isPresented: $showAlert) {
Alert(title: Text("No color matches"), message: Text(alertMessage), dismissButton: .default(Text("OK")))
}
.sheet(isPresented: $isShowingImageView) {
if let image = image {
ImageViewControllerWrapper(image: image)
.onLongPressGesture {
self.editedImage = image
print("Original image long-pressed. Assigning to editedImage.")
isShowingImageView = false // Close the ImageView
}
}
}
.onChange(of: image) { newImage, _ in
}
.onAppear {
// Print statements for debugging
print("ContentView appeared.")
}
.onDisappear {
// Print statements for debugging
print("ContentView disappeared.")
}
}
func openCamera() {
showingCameraPicker = true
}
func openCameraRoll() {
showingCameraRollPicker = true
}
}
class ImageViewController: UIViewController, ImageAnalysisInteractionDelegate {
var imageView: UIImageView!
var image: UIImage? {
didSet {
imageView?.image = image
}
}
override func viewDidLoad() {
super.viewDidLoad()
imageView = UIImageView()
imageView.contentMode = .scaleAspectFit
imageView.isUserInteractionEnabled = true
print("User interaction enabled:", imageView.isUserInteractionEnabled)
view.addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
imageView.topAnchor.constraint(equalTo: view.topAnchor),
imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
imageView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
// Configure ImageAnalysisInteraction
let interaction = ImageAnalysisInteraction()
interaction.delegate = self
interaction.preferredInteractionTypes = [.imageSubject]
imageView.addInteraction(interaction)
}
// Implement delegate method to handle subject selection
func subject(_ interaction: ImageAnalysisInteraction, didSelect subject: ImageAnalysisInteraction.Subject) {
// Handle the selection of the subject here
print("Selected subject:", subject)
}
// Additional debugging
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("ImageViewController appeared")
}
}
struct ImageViewControllerWrapper: UIViewControllerRepresentable {
var image: UIImage?
func makeUIViewController(context: Context) -> ImageViewController {
let viewController = ImageViewController()
viewController.image = image
return viewController
}
func updateUIViewController(_ uiViewController: ImageViewController, context: Context) {
uiViewController.image = image
}
}