Posts

Post not yet marked as solved
0 Replies
619 Views
Rather than selecting from a list, I'm trying to navigate between drawings like a book by using buttons to cycle through, but the canvas doesn't update. I'm following the great tutorial by DevTechie for PencilKit in SwiftUI with CoreData. The github repo is at [https://github.com/devtechie/DrawingDocuments]() Here's my ContentView and my version of the DrawingWrapper. The DrawingWrapper uses a DrawingManager (SwiftUI) to pull from CoreData and the DrawingViewController to define a PKCanvas. I wasn't sure which delegate to use and really struggling understanding how to refresh the canvas. ContentView struct ContentView: View { &#9;@StateObject var manager = DrawingManager() &#9;@State var addNewShown = false &#9;@State var pageNumber: Int = 0 &#9;@State var newVar = UUID() &#9; &#9;var body: some View { &#9; VStack{ &#9;&#9;&#9;Text(manager.docs[pageNumber].name!) &#9;&#9;&#9;HStack{ &#9;&#9;&#9;&#9;Button(action:{ &#9;&#9;&#9;&#9;&#9;pageNumber -= 1 &#9;&#9;&#9;&#9;&#9;newVar = manager.docs[pageNumber].id! &#9;&#9;&#9;&#9;&#9;//desiredDoc = manager.docs[pageNumber] &#9;&#9;&#9;&#9;}){ &#9;&#9;&#9;&#9;&#9;Image(systemName: "chevron.left") &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;Spacer() &#9;&#9;&#9;&#9;Button(action:{ &#9;&#9;&#9;&#9;&#9;pageNumber += 1 &#9;&#9;&#9;&#9;&#9;newVar = manager.docs[pageNumber].id! &#9;&#9;&#9;&#9;&#9;//desiredDoc = manager.docs[pageNumber] &#9;&#9;&#9;&#9;}){ &#9;&#9;&#9;&#9;&#9;Image(systemName: "chevron.right") &#9;&#9;&#9;&#9;} &#9;&#9;&#9;} &#9; } } DrawingWrapper struct DrawingWrapper: UIViewControllerRepresentable { &#9;var manager: DrawingManager &#9;@Binding var doc: DrawingDoc &#9; &#9;typealias UIViewControllerType = DrawingViewController &#9; &#9;class Coordinator: NSObject, PKCanvasViewDelegate { &#9;&#9;var parent: DrawingWrapper &#9;&#9;init(_ parent: DrawingWrapper){ &#9;&#9;&#9;self.parent = parent &#9;&#9;} &#9;&#9; &#9;&#9;func canvasViewDidFinishRendering(_ canvasView: PKCanvasView) { &#9;&#9;&#9;if let uiDrawing = canvasView.drawing as? PKDrawing { &#9;&#9;&#9;&#9;parent.doc.data = uiDrawing.dataRepresentation() &#9;&#9;&#9;} &#9;&#9;} &#9;} &#9; &#9;func makeCoordinator() -> Coordinator { &#9;&#9;Coordinator(self) &#9;} &#9; &#9;func makeUIViewController(context: UIViewControllerRepresentableContext<DrawingWrapper>) -> DrawingWrapper.UIViewControllerType { &#9;&#9;let viewController = DrawingViewController() &#9;&#9;viewController.drawingData = doc.data! &#9;&#9;viewController.drawingChanged = {data in &#9;&#9;&#9;manager.update(data: data, for: doc.id!) &#9;&#9;} &#9;&#9;viewController.delegate = context.coordinator &#9;&#9;return viewController &#9;} &#9; &#9;func updateUIViewController(_ uiViewController: DrawingViewController, context: UIViewControllerRepresentableContext<DrawingWrapper>) { &#9;&#9;uiViewController.drawingData = doc.data! &#9;} }
Posted
by dep3.
Last updated
.
Post not yet marked as solved
0 Replies
546 Views
I now have a view where I need to display thousands of results from Core Data and I need to figure out how to handle the delay; either by optimizing the code, adding a ProgressView() or some other option. What is the best way to let the user know something is loading, or better yet, avoid the delay? When I click on a button, there is an obvious and unwanted delay before it pushes to the new view displaying all the data. I've tried LazyVStack, but since I'm loading by sections, the view jumps around as data is loaded as the user scrolls. I thought maybe using a technique similar to loading infinite lists, but can't figure out how that would work with a Core Data fetch and the sections/foreaches. I also thought maybe using a ProgressView() but I can't figure out how to know that the results from the Core Data fetch have fully loaded. Code snippet below the example (sorry if I missed a } or a )). I have categories of grouped items and want the view to look something like this: Category 1 Group 1-1 Item 1-1-1 ... Item 1-1-n Group 1-2 Item 1-2-1 ... Item 1-2-n Category 2 Group 2-1 Item 2-1-1 and so on BigView var body: some View { &#9;&#9;&#9;&#9;&#9;&#9;ScrollView{ &#9;&#9;&#9;&#9;&#9;&#9;//categoryList is a [String] of Category Names and each Group has an attribute called CategoryName &#9;&#9;&#9;&#9;&#9;&#9;&#9;ForEach(categoryList, id:\.self){category in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Section(header: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text(category) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;){ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;VStack{ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;//Group is an entity in Core Data, and the fetchedEntity with a predicate on CategoryName &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;ForEach(groups){group in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;BigSubView(group: Group) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;} BigSubView @ObservedObject var group: Group var body: some View { &#9;&#9;&#9;&#9;Section(header: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;HStack{ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text(group.wrappedName) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;}.background(Color(UIColor.systemFill))){ &#9;&#9;&#9; &#9;&#9;&#9;&#9;VStack{ &#9;&#9;&#9;&#9;//A Group has a one-many relationship with Item entity in Core Data, I convert that to an [Item] &#9;&#9;&#9;&#9;&#9;ForEach(group.itemsArray, id:\.self){item in &#9;&#9;&#9;&#9;&#9;&#9;&#9;Text(item.wrappedName) &#9;&#9;&#9;&#9;} &#9;&#9;&#9;} &#9;&#9;} &#9;}
Posted
by dep3.
Last updated
.
Post not yet marked as solved
4 Replies
1.2k Views
I'm trying to filter such that criteria is met for each AND every item in a set vice each OR every item. If I use groceryItem IN %@, desiredGroceries it acts like an OR statement. Example: I'm trying to view all grocery lists that included milk AND eggs (defined in desiredGroceries Set). If I use the above code, it returns all lists that included milk as well as lists that included eggs. But I only want the lists that include both milk and eggs. What's the correct Predicate for this?
Posted
by dep3.
Last updated
.
Post marked as solved
2 Replies
2.3k Views
I am attempting to parse XML using an URLSession, XCode 12, SwiftUI but it keeps returning [] or nil. If I print immediately after the parse(see code), all the data is there, but for some reason, it seems to be clearing it all out. If I try it with a .xml file, the code works fine, so it must be something in my URLSession in a XMLParserDelegate class: class ParseController: NSObject, XMLParserDelegate{ var items: [Item] = [] var itemStore: [Item]? func loadData() {&#9;&#9; &#9;&#9;let url = URL(string: "website")!&#9;&#9; &#9;&#9;let request=URLRequest(url: url) &#9;&#9; &#9;&#9;let task = URLSession.shared.dataTask(with: request) { (data, response, error) in &#9;&#9;&#9;if data == nil { &#9;&#9;&#9;&#9;print("dataTaskWithRequest error: \(String(describing: error?.localizedDescription))") &#9;&#9;&#9;&#9;return &#9;&#9;&#9;} &#9;&#9;&#9; &#9;&#9;&#9;let parser = XMLParser(data: data!) &#9;&#9;&#9;parser.delegate=self &#9;&#9;&#9;parser.parse() &#9;&#9;&#9; &#9;&#9;&#9;self.itemStore=self.items &#9;&#9;&#9;print(self.itemStore) &#9;&#9;} &#9;&#9;task.resume() &#9;&#9; &#9;&#9;/* &#9;&#9;if let path = Bundle.main.url(forResource: "Items", withExtension: "xml") { &#9;&#9;&#9;&#9;if let parser = XMLParser(contentsOf: path) { &#9;&#9;&#9;&#9;&#9;&#9;parser.delegate = self &#9;&#9;&#9;&#9;&#9;&#9;parser.parse() &#9;&#9;&#9;&#9;&#9;self.itemStore=self.items &#9;&#9;&#9;&#9;} &#9;&#9;} &#9;&#9;*/ &#9;} And then I call that with a button in my View: struct MyParserView: View { &#9;@State var itemsResult: [Item]? &#9; &#9;var body: some View { &#9;&#9;if ((itemsResult?.isEmpty) == nil) { &#9;&#9;&#9;VStack { &#9;&#9;&#9;&#9;Text("Stuff here") &#9;&#9;&#9;&#9;Button(action: { &#9;&#9;&#9;&#9;&#9;let parserControl = ParseController() &#9;&#9;&#9;&#9;&#9;parserControl.loadData() &#9;&#9;&#9;&#9;&#9;itemsResult = parserControl.itemStore &#9;&#9;&#9;&#9;}){ &#9;&#9;&#9;&#9;&#9;Text("Push") &#9;&#9;&#9;&#9;} &#9;&#9;&#9;} &#9;&#9;}else { &#9;&#9;&#9;List{ &#9;&#9;&#9;&#9;ForEach(itemResult!, id:\.id){item in &#9;&#9;&#9;&#9;&#9;Text(item.name) &#9;&#9;&#9;&#9;} &#9;&#9;&#9;} &#9;&#9;} &#9;} }
Posted
by dep3.
Last updated
.
Post not yet marked as solved
0 Replies
612 Views
Using XCode12, beta 4, I am trying to separate a header area and a ScrollView. I've tried both VStack and GeometryReader; however, when I click in the header area, the navigation link in the ScrollView item beneath it is triggered. If I use a list, this undesired behavior is not observed. If I use the ScrollView in XCode 11.6 and build for iOS13, this undesired behavior is also not observed. Did something change with ScrollViews in VStacks or GeometryReaders in iOS14? struct ContentView: View {   var body: some View {    NavigationView{     //GeometryReader { geo in      VStack{       VStack{        Text("Blah")       }//.frame(height: geo.size.height*0.20)              VStack {        ScrollView {         ForEach(0..<10) { i in          NavigationLink(destination: Text("\(i)")) {           cardRow(i: i)            .frame(maxWidth: .infinity)            .foregroundColor(Color(UIColor.label))            .padding()            .background(RoundedRectangle(cornerRadius: 12))            .foregroundColor(Color(UIColor.secondarySystemFill))            .padding()          }         }        }       }//.frame(height: geo.size.height*0.90)            }    }.navigationViewStyle(StackNavigationViewStyle())   }        struct cardRow: View {    var i: Int         var body: some View {     HStack {      VStack {       Text("Www")       Text("88")      }.hidden()      .overlay(       VStack{        Text("\(i)")       }      )      Divider()       .background(Color(UIColor.label))      Spacer()     }    }   } }
Posted
by dep3.
Last updated
.