Skip to content

Commit

Permalink
Reverted selected index binding change
Browse files Browse the repository at this point in the history
  • Loading branch information
NicholasBellucci committed Dec 3, 2020
1 parent 952e535 commit 81114f7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
4 changes: 3 additions & 1 deletion Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 1140;
LastUpgradeCheck = 1140;
LastUpgradeCheck = 1220;
ORGANIZATIONNAME = "Nicholas Bellucci";
TargetAttributes = {
4C6A94472468D15000D5C6C7 = {
Expand Down Expand Up @@ -363,6 +363,7 @@
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
Expand Down Expand Up @@ -423,6 +424,7 @@
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
Expand Down
7 changes: 4 additions & 3 deletions Example/Example/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
import SwiftUI

struct ContentView: View {
@State var selectedIndex: Int = 1
@State var badgeValue: String? = "1"

var body: some View {
StatefulTabView {
StatefulTabView(selectedIndex: $selectedIndex) {
Tab(title: "Tab 1", systemImageName: "circle.fill", badgeValue: badgeValue) {
NavigationView {
List {
Expand All @@ -29,7 +30,7 @@ struct ContentView: View {
}

Tab(title: "Tab 2", systemImageName: "square.fill") {
NavigationView{
NavigationView {
List {
Section {
ForEach(0..<20, id: \.self) { index in
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ StatefulTabView {

### Selected Index

The selected index of the StatefulTabView can be set within the initializer.
The selected index of the StatefulTabView can be set within the initializer. The passed value is a binding.

```Swift
@State var selectedIndex: Int = 2

StatefulTabView(selectedIndex: selectedIndex) {
StatefulTabView(selectedIndex: $selectedIndex) {
...
}
```
Expand Down
3 changes: 2 additions & 1 deletion Sources/StatefulTabView/Helpers/TabBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ struct TabBarController: UIViewControllerRepresentable {
var unselectedItemTintColor: UIColor?
var backgroundColor: UIColor?
var tabBarConfiguration: TabBarBackgroundConfiguration?
var selectedIndex: Int

@Binding var selectedIndex: Int

func makeUIViewController(context: Context) -> UITabBarController {
let tabBarController = UITabBarController()
Expand Down
15 changes: 11 additions & 4 deletions Sources/StatefulTabView/StatefulTabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,25 @@ public struct StatefulTabView: View {
internal var viewControllers: [UIHostingController<AnyView>] = []
internal var tabBarItems: [Tab] = []

internal var selectedIndex: Int
internal var barTintColor: UIColor? = nil
internal var unselectedItemTintColor: UIColor? = nil
internal var backgroundColor: UIColor? = nil
internal var tabBarConfiguration: TabBarBackgroundConfiguration? = nil

@State private var stateIndex: Int = 0
@Binding private var bindableIndex: Int

private var useBindableIndex: Bool = false

public init(selectedIndex: Int = 0, @TabBuilder _ content: () -> [Tab]) {
self.selectedIndex = selectedIndex
public init(selectedIndex: Binding<Int>? = nil, @TabBuilder _ content: () -> [Tab]) {
if let selectedIndex = selectedIndex {
_bindableIndex = selectedIndex
useBindableIndex = true
} else {
_bindableIndex = .constant(0)
useBindableIndex = false
}

configureViewControllers(with: content())
}

Expand All @@ -33,7 +40,7 @@ public struct StatefulTabView: View {
unselectedItemTintColor: unselectedItemTintColor,
backgroundColor: backgroundColor,
tabBarConfiguration: tabBarConfiguration,
selectedIndex: selectedIndex)
selectedIndex: useBindableIndex ? $bindableIndex : $stateIndex)
.edgesIgnoringSafeArea(.all)
}
}
Expand Down

0 comments on commit 81114f7

Please sign in to comment.