Skip to content

Commit

Permalink
[Tab#509] Fix flickering in demo and warning 'Modifying state during …
Browse files Browse the repository at this point in the history
…view update'.
  • Loading branch information
michael-zimmermann committed Jun 6, 2024
1 parent c3c95d2 commit 074c672
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
7 changes: 4 additions & 3 deletions core/Sources/Components/Tab/View/SwiftUI/TabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<TabItemContent>
private var viewModel: TabViewModel<TabItemContent>
@ObservedObject private var containerViewModel = TabContainerViewModel()
@Binding private var selectedIndex: Int
@Environment(\.isEnabled) private var isEnabled: Bool

Expand Down Expand Up @@ -71,7 +72,6 @@ public struct TabView: View {
self._selectedIndex = selectedIndex
let viewModel = TabViewModel(
theme: theme,
apportionsSegmentWidthsByContent: false,
content: content,
tabSize: tabSize
)
Expand All @@ -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)
Expand All @@ -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
}
Expand Down
13 changes: 13 additions & 0 deletions core/Sources/Components/Tab/ViewModel/TabContainerViewModel.swift
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 0 additions & 13 deletions spark/Demo/Classes/View/Components/Tab/SwiftUI/TabComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down

0 comments on commit 074c672

Please sign in to comment.