SwiftUI: Integration of dictionaries and quicklook

173 Views Asked by At

I'm seeking some help for my iOS app and hope someone can get me on the right path! FYI: I'm no developper, I'm an architectural student trying to work on a project for a museum.

So, I have this view which contains multiple cards for different objects that are in the museum and when I tap on it, I go to a new view with it's image, description,... + a PlayButton (the Playbutton is a vectorial triangle with some overlay for it's frame, background..).

I have it configured in a way for me to be able to add easily more objects with it corresponding informations with a dictionary.

struct Card: Identifiable {
let id = UUID()
var index: Int
var title: String
var subtitle: String
var description: String
var text: String
var image: String
var background: String
var logo: String }

var cards = [
Card(index: 1, title: "Universale".uppercased(), subtitle: "Joe Colombo".uppercased(), description: "Short description", text: "Long description", image: "Universale", background: "Background 5", logo: "Logo 5"),]

Then I call for this variable in my main view with this code to see every cards I want to show.

var featured: some View {
    ScrollView(.horizontal, showsIndicators: false){
        HStack(spacing: 16) {
            ForEach(cards) { cards in
                    CardsItem(cards: cards)
                        
                        .onTapGesture {
                            showCourse = true
                            selectedCourse = cards
                        }
                        .accessibilityElement(children: .combine)
                        .accessibilityAddTraits(.isButton)
                
            }
        }
        .padding(20)
    }
    .tabViewStyle(.page(indexDisplayMode: .never))
    .sheet(isPresented: $showCourse) {
        CardsView(namespace: namespace, cards: $selectedCourse, isAnimated: false)
    }
}

And my complete view is coded like this:

    var content: some View {
    ScrollView {
        scrollDetection
        
        Spacer()
            .padding(.top, 40)
        
        Text("Essayez les objets de la collection chez vous!")
            .font(.body.bold())
            .foregroundColor(.primary)
            .sectionTitleModifier()
          
        Group {
            
        featured
        
        
        Text("Porte-à-faux".uppercased())
            .font(.body.bold())
            .foregroundColor(.secondary)
            .sectionTitleModifier()

        
        featured2

        .padding(.bottom, 60)
        }
    }
    .coordinateSpace(name: "scroll")
    .overlay(NavigationBar(title: "Collection", contentHasScrolled: $contentHasScrolled))        
}

At this point, everything works like I want. But now I want to add a way for each object to access a usdz file and show it in AR QuickLook when I tap on the play button. I managed to get it working (Kinda, I currently need to kill the app if I want to go back afterwards) with one file that launches on every cards but I can't find a way to launch the corresponding usdz file for each object.

How would I approach this? I hope I was clear enough. Thanks in advance!

1

There are 1 best solutions below

0
On

Here's the code I used for the AR QuickLook

class ARQLViewController: UIViewController, QLPreviewControllerDataSource {
let assetName = "Pantone"
let assetType = "usdz"
let allowsContentScaling = false
let canonicalWebPageURL = URL(string: "nil")

override func viewDidAppear(_ animated: Bool) {
    let previewController = QLPreviewController()
    previewController.dataSource = self
    present(previewController, animated: true, completion: nil)
}

func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
    return 1
}

func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
    guard let path = Bundle.main.path(forResource: assetName, ofType: assetType) else {
        fatalError("Couldn't find the supported asset file.")
    }
    let url = URL(fileURLWithPath: path)
    let previewItem = ARQuickLookPreviewItem(fileAt: url)
    previewItem.allowsContentScaling = allowsContentScaling // default = true
    previewItem.canonicalWebPageURL = canonicalWebPageURL   // default = nil
    return previewItem
}}