Skip to content

Commit 11f9d73

Browse files
committed
Adjusted all methods to manage row in order to work indexes instead of row objects
1 parent 5bec889 commit 11f9d73

File tree

8 files changed

+117
-65
lines changed

8 files changed

+117
-65
lines changed

ScrollStackController.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
64C0228623475A0E00A6D844 /* ScrollStack+Support.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64C0228523475A0E00A6D844 /* ScrollStack+Support.swift */; };
3737
64C0228723475A0E00A6D844 /* ScrollStack+Support.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64C0228523475A0E00A6D844 /* ScrollStack+Support.swift */; };
3838
64C0228823475A0E00A6D844 /* ScrollStack+Support.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64C0228523475A0E00A6D844 /* ScrollStack+Support.swift */; };
39+
64C0228A2347834300A6D844 /* VC2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64C022892347834300A6D844 /* VC2.swift */; };
3940
DD7502921C690C7A006590AF /* ScrollStackController.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 52D6D9F01BEFFFBE002C0205 /* ScrollStackController.framework */; };
4041
/* End PBXBuildFile section */
4142

@@ -76,6 +77,7 @@
7677
64C022752347360900A6D844 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
7778
64C0227C234753A100A6D844 /* VC1.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VC1.swift; sourceTree = "<group>"; };
7879
64C0228523475A0E00A6D844 /* ScrollStack+Support.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ScrollStack+Support.swift"; sourceTree = "<group>"; };
80+
64C022892347834300A6D844 /* VC2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VC2.swift; sourceTree = "<group>"; };
7981
AD2FAA261CD0B6D800659CF4 /* ScrollStackController.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = ScrollStackController.plist; sourceTree = "<group>"; };
8082
AD2FAA281CD0B6E100659CF4 /* ScrollStackControllerTests.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = ScrollStackControllerTests.plist; sourceTree = "<group>"; };
8183
DD75028D1C690C7A006590AF /* ScrollStackController-tvOS Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "ScrollStackController-tvOS Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -182,6 +184,7 @@
182184
64C022692347360800A6D844 /* SceneDelegate.swift */,
183185
64C0226B2347360800A6D844 /* ViewController.swift */,
184186
64C0227C234753A100A6D844 /* VC1.swift */,
187+
64C022892347834300A6D844 /* VC2.swift */,
185188
64C0226D2347360800A6D844 /* Main.storyboard */,
186189
64C022702347360900A6D844 /* Assets.xcassets */,
187190
64C022722347360900A6D844 /* LaunchScreen.storyboard */,
@@ -454,6 +457,7 @@
454457
64C0228823475A0E00A6D844 /* ScrollStack+Support.swift in Sources */,
455458
64C0227F2347582D00A6D844 /* ScrollStackRow.swift in Sources */,
456459
64C022802347582D00A6D844 /* ScrollStackRowHighlightable.swift in Sources */,
460+
64C0228A2347834300A6D844 /* VC2.swift in Sources */,
457461
64C022822347582D00A6D844 /* ScrollStackViewController.swift in Sources */,
458462
64C0226C2347360800A6D844 /* ViewController.swift in Sources */,
459463
64C022812347582D00A6D844 /* ScrollStackSeparator.swift in Sources */,

ScrollStackController.xcodeproj/xcuserdata/daniele.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,4 @@
33
uuid = "70499C23-D237-437B-A408-4889A202EF2D"
44
type = "1"
55
version = "2.0">
6-
<Breakpoints>
7-
<BreakpointProxy
8-
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
9-
<BreakpointContent
10-
uuid = "62E4AB23-104A-437B-8DEF-038A94AE3EE1"
11-
shouldBeEnabled = "Yes"
12-
ignoreCount = "0"
13-
continueAfterRunningActions = "No"
14-
filePath = "ScrollStackControllerDemo/VC1.swift"
15-
startingColumnNumber = "9223372036854775807"
16-
endingColumnNumber = "9223372036854775807"
17-
startingLineNumber = "19"
18-
endingLineNumber = "19"
19-
landmarkName = "sizeForAxis(_:row:in:)"
20-
landmarkType = "7">
21-
</BreakpointContent>
22-
</BreakpointProxy>
23-
</Breakpoints>
246
</Bucket>

ScrollStackControllerDemo/Base.lproj/Main.storyboard

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,18 @@
5151
</viewController>
5252
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
5353
</objects>
54-
<point key="canvasLocation" x="-339" y="160"/>
54+
<point key="canvasLocation" x="-383" y="29"/>
5555
</scene>
5656
<!--VC1-->
5757
<scene sceneID="F2C-MH-Lwo">
5858
<objects>
5959
<viewController storyboardIdentifier="VC1" id="hnx-R5-b6N" customClass="VC1" customModule="ScrollStackControllerDemo" customModuleProvider="target" sceneMemberID="viewController">
6060
<view key="view" contentMode="scaleToFill" id="aW5-uU-IfU">
61-
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
61+
<rect key="frame" x="0.0" y="0.0" width="414" height="300"/>
6262
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
6363
<subviews>
6464
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" on="YES" translatesAutoresizingMaskIntoConstraints="NO" id="yVI-0r-6fw">
65-
<rect key="frame" x="182.5" y="432.5" width="51" height="31"/>
65+
<rect key="frame" x="182.5" y="134.5" width="51" height="31"/>
6666
</switch>
6767
</subviews>
6868
<color key="backgroundColor" systemColor="systemOrangeColor" red="1" green="0.58431372549019611" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
@@ -72,10 +72,41 @@
7272
</constraints>
7373
<viewLayoutGuide key="safeArea" id="mC8-c9-Zzg"/>
7474
</view>
75+
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
76+
<size key="freeformSize" width="414" height="300"/>
7577
</viewController>
7678
<placeholder placeholderIdentifier="IBFirstResponder" id="i09-Qc-wUC" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
7779
</objects>
78-
<point key="canvasLocation" x="355" y="29"/>
80+
<point key="canvasLocation" x="259" y="-171"/>
81+
</scene>
82+
<!--VC2-->
83+
<scene sceneID="W0p-QS-jfS">
84+
<objects>
85+
<viewController storyboardIdentifier="VC2" id="1Ap-wi-SGJ" customClass="VC2" customModule="ScrollStackControllerDemo" customModuleProvider="target" sceneMemberID="viewController">
86+
<view key="view" contentMode="scaleToFill" id="dZN-dE-Enp">
87+
<rect key="frame" x="0.0" y="0.0" width="414" height="300"/>
88+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
89+
<subviews>
90+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Welcome to Home" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="PXE-1E-oq8">
91+
<rect key="frame" x="20" y="20" width="374" height="25.5"/>
92+
<fontDescription key="fontDescription" type="system" weight="medium" pointSize="21"/>
93+
<nil key="textColor"/>
94+
<nil key="highlightedColor"/>
95+
</label>
96+
</subviews>
97+
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
98+
<constraints>
99+
<constraint firstItem="PXE-1E-oq8" firstAttribute="leading" secondItem="N9x-T6-Cli" secondAttribute="leading" constant="20" id="lCR-B8-Ec4"/>
100+
<constraint firstItem="N9x-T6-Cli" firstAttribute="trailing" secondItem="PXE-1E-oq8" secondAttribute="trailing" constant="20" id="tad-L3-w90"/>
101+
<constraint firstItem="PXE-1E-oq8" firstAttribute="top" secondItem="dZN-dE-Enp" secondAttribute="top" constant="20" id="ufn-3x-2ZS"/>
102+
</constraints>
103+
<viewLayoutGuide key="safeArea" id="N9x-T6-Cli"/>
104+
</view>
105+
<size key="freeformSize" width="414" height="300"/>
106+
</viewController>
107+
<placeholder placeholderIdentifier="IBFirstResponder" id="XEK-KL-pKN" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
108+
</objects>
109+
<point key="canvasLocation" x="257.97101449275362" y="76.339285714285708"/>
79110
</scene>
80111
</scenes>
81112
</document>

ScrollStackControllerDemo/VC1.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import UIKit
1010

1111
public class VC1: UIViewController, ScrollStackContainableController {
1212

13-
public static func create() -> VC1 {
13+
public static func create(backColor: UIColor) -> VC1 {
1414
let storyboard = UIStoryboard(name: "Main", bundle: nil)
15-
return storyboard.instantiateViewController(identifier: "VC1") as! VC1
15+
let vc = storyboard.instantiateViewController(identifier: "VC1") as! VC1
16+
vc.view.backgroundColor = backColor
17+
return vc
1618
}
1719

1820
public func sizeForAxis(_ axis: NSLayoutConstraint.Axis, row: ScrollStackRow, in stackView: ScrollStack) -> CGFloat? {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// VC2.swift
3+
// ScrollStackControllerDemo
4+
//
5+
// Created by Daniele Margutti on 04/10/2019.
6+
// Copyright © 2019 ScrollStackController. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
public class VC2: UIViewController, ScrollStackContainableController {
12+
13+
public static func create(backColor: UIColor) -> VC2 {
14+
let storyboard = UIStoryboard(name: "Main", bundle: nil)
15+
let vc = storyboard.instantiateViewController(identifier: "VC2") as! VC2
16+
vc.view.backgroundColor = backColor
17+
return vc
18+
}
19+
20+
public func sizeForAxis(_ axis: NSLayoutConstraint.Axis, row: ScrollStackRow, in stackView: ScrollStack) -> CGFloat? {
21+
return 140
22+
}
23+
24+
}

ScrollStackControllerDemo/ViewController.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ class ViewController: UIViewController {
3838
}
3939

4040
@IBAction public func toggleAxis() {
41-
let otherVC = VC1.create(backColor: .blue)
42-
stackController.stackView.replaceRowAtIndex(1, withRow: otherVC, animated: true)
41+
let otherVC = VC2.create(backColor: .white)
42+
43+
stackController.stackView.setRowHidden(index: 0, isHidden: true, animated: true)
44+
// stackController.stackView.replaceRowAtIndex(1, withRow: otherVC, animated: true)
4345
// stackController.stackView.moveRowAtIndex(1, to: 2, animated: true)
4446
//stackController.stackView.axis = (stackController.stackView.axis == .horizontal ? .vertical : .horizontal)
4547
}

Sources/ScrollStackController/ScrollStack.swift

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -257,39 +257,30 @@ open class ScrollStack: UIScrollView {
257257
/// - Parameter animated: `true` to perform animated removeal, by default is `false`.
258258
open func removeAllRows(animated: Bool = false) {
259259
rows.forEach {
260-
removeRow($0, animated: animated)
260+
removeRowFromStackView($0, animated: animated)
261261
}
262262
}
263263

264-
/// Remove row at given index and return removed managed controller (if any).
265-
///
266-
/// - Parameter index: index of the row to remove.
267-
/// - Parameter animated: `true` to perform animation to remove item, by default is `false`.
268-
@discardableResult
269-
open func removeRowAtIndex(_ index: Int, animated: Bool = false) -> UIViewController? {
270-
guard index >= 0, index < rows.count else {
271-
return nil
272-
}
273-
return removeRow(rows[index])
274-
}
275-
276264
/// Remove specified row.
277265
///
278266
/// - Parameter row: row instance to remove.
279267
/// - Parameter animated: `true` to perform animation to remove item, by default is `false`.
280268
@discardableResult
281-
open func removeRow(_ row: ScrollStackRow, animated: Bool = false) -> UIViewController? {
269+
open func removeRow(index: Int, animated: Bool = false) -> UIViewController? {
270+
guard let row = safeRowAtIndex(index) else {
271+
return nil
272+
}
282273
return removeRowFromStackView(row, animated: animated)
283274
}
284275

285276
/// Remove passed rows.
286277
///
287-
/// - Parameter rows: rows to remove.
278+
/// - Parameter rowIndexes: indexes of the row to remove.
288279
/// - Parameter animated: `true` to animate the removeal, by default is `false`.
289280
@discardableResult
290-
open func removeRows(_ rows: [ScrollStackRow], animated: Bool = false) -> [UIViewController]? {
291-
return rows.compactMap {
292-
return removeRowFromStackView($0, animated: animated)
281+
open func removeRows(indexes rowIndexes: [Int], animated: Bool = false) -> [UIViewController]? {
282+
return rowIndexes.compactMap {
283+
return removeRowFromStackView(safeRowAtIndex($0), animated: animated)
293284
}
294285
}
295286

@@ -299,15 +290,15 @@ open class ScrollStack: UIScrollView {
299290
/// - Parameter controller: view controller to replace.
300291
/// - Parameter animated: `true` to animate the transition.
301292
/// - Parameter completion: optional callback called at the end of the transition.
302-
open func replaceRowAtIndex(_ sourceIndex: Int, withRow controller: UIViewController, animated: Bool = false, completion: (() -> Void)? = nil) {
293+
open func replaceRow(index sourceIndex: Int, withRow controller: UIViewController, animated: Bool = false, completion: (() -> Void)? = nil) {
303294
guard sourceIndex >= 0, sourceIndex < rows.count else {
304295
return
305296
}
306297

307298
let sourceRow = rows[sourceIndex]
308299

309300
guard animated else {
310-
removeRow(sourceRow)
301+
removeRow(index: sourceRow.index!)
311302
createRowForController(controller, insertAt: sourceIndex, animated: false)
312303
return
313304
}
@@ -332,7 +323,7 @@ open class ScrollStack: UIScrollView {
332323
/// - Parameter destIndex: destination index.
333324
/// - Parameter animated: `true` to animate the transition.
334325
/// - Parameter completion: optional callback called at the end of the transition.
335-
open func moveRowAtIndex(_ sourceIndex: Int, to destIndex: Int, animated: Bool = false, completion: (() -> Void)? = nil) {
326+
open func moveRow(index sourceIndex: Int, to destIndex: Int, animated: Bool = false, completion: (() -> Void)? = nil) {
336327
guard sourceIndex >= 0, sourceIndex < rows.count, destIndex < rows.count else {
337328
return
338329
}
@@ -361,11 +352,15 @@ open class ScrollStack: UIScrollView {
361352
/// Hide/Show row from the stack.
362353
/// Row is always on stack and it's returned from the `rows` property.
363354
///
364-
/// - Parameter row: target row.
355+
/// - Parameter rowIndex: target row index.
365356
/// - Parameter isHidden: `true` to hide the row, `false` to make it visible.
366357
/// - Parameter animated: `true` to perform animated transition.
367358
/// - Parameter completion: completion callback called once the operation did finish.
368-
open func setRowHidden(_ row: ScrollStackRow, isHidden: Bool, animated: Bool, completion: (() -> Void)? = nil) {
359+
open func setRowHidden(index rowIndex: Int, isHidden: Bool, animated: Bool, completion: (() -> Void)? = nil) {
360+
guard let row = safeRowAtIndex(rowIndex) else {
361+
return
362+
}
363+
369364
guard animated else {
370365
row.isHidden = isHidden
371366
return
@@ -384,13 +379,13 @@ open class ScrollStack: UIScrollView {
384379
/// Hide/Show selected rows.
385380
/// Rows is always on stack and it's returned from the `rows` property.
386381
///
387-
/// - Parameter rows: target rows.
382+
/// - Parameter rowIndexes: indexes of the row to hide or show.
388383
/// - Parameter isHidden: `true` to hide the row, `false` to make it visible.
389384
/// - Parameter animated: `true` to perform animated transition.
390385
/// - Parameter completion: completion callback called once the operation did finish.
391-
open func setRowsHidden(_ rows: [ScrollStackRow], isHidden: Bool, animated: Bool, completion: (() -> Void)? = nil) {
392-
rows.forEach {
393-
setRowHidden($0, isHidden: isHidden, animated: animated)
386+
open func setRowsHidden(indexes rowIndexes: [Int], isHidden: Bool, animated: Bool, completion: (() -> Void)? = nil) {
387+
rowIndexes.forEach {
388+
setRowHidden(index: $0, isHidden: isHidden, animated: animated)
394389
}
395390
}
396391

@@ -427,25 +422,25 @@ open class ScrollStack: UIScrollView {
427422
///
428423
/// - Parameter row: target row.
429424
/// - Parameter insets: new insets.
430-
open func setRowInsets(_ row: ScrollStackRow, insets: UIEdgeInsets) {
431-
row.rowInsets = insets
425+
open func setRowInsets(index rowIndex: Int, insets: UIEdgeInsets) {
426+
safeRowAtIndex(rowIndex)?.rowInsets = insets
432427
}
433428

434429
/// Set the ints of the row's content related to the parent row cell.
435430
///
436431
/// - Parameter row: target rows.
437432
/// - Parameter insets: new insets.
438-
open func setRowsInsets(_ row: [ScrollStackRow], insets: UIEdgeInsets) {
439-
row.forEach {
440-
setRowInsets($0, insets: insets)
433+
open func setRowsInsets(indexes rowIndexes: [Int], insets: UIEdgeInsets) {
434+
rowIndexes.forEach {
435+
setRowInsets(index: $0, insets: insets)
441436
}
442437
}
443438

444439
/// Return the visibility status of a row.
445440
///
446-
/// - Parameter row: row to check.
447-
open func isRowVisible(_ row: ScrollStackRow) -> RowVisibility {
448-
guard row.isHidden == false else {
441+
/// - Parameter index: index of the row to check.
442+
open func isRowVisible(index: Int) -> RowVisibility {
443+
guard let row = safeRowAtIndex(index), row.isHidden == false else {
449444
return .hidden
450445
}
451446

@@ -460,18 +455,22 @@ open class ScrollStack: UIScrollView {
460455
/// Return `true` if row is currently hidden.
461456
///
462457
/// - Parameter row: row to check.
463-
open func isRowHidden(_ row: ScrollStackRow) -> Bool {
464-
return row.isHidden
458+
open func isRowHidden(index: Int) -> Bool {
459+
return safeRowAtIndex(index)?.isHidden ?? false
465460
}
466461

467462
// MARK: - Scroll
468463

469464
/// Scroll to the passed row.
470465
///
471-
/// - Parameter row: row to make visible.
466+
/// - Parameter rowIndex: index of the row to make visible.
472467
/// - Parameter location: visibility of the row, location of the center point.
473468
/// - Parameter animated: `true` to perform animated transition.
474-
open func scrollToRow(_ row: ScrollStackRow, at position: ScrollPosition = .automatic, animated: Bool = true) {
469+
open func scrollToRow(index rowIndex: Int, at position: ScrollPosition = .automatic, animated: Bool = true) {
470+
guard let row = safeRowAtIndex(rowIndex) else {
471+
return
472+
}
473+
475474
let rowFrame = convert(row.frame, to: self)
476475

477476
if case .automatic = position {
@@ -522,6 +521,14 @@ open class ScrollStack: UIScrollView {
522521
didChangeAxis(axis)
523522
}
524523

524+
private func safeRowAtIndex(_ index: Int) -> ScrollStackRow? {
525+
guard index >= 0, index < rows.count else {
526+
return nil
527+
}
528+
return rows[index]
529+
}
530+
531+
525532
/// Remove passed row from stack view.
526533
///
527534
/// - Parameter row: row to remove.

0 commit comments

Comments
 (0)