Post

Replies

Boosts

Views

Activity

Reply to Can A Property In An ObservableObject listen to Another Property? How?
@babyj hi. tried it and it works. but i only used this section here // This line is the key part // Must be before the variable change appData.objectWillChange.send() I ran that code after every property change e.g. appData.toolbarItem1?.disabled = true appData.toolbarItem1?.objectWillChange.send() but when i placed that before as you mentioned, it worked. and i do not understand why they have it behave that way if send() is called before a property change. ps tollbarItem1 is also an ObservableObject in the appData class.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’22
Reply to Will 2 environmentObject work?
Here is my code but it's not a workign sample code. just wish to have this checked out if in theory my assumptions are correct or not struct ContentView: View {       @State var dark = PrefTool.instance.getBoolean(Keyword.DARK_MODE)   @State var show = false   @StateObject var navigationManager = SidebarNavigationManager()   @StateObject var appData = AppData()   @StateObject var toolbarData = ToolbarData()   var body: some View {     NavigationStack(path: $appData.path) {       ZStack(alignment: .leading) {         VStack(spacing: 0) {           HStack {             Button(action: {               withAnimation(.default) {                 show.toggle()               }             }) {               Image(systemName: "line.3.horizontal")             }             Text("Home")             Spacer()             ToolbarView()             OverflowMenu()               .padding([.leading], Default.instance.PADDING_BETWEEN_MENU_ITEM)           }           .padding()           .foregroundColor(.primary)           .overlay(Rectangle().stroke(Color.primary.opacity(0.1), lineWidth: 1).shadow(radius: 3).edgesIgnoringSafeArea(.top))           MainContent(navigationManager: navigationManager)             .frame(maxWidth: .infinity, maxHeight: .infinity)         }         HStack {           SideMenuView(show: $show, navigationManager: navigationManager)             .preferredColorScheme(dark ? .dark : .light)             .offset(x: self.show ? 0 : -UIScreen.main.bounds.width / 1.2)           Spacer(minLength: 0)           Rectangle().stroke(.clear).frame(width: show ? UIScreen.main.bounds.width - (UIScreen.main.bounds.width / 1.2) : 0)             .contentShape(Rectangle())             .onTapGesture {               if (show) {                 withAnimation(.default) {                   show.toggle()                 }               }             }         }         .background(Color.primary.opacity(self.show ? (self.dark ? 0.05 : 0.2) : 0))       }       .navigationBarHidden(true)     }     .navigationViewStyle(.stack)     .environmentObject(appData)     .environmentObject(toolbarData)   } } and ToolbarView and ToolbarData struct ToolbarView: View {       @EnvironmentObject var toolbarData: ToolbarData       var body: some View {     HStack {       if let item = toolbarData.toolbarItem, item.visible {         Button {           item.action()         } label: {           if let iconLink = item.iconLink {             NavigationLink(destination: iconLink) {               Image(systemName: item.icon)             }           }           else {             Image(systemName: item.icon)           }         }         .disabled(item.disabled)         .opacity(item.opacity)         .padding([.leading, .trailing], Default.instance.PADDING_BETWEEN_MENU_ITEM)       }     }   } } final class ToolbarData: ObservableObject {      @Published var toolbarItem: ToolbarItemProperties?       func disableToolbarItem(_ toolbarItem: ToolbarItemProperties?, _ disabled: Bool) {     if let toolbarItem {       objectWillChange.send()       toolbarItem.disabled = disabled     }   }       func disableToolbarItem(_ toolbarItem: ToolbarItemProperties?, _ disabled: Bool, _ deadline: DispatchTime) {     if let toolbarItem {       DispatchQueue.main.asyncAfter(deadline: deadline) {         self.objectWillChange.send()         toolbarItem.disabled = disabled       }     }   }       func resetMenuItems() {     toolbarItem = nil   } } in a custom view e.g. MainContent in the code above. i do something from a button click like this toolbarData.disableToolbarItem(toolbarData.toolbarItem, true, .now()) The thing is, only the ToolbarView() should be rebuilt. somehow, the whole ContentView() gets rebuilt instead. This should not behave like this right? Since only the ToolbarData is updated. Thoughts?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’22
Reply to How To Set Values To Binding Variable
Please ignore StringTool. its just a helper tool to ensure that if it's a nil, it will return "". In the child view i did it like this init(temp: Binding<Temp?>) {     _temp = temp } in the parent view it looks like this ChiildView(temp: $temp) when i dismiss the child view, i want to set the property cityUrlId in the parent view which is @State
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’23
Reply to Swift Chart has too many labels on x-axis. How to lessen them?
This is the chart data struct ChartData : Identifiable {   var id = UUID()   var date: String   var rainfallTo: Int   var tempMean: Int } and the chart code Chart(createChartData(city)) {               LineMark(                 x: .value(StringTool.localize("date"), $0.date),                 y: .value("Rainfall mm", $0.rainfallTo),                 series: .value("type", StringTool.localize("rainfall_mm"))               )               .foregroundStyle(by: .value("type", StringTool.localize("rainfall_mm")))               LineMark(                 x: .value(StringTool.localize("date"), $0.date),                 y: .value(StringTool.localize("temperature"), $0.tempMean),                 series: .value("type", StringTool.localize("temperature"))               )               .foregroundStyle(by: .value("type", StringTool.localize("temperature"))) }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’23
Reply to Can A Property In An ObservableObject listen to Another Property? How?
@babyj hi. tried it and it works. but i only used this section here // This line is the key part // Must be before the variable change appData.objectWillChange.send() I ran that code after every property change e.g. appData.toolbarItem1?.disabled = true appData.toolbarItem1?.objectWillChange.send() but when i placed that before as you mentioned, it worked. and i do not understand why they have it behave that way if send() is called before a property change. ps tollbarItem1 is also an ObservableObject in the appData class.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Dec ’22
Reply to Will 2 environmentObject work?
Here is my code but it's not a workign sample code. just wish to have this checked out if in theory my assumptions are correct or not struct ContentView: View {       @State var dark = PrefTool.instance.getBoolean(Keyword.DARK_MODE)   @State var show = false   @StateObject var navigationManager = SidebarNavigationManager()   @StateObject var appData = AppData()   @StateObject var toolbarData = ToolbarData()   var body: some View {     NavigationStack(path: $appData.path) {       ZStack(alignment: .leading) {         VStack(spacing: 0) {           HStack {             Button(action: {               withAnimation(.default) {                 show.toggle()               }             }) {               Image(systemName: "line.3.horizontal")             }             Text("Home")             Spacer()             ToolbarView()             OverflowMenu()               .padding([.leading], Default.instance.PADDING_BETWEEN_MENU_ITEM)           }           .padding()           .foregroundColor(.primary)           .overlay(Rectangle().stroke(Color.primary.opacity(0.1), lineWidth: 1).shadow(radius: 3).edgesIgnoringSafeArea(.top))           MainContent(navigationManager: navigationManager)             .frame(maxWidth: .infinity, maxHeight: .infinity)         }         HStack {           SideMenuView(show: $show, navigationManager: navigationManager)             .preferredColorScheme(dark ? .dark : .light)             .offset(x: self.show ? 0 : -UIScreen.main.bounds.width / 1.2)           Spacer(minLength: 0)           Rectangle().stroke(.clear).frame(width: show ? UIScreen.main.bounds.width - (UIScreen.main.bounds.width / 1.2) : 0)             .contentShape(Rectangle())             .onTapGesture {               if (show) {                 withAnimation(.default) {                   show.toggle()                 }               }             }         }         .background(Color.primary.opacity(self.show ? (self.dark ? 0.05 : 0.2) : 0))       }       .navigationBarHidden(true)     }     .navigationViewStyle(.stack)     .environmentObject(appData)     .environmentObject(toolbarData)   } } and ToolbarView and ToolbarData struct ToolbarView: View {       @EnvironmentObject var toolbarData: ToolbarData       var body: some View {     HStack {       if let item = toolbarData.toolbarItem, item.visible {         Button {           item.action()         } label: {           if let iconLink = item.iconLink {             NavigationLink(destination: iconLink) {               Image(systemName: item.icon)             }           }           else {             Image(systemName: item.icon)           }         }         .disabled(item.disabled)         .opacity(item.opacity)         .padding([.leading, .trailing], Default.instance.PADDING_BETWEEN_MENU_ITEM)       }     }   } } final class ToolbarData: ObservableObject {      @Published var toolbarItem: ToolbarItemProperties?       func disableToolbarItem(_ toolbarItem: ToolbarItemProperties?, _ disabled: Bool) {     if let toolbarItem {       objectWillChange.send()       toolbarItem.disabled = disabled     }   }       func disableToolbarItem(_ toolbarItem: ToolbarItemProperties?, _ disabled: Bool, _ deadline: DispatchTime) {     if let toolbarItem {       DispatchQueue.main.asyncAfter(deadline: deadline) {         self.objectWillChange.send()         toolbarItem.disabled = disabled       }     }   }       func resetMenuItems() {     toolbarItem = nil   } } in a custom view e.g. MainContent in the code above. i do something from a button click like this toolbarData.disableToolbarItem(toolbarData.toolbarItem, true, .now()) The thing is, only the ToolbarView() should be rebuilt. somehow, the whole ContentView() gets rebuilt instead. This should not behave like this right? Since only the ToolbarData is updated. Thoughts?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Dec ’22
Reply to iOS 13 - Attempting to store >= 4194304 bytes of data in CFPreferences/NSUserDefaults on this platform is invalid
This does not seem to be data about the value being stored but rather the number of keys being stored where the total must not exceed its limit.
Topic: App & System Services SubTopic: Core OS Tags:
Replies
Boosts
Views
Activity
Dec ’22
Reply to Use placeholder app icon in view in Swift Playgrounds 4
Image(uiImage: UIImage(named: "AppIcon") ?? UIImage()) works fine
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Dec ’22
Reply to How To Declare Optional Parameter Function
Baaah.. managed to solve it addInfo("Sample", action: () {             })
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Dec ’22
Reply to How To Set Values To Binding Variable
Please ignore StringTool. its just a helper tool to ensure that if it's a nil, it will return "". In the child view i did it like this init(temp: Binding<Temp?>) {     _temp = temp } in the parent view it looks like this ChiildView(temp: $temp) when i dismiss the child view, i want to set the property cityUrlId in the parent view which is @State
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jan ’23
Reply to Swift Chart has too many labels on x-axis. How to lessen them?
This is the chart data struct ChartData : Identifiable {   var id = UUID()   var date: String   var rainfallTo: Int   var tempMean: Int } and the chart code Chart(createChartData(city)) {               LineMark(                 x: .value(StringTool.localize("date"), $0.date),                 y: .value("Rainfall mm", $0.rainfallTo),                 series: .value("type", StringTool.localize("rainfall_mm"))               )               .foregroundStyle(by: .value("type", StringTool.localize("rainfall_mm")))               LineMark(                 x: .value(StringTool.localize("date"), $0.date),                 y: .value(StringTool.localize("temperature"), $0.tempMean),                 series: .value("type", StringTool.localize("temperature"))               )               .foregroundStyle(by: .value("type", StringTool.localize("temperature"))) }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jan ’23
Reply to Why Does Image Overlap In GridRow?
@claude31 i have not seen any replies, thought of tagging you if you do not mind since you help a lot. What do you think is wrong with the code?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jan ’23
Reply to Why Does Image Overlap In GridRow?
add .resizable() to Image is the solution.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jan ’23
Reply to Why no "background mode" capability?
Mine also does not have. But you can manually add it. There is a "+ Capability" at the top left. Click it and search for Background Modes.
Replies
Boosts
Views
Activity
Feb ’23
Reply to Callback Function As Parameter
Seems ill have to read up on closures. Thanks!
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Feb ’23
Reply to BGTaskScheduler.shared.register() does not get called
Note: I am using this in SwiftUI
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Mar ’23
Reply to BGTaskScheduler.shared.register() does not get called
Well, no matter what I do, it looks like background services are not supported by iOS. The .register just doesnt work. If i use .backgroundTask it does not meet my purpose since i want some kind of service that fetches data every 3am/pm, 6am/pm, 9am/pm, 12am/pm. I wondered how chat apps do this. Probably push notifications?
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Mar ’23
Reply to Push Notification With Background Task
i am also interested in this because this looks like how chat apps do it: fetching new data on certain intervals?
Replies
Boosts
Views
Activity
Mar ’23
Reply to Xcode 14 error Build input file cannot be found metadata-arm64
I do not understand the instructions change workspace setting > Build location > Custom & relative to workspace @chebby_ ? in the image there are 2 options. there is no option " Custom & relative to workspace"
Replies
Boosts
Views
Activity
Mar ’23