Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
xx messenger iOS
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mobile
iOS
xx messenger iOS
Commits
6ec5b742
Commit
6ec5b742
authored
2 years ago
by
Dariusz Rybicki
Browse files
Options
Downloads
Patches
Plain Diff
Add CellFactory
parent
07f51aeb
No related branches found
No related tags found
2 merge requests
!54
Releasing 1.1.4
,
!51
CollectionView library
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Sources/CollectionView/CellFactory.swift
+64
-0
64 additions, 0 deletions
Sources/CollectionView/CellFactory.swift
Tests/CollectionViewTests/CellFactoryTests.swift
+75
-0
75 additions, 0 deletions
Tests/CollectionViewTests/CellFactoryTests.swift
with
139 additions
and
0 deletions
Sources/CollectionView/CellFactory.swift
0 → 100644
+
64
−
0
View file @
6ec5b742
import
UIKit
public
struct
CellFactory
<
Model
>
{
public
struct
Registrar
{
public
var
register
:
(
UICollectionView
)
->
Void
public
func
callAsFunction
(
in
view
:
UICollectionView
)
{
register
(
view
)
}
}
public
struct
Builder
{
public
var
buildCell
:
(
Model
,
UICollectionView
,
IndexPath
)
->
UICollectionViewCell
?
public
func
callAsFunction
(
for
model
:
Model
,
in
view
:
UICollectionView
,
at
indexPath
:
IndexPath
)
->
UICollectionViewCell
?
{
buildCell
(
model
,
view
,
indexPath
)
}
}
public
var
register
:
Registrar
public
var
build
:
Builder
public
init
(
register
:
Registrar
,
build
:
Builder
)
{
self
.
register
=
register
self
.
build
=
build
}
}
extension
CellFactory
{
public
static
func
combined
(
_
factories
:
CellFactory
...
)
->
CellFactory
{
combined
(
factories
)
}
public
static
func
combined
(
_
factories
:
[
CellFactory
])
->
CellFactory
{
CellFactory
(
register
:
.
init
{
collectionView
in
factories
.
forEach
{
$0
.
register
(
in
:
collectionView
)
}
},
build
:
.
init
{
model
,
collectionView
,
indexPath
in
factories
.
lazy
.
compactMap
{
$0
.
build
(
for
:
model
,
in
:
collectionView
,
at
:
indexPath
)
}
.
first
}
)
}
}
#if DEBUG
extension
CellFactory
{
public
static
func
failing
()
->
CellFactory
{
CellFactory
(
register
:
.
init
{
_
in
fatalError
(
"Not implemented"
)
},
build
:
.
init
{
_
,
_
,
_
in
fatalError
(
"Not implemented"
)
}
)
}
}
#endif
This diff is collapsed.
Click to expand it.
Tests/CollectionViewTests/CellFactoryTests.swift
0 → 100644
+
75
−
0
View file @
6ec5b742
import
XCTest
@testable
import
CollectionView
final
class
CellFactoryTests
:
XCTestCase
{
func
testCombined
()
{
var
didRegisterFirst
=
[
UICollectionView
]()
var
didRegisterSecond
=
[
UICollectionView
]()
var
didRegisterThird
=
[
UICollectionView
]()
class
Cell
:
UICollectionViewCell
{
var
collectionView
:
UICollectionView
?
var
indexPath
:
IndexPath
?
}
let
factory
=
CellFactory
<
Int
>.
combined
(
.
init
(
register
:
.
init
{
didRegisterFirst
.
append
(
$0
)
},
build
:
.
init
{
model
,
collectionView
,
indexPath
in
guard
model
==
1
else
{
return
nil
}
let
cell
=
Cell
()
cell
.
collectionView
=
collectionView
cell
.
indexPath
=
indexPath
return
cell
}
),
.
init
(
register
:
.
init
{
didRegisterSecond
.
append
(
$0
)
},
build
:
.
init
{
model
,
collectionView
,
indexPath
in
guard
model
==
2
else
{
return
nil
}
let
cell
=
Cell
()
cell
.
collectionView
=
collectionView
cell
.
indexPath
=
indexPath
return
cell
}
),
.
init
(
register
:
.
init
{
didRegisterThird
.
append
(
$0
)
},
build
:
.
init
{
model
,
collectionView
,
indexPath
in
guard
model
==
3
else
{
return
nil
}
let
cell
=
Cell
()
cell
.
collectionView
=
collectionView
cell
.
indexPath
=
indexPath
return
cell
}
)
)
let
collectionView
=
UICollectionView
(
frame
:
.
zero
,
collectionViewLayout
:
.
init
())
factory
.
register
(
in
:
collectionView
)
XCTAssertEqual
(
didRegisterFirst
,
[
collectionView
])
XCTAssertEqual
(
didRegisterSecond
,
[
collectionView
])
XCTAssertEqual
(
didRegisterThird
,
[
collectionView
])
let
firstCell
=
factory
.
build
(
for
:
1
,
in
:
collectionView
,
at
:
IndexPath
(
item
:
0
,
section
:
1
))
as?
Cell
XCTAssertEqual
(
firstCell
?
.
collectionView
,
collectionView
)
XCTAssertEqual
(
firstCell
?
.
indexPath
,
IndexPath
(
item
:
0
,
section
:
1
))
let
secondCell
=
factory
.
build
(
for
:
2
,
in
:
collectionView
,
at
:
IndexPath
(
item
:
2
,
section
:
3
))
as?
Cell
XCTAssertEqual
(
secondCell
?
.
collectionView
,
collectionView
)
XCTAssertEqual
(
secondCell
?
.
indexPath
,
IndexPath
(
item
:
2
,
section
:
3
))
let
thirdCell
=
factory
.
build
(
for
:
3
,
in
:
collectionView
,
at
:
IndexPath
(
item
:
4
,
section
:
5
))
as?
Cell
XCTAssertEqual
(
thirdCell
?
.
collectionView
,
collectionView
)
XCTAssertEqual
(
thirdCell
?
.
indexPath
,
IndexPath
(
item
:
4
,
section
:
5
))
let
otherCell
=
factory
.
build
(
for
:
4
,
in
:
collectionView
,
at
:
IndexPath
(
item
:
0
,
section
:
0
))
XCTAssertNil
(
otherCell
)
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment