a24f2485dde77047b8b3cc47f71808a25ea8b1d0
[mirrors/VoiceChatSdk_iOS.git] /
1 //
2 //  IQScrollViewConfiguration.swift
3 //  https://github.com/hackiftekhar/IQKeyboardManager
4 //  Copyright (c) 2013-24 Iftekhar Qurashi.
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // THE SOFTWARE.
23
24 import UIKit
25
26 @available(iOSApplicationExtension, unavailable)
27 @MainActor
28 internal struct IQScrollViewConfiguration {
29     let scrollView: UIScrollView
30     let startingContentOffset: CGPoint
31     let startingScrollIndicatorInsets: UIEdgeInsets
32     let startingContentInset: UIEdgeInsets
33
34     private let canRestoreContentOffset: Bool
35
36     init(scrollView: UIScrollView, canRestoreContentOffset: Bool) {
37         self.scrollView = scrollView
38         self.canRestoreContentOffset = canRestoreContentOffset
39
40         startingContentOffset = scrollView.contentOffset
41         startingContentInset = scrollView.contentInset
42
43 #if swift(>=5.1)
44         if #available(iOS 11.1, *) {
45             startingScrollIndicatorInsets = scrollView.verticalScrollIndicatorInsets
46         } else {
47             startingScrollIndicatorInsets = scrollView.scrollIndicatorInsets
48         }
49 #else
50         startingScrollIndicatorInsets = scrollView.scrollIndicatorInsets
51 #endif
52     }
53
54     var hasChanged: Bool {
55         if scrollView.contentInset != self.startingContentInset {
56             return true
57         }
58
59         if canRestoreContentOffset,
60            scrollView.iq.restoreContentOffset,
61            !scrollView.contentOffset.equalTo(startingContentOffset) {
62             return true
63         }
64         return false
65     }
66
67     @discardableResult
68     func restore(for textFieldView: UIView?) -> Bool {
69         var success: Bool = false
70
71         if scrollView.contentInset != self.startingContentInset {
72             scrollView.contentInset = self.startingContentInset
73             scrollView.layoutIfNeeded() // (Bug ID: #1996)
74             success = true
75         }
76
77 #if swift(>=5.1)
78         if #available(iOS 11.1, *) {
79             if scrollView.verticalScrollIndicatorInsets != self.startingScrollIndicatorInsets {
80                 scrollView.verticalScrollIndicatorInsets = self.startingScrollIndicatorInsets
81             }
82         } else {
83             if scrollView.scrollIndicatorInsets != self.startingScrollIndicatorInsets {
84                 scrollView.scrollIndicatorInsets = self.startingScrollIndicatorInsets
85             }
86         }
87 #else
88         if scrollView.scrollIndicatorInsets != self.startingScrollIndicatorInsets {
89             scrollView.scrollIndicatorInsets = self.startingScrollIndicatorInsets
90         }
91 #endif
92
93         if canRestoreContentOffset,
94            scrollView.iq.restoreContentOffset,
95            !scrollView.contentOffset.equalTo(startingContentOffset) {
96
97             //  (Bug ID: #1365, #1508, #1541)
98             let stackView: UIStackView? = textFieldView?.iq.superviewOf(type: UIStackView.self,
99                                                                         belowView: scrollView)
100             // (Bug ID: #1901, #1996)
101             let animatedContentOffset: Bool = stackView != nil ||
102             scrollView is UICollectionView ||
103             scrollView is UITableView
104
105             if animatedContentOffset {
106                 scrollView.setContentOffset(startingContentOffset, animated: UIView.areAnimationsEnabled)
107             } else {
108                 scrollView.contentOffset = startingContentOffset
109             }
110             success = true
111         }
112
113         return success
114     }
115 }