From d0341a330317d46129003a4215fb0794082550af Mon Sep 17 00:00:00 2001 From: Rakuyo Date: Mon, 8 Jul 2024 20:50:46 +0800 Subject: [PATCH 1/3] feat: Add `deselectRowIfNeeded(with: animated:)` method for UITableView and UICollectionView --- .../Extensions/UICollectionView+RAK.swift | 34 ++++++++++++++++++ Sources/Core/Extensions/UITableView+RAK.swift | 35 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 Sources/Core/Extensions/UITableView+RAK.swift diff --git a/Sources/Core/Extensions/UICollectionView+RAK.swift b/Sources/Core/Extensions/UICollectionView+RAK.swift index 23249a5..55e9155 100644 --- a/Sources/Core/Extensions/UICollectionView+RAK.swift +++ b/Sources/Core/Extensions/UICollectionView+RAK.swift @@ -29,4 +29,38 @@ extension Extendable where Base: UICollectionView { } } } + +extension Extendable where Base: UICollectionView { + public func deselectRowIfNeeded( + with transitionCoordinator: UIViewControllerTransitionCoordinator?, + animated: Bool + ) { + guard let selectedIndexPaths = base.indexPathsForSelectedItems else { return } + + guard let coordinator = transitionCoordinator else { + deselectItems(at: selectedIndexPaths, animated: animated) + return + } + + coordinator.animate( + alongsideTransition: { _ in + deselectItems(at: selectedIndexPaths, animated: true) + }, + completion: { context in + guard context.isCancelled else { return } + + for selectedIndexPath in selectedIndexPaths { + base.selectItem(at: selectedIndexPath, animated: false, scrollPosition: []) + } + } + ) + } + + private func deselectItems(at selectedIndexPaths: [IndexPath], animated: Bool) { + for selectedIndexPath in selectedIndexPaths { + base.deselectItem(at: selectedIndexPath, animated: true) + base.delegate?.collectionView?(base, didDeselectItemAt: selectedIndexPath) + } + } +} #endif diff --git a/Sources/Core/Extensions/UITableView+RAK.swift b/Sources/Core/Extensions/UITableView+RAK.swift new file mode 100644 index 0000000..b1a72fe --- /dev/null +++ b/Sources/Core/Extensions/UITableView+RAK.swift @@ -0,0 +1,35 @@ +// +// UITableView+RAK.swift +// RakuyoKit +// +// Created by Rakuyo on 2024/7/8. +// Copyright © 2024 RakuyoKit. All rights reserved. +// + +#if !os(watchOS) +import UIKit + +extension Extendable where Base: UITableView { + public func deselectRowIfNeeded( + with transitionCoordinator: UIViewControllerTransitionCoordinator?, + animated: Bool + ) { + guard let selectedIndexPath = base.indexPathForSelectedRow else { return } + + guard let coordinator = transitionCoordinator else { + base.deselectRow(at: selectedIndexPath, animated: animated) + return + } + + coordinator.animate( + alongsideTransition: { _ in + base.deselectRow(at: selectedIndexPath, animated: true) + }, + completion: { context in + guard context.isCancelled else { return } + base.selectRow(at: selectedIndexPath, animated: false, scrollPosition: .none) + } + ) + } +} +#endif From 681ebb88b17cdb2d1256b9d81be57dc3377e99e4 Mon Sep 17 00:00:00 2001 From: Rakuyo Date: Mon, 8 Jul 2024 20:55:38 +0800 Subject: [PATCH 2/3] feat: Add `SectionEdgeInsets.groupCard` --- .../Epoxy/CollectionView/Tools/SectionEdgeInsets.swift | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Sources/Epoxy/CollectionView/Tools/SectionEdgeInsets.swift b/Sources/Epoxy/CollectionView/Tools/SectionEdgeInsets.swift index 590aadd..2faec01 100644 --- a/Sources/Epoxy/CollectionView/Tools/SectionEdgeInsets.swift +++ b/Sources/Epoxy/CollectionView/Tools/SectionEdgeInsets.swift @@ -37,6 +37,11 @@ public enum SectionEdgeInsets { /// Custom spacing for all four sides. case all(top: CGFloat, leading: CGFloat, bottom: CGFloat, trailing: CGFloat) + /// Same spacing as `.insetGrouped` style `UITableView` + /// + /// (top: 0, leading: 20, bottom: 35, trailing: 20) + case groupCard + /// Fully customized using `EdgeInsets`. case custom(RAKCore.EdgeInsets) @@ -67,6 +72,9 @@ extension SectionEdgeInsets { case .all(let top, let leading, let bottom, let trailing): .init(top: top, leading: leading, bottom: bottom, trailing: trailing) + case .groupCard: + .init(top: 0, leading: 20, bottom: 35, trailing: 20) + case .custom(let edge): edge.directionalEdgeInsets } From ad93fe980473907e1b93bbd1525c5e8a72b1dc3e Mon Sep 17 00:00:00 2001 From: Rakuyo Date: Mon, 8 Jul 2024 20:55:55 +0800 Subject: [PATCH 3/3] feat: Add `autoDeselectItems = false` --- Sources/Epoxy/CollectionView/CollectionView.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/Epoxy/CollectionView/CollectionView.swift b/Sources/Epoxy/CollectionView/CollectionView.swift index 9a2bb21..dec319d 100644 --- a/Sources/Epoxy/CollectionView/CollectionView.swift +++ b/Sources/Epoxy/CollectionView/CollectionView.swift @@ -21,6 +21,7 @@ open class CollectionView: EpoxyCollectionView.CollectionView { override public init(layout: UICollectionViewLayout, configuration: CollectionViewConfiguration = .shared) { super.init(layout: layout, configuration: configuration) + autoDeselectItems = false showsVerticalScrollIndicator = false showsHorizontalScrollIndicator = false }