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.
Merge pull request #706 from edx/aleffert/badges-ui
First part of badging UI changes
- Loading branch information
Showing
24 changed files
with
454 additions
and
234 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
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
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,108 @@ | ||
// | ||
// 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) | ||
} | ||
|
||
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 | ||
} | ||
} |
Oops, something went wrong.