1 |
//
|
|
2 |
// ChatLayout
|
|
3 |
// StaticViewFactory.swift
|
|
4 |
// https://github.com/ekazaev/ChatLayout
|
|
5 |
//
|
|
6 |
// Created by Eugene Kazaev in 2020-2022.
|
|
7 |
// Distributed under the MIT license.
|
|
8 |
//
|
|
9 |
|
|
10 |
import Foundation
|
|
11 |
import UIKit
|
|
12 |
|
|
13 |
/// A factory that creates optional contained `UIView`s should conform to this protocol.
|
|
14 |
public protocol StaticViewFactory {
|
|
15 |
|
|
16 |
/// A type of the view to build.
|
|
17 |
associatedtype View: UIView
|
|
18 |
|
|
19 |
/// Factory method that will be called by the corresponding container `UIView`
|
|
20 |
/// - Parameter bounds: A bounds rect of the container.
|
|
21 |
/// - Returns: Build `UIView` instance.
|
|
22 |
static func buildView(within bounds: CGRect) -> View?
|
|
23 |
|
|
24 |
}
|
|
25 |
|
|
26 |
/// Default extension build the `UIView` using its default constructor.
|
|
27 |
public extension StaticViewFactory where Self: UIView {
|
|
28 |
|
|
29 |
static func buildView(within bounds: CGRect) -> Self? {
|
! |
30 |
return Self(frame: bounds)
|
! |
31 |
}
|
! |
32 |
|
|
33 |
}
|
|
34 |
|
|
35 |
/// Use this factory to specify that this view should not be build and should be equal to nil within the container.
|
|
36 |
public struct VoidViewFactory: StaticViewFactory {
|
|
37 |
|
|
38 |
/// Nil view placeholder type.
|
|
39 |
public final class VoidView: UIView {
|
|
40 |
|
|
41 |
@available(*, unavailable, message: "This view can not be instantiated")
|
|
42 |
public required init?(coder aDecoder: NSCoder) {
|
! |
43 |
fatalError("This view can not be instantiated")
|
! |
44 |
}
|
! |
45 |
|
|
46 |
@available(*, unavailable, message: "This view can not be instantiated")
|
|
47 |
public override init(frame: CGRect) {
|
! |
48 |
fatalError("This view can not be instantiated")
|
! |
49 |
}
|
! |
50 |
|
|
51 |
@available(*, unavailable, message: "This view can not be instantiated")
|
|
52 |
public init() {
|
! |
53 |
fatalError("This view can not be instantiated")
|
! |
54 |
}
|
! |
55 |
|
|
56 |
}
|
|
57 |
|
|
58 |
public static func buildView(within bounds: CGRect) -> VoidView? {
|
! |
59 |
return nil
|
! |
60 |
}
|
! |
61 |
|
|
62 |
}
|
|