Skip to content

Commit f68ac12

Browse files
Added ‘UITableView’ support
1 parent 5915fe6 commit f68ac12

File tree

5 files changed

+264
-19
lines changed

5 files changed

+264
-19
lines changed

CollectionView.xcodeproj/project.pbxproj

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
945F470620AD939100ACFC93 /* CollectionViewContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 945F470420AD939100ACFC93 /* CollectionViewContainer.swift */; };
1111
945F470720AD939100ACFC93 /* NoContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 945F470520AD939100ACFC93 /* NoContentView.swift */; };
1212
945F470A20AD93B900ACFC93 /* CollectionDataSupport.swift in Sources */ = {isa = PBXBuildFile; fileRef = 945F470920AD93B900ACFC93 /* CollectionDataSupport.swift */; };
13+
94FC3A3721079844001A5D87 /* TableViewContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94FC3A3621079844001A5D87 /* TableViewContainer.swift */; };
14+
94FC3A39210799AE001A5D87 /* Common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94FC3A38210799AE001A5D87 /* Common.swift */; };
1315
BBB36366800A725E123C4949 /* Pods_Fwk.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F6143B2E11C5CE1B573FC0A9 /* Pods_Fwk.framework */; };
1416
D470CDA0144079E140F874BE /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 92CE2094255F79031B39479E /* Foundation.framework */; };
1517
/* End PBXBuildFile section */
@@ -34,6 +36,8 @@
3436
945F470420AD939100ACFC93 /* CollectionViewContainer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionViewContainer.swift; sourceTree = "<group>"; };
3537
945F470520AD939100ACFC93 /* NoContentView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NoContentView.swift; sourceTree = "<group>"; };
3638
945F470920AD93B900ACFC93 /* CollectionDataSupport.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionDataSupport.swift; sourceTree = "<group>"; };
39+
94FC3A3621079844001A5D87 /* TableViewContainer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableViewContainer.swift; sourceTree = "<group>"; };
40+
94FC3A38210799AE001A5D87 /* Common.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Common.swift; sourceTree = "<group>"; };
3741
C1BF7B2C19C833D043967377 /* XCECollectionView.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = XCECollectionView.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3842
F6143B2E11C5CE1B573FC0A9 /* Pods_Fwk.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Fwk.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3943
/* End PBXFileReference section */
@@ -72,8 +76,10 @@
7276
945F470320AD939100ACFC93 /* Core */ = {
7377
isa = PBXGroup;
7478
children = (
75-
945F470420AD939100ACFC93 /* CollectionViewContainer.swift */,
7679
945F470520AD939100ACFC93 /* NoContentView.swift */,
80+
94FC3A38210799AE001A5D87 /* Common.swift */,
81+
94FC3A3621079844001A5D87 /* TableViewContainer.swift */,
82+
945F470420AD939100ACFC93 /* CollectionViewContainer.swift */,
7783
);
7884
path = Core;
7985
sourceTree = "<group>";
@@ -109,7 +115,7 @@
109115
isa = PBXGroup;
110116
children = (
111117
);
112-
name = "$local";
118+
name = $local;
113119
sourceTree = "<group>";
114120
};
115121
EDDAE0CFD4849E28286C42C0 /* Sources */ = {
@@ -219,7 +225,9 @@
219225
buildActionMask = 2147483647;
220226
files = (
221227
945F470720AD939100ACFC93 /* NoContentView.swift in Sources */,
228+
94FC3A3721079844001A5D87 /* TableViewContainer.swift in Sources */,
222229
945F470620AD939100ACFC93 /* CollectionViewContainer.swift in Sources */,
230+
94FC3A39210799AE001A5D87 /* Common.swift in Sources */,
223231
945F470A20AD93B900ACFC93 /* CollectionDataSupport.swift in Sources */,
224232
);
225233
runOnlyForDeploymentPostprocessing = 0;

Sources/Core/CollectionViewContainer.swift

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,6 @@ extension CollectionViewContainer
179179
public
180180
extension CollectionViewContainer
181181
{
182-
typealias SwitchViaOpacity = (
183-
prepare: () -> Void, // call this before animation starts
184-
execute: () -> Void, // call this inside animation block
185-
cleanup: () -> Void // call after animation completion (optional)
186-
)
187-
188182
var showEmptyViaOpacity: SwitchViaOpacity
189183
{
190184
return (
@@ -240,19 +234,12 @@ extension CollectionViewContainer
240234
}
241235
}
242236

243-
//---
244-
245-
public
246-
typealias SimpleCollectionViewContainer = CollectionViewContainer<NoContentView, NoContentView>
237+
// MARK: - Helper aliases
247238

248-
//---
249-
250-
/**
251-
Shorter typealiases, would be especially convenient when use module name as a prefix.
252-
*/
253239
public
254-
typealias Container<Empty: UIView, Failure: UIView> =
240+
typealias CollectionContainer<Empty: UIView, Failure: UIView> =
255241
CollectionViewContainer<Empty, Failure>
256242

257243
public
258-
typealias SimpleContainer = SimpleCollectionViewContainer
244+
typealias StandardCollectionContainer =
245+
CollectionContainer<NoContentView, NoContentView>

Sources/Core/Common.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
3+
MIT License
4+
5+
Copyright (c) 2016 Maxim Khatskevich (maxim@khatskevi.ch)
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.
24+
25+
*/
26+
27+
public
28+
typealias SwitchViaOpacity = (
29+
prepare: () -> Void, // call this before animation starts
30+
execute: () -> Void, // call this inside animation block
31+
finalize: () -> Void // call after animation completion (optional)
32+
)

Sources/Core/TableViewContainer.swift

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
/*
2+
3+
MIT License
4+
5+
Copyright (c) 2016 Maxim Khatskevich (maxim@khatskevi.ch)
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.
24+
25+
*/
26+
27+
import UIKit
28+
29+
//---
30+
31+
open
32+
class TableViewContainer<EmptyPlaceholder, FailurePlaceholder>: UIView
33+
where
34+
EmptyPlaceholder: UIView,
35+
FailurePlaceholder: UIView
36+
{
37+
// MARK: - Subviews
38+
39+
public
40+
let emptyPlaceholder: EmptyPlaceholder
41+
42+
public
43+
let failurePlaceholder: FailurePlaceholder
44+
45+
public
46+
let table: UITableView
47+
48+
// MARK: - Helpers
49+
50+
private
51+
var nestedViews: [UIView]
52+
{
53+
return [emptyPlaceholder, failurePlaceholder, table]
54+
}
55+
56+
// MARK: - Initializers
57+
58+
public
59+
required
60+
init(
61+
showWhenEmpty empty: EmptyPlaceholder = EmptyPlaceholder.init(),
62+
showOnFailure failure: FailurePlaceholder = FailurePlaceholder.init(),
63+
style: UITableViewStyle = .plain
64+
)
65+
{
66+
self.emptyPlaceholder = empty
67+
self.failurePlaceholder = failure
68+
69+
self.table = UITableView(
70+
frame: CGRect.zero,
71+
style: style
72+
)
73+
74+
//---
75+
76+
super.init(frame: .zero)
77+
78+
//--- hierarchy
79+
80+
nestedViews.forEach{ self.addSubview($0) }
81+
82+
//--- layout
83+
84+
translatesAutoresizingMaskIntoConstraints = false
85+
86+
nestedViews.forEach{
87+
88+
$0.translatesAutoresizingMaskIntoConstraints = false
89+
90+
NSLayoutConstraint.activate([
91+
$0.topAnchor.constraint(equalTo: self.topAnchor),
92+
$0.leadingAnchor.constraint(equalTo: self.leadingAnchor),
93+
$0.trailingAnchor.constraint(equalTo: self.trailingAnchor),
94+
$0.bottomAnchor.constraint(equalTo: self.bottomAnchor)
95+
])
96+
}
97+
98+
//--- other settings
99+
100+
self.backgroundColor = .clear
101+
102+
emptyPlaceholder.alpha = 1
103+
emptyPlaceholder.isHidden = false // shown by default
104+
105+
failurePlaceholder.alpha = 1
106+
failurePlaceholder.isHidden = true // hidden
107+
108+
table.backgroundColor = .clear
109+
table.alpha = 1
110+
table.isHidden = true // hidden
111+
}
112+
113+
public
114+
required
115+
init?(coder aDecoder: NSCoder)
116+
{
117+
fatalError("init(coder:) has not been implemented")
118+
}
119+
}
120+
121+
// MARK: - Presentation logic helpers
122+
123+
public
124+
extension TableViewContainer
125+
{
126+
func showEmpty()
127+
{
128+
emptyPlaceholder.isHidden = false
129+
failurePlaceholder.isHidden = true
130+
table.isHidden = true
131+
}
132+
133+
func showFailure()
134+
{
135+
emptyPlaceholder.isHidden = true
136+
failurePlaceholder.isHidden = false
137+
table.isHidden = true
138+
}
139+
140+
func showContent()
141+
{
142+
emptyPlaceholder.isHidden = true
143+
failurePlaceholder.isHidden = true
144+
table.isHidden = false
145+
}
146+
}
147+
148+
public
149+
extension TableViewContainer
150+
{
151+
var showEmptyViaOpacity: SwitchViaOpacity
152+
{
153+
return (
154+
{
155+
self.emptyPlaceholder.alpha = 0
156+
self.bringSubview(toFront: self.emptyPlaceholder)
157+
self.emptyPlaceholder.isHidden = false
158+
},
159+
{
160+
self.emptyPlaceholder.alpha = 1
161+
},
162+
{
163+
self.failurePlaceholder.isHidden = true
164+
self.table.isHidden = true
165+
}
166+
)
167+
}
168+
169+
var showFailureViaOpacity: SwitchViaOpacity
170+
{
171+
return (
172+
{
173+
self.failurePlaceholder.alpha = 0
174+
self.bringSubview(toFront: self.failurePlaceholder)
175+
self.failurePlaceholder.isHidden = false
176+
},
177+
{
178+
self.failurePlaceholder.alpha = 1
179+
},
180+
{
181+
self.emptyPlaceholder.isHidden = true
182+
self.table.isHidden = true
183+
}
184+
)
185+
}
186+
187+
var showContentViaOpacity: SwitchViaOpacity
188+
{
189+
return (
190+
{
191+
self.table.alpha = 0
192+
self.bringSubview(toFront: self.table)
193+
self.table.isHidden = false
194+
},
195+
{
196+
self.table.alpha = 1
197+
},
198+
{
199+
self.emptyPlaceholder.isHidden = true
200+
self.failurePlaceholder.isHidden = true
201+
}
202+
)
203+
}
204+
}
205+
206+
// MARK: - Helper aliases
207+
208+
public
209+
typealias TableContainer<Empty: UIView, Failure: UIView> =
210+
TableViewContainer<Empty, Failure>
211+
212+
public
213+
typealias StandardTableContainer =
214+
TableContainer<NoContentView, NoContentView>

Sources/WithData/CollectionDataSupport.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ import XCECollectionData
33
//---
44

55
extension CollectionViewContainer: CollectionViewWrapper { }
6+
7+
//---
8+
9+
extension TableViewContainer: TableViewWrapper { }

0 commit comments

Comments
 (0)