11
22//
33// FullSpeedVStackCollectionView.swift
4- // Checkmate
4+ //
55//
66// Created by Ian Thomas on 7/26/23.
77//
88
99import SwiftUI
1010
11- // #warning("need to reimplement needsToScrollToBottom for both views")
12-
1311fileprivate struct Identifiers {
1412 fileprivate static let SupplementaryViewIdentifierHeader = " hostSupplementaryViewHeader "
1513 fileprivate static let SupplementaryViewIdentifierFooter = " hostSupplementaryViewFooter "
@@ -42,7 +40,7 @@ public struct FullSpeedVStackCollectionView<Section: SectionItemProtocol, CellIt
4240 guard let view = newValue else { return }
4341 hostController = UIHostingController ( rootView: view, ignoreSafeArea: true )
4442
45- /// This is set to .clear so that when tapping on cells the border headers, the tap selection rectangle shows.
43+ /// - Note: This is set to .clear so that when tapping on cells the border headers, the tap selection rectangle shows.
4644 hostController? . view. backgroundColor = UIColor . clear
4745 if let hostView = hostController? . view {
4846 hostView. frame = self . bounds
@@ -81,7 +79,6 @@ public struct FullSpeedVStackCollectionView<Section: SectionItemProtocol, CellIt
8179 fileprivate var dataSource : DataSource ? = nil
8280 fileprivate var sectionLayoutProvider : ( ( Int , NSCollectionLayoutEnvironment ) -> NSCollectionLayoutSection ) ?
8381 fileprivate var rowsHash : Int ? = nil
84- // fileprivate var registeredSupplementaryViewKinds: [String] = []
8582 fileprivate let backgroundColor : UIColor
8683
8784 public func scrollViewDidScroll( _ scrollView: UIScrollView ) {
@@ -99,18 +96,12 @@ public struct FullSpeedVStackCollectionView<Section: SectionItemProtocol, CellIt
9996 public func collectionView( _ collectionView: UICollectionView , willDisplay cell: UICollectionViewCell , forItemAt indexPath: IndexPath ) {
10097 self . willDisplayCell ( collectionView, cell, indexPath)
10198 }
102-
103- /// Interestingly, before the view is awoken, this is called when cells are tapped on, but after the view scrolls for the first time, this is never called again.
104- // func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
105- // print("here")
106- // }
10799 }
108100
109101 let rows : [ FullSpeedVStackSectionWithCells < Section , CellItem > ]
110102 let sectionLayoutProvider : ( Int , NSCollectionLayoutEnvironment ) -> NSCollectionLayoutSection
111103 let cell : ( IndexPath , CellItem ) -> CellView
112104 let supplementaryView : ( String , IndexPath ) -> SupplementaryView
113- // let supplementaryViewFull: SupplementaryViewProvider
114105
115106 public typealias ScrollViewAndOptionalCollectionView = ( ( UIScrollView , UICollectionView ? ) -> Void )
116107
@@ -197,29 +188,17 @@ public struct FullSpeedVStackCollectionView<Section: SectionItemProtocol, CellIt
197188 }
198189
199190 private func handleIfNeedToScrollToItem( collectionView: UICollectionView ) {
200-
201- // guard let wrappedValue = needsToScrollToBottom?.wrappedValue,
202- // wrappedValue else { return }
203-
204- // print("handleIfNeedToScrollToItem")
205191 guard let scrollItem = self . needsToScrollToItem? . wrappedValue else { return }
206-
207192 guard collectionView. isValid ( indexPath: scrollItem. indexPath) else {
208- // print("Error: indexPath \(scrollItem.indexPath) not valid")
209193 return
210194 }
211195 collectionViewScrollToIndexPath ( collectionView: collectionView, scrollItem: scrollItem)
212-
213- // NotificationCenter.default.post(name: .scrollToBottomOfChatRoomSetFalse, object: nil)
214196 }
215- //
197+
216198 private func collectionViewScrollToIndexPath( collectionView: UICollectionView , scrollItem: ScrollToItemAnimated ) {
217199
218200 DispatchQueue . main. async {
219- // print("executing scroll to: \(scrollItem.indexPath)")
220201 collectionView. scrollToItem ( at: scrollItem. indexPath, at: . centeredVertically, animated: scrollItem. animated)
221- // let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
222- // collectionView.scrollRectToVisible(rect, animated: false)
223202 NotificationCenter . default. post ( name: . FullSpeedVStackSetScrollToIndexPathNil, object: FullSpeedVStackSetScrollToIndexPathNilNotification ( collectionViewIdentifier: self . collectionViewId) )
224203 }
225204 }
@@ -242,10 +221,6 @@ public struct FullSpeedVStackCollectionView<Section: SectionItemProtocol, CellIt
242221
243222 context. coordinator. collectionViewReference = collectionView
244223
245- // collectionView.keyboardDismissMode = .interactive
246-
247- // collectionView.showsVerticalScrollIndicator = false
248-
249224 collectionView. delegate = context. coordinator
250225 collectionView. register ( HostCell . self, forCellWithReuseIdentifier: cellIdentifier)
251226
@@ -263,8 +238,7 @@ public struct FullSpeedVStackCollectionView<Section: SectionItemProtocol, CellIt
263238 /// `UIHostingConfiguration` has extra layout margins that need to be removed.
264239 . margins ( . all, 0 )
265240
266- // hostCell?.si
267- /// if the cells are not sizing properly, try adding sizeToFit(), but will likely need more handholding to make production ready.
241+ /// - Note: If the cells are not sizing properly, try adding sizeToFit()
268242
269243 if invertView {
270244 hostCell? . contentView. transform = CGAffineTransform ( scaleX: 1 , y: - 1 )
@@ -273,8 +247,7 @@ public struct FullSpeedVStackCollectionView<Section: SectionItemProtocol, CellIt
273247 }
274248 context. coordinator. dataSource = dataSource
275249
276- /// There was a memory leak that the debugger said was linked to the supplementary view, so I moved the registration outside the following closure and it appeared to work
277-
250+ /// - Note: There was a memory leak that the debugger said was linked to the supplementary view, so I moved the registration outside the following closure to resile the issue
278251 collectionView. register ( HostSupplementaryView . self,
279252 forSupplementaryViewOfKind: UICollectionView . elementKindSectionHeader,
280253 withReuseIdentifier: Identifiers . SupplementaryViewIdentifierHeader)
@@ -309,7 +282,7 @@ public struct FullSpeedVStackCollectionView<Section: SectionItemProtocol, CellIt
309282 }
310283}
311284
312- /// This is so we can override `gestureRecognizerShouldBegin`
285+ /// We subclass `UICollectionView` to override `gestureRecognizerShouldBegin`
313286final fileprivate class CustomUICollectionView : UICollectionView {
314287
315288 init ( frame: CGRect ,
@@ -327,71 +300,31 @@ final fileprivate class CustomUICollectionView: UICollectionView {
327300 self . transform = CGAffineTransform ( scaleX: 1 , y: - 1 )
328301 }
329302
330- // NotificationCenter.default.addObserver(self, selector: #selector(scrollToIndexPath), name: .FullSpeedVStackScrollToIndexPath, object: nil)
331-
332303 NotificationCenter . default. addObserver ( self , selector: #selector( awakeScroll) , name: . FullSpeedVStackAwakeScroll, object: nil )
333-
334- // TODO: add chat list scroll to top
335- // NotificationCenter.default.addObserver(self, selector: #selector(scrollToTop), name: .friendListScrollToTop, object: nil)
336304 }
337305
338306 private let collectionViewId : String
339307
340- // @objc private func scrollToBottomOfChatRoom(_ notification: Notification) {
341- //
342- // let y = contentSize.height - 1
343- // let rect = CGRect(x: 0, y: y + safeAreaInsets.bottom, width: 1, height: 1)
344- // scrollRectToVisible(rect, animated: true)
345- // }
346- //
347- // @objc private func scrollToIndexPath(_ notification: Notification) {
348- //
349- // guard let scrollObject = notification.object as? FullSpeedVStackScrollToIndexPathNotification else { return }
350- // TODO: Handle is empty chat list
351- // scrollObject.indexPath
352- // guard scrollObject.collectionViewId == self.collectionViewId else { return }
353-
354- // guard let theIndexPath.collectionViewIdentifier == self.collectionViewId else { return }
355-
356- // self.scrollToItem(at: scrollObject.indexPath, at: .bottom, animated: false)
357- // }
358-
359- #warning("add keyboard to both collection and table view")
360- // @objc private func adjustForKeyboard(notification: Notification) {
361-
362308 @objc private func keyboardWillChangeFrame( _ notification: Notification ) {
363- guard
364- let userInfo = notification. userInfo,
365- let endPos = userInfo [ UIResponder . keyboardFrameEndUserInfoKey] as? CGRect
366- else { return }
367-
368- let keyboardHeightDynamic = endPos. height
369-
370- #warning("fix this, the height / inset is not correct")
309+ // guard
310+ // let userInfo = notification.userInfo,
311+ // let endPos = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect
312+ // else { return }
313+ //
314+ // let keyboardHeightDynamic = endPos.height
371315 /*
372316 self.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardHeightDynamic, right: 0)
373317 self.scrollIndicatorInsets = self.contentInset
374- */
375- // print("keyboardHeight", keyboardHeight)
318+ */
376319 }
377320
378- //
379- // if notification.name == UIResponder.keyboardWillHideNotification {
380- // self.contentInset = .zero
381- // } else {
382- // let height = OnboardingConstants.KeyboardSizeOnly(includesSuggestionBar: false)
383- // self.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: height, right: 0)
384- // }
385- //
386- //}
387- //
388321 private var hasAwoken = false
389322
390323 required init ? ( coder: NSCoder ) {
391324 fatalError ( " init(coder:) has not been implemented " )
392325 }
393326
394- /// The missing taps issue, where the tapping on a cell has no effect until the user scrolls.
327+ /// The missing taps issue: where the tapping on a cell has no effect until the user scrolls.
395328 ///
396329 /// if you add `override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView?` to the `HostSupplementaryView` and other reasonable places it does not appear to work.
397330 ///
@@ -401,7 +334,6 @@ final fileprivate class CustomUICollectionView: UICollectionView {
401334 /// - adding to the SwiftUI View `.allowsHitTesting(true)`
402335
403336 @objc func awakeScroll( ) {
404- // print("awakeScroll")
405337
406338 guard hasAwoken == false else { return }
407339 hasAwoken = true
@@ -413,33 +345,6 @@ final fileprivate class CustomUICollectionView: UICollectionView {
413345 }
414346 }
415347
416- // @objc func scrollToTop() {
417- // self.setContentOffset(CGPoint.zero, animated: true)
418- // }
419-
420- // public func sendVisibleRows() {
421- // self.visibleCells
422- // }
423-
424-
425- // public func keepCurrentScroll(for upsertedMessages: [BaseMessage]) -> IndexPath {
426- // let firstVisibleIndexPath = tableView
427- // .indexPathsForVisibleRows?.first ?? IndexPath(row: 0, section: 0)
428- // var nextInsertedCount = 0
429- // if let newestMessage = sentMessages.first {
430- // // only filter out messages inserted at the bottom (newer) of current visible item
431- // nextInsertedCount = upsertedMessages
432- // .filter({ $0.createdAt > newestMessage.createdAt })
433- // .filter({ !SBUUtils.contains(messageId: $0.messageId, in: sentMessages) }).count
434- // }
435- //
436- // SBULog.info("New messages inserted : \(nextInsertedCount)")
437- // return IndexPath(
438- // row: firstVisibleIndexPath.row + nextInsertedCount,
439- // section: 0
440- // )
441- // }
442-
443348 var onGestureShouldBegin : ( ( _ gestureRecognizer: UIPanGestureRecognizer , _ scrollView: UIScrollView ) -> Bool ) ?
444349
445350 public override func gestureRecognizerShouldBegin( _ gestureRecognizer: UIGestureRecognizer ) -> Bool {
0 commit comments