Skip to content

Commit

Permalink
优化代码 解决Tablesection binding冲突问题
Browse files Browse the repository at this point in the history
  • Loading branch information
fun committed Mar 31, 2022
1 parent d3816f2 commit d6eee50
Show file tree
Hide file tree
Showing 59 changed files with 663 additions and 1,072 deletions.
2 changes: 1 addition & 1 deletion InsetGrouped/InsetGrouped/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Jenga
class ViewController: UIViewController, DSLAutoTable {

@TableBuilder
var tableContents: [Sectionable] {
var tableContents: [Section] {
TableSection {

NavigationRow("设置样式")
Expand Down
126 changes: 61 additions & 65 deletions Jenga.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class CustomTableViewController: UIViewController, DSLTable {
extension CustomTableViewController {

@TableBuilder
var tableContents: [Sectionable] {
var tableContents: [Section] {

TableSection(binding: $array) {
TableRow<EmojiCell>()
Expand Down
25 changes: 22 additions & 3 deletions JengaExample/JengaExample/Examples/CustomViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Jenga
class CustomViewController: UIViewController, DSLAutoTable {

@State var emojis: [String] = []

override func viewDidLoad() {
super.viewDidLoad()
setup()
Expand All @@ -30,7 +30,7 @@ class CustomViewController: UIViewController, DSLAutoTable {
extension CustomViewController {

@TableBuilder
var tableContents: [Sectionable] {
var tableContents: [Section] {

TableSection {

Expand All @@ -40,7 +40,7 @@ extension CustomViewController {
cell.delegate = self
}

SeparatorRow(10)
SpacerRow(10)

TableRow<BannerCell>()
.height(1540 / 2078 * (UIScreen.main.bounds.width - 32))
Expand All @@ -58,6 +58,25 @@ extension CustomViewController {
}
.headerHeight(UITableView.automaticDimension)

TableSection(binding: $emojis.bindEnumerated(), on: self) { (self, emoji) in
TableRow<EmojiCell>()
.data(emoji.map { "\($0.element) \($0.offset)"})
.onTap(on: self) { (self) in

}
}
.rowHeight(44)
.headerHeight(12)
.hiddenWithEmpty(true)

TableSection(binding: $emojis) {

TableRow<EmojiCell>()
.data($0)
.height(44)
}
.headerHeight(UITableView.automaticDimension)

TableSection {
TapActionRow("Random")
.onTap(on: self) { (self) in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class SettingViewController: UIViewController, DSLAutoTable {
extension SettingViewController {

@TableBuilder
var tableContents: [Sectionable] {
var tableContents: [Section] {
TableSection {
NavigationRow("账号与安全")
.onTap {
Expand Down
4 changes: 2 additions & 2 deletions JengaExample/JengaExample/Examples/TableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class TableViewController: UIViewController, DSLAutoTable {
extension TableViewController {

@TableBuilder
var tableContents: [Sectionable] {
var tableContents: [Section] {

TableSection {
NavigationRow("用户协议")
Expand Down Expand Up @@ -125,7 +125,7 @@ extension TableViewController {
self.reloadTable()
}

SeparatorRow(30)
SpacerRow(30)

TapActionRow("切换开关, reload isOn binding")
.onTap(on: self) { (self) in
Expand Down
82 changes: 39 additions & 43 deletions JengaExample/JengaExample/Extension/Icon.AsyncImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,49 @@ import Jenga
import UIKit
import Kingfisher

extension Icon {
public struct AsyncImage: Jenga.AsyncImage {

public struct AsyncImage: Equatable, Jenga.AsyncImage {

public static func == (lhs: Icon.AsyncImage, rhs: Icon.AsyncImage) -> Bool {
lhs.source.cacheKey == rhs.source.cacheKey
}

/// The initializer is kept private until v2.0 when `methodSignature` is removed.
public init(_ source: Resource, placeholder: Placeholder? = .none, options: KingfisherOptionsInfo = []) {
self.source = source
self.placeholder = placeholder
self.options = options
}

var options: KingfisherOptionsInfo = []

/// The image for the normal state.
let source: Resource

/// The image for the highlighted state.
var placeholder: Placeholder?

var processor = RoundCornerImageProcessor(cornerRadius: 0) {
didSet {
options.append(.processor(processor))
}
public var downloadURL: URL { source.downloadURL }


/// The initializer is kept private until v2.0 when `methodSignature` is removed.
public init(_ source: Resource, placeholder: Placeholder? = .none, options: KingfisherOptionsInfo = []) {
self.source = source
self.placeholder = placeholder
self.options = options
}

var options: KingfisherOptionsInfo = []

/// The image for the normal state.
let source: Resource

/// The image for the highlighted state.
var placeholder: Placeholder?

var processor = RoundCornerImageProcessor(cornerRadius: 0) {
didSet {
options.append(.processor(processor))
}
public func loadImage(with imageView: UIImageView?, _ completion: @escaping (Bool) -> Void) {
imageView?.kf.setImage(
with: source,
placeholder: placeholder,
options: options
) { result in
switch result {
case .success: return completion(true)
case .failure: return completion(false)
}
}

public func loadImage(with imageView: UIImageView?, _ completion: @escaping (Bool) -> Void) {
imageView?.kf.setImage(
with: source,
placeholder: placeholder,
options: options
) { result in
switch result {
case .success: return completion(true)
case .failure: return completion(false)
}
}
}
}

public extension Icon.AsyncImage {
public extension AsyncImage {

static func async(_ source: Resource, placeholder: Placeholder? = .none, options: KingfisherOptionsInfo = []) -> Icon.AsyncImage {
static func async(_ source: Resource, placeholder: Placeholder? = .none, options: KingfisherOptionsInfo = []) -> Self {
return .init(source, placeholder: placeholder, options: options)
}

Expand Down Expand Up @@ -92,7 +88,7 @@ public extension Icon.AsyncImage {
}
}

public extension Icon.AsyncImage {
public extension AsyncImage {

func by(cornerRadius value: CGFloat? = nil) -> Self {
var temp = self
Expand All @@ -107,14 +103,14 @@ public extension Icon.AsyncImage {
}
}

public extension RowSystemable {
public extension RowSystem {

func icon(_ value: Binding<Icon.AsyncImage>) -> Self {
func icon(_ value: Binding<AsyncImage>) -> Self {
icon = value.map { .async($0) }
return self
}

func icon(_ value: Icon.AsyncImage) -> Self {
func icon(_ value: AsyncImage) -> Self {
icon = .constant(.async(value))
return self
}
Expand Down
2 changes: 1 addition & 1 deletion JengaExample/JengaExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Jenga
class ViewController: UIViewController, DSLAutoTable {

@TableBuilder
var tableContents: [Sectionable] {
var tableContents: [Section] {
TableSection {
NavigationRow("设置样式")
.onTap(on: self) { (self) in
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ extension Binding where Value == CGFloat {
}
}

extension Binding where Value: Sequence {
public extension Binding where Value: Sequence {

func enumerated() -> Binding<EnumeratedSequence<Value>> {
func bindEnumerated() -> Binding<EnumeratedSequence<Value>> {
return self.map { $0.enumerated() }
}
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

import UIKit

public class SeparatorCell: UITableViewCell {
public class SpacerCell: UITableViewCell {

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,22 @@ open class TapActionCell: UITableViewCell, ConfigurableCell {
setUpAppearance()
}

/**
Overrides the designated initializer that returns an object initialized from data in a given unarchiver.

- parameter aDecoder: An unarchiver object.

- returns: `self`, initialized using the data in decoder.
*/
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setUpAppearance()
}

// MARK: UIView

open override func tintColorDidChange() {
super.tintColorDidChange()
textLabel?.textColor = row?.text.color.wrappedValue ?? tintColor
}

// MARK: Private Methods

private func setUpAppearance() {
textLabel?.numberOfLines = 0
textLabel?.textAlignment = .center
}

private weak var row: TapActionRowCompatible?

open func configure(with row: Row) {
guard let row = row as? TapActionRowCompatible else {
return
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import UIKit

/// A `UITableViewCell` subclass that shows a `UISwitch` as the `accessoryView`.
open class ToggleCell: UITableViewCell, ConfigurableCell {

public private(set) lazy var switchControl = UISwitch()
Expand All @@ -26,5 +25,4 @@ open class ToggleCell: UITableViewCell, ConfigurableCell {
textLabel?.numberOfLines = 0
detailTextLabel?.numberOfLines = 0
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ extension UILabel {

var edgeInsets: UIEdgeInsets {
get {
let wrapper: Wrapper<UIEdgeInsets>? = associated.get(&AssociateKey.edgeInsets)
let wrapper: Wrapper<UIEdgeInsets>? = get(&AssociateKey.edgeInsets)
return wrapper?.value ?? .zero
}
set {
let wrapper = Wrapper(newValue)
associated.set(retain: &AssociateKey.edgeInsets, wrapper)
set(retain: &AssociateKey.edgeInsets, wrapper)
UILabel.swizzled
}
}
Expand Down
45 changes: 4 additions & 41 deletions Sources/Jenga/Jenga.swift → Sources/Jenga/Core/Jenga.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import UIKit

public typealias RowBuilder = ArrayBuilder<Row>
public typealias TableBuilder = ArrayBuilder<Section>

public enum JengaProvider { }

extension JengaProvider {
Expand All @@ -18,7 +21,7 @@ extension JengaProvider {
}

// 全局样式配置 不配置使用系统样式
public typealias DefaultCellHandle = (UITableViewCell, _ style: RowSystemable) -> Void
public typealias DefaultCellHandle = (UITableViewCell, _ style: RowSystem) -> Void
public typealias TableViewHandle = (CGRect) -> UITableView

internal static var defaultHandle: DefaultCellHandle?
Expand Down Expand Up @@ -53,43 +56,3 @@ extension JengaProvider {
return tableView
}
}

public enum TableRowActionType {

case click
case clickDelete
case select
case deselect
case willSelect
case willDeselect
case willDisplay
case didEndDisplaying
case shouldHighlight
case shouldBeginMultipleSelection
case didBeginMultipleSelection
case height
case canEdit
case configure
case canDelete
case canMove
case canMoveTo
case move
case showContextMenu
case accessoryButtonTap
case custom(String)

var key: String {

switch (self) {
case .custom(let key):
return key
default:
return "_\(self)"
}
}
}

//func `deinit`(item: Any...) {
// let result = "deinit" + item.map { "\($0)" }.joined(separator: "\t")
// print(result, separator: "\t")
//}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ import UIKit

public protocol AsyncImage {

var downloadURL: URL { get }

func loadImage(with imageView: UIImageView?, _ completion: @escaping (Bool) -> Void)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@ public struct DetailText: Equatable {
var text: Text

public enum `Type` {
/// Does not show a detail text in `UITableViewCell.CellStyle.default`.
case none
/// Shows the detail text in `UITableViewCell.CellStyle.subtitle`.
case subtitle
/// Shows the detail text in `UITableViewCell.CellStyle.value1`.
case value1
/// Shows the detail text in `UITableViewCell.CellStyle.value2`.
case value2
}

/// Returns the corresponding table view cell style.
public var style: UITableViewCell.CellStyle {
switch type {
case .none: return .default
Expand Down
Loading

0 comments on commit d6eee50

Please sign in to comment.