From 074c6720e4db1983dfaad99950e18be870c3499d Mon Sep 17 00:00:00 2001 From: Michael Zimmermann Date: Thu, 6 Jun 2024 14:43:21 +0200 Subject: [PATCH] [Tab#509] Fix flickering in demo and warning 'Modifying state during view update'. --- .../Components/Tab/View/SwiftUI/TabView.swift | 7 ++++--- .../Tab/ViewModel/TabContainerViewModel.swift | 13 +++++++++++++ .../View/Components/Tab/SwiftUI/TabComponent.swift | 13 ------------- 3 files changed, 17 insertions(+), 16 deletions(-) create mode 100644 core/Sources/Components/Tab/ViewModel/TabContainerViewModel.swift diff --git a/core/Sources/Components/Tab/View/SwiftUI/TabView.swift b/core/Sources/Components/Tab/View/SwiftUI/TabView.swift index 7353537ff..dd76af559 100644 --- a/core/Sources/Components/Tab/View/SwiftUI/TabView.swift +++ b/core/Sources/Components/Tab/View/SwiftUI/TabView.swift @@ -11,7 +11,8 @@ import SwiftUI /// TabView is the similar to a SegmentControl public struct TabView: View { private let intent: TabIntent - @ObservedObject private var viewModel: TabViewModel + private var viewModel: TabViewModel + @ObservedObject private var containerViewModel = TabContainerViewModel() @Binding private var selectedIndex: Int @Environment(\.isEnabled) private var isEnabled: Bool @@ -71,7 +72,6 @@ public struct TabView: View { self._selectedIndex = selectedIndex let viewModel = TabViewModel( theme: theme, - apportionsSegmentWidthsByContent: false, content: content, tabSize: tabSize ) @@ -82,7 +82,7 @@ public struct TabView: View { public var body: some View { let viewModel = self.viewModel.setIsEnabled(self.isEnabled) - if viewModel.apportionsSegmentWidthsByContent { + if self.containerViewModel.apportionsSegmentWidthsByContent { TabApportionsSizeView(viewModel: viewModel, intent: self.intent, selectedIndex: self.$selectedIndex) } else { TabEqualSizeView(viewModel: viewModel, intent: self.intent, selectedIndex: self.$selectedIndex) @@ -92,6 +92,7 @@ public struct TabView: View { // MARK: - Public view modifiers /// Indicates whether the control attempts to adjust segment widths based on their content widths. public func apportionsSegmentWidthsByContent(_ value: Bool) -> Self { + self.containerViewModel.apportionsSegmentWidthsByContent = value self.viewModel.apportionsSegmentWidthsByContent = value return self } diff --git a/core/Sources/Components/Tab/ViewModel/TabContainerViewModel.swift b/core/Sources/Components/Tab/ViewModel/TabContainerViewModel.swift new file mode 100644 index 000000000..4a457b544 --- /dev/null +++ b/core/Sources/Components/Tab/ViewModel/TabContainerViewModel.swift @@ -0,0 +1,13 @@ +// +// TabContainerViewModel.swift +// SparkCore +// +// Created by Michael Zimmermann on 06.06.24. +// Copyright © 2024 Adevinta. All rights reserved. +// + +import SwiftUI + +final class TabContainerViewModel: ObservableObject { + @Published var apportionsSegmentWidthsByContent: Bool = false +} diff --git a/spark/Demo/Classes/View/Components/Tab/SwiftUI/TabComponent.swift b/spark/Demo/Classes/View/Components/Tab/SwiftUI/TabComponent.swift index f293a8076..1708d88ce 100644 --- a/spark/Demo/Classes/View/Components/Tab/SwiftUI/TabComponent.swift +++ b/spark/Demo/Classes/View/Components/Tab/SwiftUI/TabComponent.swift @@ -143,19 +143,6 @@ struct TabComponent: View { .disabled(self.allDisabled == .selected) } ) - .onChange(of: self.numberOfTabs) { _ in - self.redraw() - } - .onChange(of: self.stateVals) { _ in - self.redraw() - } - } - - func redraw() { - self.equalSize.toggle() - DispatchQueue.main.async { - self.equalSize.toggle() - } } func badge() -> BadgeView? {