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