Skip to content

Commit

Permalink
Ontap 泛型支持
Browse files Browse the repository at this point in the history
  • Loading branch information
fun committed Jun 30, 2022
1 parent 681c5c0 commit a0246ed
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 21 deletions.
4 changes: 4 additions & 0 deletions JengaExample/JengaExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
B32FABA427FD4F08006A37BE /* HeaderFooterViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B32FABA327FD4F08006A37BE /* HeaderFooterViewController.swift */; };
B32FABA627FD7B93006A37BE /* Runtime.swift in Sources */ = {isa = PBXBuildFile; fileRef = B32FABA527FD7B92006A37BE /* Runtime.swift */; };
B331732527FD7D2C008DFF5E /* UILabel.Inset.swift in Sources */ = {isa = PBXBuildFile; fileRef = B331732427FD7D2C008DFF5E /* UILabel.Inset.swift */; };
B3436763286D895E003C213E /* OnTapViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3436762286D895E003C213E /* OnTapViewController.swift */; };
B3496B3627F45338007D7A1D /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3496B3527F45338007D7A1D /* AppDelegate.swift */; };
B3496B3827F45338007D7A1D /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3496B3727F45338007D7A1D /* SceneDelegate.swift */; };
B3496B3A27F45338007D7A1D /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3496B3927F45338007D7A1D /* ViewController.swift */; };
Expand Down Expand Up @@ -64,6 +65,7 @@
B32FABA327FD4F08006A37BE /* HeaderFooterViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HeaderFooterViewController.swift; sourceTree = "<group>"; };
B32FABA527FD7B92006A37BE /* Runtime.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Runtime.swift; sourceTree = "<group>"; };
B331732427FD7D2C008DFF5E /* UILabel.Inset.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UILabel.Inset.swift; sourceTree = "<group>"; };
B3436762286D895E003C213E /* OnTapViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnTapViewController.swift; sourceTree = "<group>"; };
B3496B3227F45338007D7A1D /* JengaExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = JengaExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
B3496B3527F45338007D7A1D /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
B3496B3727F45338007D7A1D /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -165,6 +167,7 @@
B32FABA127FD3649006A37BE /* StateViewController.swift */,
B32FABA327FD4F08006A37BE /* HeaderFooterViewController.swift */,
B372D8EF2859C49D0054BCA8 /* ViewRowViewController.swift */,
B3436762286D895E003C213E /* OnTapViewController.swift */,
);
path = Examples;
sourceTree = "<group>";
Expand Down Expand Up @@ -284,6 +287,7 @@
B3496BB327F45799007D7A1D /* NavigationRow2.swift in Sources */,
B32FABA227FD3649006A37BE /* StateViewController.swift in Sources */,
B32FAB7D27F827BD006A37BE /* BaseViewController.swift in Sources */,
B3436763286D895E003C213E /* OnTapViewController.swift in Sources */,
B32FABA627FD7B93006A37BE /* Runtime.swift in Sources */,
B3496B3A27F45338007D7A1D /* ViewController.swift in Sources */,
B3496B3627F45338007D7A1D /* AppDelegate.swift in Sources */,
Expand Down
4 changes: 4 additions & 0 deletions JengaExample/JengaExample/Examples/CustomViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ class CustomViewController: BaseViewController, DSLAutoTable {
SpacerRow(10)

TableRow<BannerCell>()
.isSelectable(true)
.height(1540 / 2078 * (UIScreen.main.bounds.width - 40))
.data("image2")
.onTap { cell in
print(cell)
}
.customize { (cell, value) in
print(cell, value)
}
Expand Down
107 changes: 107 additions & 0 deletions JengaExample/JengaExample/Examples/OnTapViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//
// OnTapViewController.swift
// JengaExample
//
// Created by 方林威 on 2022/6/30.
//

import UIKit
import Jenga

class OnTapViewController: BaseViewController, DSLAutoTable {

override var pageTitle: String { get { "OnTap 泛型支持" } }

@State private var string: String = "123"

var tableBody: [Table] {

TableSection {
NavigationRow<NavigationCell>("自定义 Navigation Cell")
.height(52)
.onTap { cell in
print(cell)
}

TableRow<BannerCell>(1993)
.height(1184 / 2256 * (UIScreen.main.bounds.width - 40))
.onTap { (view, data) in
print(view.id, data)
}

WrapperRow<BannerView>(1994)
.height(1184 / 2256 * (UIScreen.main.bounds.width - 40))
.onTap { (view, data) in
print(view.id, data)
}
}
}
}

private extension OnTapViewController {

class BannerView: UIView, TableRowView {

private lazy var coverImageView = UIImageView(image: UIImage(named: "image1")!)

let id: Int = 1993

override init(frame: CGRect) {
super.init(frame: frame)
setup()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}

private func setup() {
addSubview(coverImageView)
}

override func layoutSubviews() {
super.layoutSubviews()
coverImageView.frame = bounds
}

func configure(with data: Int) {
print(data)
}
}

class BannerCell: UITableViewCell, ConfigurableCell {

private lazy var coverImageView = UIImageView(image: UIImage(named: "image2")!)

let id: Int = 1994

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setup()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}

private func setup() {
addSubview(coverImageView)
}

override func layoutSubviews() {
super.layoutSubviews()
coverImageView.frame = bounds
}

func configure(with data: Int) {
print(data)
}
}

class NavigationCell: UITableViewCell {

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class ViewRowViewController: BaseViewController, DSLAutoTable {
TableSection {
WrapperRow<BannerView>(123)
.height(1184 / 2256 * (UIScreen.main.bounds.width - 40))
.onTap { (view, data) in
print(view, data)
}

WrapperRow<UILabel>("456")
.customize { label, data in
Expand Down Expand Up @@ -51,7 +54,6 @@ fileprivate class BannerView: UIView, TableRowView {

button.setTitle("点击", for: .normal)
button.setTitleColor(.red, for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
}

override func layoutSubviews() {
Expand All @@ -66,10 +68,5 @@ fileprivate class BannerView: UIView, TableRowView {
print("===============")
print(data)
}

@objc
private func buttonAction() {
print("123")
}
}

4 changes: 4 additions & 0 deletions JengaExample/JengaExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class ViewController: UIViewController, DSLAutoTable {
.onTap(on: self) { (self) in
self.navigationController?.pushViewController(ViewRowViewController(), animated: true)
}
NavigationRow("OnTap 泛型支持")
.onTap(on: self) { (self) in
self.navigationController?.pushViewController(OnTapViewController(), animated: true)
}
NavigationRow("测试")
.onTap(on: self) { (self) in
self.navigationController?.pushViewController(TableViewController(), animated: true)
Expand Down
11 changes: 11 additions & 0 deletions Sources/Jenga/Core/Row/BasicRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ public extension BasicRow {
func customize(_ value: @escaping ((T) -> Void)) -> Self {
reform { $0.customize = value }
}

func onTap(_ value: @escaping ((T) -> Void)) -> Self {
reform {
$0.action = { cell in
guard let cell = cell as? T else {
return
}
value(cell)
}
}
}
}

extension BasicRow {
Expand Down
17 changes: 10 additions & 7 deletions Sources/Jenga/Core/Row/Row.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ public protocol RowActionable {
var isSelectable: Bool { get set }

var action: RowAction? { get set }

func onTap<S>(on target: S, _ value: @escaping (S) -> Void) -> Self
func onTap<S>(on target: S, _ value: @escaping (S) -> Void) -> Self where S: AnyObject
}

public protocol Row: JengaHashable, RowActionable, Reform {
Expand Down Expand Up @@ -37,18 +40,18 @@ public extension Row {
func selectionStyle(_ value: UITableViewCell.SelectionStyle) -> Self {
reform { $0.selectionStyle = value }
}

func onTap(_ value: RowAction?) -> Self {
reform { $0.action = value }
}

func onTap(_ value: @escaping (() -> Void)) -> Self {
reform { $0.action = { _ in value() } }
}

func onTap<S>(on target: S, _ value: @escaping (S) -> Void) -> Self {
reform { $0.action = { value(target) } }
reform { $0.action = { _ in value(target) } }
}

func onTap<S>(on target: S, _ value: @escaping (S) -> Void) -> Self where S: AnyObject {
reform {
$0.action = { [weak target] in
$0.action = { [weak target] _ in
guard let target = target else { return }
value(target)
}
Expand Down Expand Up @@ -78,5 +81,5 @@ public enum RowHeight {
}
}

public typealias RowAction = () -> Void
public typealias RowAction = (UITableViewCell) -> Void

4 changes: 2 additions & 2 deletions Sources/Jenga/Core/Row/System/NavigationRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ open class NavigationRow<T: UITableViewCell>: BasicRow<T>, NavigationRowCompatib
set {}
}

public var accessoryButtonAction: RowAction?
public var accessoryButtonAction: (() -> Void)?

public override var isSelectable: Bool {
get { action != nil }
Expand All @@ -33,7 +33,7 @@ open class NavigationRow<T: UITableViewCell>: BasicRow<T>, NavigationRowCompatib

extension NavigationRow {

public func accessoryButtonAction(_ value: RowAction?) -> Self {
public func accessoryButtonAction(_ value: (() -> Void)?) -> Self {
reform { $0.accessoryButtonAction = value }
}

Expand Down
23 changes: 21 additions & 2 deletions Sources/Jenga/Core/Row/System/OptionRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import UIKit

/// TODO: 功能待开发
open class OptionRow<T: UITableViewCell>: BasicRow<T>, OptionRowCompatible, Equatable {

public typealias OptionRowAction = (() -> Void)
public init(text: Binding<String>, isSelected: Bool = false) {
super.init(text)
self.isSelected = isSelected
Expand All @@ -14,11 +14,13 @@ open class OptionRow<T: UITableViewCell>: BasicRow<T>, OptionRowCompatible, Equa
return
}
DispatchQueue.main.async {
self.action?()
self.optionRowAction?()
}
}
}

public var optionRowAction: OptionRowAction?

public override var accessoryType: UITableViewCell.AccessoryType {
get { isSelected ? .checkmark : .none }
set {}
Expand All @@ -37,4 +39,21 @@ public extension OptionRow {
func isSelected(_ value: Bool) -> Self {
reform { $0.isSelected = value }
}

func onTap(_ value: OptionRowAction?) -> Self {
reform { $0.optionRowAction = value }
}

func onTap<S>(on target: S, _ value: @escaping (S) -> Void) -> Self {
reform { $0.optionRowAction = { value(target) } }
}

func onTap<S>(on target: S, _ value: @escaping (S) -> Void) -> Self where S: AnyObject {
reform {
$0.optionRowAction = { [weak target] in
guard let target = target else { return }
value(target)
}
}
}
}
2 changes: 1 addition & 1 deletion Sources/Jenga/Core/Row/System/SystemRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public extension SystemRow {

public protocol NavigationRowCompatible: SystemRow {

var accessoryButtonAction: RowAction? { get }
var accessoryButtonAction: (() -> Void)? { get }
}

public protocol BadgeRowCompatible: SystemRow, AnyObject {
Expand Down
22 changes: 22 additions & 0 deletions Sources/Jenga/Core/Row/TableRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,26 @@ public extension TableRow {
func customize(_ value: @escaping (Cell) -> Void) -> Self {
reform { $0.customize = { (cell, _) in value(cell) } }
}

func onTap(_ value: @escaping ((Cell) -> Void)) -> Self {
reform {
$0.action = { cell in
guard let cell = cell as? Cell else {
return
}
value(cell)
}
}
}

func onTap(_ value: @escaping ((Cell, Cell.CellData) -> Void)) -> Self {
reform {
$0.action = { cell in
guard let cell = cell as? Cell, let data = item?.wrappedValue else {
return
}
value(cell, data)
}
}
}
}
23 changes: 23 additions & 0 deletions Sources/Jenga/Core/Row/WrapperRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,29 @@ public extension WrapperRow {
func customize(_ value: @escaping (View) -> Void) -> Self {
reform { $0.customize = { (view, _) in value(view) } }
}


func onTap(_ value: @escaping ((View) -> Void)) -> Self {
reform {
$0.action = { cell in
guard let cell = cell as? Cell else {
return
}
value(cell.view)
}
}
}

func onTap(_ value: @escaping ((View, Data) -> Void)) -> Self {
reform {
$0.action = { cell in
guard let cell = cell as? Cell, let data = item?.wrappedValue else {
return
}
value(cell.view, data)
}
}
}
}

extension WrapperRow: Table { }
Expand Down
Loading

0 comments on commit a0246ed

Please sign in to comment.