Commit 1bdde898 by Daniel Dahan

development: added Menu with internal CollectionView

parent 994062aa
...@@ -36,7 +36,7 @@ public enum ContentViewAlignment: Int { ...@@ -36,7 +36,7 @@ public enum ContentViewAlignment: Int {
case center case center
} }
open class Bar: PulseView { open class Bar: View {
/// Will layout the view. /// Will layout the view.
open var willLayout: Bool { open var willLayout: Bool {
return 0 < width && 0 < height && nil != superview return 0 < width && 0 < height && nil != superview
...@@ -275,7 +275,6 @@ open class Bar: PulseView { ...@@ -275,7 +275,6 @@ open class Bar: PulseView {
open override func prepare() { open override func prepare() {
super.prepare() super.prepare()
heightPreset = .normal heightPreset = .normal
pulseAnimation = .none
autoresizingMask = .flexibleWidth autoresizingMask = .flexibleWidth
interimSpacePreset = .interimSpace3 interimSpacePreset = .interimSpace3
contentEdgeInsetsPreset = .square1 contentEdgeInsetsPreset = .square1
......
...@@ -42,13 +42,21 @@ open class MenuItem: Toolbar { ...@@ -42,13 +42,21 @@ open class MenuItem: Toolbar {
open override func prepare() { open override func prepare() {
super.prepare() super.prepare()
heightPreset = .normal heightPreset = .normal
pulseAnimation = .pointWithBacking
titleLabel.textAlignment = .left titleLabel.textAlignment = .left
detailLabel.textAlignment = .left detailLabel.textAlignment = .left
} }
} }
open class MenuCard: Card {} class MenuCollectionViewCell: CollectionViewCell {
open var menuItem: MenuItem? {
didSet {
oldValue?.removeFromSuperview()
if let v = menuItem {
contentView.addSubview(v)
}
}
}
}
@objc(MenuDelegate) @objc(MenuDelegate)
public protocol MenuDelegate { public protocol MenuDelegate {
...@@ -92,7 +100,7 @@ public protocol MenuDelegate { ...@@ -92,7 +100,7 @@ public protocol MenuDelegate {
optional func menu(menu: Menu, tappedAt point: CGPoint, isOutside: Bool) optional func menu(menu: Menu, tappedAt point: CGPoint, isOutside: Bool)
@objc @objc
optional func menu(menu: Menu, didSelect menuItem: MenuItem) optional func menu(menu: Menu, didSelect menuItem: MenuItem, at indexPath: IndexPath)
} }
...@@ -105,27 +113,28 @@ open class Menu: Button { ...@@ -105,27 +113,28 @@ open class Menu: Button {
} }
} }
/// A reference to the card. /// A reference to the dataSourceItems.
open let card = MenuCard() open fileprivate(set) var dataSourceItems = [DataSourceItem]()
/// A preset wrapper around cardEdgeInsets. /// An index of IndexPath to MenuItem.
open var cardEdgeInsetsPreset = EdgeInsetsPreset.none { open fileprivate(set) var indexForDataSourceItems = [IndexPath: MenuItem]()
didSet {
cardEdgeInsets = EdgeInsetsPresetToValue(preset: cardEdgeInsetsPreset)
}
}
/// A reference to cardEdgeInsets. /// A reference to the collectionView.
@IBInspectable open let collectionView = CollectionView()
open var cardEdgeInsets = EdgeInsets.zero {
didSet { /// A reference to the card.
layoutSubviews() open let card = Card()
}
}
/// A reference to the MenuItems. /// A reference to the MenuItems.
open var items = [MenuItem]() { open var items = [MenuItem]() {
didSet { didSet {
dataSourceItems.removeAll()
indexForDataSourceItems.removeAll()
for item in items {
dataSourceItems.append(DataSourceItem(data: item, width: item.width, height: item.height))
}
layoutSubviews() layoutSubviews()
} }
} }
...@@ -143,37 +152,40 @@ open class Menu: Button { ...@@ -143,37 +152,40 @@ open class Menu: Button {
open override func prepare() { open override func prepare() {
super.prepare() super.prepare()
prepareCollectionView()
prepareCard() prepareCard()
prepareHandler() prepareHandler()
} }
open func reload() { open func reload() {
let screen = Screen.bounds if 0 == card.width {
card.width = screen.width - cardEdgeInsets.left - cardEdgeInsets.right card.width = Screen.bounds.width
guard let contentView = card.contentView else {
return
} }
contentView.grid.begin() if 0 == collectionView.height {
contentView.grid.axis.rows = items.count var h: CGFloat = 0
contentView.grid.axis.direction = .vertical for dataSourceItem in dataSourceItems {
h += dataSourceItem.height ?? 0
var h: CGFloat = 0 }
for v in items { collectionView.height = h
h += v.height
} }
contentView.height = h collectionView.reloadData()
contentView.grid.views = items
contentView.grid.commit()
} }
} }
extension Menu { extension Menu {
/// Prepares the collectionView.
fileprivate func prepareCollectionView() {
collectionView.delegate = self
collectionView.dataSource = self
collectionView.interimSpacePreset = .none
collectionView.register(MenuCollectionViewCell.self, forCellWithReuseIdentifier: "MenuCollectionViewCell")
}
/// Prepares the card. /// Prepares the card.
fileprivate func prepareCard() { fileprivate func prepareCard() {
card.contentView = UIView() card.contentView = collectionView
} }
/// Prepares the handler. /// Prepares the handler.
...@@ -194,19 +206,6 @@ extension Menu { ...@@ -194,19 +206,6 @@ extension Menu {
return super.hitTest(point, with: event) return super.hitTest(point, with: event)
} }
guard let contentView = card.contentView else {
return nil
}
for v in contentView.subviews {
let p = v.convert(point, from: self)
if v.bounds.contains(p) {
if let item = v as? MenuItem {
delegate?.menu?(menu: self, didSelect: item)
}
}
}
for v in subviews { for v in subviews {
let p = v.convert(point, from: self) let p = v.convert(point, from: self)
if v.bounds.contains(p) { if v.bounds.contains(p) {
...@@ -276,3 +275,41 @@ extension Menu { ...@@ -276,3 +275,41 @@ extension Menu {
close() close()
} }
} }
extension Menu: CollectionViewDelegate {
open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let menuItem = indexForDataSourceItems[indexPath] else {
return
}
delegate?.menu?(menu: self, didSelect: menuItem, at: indexPath)
}
}
extension Menu: CollectionViewDataSource {
@objc
open func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
@objc
open func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataSourceItems.count
}
@objc
open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MenuCollectionViewCell", for: indexPath) as! MenuCollectionViewCell
guard let menuItem = dataSourceItems[indexPath.item].data as? MenuItem else {
return cell
}
indexForDataSourceItems[indexPath] = menuItem
cell.menuItem = menuItem
cell.menuItem?.width = cell.width
return cell
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment