Skip to content

Commit

Permalink
Add applyScrollOptions to UIScrollView components (#75)
Browse files Browse the repository at this point in the history
* Add applyScrollOptions to UIScrollView components

* Add ScrollOptions to CollectionView, ListView, ScrollView

* Rename scrollProps to scrollOptions

* Change ScrollOptions init in Example

* Refactor ScrollView

* Remove storage ScrollOptions in ScrollView
  • Loading branch information
hodovani committed Mar 27, 2019
1 parent 895e50b commit b2cd397
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 107 deletions.
15 changes: 12 additions & 3 deletions Sources/Tokamak/Components/Host/CollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,34 @@ public struct CollectionView<T: CellProvider>: HostComponent {
public let sections: T.Sections
public let onSelect: Handler<CellPath>?
public let style: Style?
public let scrollOptions: ScrollOptions?

public init(
_ style: Style? = nil,
cellProps: T.Props,
onSelect: Handler<CellPath>? = nil,
scrollOptions: ScrollOptions? = nil,
singleSection: T.Sections.Element
) {
self.cellProps = cellProps
sections = T.Sections.single(section: singleSection)
self.onSelect = onSelect
self.style = style
self.scrollOptions = scrollOptions
}

public init(
_ style: Style? = nil,
cellProps: T.Props,
sections: T.Sections,
onSelect: Handler<CellPath>? = nil
onSelect: Handler<CellPath>? = nil,
scrollOptions: ScrollOptions? = nil
) {
self.cellProps = cellProps
self.sections = sections
self.onSelect = onSelect
self.style = style
self.scrollOptions = scrollOptions
}
}

Expand All @@ -44,22 +49,26 @@ extension CollectionView.Props where T.Props == Null {
public init(
_ style: Style? = nil,
sections: T.Sections,
onSelect: Handler<CellPath>? = nil
onSelect: Handler<CellPath>? = nil,
scrollOptions: ScrollOptions? = nil
) {
cellProps = Null()
self.sections = sections
self.onSelect = onSelect
self.style = style
self.scrollOptions = scrollOptions
}

public init(
_ style: Style? = nil,
onSelect: Handler<CellPath>? = nil,
singleSection: T.Sections.Element
singleSection: T.Sections.Element,
scrollOptions: ScrollOptions? = nil
) {
cellProps = Null()
sections = T.Sections.single(section: singleSection)
self.onSelect = onSelect
self.style = style
self.scrollOptions = scrollOptions
}
}
13 changes: 11 additions & 2 deletions Sources/Tokamak/Components/Host/ListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,34 @@ public struct ListView<T: CellProvider>: HostComponent {
public let model: T.Sections
public let onSelect: Handler<CellPath>?
public let style: Style?
public let scrollOptions: ScrollOptions?

public init(
_ style: Style? = nil,
cellProps: T.Props,
onSelect: Handler<CellPath>? = nil,
scrollOptions: ScrollOptions? = nil,
singleSection: T.Sections.Element
) {
self.cellProps = cellProps
model = T.Sections.single(section: singleSection)
self.onSelect = onSelect
self.style = style
self.scrollOptions = scrollOptions
}

public init(
_ style: Style? = nil,
cellProps: T.Props,
model: T.Sections,
onSelect: Handler<CellPath>? = nil
onSelect: Handler<CellPath>? = nil,
scrollOptions: ScrollOptions? = nil
) {
self.cellProps = cellProps
self.model = model
self.onSelect = onSelect
self.style = style
self.scrollOptions = scrollOptions
}
}

Expand All @@ -110,22 +115,26 @@ extension ListView.Props where T.Props == Null {
public init(
_ style: Style? = nil,
model: T.Sections,
onSelect: Handler<CellPath>? = nil
onSelect: Handler<CellPath>? = nil,
scrollOptions: ScrollOptions? = nil
) {
cellProps = Null()
self.model = model
self.onSelect = onSelect
self.style = style
self.scrollOptions = scrollOptions
}

public init(
_ style: Style? = nil,
onSelect: Handler<CellPath>? = nil,
scrollOptions: ScrollOptions? = nil,
singleSection: T.Sections.Element
) {
cellProps = Null()
model = T.Sections.single(section: singleSection)
self.onSelect = onSelect
self.style = style
self.scrollOptions = scrollOptions
}
}
82 changes: 22 additions & 60 deletions Sources/Tokamak/Components/Host/ScrollView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,85 +11,47 @@ public struct ScrollView: HostComponent {
return Props()
}

public struct EdgeInsets: Equatable {
public let bottom: Float
public let left: Float
public let right: Float
public let top: Float

public init(
top: Float = 0,
left: Float = 0,
bottom: Float = 0,
right: Float = 0
) {
self.top = top
self.left = left
self.bottom = bottom
self.right = right
}
}

public enum IndicatorStyle {
case `default`
case black
case white
}

public let style: Style?
public let alwaysBounceHorizontal: Bool
public let alwaysBounceVertical: Bool
public let bounces: Bool
public let bouncesZoom: Bool
public let contentInset: EdgeInsets
public let indicatorStyle: IndicatorStyle
public let isDirectionalLockEnabled: Bool
public let isPagingEnabled: Bool
public let isScrollEnabled: Bool
public let maximumZoomScale: Float
public let minimumZoomScale: Float
public let scrollIndicatorInsets: EdgeInsets
public let scrollsToTop: Bool
public let showsHorizontalScrollIndicator: Bool
public let showsVerticalScrollIndicator: Bool
public let zoomScale: Float
public let scrollOptions: ScrollOptions

public init(
_ style: Style? = nil,
alwaysBounceHorizontal: Bool = false,
alwaysBounceVertical: Bool = false,
bounces: Bool = true,
bouncesZoom: Bool = true,
contentInset: EdgeInsets = EdgeInsets(),
indicatorStyle: IndicatorStyle = .default,
contentInset: ScrollOptions.EdgeInsets = ScrollOptions.EdgeInsets(),
indicatorStyle: ScrollOptions.IndicatorStyle = .default,
isDirectionalLockEnabled: Bool = false,
isPagingEnabled: Bool = false,
isScrollEnabled: Bool = true,
maximumZoomScale: Float = 1.0,
minimumZoomScale: Float = 1.0,
scrollIndicatorInsets: EdgeInsets = EdgeInsets(),
scrollIndicatorInsets: ScrollOptions.EdgeInsets = ScrollOptions.EdgeInsets(),
scrollsToTop: Bool = true,
showsHorizontalScrollIndicator: Bool = true,
showsVerticalScrollIndicator: Bool = true,
zoomScale: Float = 1.0
) {
self.style = style
self.alwaysBounceHorizontal = alwaysBounceHorizontal
self.alwaysBounceVertical = alwaysBounceVertical
self.bounces = bounces
self.bouncesZoom = bouncesZoom
self.contentInset = contentInset
self.isDirectionalLockEnabled = isDirectionalLockEnabled
self.isPagingEnabled = isPagingEnabled
self.indicatorStyle = indicatorStyle
self.isScrollEnabled = isScrollEnabled
self.maximumZoomScale = maximumZoomScale
self.minimumZoomScale = minimumZoomScale
self.scrollIndicatorInsets = scrollIndicatorInsets
self.scrollsToTop = scrollsToTop
self.showsVerticalScrollIndicator = showsVerticalScrollIndicator
self.showsHorizontalScrollIndicator = showsHorizontalScrollIndicator
self.zoomScale = zoomScale
scrollOptions = ScrollOptions(
alwaysBounceHorizontal: alwaysBounceHorizontal,
alwaysBounceVertical: alwaysBounceVertical,
bounces: bounces,
bouncesZoom: bouncesZoom,
contentInset: contentInset,
indicatorStyle: indicatorStyle,
isDirectionalLockEnabled: isDirectionalLockEnabled,
isPagingEnabled: isPagingEnabled,
isScrollEnabled: isScrollEnabled,
maximumZoomScale: maximumZoomScale,
minimumZoomScale: minimumZoomScale,
scrollIndicatorInsets: scrollIndicatorInsets,
scrollsToTop: scrollsToTop,
showsHorizontalScrollIndicator: showsHorizontalScrollIndicator,
showsVerticalScrollIndicator: showsVerticalScrollIndicator,
zoomScale: zoomScale
)
}
}

Expand Down
90 changes: 90 additions & 0 deletions Sources/Tokamak/Components/Props/ScrollOptions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//
// ScrollOptions.swift
// Tokamak
//
// Created by Matvii Hodovaniuk on 3/27/19.
//

public struct ScrollOptions: Equatable, Default {
public static var defaultValue: ScrollOptions {
return ScrollOptions()
}

public struct EdgeInsets: Equatable {
public let bottom: Float
public let left: Float
public let right: Float
public let top: Float

public init(
top: Float = 0,
left: Float = 0,
bottom: Float = 0,
right: Float = 0
) {
self.top = top
self.left = left
self.bottom = bottom
self.right = right
}
}

public enum IndicatorStyle {
case `default`
case black
case white
}

public let alwaysBounceHorizontal: Bool
public let alwaysBounceVertical: Bool
public let bounces: Bool
public let bouncesZoom: Bool
public let contentInset: EdgeInsets
public let indicatorStyle: IndicatorStyle
public let isDirectionalLockEnabled: Bool
public let isPagingEnabled: Bool
public let isScrollEnabled: Bool
public let maximumZoomScale: Float
public let minimumZoomScale: Float
public let scrollIndicatorInsets: EdgeInsets
public let scrollsToTop: Bool
public let showsHorizontalScrollIndicator: Bool
public let showsVerticalScrollIndicator: Bool
public let zoomScale: Float

public init(
alwaysBounceHorizontal: Bool = false,
alwaysBounceVertical: Bool = false,
bounces: Bool = true,
bouncesZoom: Bool = true,
contentInset: EdgeInsets = EdgeInsets(),
indicatorStyle: IndicatorStyle = .default,
isDirectionalLockEnabled: Bool = false,
isPagingEnabled: Bool = false,
isScrollEnabled: Bool = true,
maximumZoomScale: Float = 1.0,
minimumZoomScale: Float = 1.0,
scrollIndicatorInsets: EdgeInsets = EdgeInsets(),
scrollsToTop: Bool = true,
showsHorizontalScrollIndicator: Bool = true,
showsVerticalScrollIndicator: Bool = true,
zoomScale: Float = 1.0
) {
self.alwaysBounceHorizontal = alwaysBounceHorizontal
self.alwaysBounceVertical = alwaysBounceVertical
self.bounces = bounces
self.bouncesZoom = bouncesZoom
self.contentInset = contentInset
self.isDirectionalLockEnabled = isDirectionalLockEnabled
self.isPagingEnabled = isPagingEnabled
self.indicatorStyle = indicatorStyle
self.isScrollEnabled = isScrollEnabled
self.maximumZoomScale = maximumZoomScale
self.minimumZoomScale = minimumZoomScale
self.scrollIndicatorInsets = scrollIndicatorInsets
self.scrollsToTop = scrollsToTop
self.showsVerticalScrollIndicator = showsVerticalScrollIndicator
self.showsHorizontalScrollIndicator = showsHorizontalScrollIndicator
self.zoomScale = zoomScale
}
}
4 changes: 4 additions & 0 deletions Sources/TokamakUIKit/Components/Host/CollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ extension CollectionView: UIViewComponent {
return
}

if let scrollOptions = props.scrollOptions {
applyScrollOptions(box, scrollOptions)
}

box.props = props
}
}
4 changes: 4 additions & 0 deletions Sources/TokamakUIKit/Components/Host/ListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ extension ListView: UIViewComponent {
return
}

if let scrollOptions = props.scrollOptions {
applyScrollOptions(box, scrollOptions)
}

box.props = props
}
}
Loading

0 comments on commit b2cd397

Please sign in to comment.