{"id":3374,"date":"2023-10-29T15:16:55","date_gmt":"2023-10-29T19:16:55","guid":{"rendered":"https:\/\/michaelrowe01.com\/?p=3374"},"modified":"2023-10-29T15:17:14","modified_gmt":"2023-10-29T19:17:14","slug":"deletes-in-holiday-card-tracker","status":"publish","type":"post","link":"https:\/\/michaelrowe01.com\/index.php\/blog\/deletes-in-holiday-card-tracker\/","title":{"rendered":"Deletes in Holiday Card Tracker"},"content":{"rendered":"\n<p>I&#8217;ve been working on a Greeting Card tracking app, in my spare time for years now, five to be exact.  I may get it on the App Store one day, but primarily it is just used to track the cards my wife and I send to friends and family.  I&#8217;ve re-written it a few times, going from UIKit to SwiftUI, and then improving the Swift code to do CoreData with CloudKit syncing.  I am hoping to rewrite it sometime in the next year to fully SwiftData, but right now I am struggling with deleting cards correctly.<\/p>\n\n\n\n<p>Conceptually, deleting a child object in a parent-child relationship in CoreData is not hard.  You just have to delete the child and CoreData should handle the rest; however, I am showing all the children in a LazyVGrid, so that you can get a quick and easy overview of all the cards you have sent to a single recipient.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"660\" height=\"461\" data-attachment-id=\"3379\" data-permalink=\"https:\/\/michaelrowe01.com\/index.php\/blog\/deletes-in-holiday-card-tracker\/attachment\/screenshot-2023-10-29-at-15-14-39\/\" data-orig-file=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/10\/Screenshot-2023-10-29-at-15.14.39.png?fit=1583%2C1107&amp;ssl=1\" data-orig-size=\"1583,1107\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Screenshot-2023-10-29-at-15.14.39\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/10\/Screenshot-2023-10-29-at-15.14.39.png?fit=660%2C461&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/10\/Screenshot-2023-10-29-at-15.14.39.png?resize=660%2C461&#038;ssl=1\" alt=\"\" class=\"wp-image-3379\" srcset=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/10\/Screenshot-2023-10-29-at-15.14.39.png?resize=1024%2C716&amp;ssl=1 1024w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/10\/Screenshot-2023-10-29-at-15.14.39.png?resize=300%2C210&amp;ssl=1 300w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/10\/Screenshot-2023-10-29-at-15.14.39.png?resize=768%2C537&amp;ssl=1 768w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/10\/Screenshot-2023-10-29-at-15.14.39.png?resize=1536%2C1074&amp;ssl=1 1536w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/10\/Screenshot-2023-10-29-at-15.14.39.png?resize=150%2C105&amp;ssl=1 150w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/10\/Screenshot-2023-10-29-at-15.14.39.png?resize=214%2C150&amp;ssl=1 214w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/10\/Screenshot-2023-10-29-at-15.14.39.png?w=1583&amp;ssl=1 1583w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/10\/Screenshot-2023-10-29-at-15.14.39.png?w=1320&amp;ssl=1 1320w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/figure>\n\n\n\n<p>As you can see above, we have at least 6 cards visible at once, and each card will allow you to edit, view in detail, or delete.  Each of these &#8220;events&#8221; is a separate SwiftUI View, and on top of it is the MenuOverlayView:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>\/\/\n\/\/  MenuOverlayView.swift\n\/\/  Card Tracker\n\/\/\n\/\/  Created by Michael Rowe on 4\/16\/22.\n\/\/  Copyright \u00a9 2022 Michael Rowe. All rights reserved.\n\/\/\n\nimport SwiftUI\n\nstruct MenuOverlayView: View {\n    @Environment(\\.managedObjectContext) var moc\n    @Environment(\\.presentationMode) var presentationMode\n\n    @State var areYouSure: Bool = false\n    @State var isEditActive: Bool = false\n    @State var isCardActive: Bool = false\n\n    private let blankCardFront = UIImage(contentsOfFile: \"frontImage\")\n    private var iPhone = false\n    private var event: Event\n    private var recipient: Recipient\n\n    init(recipient: Recipient, event: Event) {\n        if UIDevice.current.userInterfaceIdiom == .phone {\n            iPhone = true\n        }\n        self.recipient = recipient\n        self.event = event\n    }\n\n    var body: some View {\n        HStack {\n            Spacer()\n            NavigationLink {\n                EditAnEvent(event: event, recipient: recipient)\n            } label: {\n                Image(systemName: \"square.and.pencil\")\n                    .foregroundColor(.green)\n                    .font(iPhone ? .caption : .title3)\n            }\n            NavigationLink {\n                CardView(\n                    cardImage: (event.cardFrontImage ?? blankCardFront)!,\n                    event: event.event ?? \"Unknown Event\",\n                    eventDate: event.eventDate! as Date)\n            } label: {\n                Image(systemName: \"doc.text.image\")\n                    .foregroundColor(.green)\n                    .font(iPhone ? .caption : .title3)\n            }\n            Button(action: {\n                areYouSure.toggle()\n            }, label: {\n                Image(systemName: \"trash\")\n                    .foregroundColor(.red)\n                    .font(iPhone ? .caption : .title3)\n            })\n            .confirmationDialog(\"Are you Sure\", isPresented: $areYouSure, titleVisibility: .visible) {\n                Button(\"Yes\", role: .destructive) {\n                    withAnimation {\n                        deleteEvent(event: event)\n                    }\n                }\n                Button(\"No\") {\n                    withAnimation {\n                    }\n                } .keyboardShortcut(.defaultAction)\n            }\n        }\n    }\n\n    private func deleteEvent(event: Event) {\n        let taskContext = moc\n        taskContext.perform {\n            taskContext.delete(event)\n            do {\n                try taskContext.save()\n            } catch {\n                let nsError = error as NSError\n                fatalError(\"Unresolved error \\(nsError), \\(nsError.userInfo)\")\n            }\n        }\n    }\n}\n\n<\/code><\/pre>\n\n\n\n<p>It&#8217;s a pretty simple view, with a menu overlay to Edit, View, or delete.  The delete function just calls CoreData&#8217;s delete() and then save() methods on the current managedObjectContext.  No problems here.  <\/p>\n\n\n\n<p>The problem is actually in the parent view&#8230;I won&#8217;t show all the code, but will show a simplified version of the LazyVGrid:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>LazyVGrid(columns: gridLayout, alignment: .center, spacing: 5) {\n                        ForEach(events, id: \\.self) { event in\n                            HStack {\n                                VStack {\n                                    Image(uiImage: (event.cardFrontImage ?? blankCardFront)!)\n                                        .resizable()\n                                        .aspectRatio(contentMode: .fit)\n                                        .scaledToFit()\n                                        .frame(width: iPhone ? 120 : 200, height: iPhone ? 120 : 200)\n                                        .padding(.top, iPhone ? 2: 5)\n                                    HStack {\n                                        VStack {\n                                            Text(\"\\(event.event ?? \"\")\")\n                                                .foregroundColor(.green)\n                                            Spacer()\n                                            HStack {\n                                                Text(\"\\(event.eventDate ?? NSDate(), formatter: ViewEventsView.eventDateFormatter)\")\n                                                    .fixedSize()\n                                                    .foregroundColor(.green)\n                                                MenuOverlayView(recipient: recipient, event: event)\n                                            }\n                                        }\n                                        .padding(iPhone ? 1 : 5)\n                                        .font(iPhone ? .caption : .title3)\n                                        .foregroundColor(.primary)\n                                    }\n                                }\n                            }\n                            .padding()\n                            .frame(minWidth: iPhone ? 160 : 320, maxWidth: .infinity,\n                                   minHeight: iPhone ? 160 : 320, maxHeight: .infinity)\n                            .background(Color(UIColor.systemGroupedBackground))\n                            .mask(RoundedRectangle(cornerRadius: 20))\n                            .shadow(radius: 5)\n                            .padding(iPhone ? 5: 10)\n                        }<\/code><\/pre>\n\n\n\n<p>As you can see it just displays all the events, where each event is a card.  Well since the delete method is in the MenuOverlayView, as defined above.  So what happens is when the overlay delete occurs, I should pass back a message to reload the events in the LazyVGrid. But I don&#8217;t have that working, as I can&#8217;t figure out the correct way to do this.<\/p>\n\n\n\n<p>At least this week, I was able to update my WastedTime in TestFlight to see if I have fixed an issue with the Gauge Complication not displaying the data correctly.  So far so goo. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working on a Greeting Card tracking app, in my spare time for years now, five to be exact. I may get it on the App Store one day, but primarily it is just used to track the cards my wife and I send to friends and family. I&#8217;ve re-written it a few times, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"hide_page_title":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[2,3],"tags":[592,594,117,527],"class_list":["post-3374","post","type-post","status-publish","format-standard","hentry","category-blog","category-personal-softwareandit","tag-bug","tag-card-tracker","tag-development","tag-swiftui"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2aMa8-Sq","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts\/3374","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/comments?post=3374"}],"version-history":[{"count":1,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts\/3374\/revisions"}],"predecessor-version":[{"id":3380,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts\/3374\/revisions\/3380"}],"wp:attachment":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/media?parent=3374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/categories?post=3374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/tags?post=3374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}