Merge pull request #27 from xmartlabs/reviewMartin
remove unnecessary items count Biding.
This commit is contained in:
commit
a0689471a8
|
@ -21,7 +21,14 @@ class DataItem {
|
|||
}
|
||||
|
||||
class DataStore: ObservableObject {
|
||||
@Published var items = [Int: DataItem]()
|
||||
@Published var items = [Int: DataItem](){
|
||||
didSet {
|
||||
itemsCount = items.count
|
||||
}
|
||||
}
|
||||
|
||||
@Published private(set) var itemsCount: Int = 0
|
||||
|
||||
|
||||
func setView(_ view: AnyView, tabViewDelegate: PagerTabViewDelegate? = nil, at index: Int) {
|
||||
if let item = items[index] {
|
||||
|
|
|
@ -10,23 +10,23 @@ import SwiftUI
|
|||
struct NavBarModifier: ViewModifier {
|
||||
|
||||
@Binding private var selection: Int
|
||||
@Binding private var itemCount: Int
|
||||
@StateObject private var dataStore: DataStore
|
||||
|
||||
private var navBarItemWidth: CGFloat {
|
||||
let totalItemWidth = (settings.width - (style.tabItemSpacing * CGFloat(itemCount - 1)))
|
||||
return totalItemWidth / CGFloat(itemCount)
|
||||
let totalItemWidth = (settings.width - (style.tabItemSpacing * CGFloat(dataStore.itemsCount - 1)))
|
||||
return totalItemWidth / CGFloat(dataStore.itemsCount)
|
||||
}
|
||||
|
||||
public init(itemCount: Binding<Int>, selection: Binding<Int>) {
|
||||
public init(dataStore: StateObject<DataStore>, selection: Binding<Int>) {
|
||||
self._selection = selection
|
||||
self._itemCount = itemCount
|
||||
self._dataStore = dataStore
|
||||
}
|
||||
|
||||
func body(content: Content) -> some View {
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
HStack(spacing: style.tabItemSpacing) {
|
||||
if itemCount > 0 && settings.width > 0 {
|
||||
ForEach(0...itemCount-1, id: \.self) { idx in
|
||||
if dataStore.itemsCount > 0 && settings.width > 0 {
|
||||
ForEach(0...dataStore.itemsCount-1, id: \.self) { idx in
|
||||
NavBarItem(id: idx, selection: $selection)
|
||||
.frame(height: style.tabItemHeight)
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ struct NavBarModifier: ViewModifier {
|
|||
.frame(height: style.tabItemHeight)
|
||||
HStack {
|
||||
if let width = navBarItemWidth, width > 0, width <= settings.width {
|
||||
let x = -settings.contentOffset / CGFloat(itemCount) + width / 2
|
||||
let x = -settings.contentOffset / CGFloat(dataStore.itemsCount) + width / 2
|
||||
Rectangle()
|
||||
.fill(style.indicatorBarColor)
|
||||
.animation(.default)
|
||||
|
|
|
@ -55,8 +55,6 @@ private struct WrapperPagerTabStripView<Content> : View where Content: View {
|
|||
self.settings.contentOffset = currentOffset
|
||||
}
|
||||
}
|
||||
|
||||
@State private var itemCount : Int = 0
|
||||
@GestureState private var translation: CGFloat = 0
|
||||
|
||||
public init(selection: Binding<Int>,
|
||||
|
@ -82,7 +80,7 @@ private struct WrapperPagerTabStripView<Content> : View where Content: View {
|
|||
let normTrans = valueWidth / (gproxy.size.width + 50)
|
||||
let logValue = log(1 + normTrans)
|
||||
state = gproxy.size.width/1.5 * logValue
|
||||
} else if (selection == itemCount - 1 && value.translation.width < 0) {
|
||||
} else if (selection == dataStore.itemsCount - 1 && value.translation.width < 0) {
|
||||
let valueWidth = -value.translation.width
|
||||
let normTrans = valueWidth / (gproxy.size.width + 50)
|
||||
let logValue = log(1 + normTrans)
|
||||
|
@ -93,7 +91,7 @@ private struct WrapperPagerTabStripView<Content> : View where Content: View {
|
|||
}.onEnded { value in
|
||||
let offset = value.predictedEndTranslation.width / gproxy.size.width
|
||||
let newPredictedIndex = (CGFloat(self.selection) - offset).rounded()
|
||||
let newIndex = min(max(Int(newPredictedIndex), 0), self.itemCount - 1)
|
||||
let newIndex = min(max(Int(newPredictedIndex), 0), dataStore.itemsCount - 1)
|
||||
if abs(self.selection - newIndex) > 1 {
|
||||
self.selection = newIndex > self.selection ? self.selection + 1 : self.selection - 1
|
||||
} else {
|
||||
|
@ -125,17 +123,16 @@ private struct WrapperPagerTabStripView<Content> : View where Content: View {
|
|||
.onChange(of: settings.width) { _ in
|
||||
self.currentOffset = self.offsetFor(index: self.selection)
|
||||
}
|
||||
.onChange(of: itemCount) { _ in
|
||||
self.selection = selection >= itemCount ? itemCount - 1 : selection
|
||||
.onChange(of: dataStore.items.count) { _ in
|
||||
self.selection = selection >= dataStore.items.count ? dataStore.items.count - 1 : selection
|
||||
}
|
||||
.onAppear {
|
||||
settings.width = gproxy.size.width
|
||||
}
|
||||
}
|
||||
.modifier(NavBarModifier(itemCount: $itemCount, selection: $selection))
|
||||
.modifier(NavBarModifier(dataStore: self._dataStore, selection: $selection))
|
||||
.environmentObject(self.dataStore)
|
||||
.onReceive(self.dataStore.$items.throttle(for: 0.05, scheduler: DispatchQueue.main, latest: true)) { items in
|
||||
self.itemCount = items.keys.count
|
||||
if let tabViewDelegate = dataStore.items[selection]?.tabViewDelegate {
|
||||
tabViewDelegate.setState(state: .selected)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue