From 5135a007cb9e6c1de8c12f8de0f77e59f025d471 Mon Sep 17 00:00:00 2001 From: Ivan Vorobei Date: Mon, 3 Jan 2022 13:58:44 +0300 Subject: [PATCH] Moved delegates to data source object. Updated providers models and naming. --- Readme.md | 196 +++++++----------- SPDiffable.podspec | 2 +- .../SPDiffableCollectionDataSource+Set.swift | 2 +- ...nDataSource+UICollectionViewDelegate.swift | 38 ++++ .../SPDiffableCollectionDataSource.swift | 29 +-- .../SPDiffableCollectionDelegate.swift | 2 +- .../SPDiffableCollectionCellProvider.swift | 4 +- ...ffableCollectionHeaderFooterProvider.swift | 37 ++++ .../SPDiffableCollectionController.swift | 21 +- .../Collection/SPDiffableCollectionView.swift | 25 +-- .../SPDiffableSideBarController.swift | 1 - ...eTableDataSource+UITableViewDelegate.swift | 48 +++++ .../SPDiffableTableDataSource.swift | 22 +- .../SPDiffableTableCellProvider.swift | 0 .../SPDiffableTableHeaderFooterProvider.swift | 32 +++ .../Table/SPDiffableTableController.swift | 33 ++- .../Table/SPDiffableTableView.swift | 27 +++ TODO.md | 2 + 18 files changed, 317 insertions(+), 204 deletions(-) create mode 100644 Sources/SPDiffable/Collection/DataSource/SPDiffableCollectionDataSource+UICollectionViewDelegate.swift rename Sources/SPDiffable/Collection/{ => Providers}/SPDiffableCollectionCellProvider.swift (98%) create mode 100644 Sources/SPDiffable/Collection/Providers/SPDiffableCollectionHeaderFooterProvider.swift create mode 100644 Sources/SPDiffable/Table/DataSource/SPDiffableTableDataSource+UITableViewDelegate.swift rename Sources/SPDiffable/Table/{ => Providers}/SPDiffableTableCellProvider.swift (100%) create mode 100644 Sources/SPDiffable/Table/Providers/SPDiffableTableHeaderFooterProvider.swift create mode 100644 Sources/SPDiffable/Table/SPDiffableTableView.swift diff --git a/Readme.md b/Readme.md index 1ee7615..9f339d2 100644 --- a/Readme.md +++ b/Readme.md @@ -14,17 +14,15 @@ Apple's diffable API requerid models for each object type. If you want use it in - [Swift Package Manager](#swift-package-manager) - [CocoaPods](#cocoapods) - [Manually](#manually) +- [Ready Use](#ready-use) + - [Example](#ready-use) + - [Available Classes](#available-classes) - [Usage](#usage) - [How it work](#usage) - [Set Content](#set-content) - [Get Content](#get-content) - - [Mediator](#mediator) - - [Diffable Delegate](#diffable-delegate) - - [Sidebar](#sidebar) -- [Ready Use](#ready-use) - - [Example](#ready-use) - - [List classes](#ready-use-classes) - [Wrapper](#wrapper) + - [Sidebar](#sidebar) - [Russian Community](#russian-community) ## Installation @@ -55,6 +53,72 @@ pod 'SPDiffable' If you prefer not to use any of dependency managers, you can integrate manually. Put `Sources/SPDiffable` folder in your Xcode project. Make sure to enable `Copy items if needed` and `Create groups`. +## Ready Use + +For example you need simple table with native cells. You need create content with `SPDiffableTableRow`: + +```swift +let section = SPDiffableSection( + id: "example section", + header: SPDiffableTextHeaderFooter(text: "Header"), + footer: SPDiffableTextHeaderFooter(text: "Footer"), + items: [ + SPDiffableTableRow(text: "First Cell", accessoryType: .disclosureIndicator, selectionStyle: .default, action: { [weak self] indexPath in + guard let self = self else { return } + self.tableView.deselectRow(at: indexPath, animated: true) + }), + SPDiffableTableRow(text: "Second Cell", accessoryType: .disclosureIndicator, selectionStyle: .default, action: { [weak self] indexPath in + guard let self = self else { return } + self.tableView.deselectRow(at: indexPath, animated: true) + }), + ] +) +``` + +You init cell model and pass action, choose selection style and other. As you see, model describe native table cell. Next, you need set cell provider, but it also already available, for get it call `SPDiffableTableController.default`. + +```swift +setCellProviders(SPDiffableTableCellProvider.default, sections: [section]) +``` + +Now project's models automatically converting to cell. No need any additional work. That all code. +If you use custom table view or table controller, don't forget register cells classes. For `SPDiffableTableController` all cells already registered. + +## Available Classes + +It list models which you can use now, it shoud close your task without code. Of couse you can create your models. +Now in project you can find this ready-use models: + +- `SPDiffableItem` it basic class. All item models shoud be extend from it model. Header and footer also. +- `SPDiffableSection` section class. Included footer and header properties, also items (cells). +- `SPDiffableTextHeaderFooter` header or footer class with text. + +#### For Table: + +Here provided models: + +- `SPDiffableTableRow` it native item for table cell. Support all basic styles and action for tap event. +- `SPDiffableTableRowSubtitle` it native item for table cell with subtitle. Support all as before. +- `SPDiffableTableRowButton` item for table in style as button. Support table styles and action for tap. +- `SPDiffableTableRowStepper` item for table cell with stepper. Has maximum value and minimum, also incuded action with passed value. +- `SPDiffableTableRowSwitch` item for table with switch, included default state and action for change event. + +Here provided cells: + +- `SPDiffableTableViewCell` basic table cell with detail text right side. +- `SPDiffableSubtitleTableViewCell` basic table cell with two lines of texts. +- `SPDiffableStepper` cell with stepper control. Using with `SPDiffableTableRowStepper` model. +- `SPDiffableSwitch` cell with switch. Using with `SPDiffableTableRowSwitch` model. +- `SPDiffableSlider` cell with slider. + +#### For Collection: + +Provided only models, becouse for most items using list registration and no need specific cell class. + +- `SPDiffableSideBarItem` menu item in side bar. Support accessories and actions. +- `SPDiffableSideBarButton` button item in side bar. Color of title similar to tint. +- `SPDiffableSideBarHeader` header model for side bar item. + ## Usage Before read it, highly recomded check `Example` target in project. It examle show all features, like use stepper and switch, like process actions, create custom models and many other. Also you can skip full undestand logic and read [Ready-use section](https://github.com/ivanvorobei/SPDiffable#ready-use) with minimum of code for start. @@ -148,7 +212,6 @@ diffableDataSource.reload(content) // Reload only specific items diffableDataSource.reload(items) // or for 1 item `reload(item)` - ``` Changes apply without animation and like deep reload. @@ -165,51 +228,12 @@ func section(for index: Int) -> SPDiffableSection? {} func cell(_ type: T.Type, for itemID: SPDiffableItem.Identifier) -> T? {} ``` -### Mediator - -Some methods in diffable data source can't ovveride without custom data source. It solved with mediator delegate. It simple. Next example for table. Set delegate `SPTableDiffableMediator`, all method optional: - -```swift -class DiffableTableController: SPDiffableTableController, SPTableDiffableMediator { - - override func viewDidLoad() { - super.viewDidLoad() - setCellProviders([cellProvider], sections: content) - diffableDataSource?.mediator = self - } -} -``` - -Now you can implemented requerid methods, for example title of header: +### Wrapper -```swift -func diffableTableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { - return "Overridden in of diffable mediator" -} -``` - -In protocol you can find more methods, like `canEdit` and other. - -### Diffable Delegate - -For handle some useful extensions, you can use delegates `SPDiffableTableDelegate`, `SPDiffableCollectionDelegate`. For example, when you need get which model did select, use this: +In project you can find class `SPDiffableWrapperItem`. Using it, when you don't want create custom item model for you diffable struct. You can pass any your model and uwrap it later in cell provider. ```swift -class DiffableTableController: SPDiffableTableController, SPDiffableTableDelegate { - - override func viewDidLoad() { - super.viewDidLoad() - - // Hidded code for cell providers and content - - setCellProviders([cellProvider], sections: content) - diffableDelegate = self - } - - func diffableTableView(_ tableView: UITableView, didSelectItem item: SPDiffableItem) { - // Here you get model, which did select. - } -} +let item = SPDiffableWrapperItem(id: "uniq-identifier", model: LocationRowModel(city: "Minsk")) ``` ### Sidebar @@ -245,80 +269,6 @@ SPDiffableSection( ) ``` -## Ready Use - -You can save time and count lines of code using ready-use classes. In project available models and views. For example you need simple table with native cells. You need create content with `SPDiffableTableRow`: - -```swift -let section = SPDiffableSection( - id: "example section", - header: SPDiffableTextHeaderFooter(text: "Header"), - footer: SPDiffableTextHeaderFooter(text: "Footer"), - items: [ - SPDiffableTableRow(text: "First Cell", accessoryType: .disclosureIndicator, selectionStyle: .default, action: { [weak self] indexPath in - guard let self = self else { return } - self.tableView.deselectRow(at: indexPath, animated: true) - }), - SPDiffableTableRow(text: "Second Cell", accessoryType: .disclosureIndicator, selectionStyle: .default, action: { [weak self] indexPath in - guard let self = self else { return } - self.tableView.deselectRow(at: indexPath, animated: true) - }), - ] -) -``` - -You init cell model and pass action, choose selection style and other. As you see, model describe native table cell. Next, you need set cell provider, but it also already available, for get it call `SPDiffableTableController.defaultCellProvider`. - -```swift -setCellProviders(SPDiffableTableCellProvider.default, sections: [section]) -``` - -Now project's models automatically converting to cell. No need any additional work. That all code. -If you use custom table view or table controller, don't forget register cells classes. For `SPDiffableTableController` all cells already registered. - -## Ready-use classes - -It list models which you can use now, it shoud close your task without code. Of couse you can create your models. -Now in project you can find this ready-use models: - -- `SPDiffableItem` it basic class. All item models shoud be extend from it model. Header and footer also. -- `SPDiffableSection` section class. Included footer and header properties, also items (cells). -- `SPDiffableTextHeaderFooter` header or footer class with text. - -#### For Table: - -Here provided models: - -- `SPDiffableTableRow` it native item for table cell. Support all basic styles and action for tap event. -- `SPDiffableTableRowSubtitle` it native item for table cell with subtitle. Support all as before. -- `SPDiffableTableRowButton` item for table in style as button. Support table styles and action for tap. -- `SPDiffableTableRowStepper` item for table cell with stepper. Has maximum value and minimum, also incuded action with passed value. -- `SPDiffableTableRowSwitch` item for table with switch, included default state and action for change event. - -Here provided cells: - -- `SPDiffableTableViewCell` basic table cell with detail text right side. -- `SPDiffableSubtitleTableViewCell` basic table cell with two lines of texts. -- `SPDiffableStepper` cell with stepper control. Using with `SPDiffableTableRowStepper` model. -- `SPDiffableSwitch` cell with switch. Using with `SPDiffableTableRowSwitch` model. -- `SPDiffableSlider` cell with slider. - -#### For Collection: - -Provided only models, becouse for most items using list registration and no need specific cell class. - -- `SPDiffableSideBarItem` menu item in side bar. Support accessories and actions. -- `SPDiffableSideBarButton` button item in side bar. Color of title similar to tint. -- `SPDiffableSideBarHeader` header model for side bar item. - -## Wrapper - -In project you can find class `SPDiffableWrapperItem`. Using it, when you don't want create custom item model for you diffable struct. You can pass any your model and uwrap it later in cell provider. - -```swift -let item = SPDiffableWrapperItem(id: "uniq-identifier", model: LocationRowModel(city: "Minsk")) -``` - ## Russian Community Я веду [телеграм-канал](https://sparrowcode.by/telegram), там публикую новости и туториалы.
diff --git a/SPDiffable.podspec b/SPDiffable.podspec index c9a5d65..8e63b4b 100644 --- a/SPDiffable.podspec +++ b/SPDiffable.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = 'SPDiffable' - s.version = '2.2.0' + s.version = '3.0.0' s.summary = 'Extension of Diffable API which allow not duplicate code and use less models. Included example for SideBar.' s.homepage = 'https://github.com/ivanvorobei/SPDiffable' s.source = { :git => 'https://github.com/ivanvorobei/SPDiffable.git', :tag => s.version } diff --git a/Sources/SPDiffable/Collection/DataSource/SPDiffableCollectionDataSource+Set.swift b/Sources/SPDiffable/Collection/DataSource/SPDiffableCollectionDataSource+Set.swift index bcb51fc..43edb6a 100644 --- a/Sources/SPDiffable/Collection/DataSource/SPDiffableCollectionDataSource+Set.swift +++ b/Sources/SPDiffable/Collection/DataSource/SPDiffableCollectionDataSource+Set.swift @@ -158,7 +158,7 @@ extension SPDiffableCollectionDataSource { /** SPDiffable: Update layout. - + - parameter animating: Shoud update layout with animation or not. - parameter completion: A closure to execute when the updating complete. */ diff --git a/Sources/SPDiffable/Collection/DataSource/SPDiffableCollectionDataSource+UICollectionViewDelegate.swift b/Sources/SPDiffable/Collection/DataSource/SPDiffableCollectionDataSource+UICollectionViewDelegate.swift new file mode 100644 index 0000000..89ac263 --- /dev/null +++ b/Sources/SPDiffable/Collection/DataSource/SPDiffableCollectionDataSource+UICollectionViewDelegate.swift @@ -0,0 +1,38 @@ +// The MIT License (MIT) +// Copyright © 2020 Ivan Vorobei (hello@ivanvorobei.by) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +import UIKit + +@available(iOS 13.0, *) +extension SPDiffableCollectionDataSource: UICollectionViewDelegate { + + // Process actions after tap. + open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { + guard let item = item(for: indexPath) else { return } + diffableDelegate?.diffableCollectionView?(collectionView, didSelectItem: item, indexPath: indexPath) + switch item { + case let model as SPDiffableActionableItem: + model.action?(model, indexPath) + default: + break + } + } +} diff --git a/Sources/SPDiffable/Collection/DataSource/SPDiffableCollectionDataSource.swift b/Sources/SPDiffable/Collection/DataSource/SPDiffableCollectionDataSource.swift index 0ca7394..a3d562c 100644 --- a/Sources/SPDiffable/Collection/DataSource/SPDiffableCollectionDataSource.swift +++ b/Sources/SPDiffable/Collection/DataSource/SPDiffableCollectionDataSource.swift @@ -37,13 +37,22 @@ open class SPDiffableCollectionDataSource: UICollectionViewDiffableDataSource UICollectionReusableView? in - for provider in supplementaryViewProviders { - if let view = provider(collectionView, kind, indexPath) { + for provider in headerFooterProviders { + if let view = provider.clouser(collectionView, kind, indexPath) { return view } } return nil } } + + collectionView.delegate = self } } - -/** - SPDiffable: Wrapper of collection supplementary view provider. - */ -@available(iOS 13.0, *) -public typealias SPDiffableCollectionSupplementaryViewProvider = SPDiffableCollectionDataSource.SupplementaryViewProvider diff --git a/Sources/SPDiffable/Collection/Protocols/SPDiffableCollectionDelegate.swift b/Sources/SPDiffable/Collection/Protocols/SPDiffableCollectionDelegate.swift index bcd9ea6..a5c4c3c 100644 --- a/Sources/SPDiffable/Collection/Protocols/SPDiffableCollectionDelegate.swift +++ b/Sources/SPDiffable/Collection/Protocols/SPDiffableCollectionDelegate.swift @@ -22,7 +22,7 @@ import UIKit /** - SPDiffable: Protocol with collection events. + SPDiffable: Protocol for mirrir `UICollectionViewDelegate`. */ @available(iOS 13.0, *) @objc public protocol SPDiffableCollectionDelegate: AnyObject { diff --git a/Sources/SPDiffable/Collection/SPDiffableCollectionCellProvider.swift b/Sources/SPDiffable/Collection/Providers/SPDiffableCollectionCellProvider.swift similarity index 98% rename from Sources/SPDiffable/Collection/SPDiffableCollectionCellProvider.swift rename to Sources/SPDiffable/Collection/Providers/SPDiffableCollectionCellProvider.swift index 1f076db..d2a60e5 100644 --- a/Sources/SPDiffable/Collection/SPDiffableCollectionCellProvider.swift +++ b/Sources/SPDiffable/Collection/Providers/SPDiffableCollectionCellProvider.swift @@ -21,10 +21,8 @@ import UIKit -import UIKit - /** - SPDiffable: Wrapper of collection cell provider. + SPDiffable: Cell Provider. */ @available(iOS 13.0, *) open class SPDiffableCollectionCellProvider { diff --git a/Sources/SPDiffable/Collection/Providers/SPDiffableCollectionHeaderFooterProvider.swift b/Sources/SPDiffable/Collection/Providers/SPDiffableCollectionHeaderFooterProvider.swift new file mode 100644 index 0000000..c70befd --- /dev/null +++ b/Sources/SPDiffable/Collection/Providers/SPDiffableCollectionHeaderFooterProvider.swift @@ -0,0 +1,37 @@ +// The MIT License (MIT) +// Copyright © 2020 Ivan Vorobei (hello@ivanvorobei.by) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +import UIKit + +/** + SPDiffable: Header and Footer provider. + + In native diffable called `SupplementaryViewProvider`. + */ +@available(iOS 13.0, *) +open class SPDiffableCollectionHeaderFooterProvider { + + open var clouser: SPDiffableCollectionDataSource.SupplementaryViewProvider + + public init(clouser: @escaping SPDiffableCollectionDataSource.SupplementaryViewProvider) { + self.clouser = clouser + } +} diff --git a/Sources/SPDiffable/Collection/SPDiffableCollectionController.swift b/Sources/SPDiffable/Collection/SPDiffableCollectionController.swift index e78921b..237a864 100644 --- a/Sources/SPDiffable/Collection/SPDiffableCollectionController.swift +++ b/Sources/SPDiffable/Collection/SPDiffableCollectionController.swift @@ -32,8 +32,6 @@ open class SPDiffableCollectionController: UICollectionViewController { open var diffableDataSource: SPDiffableCollectionDataSource? - open weak var diffableDelegate: SPDiffableCollectionDelegate? - // MARK: - Lifecycle open override func viewDidLoad() { @@ -50,35 +48,22 @@ open class SPDiffableCollectionController: UICollectionViewController { - warning: Changes applied not animatable. - parameter cellProviders: Cell providers with valid order for processing. - - parameter supplementaryViewProviders: Supplementary view providers with valid order for processing. + - parameter headerFooterProviders: Header and Footer providers with valid order for processing. - parameter headerAsFirstCell: Flag for use header as cell or supplementary. - parameter sections: Content as array of `SPDiffableSection`. */ open func setCellProviders( _ cellProviders: [SPDiffableCollectionCellProvider], - supplementaryViewProviders: [SPDiffableCollectionSupplementaryViewProvider] = [], + headerFooterProviders: [SPDiffableCollectionHeaderFooterProvider] = [], headerAsFirstCell: Bool = true, sections: [SPDiffableSection] ) { diffableDataSource = SPDiffableCollectionDataSource( collectionView: collectionView, cellProviders: cellProviders, - supplementaryViewProviders: supplementaryViewProviders, + headerFooterProviders: headerFooterProviders, headerAsFirstCell: headerAsFirstCell ) diffableDataSource?.apply(sections, animated: false) } - - // MARK: - UICollectionViewDelegate - - open override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { - guard let item = diffableDataSource?.item(for: indexPath) else { return } - diffableDelegate?.diffableCollectionView?(collectionView, didSelectItem: item, indexPath: indexPath) - switch item { - case let model as SPDiffableItemActionable: - model.action?(item, indexPath) - default: - break - } - } } diff --git a/Sources/SPDiffable/Collection/SPDiffableCollectionView.swift b/Sources/SPDiffable/Collection/SPDiffableCollectionView.swift index 6793b47..738c08c 100644 --- a/Sources/SPDiffable/Collection/SPDiffableCollectionView.swift +++ b/Sources/SPDiffable/Collection/SPDiffableCollectionView.swift @@ -32,8 +32,6 @@ open class SPDiffableCollectionView: UICollectionView, UICollectionViewDelegate open var diffableDataSource: SPDiffableCollectionDataSource? - open weak var diffableDelegate: SPDiffableCollectionDelegate? - // MARK: - Init public init() { @@ -47,7 +45,7 @@ open class SPDiffableCollectionView: UICollectionView, UICollectionViewDelegate } private func commonInit() { - delegate = self + delaysContentTouches = false } /** @@ -57,37 +55,22 @@ open class SPDiffableCollectionView: UICollectionView, UICollectionViewDelegate - warning: Changes applied not animatable. - parameter cellProviders: Cell providers with valid order for processing. - - parameter supplementaryViewProviders: Supplementary view providers with valid order for processing. + - parameter headerFooterProviders: Header and Footer providers with valid order for processing. - parameter headerAsFirstCell: Flag for use header as cell or supplementary. - parameter sections: Content as array of `SPDiffableSection`. */ open func setCellProviders( _ cellProviders: [SPDiffableCollectionCellProvider], - supplementaryViewProviders: [SPDiffableCollectionSupplementaryViewProvider] = [], + headerFooterProviders: [SPDiffableCollectionHeaderFooterProvider] = [], headerAsFirstCell: Bool = true, sections: [SPDiffableSection] ) { diffableDataSource = SPDiffableCollectionDataSource( collectionView: self, cellProviders: cellProviders, - supplementaryViewProviders: supplementaryViewProviders, + headerFooterProviders: headerFooterProviders, headerAsFirstCell: headerAsFirstCell ) diffableDataSource?.apply(sections, animated: false) } - - // MARK: - UICollectionViewDelegate - - open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { - guard let item = diffableDataSource?.item(for: indexPath) else { return } - diffableDelegate?.diffableCollectionView?(collectionView, didSelectItem: item, indexPath: indexPath) - switch item { - case let model as SPDiffableActionableItem: - model.action?(model, indexPath) - case let model as SPDiffableWrapperItem: - model.action?(model, indexPath) - default: - break - } - } } diff --git a/Sources/SPDiffable/Collection/SPDiffableSideBarController.swift b/Sources/SPDiffable/Collection/SPDiffableSideBarController.swift index 58985ff..87aa1d0 100644 --- a/Sources/SPDiffable/Collection/SPDiffableSideBarController.swift +++ b/Sources/SPDiffable/Collection/SPDiffableSideBarController.swift @@ -38,7 +38,6 @@ open class SPDiffableSideBarController: SPDiffableCollectionController { public init() { super.init(collectionViewLayout: UICollectionViewFlowLayout()) commonInit() - } public required init?(coder: NSCoder) { diff --git a/Sources/SPDiffable/Table/DataSource/SPDiffableTableDataSource+UITableViewDelegate.swift b/Sources/SPDiffable/Table/DataSource/SPDiffableTableDataSource+UITableViewDelegate.swift new file mode 100644 index 0000000..079d0cb --- /dev/null +++ b/Sources/SPDiffable/Table/DataSource/SPDiffableTableDataSource+UITableViewDelegate.swift @@ -0,0 +1,48 @@ +// The MIT License (MIT) +// Copyright © 2020 Ivan Vorobei (hello@ivanvorobei.by) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +import UIKit + +@available(iOS 13.0, *) +extension SPDiffableTableDataSource: UITableViewDelegate { + + open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + guard let item = item(for: indexPath) else { return } + diffableDelegate?.diffableTableView?(tableView, didSelectItem: item, indexPath: indexPath) + + switch item { + case let model as SPDiffableItemActionable: + model.action?(item, indexPath) + default: + break + } + } + + open func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { + guard let headerItem = self.section(for: section)?.header else { return nil } + for provider in headerFooterProviders { + if let view = provider.clouser(tableView, section, headerItem) { + return view + } + } + return nil + } +} diff --git a/Sources/SPDiffable/Table/DataSource/SPDiffableTableDataSource.swift b/Sources/SPDiffable/Table/DataSource/SPDiffableTableDataSource.swift index 64e2928..74be8b9 100644 --- a/Sources/SPDiffable/Table/DataSource/SPDiffableTableDataSource.swift +++ b/Sources/SPDiffable/Table/DataSource/SPDiffableTableDataSource.swift @@ -37,15 +37,28 @@ open class SPDiffableTableDataSource: UITableViewDiffableDataSource UITableViewCell? in for provider in cellProviders { if let cell = provider.clouser(tableView, indexPath, item) { @@ -54,6 +67,7 @@ open class SPDiffableTableDataSource: UITableViewDiffableDataSource UIView? } diff --git a/Sources/SPDiffable/Table/SPDiffableTableCellProvider.swift b/Sources/SPDiffable/Table/Providers/SPDiffableTableCellProvider.swift similarity index 100% rename from Sources/SPDiffable/Table/SPDiffableTableCellProvider.swift rename to Sources/SPDiffable/Table/Providers/SPDiffableTableCellProvider.swift diff --git a/Sources/SPDiffable/Table/Providers/SPDiffableTableHeaderFooterProvider.swift b/Sources/SPDiffable/Table/Providers/SPDiffableTableHeaderFooterProvider.swift new file mode 100644 index 0000000..4d37b94 --- /dev/null +++ b/Sources/SPDiffable/Table/Providers/SPDiffableTableHeaderFooterProvider.swift @@ -0,0 +1,32 @@ +// The MIT License (MIT) +// Copyright © 2020 Ivan Vorobei (hello@ivanvorobei.by) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +import UIKit + +@available(iOS 13.0, *) +open class SPDiffableTableHeaderFooterProvider { + + open var clouser: SPDiffableTableDataSource.HeaderFooterProvider + + public init(clouser: @escaping SPDiffableTableDataSource.HeaderFooterProvider) { + self.clouser = clouser + } +} diff --git a/Sources/SPDiffable/Table/SPDiffableTableController.swift b/Sources/SPDiffable/Table/SPDiffableTableController.swift index a7db61d..21401e6 100644 --- a/Sources/SPDiffable/Table/SPDiffableTableController.swift +++ b/Sources/SPDiffable/Table/SPDiffableTableController.swift @@ -32,15 +32,11 @@ open class SPDiffableTableController: UITableViewController { open var diffableDataSource: SPDiffableTableDataSource? - open weak var diffableDelegate: SPDiffableTableDelegate? - // MARK: - Lifecycle open override func viewDidLoad() { super.viewDidLoad() - tableView.delaysContentTouches = false - tableView.register(SPDiffableTableViewCell.self, forCellReuseIdentifier: SPDiffableTableViewCell.reuseIdentifier) tableView.register(SPDiffableSubtitleTableViewCell.self, forCellReuseIdentifier: SPDiffableSubtitleTableViewCell.reuseIdentifier) } @@ -53,25 +49,20 @@ open class SPDiffableTableController: UITableViewController { If need custom logic, you can manually init and apply data when you need. - warning: Changes applied not animatable. - - parameter providers: Cell Providers with valid order for processing. + - parameter cellProviders: Cell Providers with valid order for processing. + - parameter headerFooterProviders: Header and Footer providers with valid order for processing. - parameter sections: Content as array of `SPDiffableSection`. */ - open func setCellProviders( _ providers: [SPDiffableTableCellProvider], sections: [SPDiffableSection]) { - diffableDataSource = SPDiffableTableDataSource(tableView: tableView, cellProviders: providers) + open func setCellProviders( + _ cellProviders: [SPDiffableTableCellProvider], + headerFooterProviders: [SPDiffableTableHeaderFooterProvider] = [], + sections: [SPDiffableSection] + ) { + diffableDataSource = SPDiffableTableDataSource( + tableView: tableView, + cellProviders: cellProviders, + headerFooterProviders: headerFooterProviders + ) diffableDataSource?.apply(sections, animated: false) } - - // MARK: - UITableViewDelegate - - open override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { - guard let item = diffableDataSource?.item(for: indexPath) else { return } - diffableDelegate?.diffableTableView?(tableView, didSelectItem: item, indexPath: indexPath) - - switch item { - case let model as SPDiffableItemActionable: - model.action?(item, indexPath) - default: - break - } - } } diff --git a/Sources/SPDiffable/Table/SPDiffableTableView.swift b/Sources/SPDiffable/Table/SPDiffableTableView.swift new file mode 100644 index 0000000..b9c069f --- /dev/null +++ b/Sources/SPDiffable/Table/SPDiffableTableView.swift @@ -0,0 +1,27 @@ +// The MIT License (MIT) +// Copyright © 2020 Ivan Vorobei (hello@ivanvorobei.by) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +import UIKit + +@available(iOS 13.0, *) +open class SPDiffableTableView: UITableView { + +} diff --git a/TODO.md b/TODO.md index f3d6db6..f8da982 100644 --- a/TODO.md +++ b/TODO.md @@ -1,3 +1,5 @@ # TODO Here provided ideas or features which will be implemented soon. + +- Complete class `SPDiffableTableView`.