Skip to content

Commit b80976d

Browse files
committed
Added diffable collection view with same logic. Updated side bar controller and clean code. Added codestyle and to-do list.
1 parent 9ff2955 commit b80976d

File tree

9 files changed

+143
-59
lines changed

9 files changed

+143
-59
lines changed

CODESTYLE.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Documentation
2+
3+
## Using marks
4+
5+
For clean struct code good is using marks.
6+
7+
```swift
8+
class Example {
9+
10+
// MARK: - Init
11+
12+
init() {}
13+
}
14+
```
15+
16+
Here you find all which using in project.
17+
18+
19+
- // MARK: - Init
20+
- // MARK: - Configure
21+
- // MARK: - Lifecycle
22+
- // MARK: - Properties
23+
24+
25+
If you can't find valid, add new to codestyle agreements please. Other can be use wit if class is large and need struct it even without adding to codestyle agreements.

SPDiffable.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22

33
s.name = 'SPDiffable'
4-
s.version = '1.1.6'
4+
s.version = '1.1.7'
55
s.summary = 'Extenshion of Diffable API which allow not duplicate code and use less models.'
66
s.homepage = 'https://github.com/ivanvorobei/SPDiffable'
77
s.source = { :git => 'https://github.com/ivanvorobei/SPDiffable.git', :tag => s.version }

SPDiffable.xcodeproj/project.pbxproj

+12-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
F4920A6424F3BFFA007F7EE9 /* SPDiffableWrapperItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4920A6324F3BFFA007F7EE9 /* SPDiffableWrapperItem.swift */; };
1818
F4920A6924F447D6007F7EE9 /* SPDiffableTableCellProviders.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4920A6824F447D6007F7EE9 /* SPDiffableTableCellProviders.swift */; };
1919
F4920A6C24F44834007F7EE9 /* SPDiffableCollectionCellProviders.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4920A6B24F44834007F7EE9 /* SPDiffableCollectionCellProviders.swift */; };
20+
F4AB9BE326315F4400EC27A5 /* SPDiffableCollectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4AB9BE226315F4400EC27A5 /* SPDiffableCollectionView.swift */; };
21+
F4AB9BE9263161DA00EC27A5 /* SPDiffableSideBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4AB9BE8263161DA00EC27A5 /* SPDiffableSideBarController.swift */; };
2022
F4D77FAC260B2FFC004660B8 /* SPDiffableCollectionActionableItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4D77FAB260B2FFC004660B8 /* SPDiffableCollectionActionableItem.swift */; };
2123
F4FBB993260912A50036E67D /* SPDiffableCollectionDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4FBB98726090FFE0036E67D /* SPDiffableCollectionDelegate.swift */; };
2224
F4FBB995260912A70036E67D /* SPDiffableTableDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4FBB98B260910D40036E67D /* SPDiffableTableDelegate.swift */; };
@@ -25,7 +27,6 @@
2527
OBJ_53 /* SPDiffableCollectionController.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_10 /* SPDiffableCollectionController.swift */; };
2628
OBJ_54 /* SPDiffableCollectionDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_11 /* SPDiffableCollectionDataSource.swift */; };
2729
OBJ_55 /* SPDiffableSideBarButtonCollectionViewListCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_12 /* SPDiffableSideBarButtonCollectionViewListCell.swift */; };
28-
OBJ_56 /* SPDiffableSideBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_13 /* SPDiffableSideBarController.swift */; };
2930
OBJ_57 /* SPDiffableSideBarButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_16 /* SPDiffableSideBarButton.swift */; };
3031
OBJ_58 /* SPDiffableSideBarHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_17 /* SPDiffableSideBarHeader.swift */; };
3132
OBJ_59 /* SPDiffableSideBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_18 /* SPDiffableSideBarItem.swift */; };
@@ -85,6 +86,10 @@
8586
F4920A6824F447D6007F7EE9 /* SPDiffableTableCellProviders.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SPDiffableTableCellProviders.swift; sourceTree = "<group>"; };
8687
F4920A6B24F44834007F7EE9 /* SPDiffableCollectionCellProviders.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SPDiffableCollectionCellProviders.swift; sourceTree = "<group>"; };
8788
F4977BC6252E079A00B20771 /* Example iOS.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = "Example iOS.entitlements"; sourceTree = "<group>"; };
89+
F4AB9BE226315F4400EC27A5 /* SPDiffableCollectionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SPDiffableCollectionView.swift; sourceTree = "<group>"; };
90+
F4AB9BE626315FB400EC27A5 /* CODESTYLE.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = CODESTYLE.md; sourceTree = "<group>"; };
91+
F4AB9BE8263161DA00EC27A5 /* SPDiffableSideBarController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SPDiffableSideBarController.swift; sourceTree = "<group>"; };
92+
F4AB9BED263163B600EC27A5 /* TODO.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = TODO.md; sourceTree = "<group>"; };
8893
F4D6FB12256646FC0046A328 /* FUNDING.yml */ = {isa = PBXFileReference; lastKnownFileType = text.yaml; path = FUNDING.yml; sourceTree = "<group>"; };
8994
F4D6FB16256646FD0046A328 /* .gitignore */ = {isa = PBXFileReference; lastKnownFileType = text; path = .gitignore; sourceTree = "<group>"; };
9095
F4D77FAB260B2FFC004660B8 /* SPDiffableCollectionActionableItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SPDiffableCollectionActionableItem.swift; sourceTree = "<group>"; };
@@ -93,7 +98,6 @@
9398
OBJ_10 /* SPDiffableCollectionController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SPDiffableCollectionController.swift; sourceTree = "<group>"; };
9499
OBJ_11 /* SPDiffableCollectionDataSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SPDiffableCollectionDataSource.swift; sourceTree = "<group>"; };
95100
OBJ_12 /* SPDiffableSideBarButtonCollectionViewListCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SPDiffableSideBarButtonCollectionViewListCell.swift; sourceTree = "<group>"; };
96-
OBJ_13 /* SPDiffableSideBarController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SPDiffableSideBarController.swift; sourceTree = "<group>"; };
97101
OBJ_16 /* SPDiffableSideBarButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SPDiffableSideBarButton.swift; sourceTree = "<group>"; };
98102
OBJ_17 /* SPDiffableSideBarHeader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SPDiffableSideBarHeader.swift; sourceTree = "<group>"; };
99103
OBJ_18 /* SPDiffableSideBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SPDiffableSideBarItem.swift; sourceTree = "<group>"; };
@@ -288,6 +292,8 @@
288292
OBJ_47 /* README.md */,
289293
F4D6FB16256646FD0046A328 /* .gitignore */,
290294
OBJ_45 /* SPDiffable.podspec */,
295+
F4AB9BE626315FB400EC27A5 /* CODESTYLE.md */,
296+
F4AB9BED263163B600EC27A5 /* TODO.md */,
291297
OBJ_6 /* Package.swift */,
292298
OBJ_7 /* Sources */,
293299
F47A41CF24EB222C0054DBB2 /* Example iOS */,
@@ -322,8 +328,9 @@
322328
children = (
323329
OBJ_15 /* Models */,
324330
OBJ_10 /* SPDiffableCollectionController.swift */,
331+
F4AB9BE226315F4400EC27A5 /* SPDiffableCollectionView.swift */,
325332
OBJ_12 /* SPDiffableSideBarButtonCollectionViewListCell.swift */,
326-
OBJ_13 /* SPDiffableSideBarController.swift */,
333+
F4AB9BE8263161DA00EC27A5 /* SPDiffableSideBarController.swift */,
327334
);
328335
path = Collection;
329336
sourceTree = "<group>";
@@ -434,8 +441,9 @@
434441
OBJ_54 /* SPDiffableCollectionDataSource.swift in Sources */,
435442
F4920A6C24F44834007F7EE9 /* SPDiffableCollectionCellProviders.swift in Sources */,
436443
OBJ_55 /* SPDiffableSideBarButtonCollectionViewListCell.swift in Sources */,
437-
OBJ_56 /* SPDiffableSideBarController.swift in Sources */,
438444
OBJ_57 /* SPDiffableSideBarButton.swift in Sources */,
445+
F4AB9BE326315F4400EC27A5 /* SPDiffableCollectionView.swift in Sources */,
446+
F4AB9BE9263161DA00EC27A5 /* SPDiffableSideBarController.swift in Sources */,
439447
F4920A6924F447D6007F7EE9 /* SPDiffableTableCellProviders.swift in Sources */,
440448
OBJ_58 /* SPDiffableSideBarHeader.swift in Sources */,
441449
OBJ_59 /* SPDiffableSideBarItem.swift in Sources */,

Sources/SPDiffable/Collection/SPDiffableCollectionController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ open class SPDiffableCollectionController: UICollectionViewController {
5555
// MARK: - UICollectionViewDelegate
5656

5757
open override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
58-
guard let item = diffableDataSource?.itemIdentifier(for: indexPath) else { return }
58+
guard let item = diffableDataSource?.item(for: indexPath) else { return }
5959
diffableDelegate?.diffableCollectionView?(collectionView, didSelectItem: item)
6060
switch item {
6161
case let model as SPDiffableCollectionActionableItem:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// The MIT License (MIT)
2+
// Copyright © 2020 Ivan Vorobei (hello@ivanvorobei.by)
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in all
12+
// copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
// SOFTWARE.
21+
22+
import UIKit
23+
24+
/**
25+
SPDiffable: Basic diffable collection view.
26+
27+
For common init call `setCellProviders` with default data and providers for it models.
28+
If need init manually, shoud init `diffableDataSource` first, and next apply content when you need it.
29+
*/
30+
@available(iOS 13.0, *)
31+
open class SPDiffableCollectionView: UICollectionView, UICollectionViewDelegate {
32+
33+
// MARK: - Properties
34+
35+
open var diffableDataSource: SPDiffableCollectionDataSource?
36+
37+
open weak var diffableDelegate: SPDiffableCollectionDelegate?
38+
39+
// MARK: - Init
40+
41+
public init() {
42+
super.init(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
43+
commonInit()
44+
}
45+
46+
required public init?(coder: NSCoder) {
47+
super.init(coder: coder)
48+
commonInit()
49+
}
50+
51+
private func commonInit() {
52+
delegate = self
53+
}
54+
55+
// MARK: - Configure
56+
57+
/**
58+
SPDiffable: Init `diffableDataSource` and apply content to data source without animation.
59+
60+
If need custom logic, you can manually init and apply data when you need.
61+
62+
- warning: Changes applied not animatable.
63+
- parameter providers: Cell Providers with valid order for processing.
64+
- parameter sections: Content as array of `SPDiffableSection`.
65+
*/
66+
open func setCellProviders( _ providers: [SPDiffableCollectionCellProvider], sections: [SPDiffableSection]) {
67+
diffableDataSource = SPDiffableCollectionDataSource(collectionView: self, cellProviders: providers)
68+
diffableDataSource?.apply(sections, animated: false)
69+
}
70+
71+
// MARK: - UICollectionViewDelegate
72+
73+
open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
74+
guard let item = diffableDataSource?.item(for: indexPath) else { return }
75+
diffableDelegate?.diffableCollectionView?(collectionView, didSelectItem: item)
76+
switch item {
77+
case let model as SPDiffableCollectionActionableItem:
78+
model.action?(indexPath)
79+
default:
80+
break
81+
}
82+
}
83+
}

Sources/SPDiffable/Collection/SPDiffableSideBarController.swift

+17-52
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,28 @@ import UIKit
3131
Layout detect data source and automatically set header and footer mode.
3232
*/
3333
@available(iOS 14, *)
34-
open class SPDiffableSideBarController: UIViewController, UICollectionViewDelegate {
34+
open class SPDiffableSideBarController: SPDiffableCollectionController {
3535

36-
// MARK: - Properties
36+
// MARK: - Init
3737

38-
open lazy var collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
38+
public init() {
39+
super.init(collectionViewLayout: UICollectionViewFlowLayout())
40+
commonInit()
41+
42+
}
43+
44+
public required init?(coder: NSCoder) {
45+
super.init(coder: coder)
46+
commonInit()
47+
}
3948

40-
open var diffableDataSource: SPDiffableCollectionDataSource?
49+
private func commonInit() {
50+
collectionView.setCollectionViewLayout(makeLayout(), animated: false)
51+
}
4152

42-
open weak var diffableDelegate: SPDiffableCollectionDelegate?
53+
// MARK: - Layout
4354

44-
private var layout: UICollectionViewCompositionalLayout {
55+
private func makeLayout() -> UICollectionViewCompositionalLayout {
4556
return UICollectionViewCompositionalLayout { [weak self] (section, layoutEnvironment) -> NSCollectionLayoutSection? in
4657
var configuration = UICollectionLayoutListConfiguration(appearance: .sidebar)
4758
let header = self?.diffableDataSource?.snapshot().sectionIdentifiers[section].header
@@ -51,50 +62,4 @@ open class SPDiffableSideBarController: UIViewController, UICollectionViewDelega
5162
return NSCollectionLayoutSection.list(using: configuration, layoutEnvironment: layoutEnvironment)
5263
}
5364
}
54-
55-
// MARK: - Configure
56-
57-
/**
58-
SPDiffable: Init `diffableDataSource` and apply content to data source without animation.
59-
60-
If need custom logic, you can manually init and apply data when you need.
61-
62-
- warning: Changes applied not animatable.
63-
- parameter providers: Cell Providers with valid order for processing.
64-
- parameter sections: Content as array of `SPDiffableSection`.
65-
*/
66-
public func setCellProviders( _ providers: [SPDiffableCollectionCellProvider], sections: [SPDiffableSection]) {
67-
diffableDataSource = SPDiffableCollectionDataSource(collectionView: collectionView, cellProviders: providers)
68-
diffableDataSource?.apply(sections, animated: false)
69-
}
70-
71-
// MARK: - Lifecycle
72-
73-
open override func viewDidLoad() {
74-
super.viewDidLoad()
75-
collectionView.delegate = self
76-
collectionView.translatesAutoresizingMaskIntoConstraints = false
77-
view.addSubview(collectionView)
78-
NSLayoutConstraint.activate([
79-
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
80-
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
81-
collectionView.topAnchor.constraint(equalTo: view.topAnchor),
82-
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
83-
])
84-
}
85-
86-
// MARK: - UICollectionViewDelegate
87-
88-
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
89-
guard let item = diffableDataSource?.itemIdentifier(for: indexPath) else { return }
90-
diffableDelegate?.diffableCollectionView?(collectionView, didSelectItem: item)
91-
switch item {
92-
case let item as SPDiffableSideBarItem:
93-
item.action(indexPath)
94-
case let item as SPDiffableSideBarButton:
95-
item.action(indexPath)
96-
default:
97-
break
98-
}
99-
}
10065
}

Sources/SPDiffable/DataSource/SPDiffableCollectionDataSource.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ open class SPDiffableCollectionDataSource: UICollectionViewDiffableDataSource<SP
4747
}
4848
}
4949

50-
// MARK: - Apply Wrappers
50+
// MARK: - Apply Content
5151

5252
/**
5353
SPDiffable: Applying sections to current snapshot.

TODO.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# To Do List
2+
3+
- Update `SPDiffableCollectionDelegate` & `SPDiffableTableDelegate` for process index path.

0 commit comments

Comments
 (0)