This repository has been archived by the owner on May 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tab view container. Used for profile badges
Adds general support for multiple panes to the user profile. Still need to actually support for any other panes. JIRA: https://openedx.atlassian.net/browse/MA-1670
- Loading branch information
Akiva Leffert
committed
Apr 7, 2016
1 parent
1610cdc
commit 2b65a63
Showing
4 changed files
with
203 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// | ||
// TabContainerView.swift | ||
// edX | ||
// | ||
// Created by Akiva Leffert on 4/5/16. | ||
// Copyright © 2016 edX. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// Simple tab view with a segmented control at the top | ||
class TabContainerView : UIView { | ||
|
||
struct Item { | ||
let name : String | ||
let view : UIView | ||
let identifier : String | ||
} | ||
|
||
private let control = UISegmentedControl() | ||
|
||
private let stackView = TZStackView() | ||
private var activeTabBodyView : UIView? = nil | ||
|
||
private var currentIdentifier : String? | ||
|
||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
stackView.insertArrangedSubview(control, atIndex: 0) | ||
stackView.axis = .Vertical | ||
stackView.alignment = .Fill | ||
stackView.spacing = StandardVerticalMargin | ||
|
||
addSubview(stackView) | ||
stackView.snp_makeConstraints {make in | ||
make.leading.equalTo(self.snp_leadingMargin) | ||
make.trailing.equalTo(self.snp_trailingMargin) | ||
make.top.equalTo(self.snp_topMargin) | ||
make.bottom.equalTo(self.snp_bottomMargin) | ||
} | ||
|
||
let selectedAttributes = OEXTextStyle(weight: .Normal, size: .Small, color: OEXStyles.sharedStyles().neutralBlackT()) | ||
let unselectedAttributes = OEXTextStyle(weight: .Normal, size: .Small, color: OEXStyles.sharedStyles().neutralDark()) | ||
control.setTitleTextAttributes(selectedAttributes.attributes, forState: .Selected) | ||
control.setTitleTextAttributes(unselectedAttributes.attributes, forState: .Normal) | ||
control.tintColor = OEXStyles.sharedStyles().primaryXLightColor() | ||
control.oex_addAction({[weak self] control in | ||
let index = (control as! UISegmentedControl).selectedSegmentIndex | ||
self?.showTabAtIndex(index) | ||
}, forEvents: .ValueChanged) | ||
} | ||
|
||
required init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
var items : [Item] = [] { | ||
didSet { | ||
control.removeAllSegments() | ||
|
||
for (index, item) in items.enumerate() { | ||
control.insertSegmentWithTitle(item.name, atIndex: control.numberOfSegments, animated: false) | ||
if item.identifier == currentIdentifier { | ||
showTabAtIndex(index) | ||
} | ||
} | ||
if control.selectedSegmentIndex == UISegmentedControlNoSegment && items.count > 0 { | ||
showTabAtIndex(0) | ||
} | ||
else { | ||
currentIdentifier = nil | ||
} | ||
|
||
if items.count < 2 { | ||
control.hidden = true | ||
} | ||
} | ||
} | ||
|
||
private func showTabAtIndex(index: Int) { | ||
guard index != UISegmentedControlNoSegment else { | ||
return | ||
} | ||
|
||
activeTabBodyView?.removeFromSuperview() | ||
|
||
let item = items[index] | ||
control.selectedSegmentIndex = index | ||
currentIdentifier = item.identifier | ||
stackView.addArrangedSubview(item.view) | ||
activeTabBodyView = item.view | ||
} | ||
|
||
private func indexOfItemWithIdentifier(identifier : String) -> Int? { | ||
return items.firstIndexMatching {$0.identifier == identifier } | ||
} | ||
|
||
func showTabWithIdentifier(identifier : String) { | ||
if let index = indexOfItemWithIdentifier(identifier) { | ||
showTabAtIndex(index) | ||
} | ||
} | ||
} | ||
|
||
// Only used for testing | ||
extension TabContainerView { | ||
func t_isShowingViewForItem(item : Item) -> Bool { | ||
let viewsMatch = stackView.arrangedSubviews == [control, item.view] | ||
let indexMatches = indexOfItemWithIdentifier(item.identifier) == control.selectedSegmentIndex | ||
let identifierMatches = currentIdentifier == item.identifier | ||
return viewsMatch && indexMatches && identifierMatches | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// TabContainerViewTests.swift | ||
// edX | ||
// | ||
// Created by Akiva Leffert on 4/5/16. | ||
// Copyright © 2016 edX. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import XCTest | ||
|
||
@testable import edX | ||
|
||
class TabContainerViewTests : XCTestCase { | ||
|
||
var testItems : (TabContainerView.Item, TabContainerView.Item) = ( | ||
TabContainerView.Item(name: "Name", view: UIView(), identifier: "item1"), | ||
TabContainerView.Item(name: "Name", view: UIView(), identifier: "item2") | ||
) | ||
|
||
func testSettingItemsChoosesFirstItem() { | ||
let tabView = TabContainerView() | ||
|
||
let (firstItem, secondItem) = testItems | ||
tabView.items = [firstItem, secondItem] | ||
|
||
XCTAssertTrue(tabView.t_isShowingViewForItem(firstItem)) | ||
} | ||
|
||
func testSettingEmptyItemsClears() { | ||
let tabView = TabContainerView() | ||
|
||
let (firstItem, secondItem) = testItems | ||
tabView.items = [firstItem, secondItem] | ||
XCTAssertTrue(tabView.t_isShowingViewForItem(firstItem)) | ||
|
||
tabView.items = [] | ||
XCTAssertTrue(!tabView.t_isShowingViewForItem(firstItem)) | ||
} | ||
|
||
func testSwitchingTabs() { | ||
let tabView = TabContainerView() | ||
|
||
let (firstItem, secondItem) = testItems | ||
tabView.items = [firstItem, secondItem] | ||
XCTAssertTrue(tabView.t_isShowingViewForItem(firstItem)) | ||
|
||
tabView.showTabWithIdentifier(secondItem.identifier) | ||
XCTAssertTrue(tabView.t_isShowingViewForItem(secondItem)) | ||
|
||
tabView.showTabWithIdentifier(firstItem.identifier) | ||
XCTAssertTrue(tabView.t_isShowingViewForItem(firstItem)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters