宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-04-06 a1ae6802080a22e6e6ce6d0935e95facb1daca5c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
//  EKAlertMessageView.swift
//  SwiftEntryKit
//
//  Created by Daniel Huri on 5/5/18.
//
 
import UIKit
 
final public class EKAlertMessageView: EKSimpleMessageView, EntryAppearanceDescriptor {
    
    // MARK: Props
    var buttonBarView: EKButtonBarView!
    private var buttonsBarCompressedConstraint: NSLayoutConstraint!
    private var buttonsBarExpandedConstraint: NSLayoutConstraint!
    
    // MARK: EntryAppearenceDescriptor
    
    var bottomCornerRadius: CGFloat = 0 {
        didSet {
            buttonBarView.bottomCornerRadius = bottomCornerRadius
        }
    }
    
    // MARK: Setup
    public init(with message: EKAlertMessage) {
        super.init(with: message.simpleMessage)
        setupButtonBarView(with: message.buttonBarContent)
        layoutContent(with: message)
    }
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupButtonBarView(with content: EKProperty.ButtonBarContent) {
        buttonBarView = EKButtonBarView(with: content)
        buttonBarView.clipsToBounds = true
        addSubview(buttonBarView)
    }
    
    func layoutContent(with message: EKAlertMessage) {
        switch message.imagePosition {
        case .top:
            messageContentView.verticalMargins = 16
            messageContentView.horizontalMargins = 16
            messageContentView.labelsOffset = 5
            
            if let thumbImageView = thumbImageView {
                thumbImageView.layoutToSuperview(.top, offset: 20)
                thumbImageView.layoutToSuperview(.centerX)
                messageContentView.layout(.top, to: .bottom, of: thumbImageView)
            } else {
                messageContentView.layoutToSuperview(.top)
            }
 
            messageContentView.layoutToSuperview(axis: .horizontally)
            buttonBarView.layout(.top, to: .bottom, of: messageContentView)
        case .left:
            messageContentView.verticalMargins = 0
            messageContentView.horizontalMargins = 0
            messageContentView.labelsOffset = 5
            
            if let thumbImageView = thumbImageView {
                thumbImageView.layoutToSuperview(.top, .left, offset: 16)
                messageContentView.layout(.left, to: .right, of: thumbImageView, offset: 12)
                messageContentView.layout(to: .top, of: thumbImageView, offset: 2)
            } else {
                messageContentView.layoutToSuperview(.left, .top, offset: 16)
            }
 
            messageContentView.layoutToSuperview(.right, offset: -16)
            buttonBarView.layout(.top, to: .bottom, of: messageContentView, offset: 10)
        }
        
        buttonBarView.layoutToSuperview(axis: .horizontally)
        buttonBarView.layoutToSuperview(.bottom)
        buttonBarView.alpha = 0
 
        if !message.buttonBarContent.content.isEmpty {
            if message.buttonBarContent.expandAnimatedly {
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
                    self.buttonBarView.expand()
                }
            } else {
                buttonBarView.expand()
            }
        }
    }
}