Home > Net >  Updating a UUID with another UUID in SwiftUI
Updating a UUID with another UUID in SwiftUI

Time:01-03

I am not sure if this is even possible. I still new to SwiftUI development.

So here is what I am trying to accomplish.

I am using a drag and drop swift package that allows an item to be dragged and drop from one location to a specific spot.

It uses a UUID in order to drag from one location(holding location) to another.

However the problem I am running into is that if I use just one item IE Text or an Image it's fine and operates accordingly.

However, I am using a grid that has multiple items that are populated from a data model.

// MARK: - Rune MODEL

struct Rune: Codable, Identifiable, Comparable  {
    static func < (lhs: Rune, rhs: Rune) -> Bool {
        lhs.title < rhs.title
    }
    
    let runeid = UUID()
    var id: Int
    var title: String
    var headline: String
    var image: String
    var pronunciation: String
    var sound: String
    var aett: String
    var god: String
    var knownas: [String]
    var keywords: [String]
    var translation: String
    var magick: [String]
    var gallery: [String]
    var meanings: [String]
    var runelore: [String]
    var poeticedda: [String]
    
    
}

Each item has its own UUID and that works great.

In the view I loop through each item that is in the data model populated from a JSON file and all is well.

Each item is displayed in the grid.

   LazyVGrid(columns: columns, spacing: 5) {
                                        ForEach(runes) { rune in

                                            RuneFrontView(rune: rune, width: width, height: height, degree: $frontDegree)

                                            RuneBackView(rune: rune, width: width, height: height, degree: $backDegree)
                                            
                                            switch rune.id {
                                            case 1:
                                                Text("rune 1")
//                                                runeofday = rune.runeid

                                            case 2:
                                                Text("rune 2")
//                                              runeofday = rune.runeid

                                            case 3:
                                                Text("rune 3")
//                                               runeofday = rune.runeid
                                            case 4:
                                                Text("rune 4")
//                                                runeofday = rune.runeid
                                            case 5:
                                                Text("rune 5")
//                                                runeofday = rune.runeid
                                            case 6:
                                                Text("rune 6")
//                                                runeofday = rune.runeid

                                            default:
                                                Text("rune default")
                                            } //:END OF SWITCH

                                        }.onTapGesture {
                                            flipCard ()
                                        }//: END OF ON TAP
                                    }//: END OF LAZY V GRID

In the drag and drop package that I am using it has 2 views a drop (receiving location) and a drag (from location where all of the items in the grid are at).

In the drop view it requires a UUID associated with the item that is being dragged into that drop zone. This will also allow me to get what was dropped so I can use it later.

DragView(**id: runeofday**) { dragInfo in
                                Text(dragInfo.isDragging ? "Im being dragged" : "Drag me 1")
                                        .padding()
                                        .background{
                                            Color.mint
                                        } //:END OF BACK GROUND

I have declared a variable called runeofday that I need to be able to update with the UUID that is already assigned to each item based on the ID of that item.

This means that I have 6 Runes and each has a UUID called runeid and I want to update the runeoftheday variable with the rune.runeid UUID value and feed that to DragView/drop zone(which is required to be a UUID).

If there is a better way of handling not using this package I am happy to change logic.

My end goal is to create a drop zone where an item can be dragged into and I can capture what was dropped in there to populate data downstream. I am using images that will show as blank and after they drop the image in the drop zone they them can tap on the item and it flips it over.

Hopefully this makes sense.

I have tried different variable types and have been unable to set the runeofday variable and then update it to another UUID.

CodePudding user response:

I actually ended up restructuring the code and not using the case statement. I was able to pass each of the UUID's from the pre-established data model.

LazyVGrid(columns: columns, spacing: 10) {
                                    
      ForEach(runes.shuffled()) { rune in
                                        
           if viewRouter.runeofDayUseable {
               DragView(id: rune.runeid) { dragInfo in
                                                
                 RuneFlipView(rune: rune, isFlipable: true)

              } //: END OF DRAG VIEW
          .onDraggingEndedAction { isSuccessfullDrop in
                viewRouter.runeofDay = rune.id                                                viewRouter.runeofDayUseable.toggle()
            } //: END OF ON DRAGGING ENDED
      } //: END OF IF
    } //: END OF FOR
  } //: END OF LAZY V GRID
  • Related