Skip to content

Commit

Permalink
Merge pull request #13 from voyages-sncf-technologies/fix-collor-diff
Browse files Browse the repository at this point in the history
Fix collor diff
  • Loading branch information
gdeff authored Jul 13, 2021
2 parents 106060a + 8b48c98 commit cdaf9ad
Show file tree
Hide file tree
Showing 11 changed files with 468 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Collor.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Collor'
s.version = '1.1.40'
s.version = '1.1.41'
s.summary = 'A declarative data-oriented framework for UICollectionView.'
s.homepage = 'https://github.com/voyages-sncf-technologies/Collor'
s.screenshots = 'https://raw.githubusercontent.com/voyages-sncf-technologies/Collor/master/resources/screenshot.jpg'
Expand Down
31 changes: 31 additions & 0 deletions Collor/Classes/UICollectionView+CollectionDescriptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import UIKit
import Foundation

extension UICollectionView {

@available(*, deprecated, message: "Use `perforCollorDiff(_:completion)` instead.")
public func performUpdates(with result: UpdateCollectionResult, completion: ((Bool) -> Void)? = nil) {
self.performBatchUpdates({
self.insertSections(result.insertedSectionsIndexSet)
Expand All @@ -32,6 +34,35 @@ extension UICollectionView {
}
}, completion: completion)
}

public func performCollorDiff(_ updates: () -> UpdateCollectionResult?,
completion: ((Bool) -> Void)? = nil) {
self.performBatchUpdates({
guard let result = updates() else {
return
}

self.insertSections(result.insertedSectionsIndexSet)
self.deleteSections(result.deletedSectionsIndexSet)
self.reloadSections(result.reloadedSectionsIndexSet)
let insertedIndexPaths = result.insertedIndexPaths.filter {
!result.insertedSectionsIndexSet.contains($0.section)
}
self.insertItems(at: insertedIndexPaths)
let deletedIndexPaths = result.deletedIndexPaths.filter {
!result.deletedSectionsIndexSet.contains($0.section)
}
self.deleteItems(at: deletedIndexPaths)
let reloadedIndexPaths = result.reloadedIndexPaths.filter {
!result.reloadedSectionsIndexSet.contains($0.section)
}
self.reloadItems(at: reloadedIndexPaths)
result.movedIndexPaths.forEach { (from,to) in
self.moveItem(at: from, to: to)
}
}, completion: completion)
}

}

fileprivate extension Equatable {
Expand Down
48 changes: 48 additions & 0 deletions Example/Collor.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
607FACE01AFB9204008FA782 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 607FACDE1AFB9204008FA782 /* LaunchScreen.xib */; };
C251BD9E2165AC3E002DBA69 /* NiblessCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = C251BD9D2165AC3E002DBA69 /* NiblessCollectionViewCell.swift */; };
D087F571AC64EBF37DB329D2 /* Pods_Collor_Example.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DB5457BEEDD2F7D6696609DC /* Pods_Collor_Example.framework */; };
DF0014EB2464ACCA00012172 /* TextFieldViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF0014E92464ACCA00012172 /* TextFieldViewController.swift */; };
DF0014EE2464AEA700012172 /* TextFieldCollectionData.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF0014ED2464AEA700012172 /* TextFieldCollectionData.swift */; };
DF0014F42464AFD500012172 /* TextFieldCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = DF0014F22464AFD500012172 /* TextFieldCell.xib */; };
DF0014F52464AFD500012172 /* TextFieldCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF0014F32464AFD500012172 /* TextFieldCell.swift */; };
DF0014F82464B15D00012172 /* TextFieldSectionDescriptor.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF0014F72464B15D00012172 /* TextFieldSectionDescriptor.swift */; };
DF480064246929CB00A06D4B /* UIView+Autolayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF480063246929CB00A06D4B /* UIView+Autolayout.swift */; };
F01A440B205FE483001BB25E /* TestCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0D318D11EC3362200A9CBEA /* TestCollectionViewCell.swift */; };
F01A440C205FE4AD001BB25E /* TestCollectionViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = F0D318D21EC3362200A9CBEA /* TestCollectionViewCell.xib */; };
F02F6BAF22379C4400C0D9D6 /* SupplementaryViewsHandlerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = F02F6BAE22379C4400C0D9D6 /* SupplementaryViewsHandlerTest.swift */; };
Expand Down Expand Up @@ -143,6 +149,12 @@
C251BD9D2165AC3E002DBA69 /* NiblessCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NiblessCollectionViewCell.swift; sourceTree = "<group>"; };
C28F175CBC84A17630BED1A3 /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
DB5457BEEDD2F7D6696609DC /* Pods_Collor_Example.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Collor_Example.framework; sourceTree = BUILT_PRODUCTS_DIR; };
DF0014E92464ACCA00012172 /* TextFieldViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextFieldViewController.swift; sourceTree = "<group>"; };
DF0014ED2464AEA700012172 /* TextFieldCollectionData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextFieldCollectionData.swift; sourceTree = "<group>"; };
DF0014F22464AFD500012172 /* TextFieldCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = TextFieldCell.xib; sourceTree = "<group>"; };
DF0014F32464AFD500012172 /* TextFieldCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TextFieldCell.swift; sourceTree = "<group>"; };
DF0014F72464B15D00012172 /* TextFieldSectionDescriptor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextFieldSectionDescriptor.swift; sourceTree = "<group>"; };
DF480063246929CB00A06D4B /* UIView+Autolayout.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+Autolayout.swift"; sourceTree = "<group>"; };
F02F6BAE22379C4400C0D9D6 /* SupplementaryViewsHandlerTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SupplementaryViewsHandlerTest.swift; sourceTree = "<group>"; };
F02F6BB22238ED1700C0D9D6 /* IndexPathTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IndexPathTest.swift; sourceTree = "<group>"; };
F02F6BB522391BB000C0D9D6 /* TestReusableView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestReusableView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -351,6 +363,7 @@
DF0014D62464A8C300012172 /* PantoneSample */,
DF0014D72464A8D000012172 /* RandomSample */,
DF0014D82464A8DB00012172 /* WeatherSample */,
DF0014E82464AC2A00012172 /* TextFieldSample */,
);
path = Collor;
sourceTree = "<group>";
Expand Down Expand Up @@ -752,6 +765,35 @@
path = view;
sourceTree = "<group>";
};
DF0014E82464AC2A00012172 /* TextFieldSample */ = {
isa = PBXGroup;
children = (
DF0014EF2464AF6B00012172 /* cell */,
DF0014F62464B14500012172 /* section */,
DF0014E92464ACCA00012172 /* TextFieldViewController.swift */,
DF0014ED2464AEA700012172 /* TextFieldCollectionData.swift */,
DF480063246929CB00A06D4B /* UIView+Autolayout.swift */,
);
path = TextFieldSample;
sourceTree = "<group>";
};
DF0014EF2464AF6B00012172 /* cell */ = {
isa = PBXGroup;
children = (
DF0014F32464AFD500012172 /* TextFieldCell.swift */,
DF0014F22464AFD500012172 /* TextFieldCell.xib */,
);
path = cell;
sourceTree = "<group>";
};
DF0014F62464B14500012172 /* section */ = {
isa = PBXGroup;
children = (
DF0014F72464B15D00012172 /* TextFieldSectionDescriptor.swift */,
);
path = section;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -849,6 +891,7 @@
F07823591F5E7D8E00FE9CC0 /* TweetInfoCollectionViewCell.xib in Resources */,
F0CEDE252200B4C900817466 /* LetterCollectionReusableView.xib in Resources */,
F0D318D61EC3386700A9CBEA /* TestCollectionViewCell.xib in Resources */,
DF0014F42464AFD500012172 /* TextFieldCell.xib in Resources */,
607FACE01AFB9204008FA782 /* LaunchScreen.xib in Resources */,
F0D3ED5C1F41E8340087CBD2 /* MenuViewController.xib in Resources */,
F089D9B01F3861A500395056 /* LabelCollectionViewCell.xib in Resources */,
Expand Down Expand Up @@ -1001,8 +1044,12 @@
F0D3ED681F41F5EA0087CBD2 /* MenuItemAdapter.swift in Sources */,
F0CEDE132200B09B00817466 /* AlphabetCollectionData.swift in Sources */,
F0577CCB1F39EA420051BD18 /* PantoneColorAdapter.swift in Sources */,
DF0014EE2464AEA700012172 /* TextFieldCollectionData.swift in Sources */,
DF0014F82464B15D00012172 /* TextFieldSectionDescriptor.swift in Sources */,
DF0014F52464AFD500012172 /* TextFieldCell.swift in Sources */,
F07823511F5E7D5700FE9CC0 /* TweetAdapter.swift in Sources */,
F089D9C91F386FAA00395056 /* RandomCollectionData.swift in Sources */,
DF0014EB2464ACCA00012172 /* TextFieldViewController.swift in Sources */,
F078234D1F5E7CFB00FE9CC0 /* TweetSectionDescriptor.swift in Sources */,
F0CEDE182200B0CC00817466 /* AlphabetModel.swift in Sources */,
F0577CCC1F39EA420051BD18 /* PantoneColorCollectionViewCell.swift in Sources */,
Expand All @@ -1023,6 +1070,7 @@
F0577CBD1F39C6910051BD18 /* MetallicPantone.swift in Sources */,
F0CEDE152200B09B00817466 /* AlphabetViewController.swift in Sources */,
F0E987801F5FCC50001870F7 /* RealTimeLayout.swift in Sources */,
DF480064246929CB00A06D4B /* UIView+Autolayout.swift in Sources */,
F089D9AA1F3861A500395056 /* WeatherCollectionData.swift in Sources */,
F078235B1F5E812B00FE9CC0 /* RealTimeAdapters.swift in Sources */,
F089D9B31F3861A500395056 /* WeatherStyle.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,17 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
codeCoverageEnabled = "YES"
shouldUseLaunchSchemeArgsEnv = "YES">
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "607FACCF1AFB9204008FA782"
BuildableName = "Collor_Example.app"
BlueprintName = "Collor_Example"
ReferencedContainer = "container:Collor.xcodeproj">
</BuildableReference>
</MacroExpansion>
<Testables>
<TestableReference
skipped = "NO">
Expand All @@ -54,17 +63,6 @@
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "607FACCF1AFB9204008FA782"
BuildableName = "Collor_Example.app"
BlueprintName = "Collor_Example"
ReferencedContainer = "container:Collor.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
Expand All @@ -86,8 +84,6 @@
ReferencedContainer = "container:Collor.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down
13 changes: 7 additions & 6 deletions Example/Collor/MenuSample/MenuViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import Collor

// model
struct Example {
let title:String
let controllerClass:UIViewController.Type
let title: String
let controllerClass: UIViewController.Type
}

// user event
enum MenuUserEvent {
case itemTap(Example)
}

protocol MenuUserEventDelegate : CollectionUserEventDelegate {
func onUserEvent(userEvent:MenuUserEvent)
protocol MenuUserEventDelegate: CollectionUserEventDelegate {
func onUserEvent(userEvent: MenuUserEvent)
}

// controller
Expand All @@ -36,7 +36,8 @@ class MenuViewController: UIViewController {
Example(title: "Random", controllerClass: RandomViewController.self),
Example(title: "Weather", controllerClass: WeatherViewController.self),
Example(title: "Real Time", controllerClass: RealTimeViewController.self),
Example(title: "Alphabet", controllerClass: AlphabetViewController.self)]
Example(title: "Alphabet", controllerClass: AlphabetViewController.self),
Example(title: "TextField", controllerClass: TextFieldViewController.self)]


lazy var collectionData: MenuCollectionData = MenuCollectionData(examples: self.examples)
Expand All @@ -51,7 +52,7 @@ class MenuViewController: UIViewController {
}
}

extension MenuViewController : MenuUserEventDelegate {
extension MenuViewController: MenuUserEventDelegate {
func onUserEvent(userEvent: MenuUserEvent) {
switch userEvent {
case .itemTap(let example):
Expand Down
43 changes: 43 additions & 0 deletions Example/Collor/TextFieldSample/TextFieldCollectionData.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// TextFieldCollectionData.swift
// Collor_Example
//
// Created by Deffrasnes Ghislain on 07/05/2020.
// Copyright © 2020 CocoaPods. All rights reserved.
//

import Foundation
import Collor

final class TextFieldCollectionData: CollectionData {

var numberOfCell = 2

var sectionToReload: CollectionSectionDescribable?

override func reloadData() {
super.reloadData()

let section1 = TextFieldSectionDescriptor().uid("section1_id").reloadSection { cells in
let cellDescriptor = TextFieldDescriptor(adapter: TextFieldAdapter()).uid("cell1_id")
cells.append(cellDescriptor)
}
sections.append(section1)

let section2 = TextFieldSectionDescriptor().uid("section2_id").reloadSection { [weak self] cells in
guard let self = self else {
return
}

if self.numberOfCell == 2 {
let cellDescriptor = TextFieldDescriptor(adapter: TextFieldAdapter()).uid("cell2_id")
cells.append(cellDescriptor)
}

self.numberOfCell = self.numberOfCell == 2 ? 1 : 2
}
sections.append(section2)
self.sectionToReload = section2
}

}
Loading

0 comments on commit cdaf9ad

Please sign in to comment.