Skip to content

Commit

Permalink
Refactor NavBarModifier (#28)
Browse files Browse the repository at this point in the history
delete .onReceive(self.dataStore.$items.throttle(for: 0.05, scheduler: DispatchQueue.main, latest: true))
  • Loading branch information
Martin Barreto authored Jul 28, 2021
1 parent a068947 commit 7bacbfd
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 25 deletions.
1 change: 0 additions & 1 deletion Example/Shared/Views/Youtube/Models/AccountModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import Foundation

class AccountModel: ObservableObject {
var navBarItem = YoutubeNavBarItem(title: "Account", imageName: "account")

var post: Post {
PostsFactory.shared.posts[0]
Expand Down
1 change: 0 additions & 1 deletion Example/Shared/Views/Youtube/Models/HomeModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import Foundation

class HomeModel: ObservableObject {
var navBarItem = YoutubeNavBarItem(title: "Home", imageName: "home")

var posts: [Post] {
PostsFactory.shared.posts
Expand Down
1 change: 0 additions & 1 deletion Example/Shared/Views/Youtube/Models/TrendingModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import Foundation

class TrendingModel: ObservableObject {
var navBarItem = YoutubeNavBarItem(title: "Trending", imageName: "trending")

var posts: [Post] {
PostsFactory.shared.posts
Expand Down
6 changes: 3 additions & 3 deletions Example/Shared/Views/Youtube/YoutubeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct YoutubeView: View {
var body: some View {
PagerTabStripView() {
PostsList(isLoading: $homeModel.isLoading, items: homeModel.posts).pagerTabItem {
homeModel.navBarItem
YoutubeNavBarItem(title: "Home", imageName: "home")
}.onPageAppear {
homeModel.isLoading = true
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
Expand All @@ -26,7 +26,7 @@ struct YoutubeView: View {
}

PostsList(isLoading: $trendingModel.isLoading, items: trendingModel.posts, withDescription: false).pagerTabItem {
trendingModel.navBarItem
YoutubeNavBarItem(title: "Trending", imageName: "trending")
}
.onPageAppear {
trendingModel.isLoading = true
Expand All @@ -36,7 +36,7 @@ struct YoutubeView: View {
}

PostDetail(post: accountModel.post).pagerTabItem {
accountModel.navBarItem
YoutubeNavBarItem(title: "Account", imageName: "account")
}
}
.pagerTabStripViewStyle(PagerTabViewStyle(tabItemSpacing: 0, tabItemHeight: 80, indicatorBarHeight: 7, indicatorBarColor: selectedColor))
Expand Down
5 changes: 2 additions & 3 deletions Sources/NavBarModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ import SwiftUI
struct NavBarModifier: ViewModifier {

@Binding private var selection: Int
@StateObject private var dataStore: DataStore
@EnvironmentObject private var dataStore: DataStore

private var navBarItemWidth: CGFloat {
let totalItemWidth = (settings.width - (style.tabItemSpacing * CGFloat(dataStore.itemsCount - 1)))
return totalItemWidth / CGFloat(dataStore.itemsCount)
}

public init(dataStore: StateObject<DataStore>, selection: Binding<Int>) {
public init(selection: Binding<Int>) {
self._selection = selection
self._dataStore = dataStore
}

func body(content: Content) -> some View {
Expand Down
25 changes: 9 additions & 16 deletions Sources/PagerTabStripView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ 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 newPredictedIndex = (CGFloat(selection) - offset).rounded()
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
if abs(selection - newIndex) > 1 {
selection = newIndex > selection ? selection + 1 : selection - 1
} else {
self.selection = newIndex
selection = newIndex
}
if translation > 0 {
self.currentOffset = translation
Expand All @@ -107,12 +107,8 @@ private struct WrapperPagerTabStripView<Content> : View where Content: View {
})
.onChange(of: self.selection) { [selection] newIndex in
self.currentOffset = self.offsetFor(index: newIndex)
if let callback = dataStore.items[newIndex]?.appearCallback {
callback()
}
if let tabViewDelegate = dataStore.items[selection]?.tabViewDelegate {
tabViewDelegate.setState(state: .normal)
}
dataStore.items[newIndex]?.appearCallback?()
dataStore.items[selection]?.tabViewDelegate?.setState(state: .normal)
if let tabViewDelegate = dataStore.items[newIndex]?.tabViewDelegate, newIndex != selection {
tabViewDelegate.setState(state: .selected)
}
Expand All @@ -125,18 +121,15 @@ private struct WrapperPagerTabStripView<Content> : View where Content: View {
}
.onChange(of: dataStore.items.count) { _ in
self.selection = selection >= dataStore.items.count ? dataStore.items.count - 1 : selection
dataStore.items[selection]?.tabViewDelegate?.setState(state: .selected)
dataStore.items[selection]?.appearCallback?()
}
.onAppear {
settings.width = gproxy.size.width
}
}
.modifier(NavBarModifier(dataStore: self._dataStore, selection: $selection))
.modifier(NavBarModifier(selection: $selection))
.environmentObject(self.dataStore)
.onReceive(self.dataStore.$items.throttle(for: 0.05, scheduler: DispatchQueue.main, latest: true)) { items in
if let tabViewDelegate = dataStore.items[selection]?.tabViewDelegate {
tabViewDelegate.setState(state: .selected)
}
}
.clipped()
}

Expand Down

0 comments on commit 7bacbfd

Please sign in to comment.