Post

Replies

Boosts

Views

Activity

Reply to Opening a New Document from File URL?
Taking a look at openDocument, it says it can open document at a specific URL, provided that its bookmark is resolved. And it does open a file with security-scoped bookmark. func loadBookmarks() async { for bookmarkItem in bookmarkViewModel.bookmarkItems { // resolving a bookmark if let _ = resolveBookmark(bookmarkData: bookmarkItem.bookmarkData) { do { try await openDocument(at: bookmarkItem.bookmarkURL) } catch { print("\(error.localizedDescription)") } } } } struct BookmarkItem: Codable, Hashable { let bookmarkURL: URL let date: Date let bookmarkData: Data let open: Bool }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25
Reply to onReceive(_:perform:) on Frontmost Window Only?
In reference to this article ( https://nilcoalescing.com/blog/ProvidingTheCurrentDocumentToMenuCommands/ ), you can do it with @FocusedBinding. I don't even know what this @FocusedBinding guy is. It's been around since macOS 11? Ugh... import SwiftUI @main struct FocusedBindingCrazyMamaApp: App { var body: some Scene { DocumentGroup(newDocument: FocusedBindingCrazyMamaDocument()) { file in ContentView(document: file.$document) .focusedSceneValue(\.focusedDocument, file.$document) } .commands { CommandGroup(replacing: .saveItem) { UppercasedText() LowercasedText() } } } } extension FocusedValues { struct DocumentFocusedValues: FocusedValueKey { typealias Value = Binding<FocusedBindingCrazyMamaDocument> } var focusedDocument: Binding<FocusedBindingCrazyMamaDocument>? { get { self[DocumentFocusedValues.self] } set { self[DocumentFocusedValues.self] = newValue } } } struct UppercasedText: View { @FocusedBinding(\.focusedDocument) var focusedDocument var body: some View { Button { guard let text = focusedDocument?.text else { return } focusedDocument?.text = text.uppercased() } label: { Text("Uppercase Text") } .disabled(focusedDocument == nil) .keyboardShortcut("u", modifiers: [.command, .shift]) } } struct LowercasedText: View { @FocusedBinding(\.focusedDocument) var focusedDocument var body: some View { Button { guard let text = focusedDocument?.text else { return } focusedDocument?.text = text.lowercased() } label: { Text("Lowercase Text") } .disabled( focusedDocument == nil ) .keyboardShortcut("l", modifiers: [.command, .shift]) } } // ContentView.swift // import SwiftUI struct ContentView: View { @Binding var document: FocusedBindingCrazyMamaDocument var body: some View { TextEditor(text: $document.text) .foregroundStyle(.white) .font(.system(size: 24)) .scrollContentBackground(.hidden) .background(.black) } } #Preview { ContentView(document: .constant(FocusedBindingCrazyMamaDocument())) }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25
Reply to WidgetKit with Data from CoreData
A couple of articles that I have read suggest that I get CoreData records from my DataManager like the following. It's kind of redundant as it accesses DataManager inside the TimelineProvider struct and also inside the Widget struct. That's why I wonder if this is the right approach. import WidgetKit import SwiftUI import CoreData struct Provider: TimelineProvider { func placeholder(in context: Context) -> SimpleEntry { ... } func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) { ... } func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) { let manager = CloudDataManager() var records: [Little] = [] let request = NSFetchRequest<Little>(entityName: "Little") do { records = try manager.context.fetch(request) records.sort { lhs, rhs in lhs.trashDate! < rhs.trashDate! } } catch { print("Fetch error for CloudDataManager: \(error.localizedDescription)") } var items: [Item] = [] for record in records { let item = Item(trashDate: record.trashDate ?? Date.now, imageSelection: Int(record.imageSelection)) items.append(item) } let entry = Timeline(entries: [SimpleEntry(date: Date(), items: items)], policy: .atEnd) completion(entry) } } struct SimpleEntry: TimelineEntry { let date: Date let items: [Item] } struct LittleGuyEntryView : View { var entry: Provider.Entry var body: some View { ZStack { ContainerRelativeShape() .fill(.widgetBackground) VStack { if entry.items.count > 0 { ForEach(entry.items, id: \.id) { item in ... } } } .padding() } } } struct LittleGuy: Widget { let manager = DataManager() let kind: String = "App name" var body: some WidgetConfiguration { StaticConfiguration(kind: kind, provider: Provider()) { entry in LittleTrashEntryView(entry: entry) .environment(\.managedObjectContext, manager.context) .containerBackground(.fill.tertiary, for: .widget) } .contentMarginsDisabled() .supportedFamilies([.systemSmall]) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25
Reply to WidgetKit with Data from CoreData
Thanks for the tip, darkpaw. But I'm afraid you misunderstand my question. It's underlined. Do I have to let the widget pull data directly from CoreData data manager (DataManager)? In other words, is there any way I could use my existing view model (ViewModel) to deliver data to the widget? You seem to be writing your fetchRecords() method to get your data inside the getTimeline() method, i.e.: That should be put into your DataManager() class. Okay. Thanks. I have done all dirty work through my view model (ViewModel) including the data sorting. That's why I want to know if I need to do it with the data manager, again, just for the widget. That's what I mean by 'redundant' or superfluous.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25
Reply to NWPathMonitor Failing
Hello. Thank you for your feedback, The Eskimo. It's well noted. I'll take a deeper look. I check the network connection with it (NWPathMonitor) so that the users are able to access App Store to make in-app purchases. If they are not connected, I just show some text with a progress guy, never showing the purchase list.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25
Reply to NWPathMonitor Failing
I appreciate your kind recommendation. I realize that NWPathMonitor can return unexpected results. That has initiated this question in the first place. In my app, I have employed a measure as to what to do when it ges an unexpected connection result. Yet, I'll see if I need additional measures.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25