Post

Replies

Boosts

Views

Activity

Reply to Siri Shortcuts with SwiftUI and Core Data
I tried to observe NSPersistentStoreRemoteChange like this: .onReceive(NotificationCenter.default.publisher(for: .NSPersistentStoreRemoteChange)) { notification in DispatchQueue.main.async { let predicate = items.nsPredicate items.nsPredicate = nil items.nsPredicate = NSPredicate(value: true) items.nsPredicate = predicate } } but the operation that I execute in the closure isn't efficient and gives me an issue: Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’21
Reply to PersistenceController and CloudKit
struct PersistenceController { static let shared = PersistenceController() let container: NSPersistentCloudKitContainer init() { container = NSPersistentCloudKitContainer(name: "MJ") guard let fileContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.Jad.MJ")?.appendingPathComponent("MJ.sqlite") else { fatalError("Shared file container could not be created.") } let storeDescription = NSPersistentStoreDescription(url: fileContainer) storeDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) storeDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) storeDescription.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "YOUR containerIdentifier") container.persistentStoreDescriptions = [storeDescription] container.loadPersistentStores(completionHandler: { (storeDescription, error) in if let error = error as NSError? { fatalError("Unresolved error \(error), \(error.userInfo)") } }) container.viewContext.automaticallyMergesChangesFromParent = true container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’21
Reply to Add Text to an animated object
Or maybe this: struct ContentView: View { @State private var flipped = false var body: some View { Group { ZStack{ RoundedRectangle(cornerRadius: 20) .frame(width: 140, height: 170) .foregroundColor(flipped ? .red : .orange) .padding() Text("Hello, world!") .rotation3DEffect(.degrees(flipped ? 180 : 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0))) } .rotation3DEffect(Angle(degrees: flipped ? 180 : 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0))) .onTapGesture { withAnimation(.linear(duration: 1)) { flipped.toggle() } } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’22
Reply to Add Text to an animated object
struct ContentView: View { @State private var flipped = false var body: some View { Group { RoundedRectangle(cornerRadius: 20) .frame(width: 140, height: 170) .foregroundColor(flipped ? .yellow : .purple) .padding() .overlay(Text("Hello, world!").rotation3DEffect(Angle(degrees: flipped ? 180 : 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0))).opacity(flipped ? 1 : 0)) .rotation3DEffect(flipped ? Angle(degrees: 180): Angle(degrees: 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0))) .animation(.default, value: flipped) .onTapGesture { flipped.toggle() } } } } Is this ok?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’22
Reply to Add Text to an animated object
struct ContentView: View { @State private var flipped = false var body: some View { Group { RoundedRectangle(cornerRadius: 20) .frame(width: 140, height: 170) .foregroundColor(flipped ? .red : .orange) .padding() .overlay(Text("Hello, world!").rotation3DEffect(Angle(degrees: flipped ? 180 : 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0))).opacity(flipped ? 1 : 0)) .rotation3DEffect(flipped ? Angle(degrees: 180): Angle(degrees: 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0))) .animation(.default, value: flipped) .onTapGesture { flipped.toggle() } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’22
Reply to Flip animation in SwiftUI
I think that the solution is in your previous thread, anyway here is the solution: struct ContentView: View { @State private var flip: Bool = false var body: some View { Group { Button { withAnimation { flip.toggle() } } label: { Rectangle() .frame(width: 140, height: 170) .foregroundColor(Color(.systemTeal)) .cornerRadius(20) .rotation3DEffect(.degrees(flip ? 180 : 0), axis: (x: 0, y: 1, z: 0)) } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’22
Reply to .onDelete resets NSPredicate (Core Data, SwiftUI)
I found a solution. @FetchRequest( entity: LifetimeInputs.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \LifetimeInputs.date, ascending: true)], predicate: nil ) var lifetimeInputsModel: FetchedResults<LifetimeInputs> Becomes this (in my case) @FetchRequest var lifetimeInputs: FetchedResults<LifetimeInputs> init(nsPredicate: NSPredicate, sortDescriptors: [NSSortDescriptor]) { let entity = LifetimeInputs.entity() let fetchRequest = FetchRequest<LifetimeInputs>(entity: entity, sortDescriptors: sortDescriptors, predicate: nsPredicate, animation: .default) self._lifetimeInputs = fetchRequest } The nsPredicate and the sortDescriptors must be passed to a parent view and you have to change/update (if you need to) them in that parent view. Hope it works also for you
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’22
Reply to Need a help with my app
Try this: import SwiftUI struct Card: Identifiable{ var id = UUID() var content: String var isFacedUp: Bool = false var isMatched = false } class ViewModel: ObservableObject{ @Published var cards: [Card] = [ Card(content: "😀"), Card(content: "😀"), Card(content: "😘"), Card(content: "🥶"), Card(content: "😡"), Card(content: "🥶"), Card(content: "😘"), Card(content: "😡") ] func tapped(_ card: Card){ if let chosenIndex = cards.firstIndex(where: { $0.id == card.id}), !cards[chosenIndex].isFacedUp, !cards[chosenIndex].isMatched{ if let potenialMatcehIndex = cards.indices.filter({ cards[$0].isFacedUp}).oneAndOnly{ print("ONE") if cards[chosenIndex].content == cards[potenialMatcehIndex].content{ print("TWO") cards[potenialMatcehIndex].isMatched = true cards[chosenIndex].isMatched = true } cards[chosenIndex].isFacedUp = true } else{ print("THREE") cards.indices.forEach({cards[$0].isFacedUp = ($0 == chosenIndex)}) } } } } struct ContentView: View { let columns = [GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())] @StateObject var viewModel = ViewModel() var body: some View { LazyVGrid(columns: columns) { ForEach(viewModel.cards) { card in RoundedRectangle(cornerRadius: 20) .frame(width: 70, height: 70) .foregroundColor(card.isFacedUp || card.isMatched ? Color(.systemIndigo) : .purple) .padding() .overlay(Text(card.content).font(.system(size: 30)).rotation3DEffect(Angle(degrees: card.isFacedUp || card.isMatched ? 180 : 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0))).opacity(card.isFacedUp || card.isMatched ? 1 : 0)) .rotation3DEffect(card.isFacedUp || card.isMatched ? Angle(degrees: 180): Angle(degrees: 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0))) .animation(.default, value: card.isFacedUp) .onTapGesture { viewModel.tapped(card) } } } } } extension Array{ var oneAndOnly: Element?{ if self.count == 1{ return self.first } else{ return nil } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’22
Reply to Keep face up the cards if equal
import SwiftUI struct Card: Identifiable{ var id = UUID() var content: String var isFacedUp: Bool = false var isMatched = false } class ViewModel: ObservableObject{ @Published var cards: [Card] = [ Card(content: "😀"), Card(content: "😀"), Card(content: "😘"), Card(content: "🥶"), Card(content: "😡"), Card(content: "🥶"), Card(content: "😘"), Card(content: "😡") ] func tapped(_ card: Card){ if let chosenIndex = cards.firstIndex(where: { $0.id == card.id}), !cards[chosenIndex].isFacedUp, !cards[chosenIndex].isMatched{ if let potenialMatcehIndex = cards.indices.filter({ cards[$0].isFacedUp}).oneAndOnly{ print("ONE") if cards[chosenIndex].content == cards[potenialMatcehIndex].content{ print("TWO") cards[potenialMatcehIndex].isMatched = true cards[chosenIndex].isMatched = true } cards[chosenIndex].isFacedUp = true } else{ print("THREE") cards.indices.forEach({cards[$0].isFacedUp = ($0 == chosenIndex)}) } } } } struct ContentView: View { let columns = [GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())] @StateObject var viewModel = ViewModel() var body: some View { LazyVGrid(columns: columns) { ForEach(viewModel.cards) { card in RoundedRectangle(cornerRadius: 20) .frame(width: 70, height: 70) .foregroundColor(card.isFacedUp || card.isMatched ? Color(.systemIndigo) : .purple) .padding() .overlay(Text(card.content).font(.system(size: 30)).rotation3DEffect(Angle(degrees: card.isFacedUp || card.isMatched ? 180 : 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0))).opacity(card.isFacedUp || card.isMatched ? 1 : 0)) .rotation3DEffect(card.isFacedUp || card.isMatched ? Angle(degrees: 180): Angle(degrees: 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0))) .animation(.default, value: card.isFacedUp) .onTapGesture { viewModel.tapped(card) } } } } } extension Array{ var oneAndOnly: Element?{ if self.count == 1{ return self.first } else{ return nil } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’22
Reply to Keep face up the cards if equal
@Published var cards: [Card] = [ Card(content: "😀"), Card(content: "😀"), Card(content: "😘"), Card(content: "🥶"), Card(content: "😡"), Card(content: "🥶"), Card(content: "😘"), Card(content: "😡") ] Represents all your cards, in this case 8 cards in the array
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’22