Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Fixed

- Fixed a confusing compilation error when omitting the `.content` property on read-only `KeyPath` lookups on `ListProperties`.

### Added

### Removed
Expand Down
27 changes: 25 additions & 2 deletions ListableUI/Sources/ListProperties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,31 @@ import UIKit
configure(&self)
}

//
// MARK: Reading Content
//

/// Allows directly reading properties on the list's `Content`, without having to explicitly specify
/// the `.content` component.
///
/// Eg, you can now replace:
/// ```
/// ListProperties { list in
/// ... = list.content.firstItem
/// ... = list.content.lastItem
/// }
/// ```
/// With:
/// ```
/// ListProperties { list in
/// ... = list.firstItem
/// ... = list.lastItem
/// }
/// ```
public subscript<Value>(dynamicMember keyPath: KeyPath<Content, Value>) -> Value {
get { self.content[keyPath: keyPath] }
}

//
// MARK: Adding Content
//
Expand All @@ -208,15 +233,13 @@ import UIKit
/// ListProperties { list in
/// list.content.header = ...
/// list.content.footer = ...
/// ...
/// }
/// ```
/// With:
/// ```
/// ListProperties { list in
/// list.header = ...
/// list.footer = ...
/// ...
/// }
/// ```
public subscript<Value>(dynamicMember keyPath: WritableKeyPath<Content, Value>) -> Value {
Expand Down
30 changes: 29 additions & 1 deletion ListableUI/Tests/ListPropertiesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,37 @@
// Created by Kyle Van Essen on 11/27/19.
//

import ListableUI
import XCTest

class ListPropertiesTests : XCTestCase
{

private func properties() -> ListProperties {
ListProperties(
animatesChanges: true,
layout: .flow(),
appearance: .init(),
scrollIndicatorInsets: .zero,
behavior: .init(),
autoScrollAction: .none,
accessibilityIdentifier: "",
debuggingIdentifier: "") { _ in }
}

func test_read_only_dynamic_member_lookup() {
let properties = properties()
// Ensure that this compiles. Previously we were missing the read-only KeyPath
// dynamicMember subscript implementation which would lead to this error:
// Cannot assign to property: 'lastItem' is a get-only property
let item = properties.lastItem
XCTAssertNil(item)
}

func test_writeable_dynamic_member_lookup() {
var properties = properties()
// Ensure that this compiles and writes through to the underlying content.
properties.identifier = "Hello"
XCTAssertEqual(properties.identifier, "Hello")
XCTAssertEqual(properties.identifier, properties.content.identifier)
}
}