Skip to content

Commit f0d3266

Browse files
committed
CoreUI 1.0.0
0 parents  commit f0d3266

25 files changed

+1646
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
/*.xcodeproj
5+
xcuserdata/
6+
DerivedData/
7+
.swiftpm/config/registries.json
8+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Ivan Koshkin
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Package.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// swift-tools-version:5.3
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "CoreUI",
8+
platforms: [.iOS(.v11)],
9+
products: [
10+
.library(name: "CoreUI", targets: ["CoreUI"]),
11+
],
12+
dependencies: [],
13+
targets: [
14+
.target(
15+
name: "CoreUI",
16+
dependencies: []),
17+
.testTarget(
18+
name: "CoreUITests",
19+
dependencies: ["CoreUI"]),
20+
]
21+
)

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# CoreUI
2+
3+
Library to build UI in declarative and functional style. Built on top of UIKit and supports old iOS versions.
4+
5+
```swift
6+
import UIKit
7+
import CoreUI
8+
9+
class MyViewController: UIViewController {
10+
11+
override func viewDidLoad() {
12+
super.viewDidLoad()
13+
14+
addSubview(
15+
UI.VScrollView(
16+
contentInset: .vertical(12),
17+
UI.VStack(
18+
UI.View(backgroundColor: .red, height: 20),
19+
UI.Label("Are you sure?", font: .systemFont(ofSize: 24), color: .label),
20+
UI.Spacer(minHeight: 30),
21+
UI.HStack(
22+
spacing: 12,
23+
UI.Button("Yes").apply { $0.addTarget(self, action: #selector(yesTapped), for: .touchUpInside) },
24+
UI.Button("No").apply { $0.addTarget(self, action: #selector(noTapped), for: .touchUpInside) }
25+
)
26+
),
27+
.leading(6), .trailing(-6), .safeTop(0), .safeBottom(0)
28+
)
29+
)
30+
}
31+
32+
}
33+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
3+
/// Extension for `Optional` to use as `someOptionalVar.let { ... }`
4+
/// No actual need for this in real life, we can always use `someOptionalVar?.let { ... }`.
5+
/// Keeping it here for future reference.
6+
///
7+
extension Optional where Wrapped: ApplyProtocol {
8+
/// Applies `block` to some optional `object` and returns block result or `nil`.
9+
/// If `object` is non-nil, block receives `object` as its first parameter.
10+
/// - Parameter block: block receiving Self as its first parameter and returning
11+
/// - Returns: block return value
12+
///
13+
/// # Example
14+
/// ```
15+
/// var optionalValue: SomeClass? = nil
16+
/// let propertyOrNil = optionalValue.let {
17+
/// return $0.someProperty
18+
/// }
19+
/// XCAssertNil(propertyOrNil)
20+
///
21+
/// optionalValue = SomeClass()
22+
///
23+
/// ```
24+
@inlinable
25+
func `let`<R>(block: (Wrapped) -> R?) -> R? {
26+
guard let `self` = self else { return nil }
27+
return block(self)
28+
}
29+
}
30+
31+
*/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Foundation
2+
import UIKit
3+
4+
/// Adding ApplyProtocol to some manually selected classes only (UIView, etc).
5+
/// Do not adding ApplyProtocol to NSObject: it will be too wide.
6+
extension UIView: ApplyProtocol {}
7+
extension UIViewController: ApplyProtocol {}
8+
extension NSLayoutConstraint: ApplyProtocol {}
9+
extension UIGestureRecognizer: ApplyProtocol {}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/// Kotlin-like .apply and .let function
2+
public protocol ApplyProtocol {}
3+
4+
public extension ApplyProtocol {
5+
6+
/// Applies `block` to some `object` and returns `object`.
7+
/// Block receives `object` as its first parameter.
8+
/// - Parameter block: block which is applied to self
9+
/// - Returns: self
10+
///
11+
/// # Example
12+
/// ```
13+
/// let view = UIView().apply {
14+
/// $0.backgroundColor = .red
15+
/// $0.translatesAutoresizingMaskIntoConstraints = false
16+
/// // implicitly returns modified UIView
17+
/// }
18+
/// ```
19+
@inlinable
20+
@discardableResult
21+
func apply(_ block: (Self) -> Void) -> Self {
22+
block(self)
23+
return self
24+
}
25+
26+
/// Applies `block` to some `object` and returns block result.
27+
/// Block receives `object` as its first parameter.
28+
/// - Parameter block: block which is applied to self
29+
/// - Returns: block return value
30+
///
31+
/// # Example
32+
/// ```
33+
/// let xFromFrameOriginWithAddition = UIView(frame: .zero).let {
34+
/// // do something with new UIView here
35+
/// return $0.frame.origin.x + 200
36+
/// }
37+
/// XCTAssertEqual(xFromFrameOriginWithAddition, 200)
38+
/// ```
39+
@inlinable
40+
func `let`<R>(block: (Self) -> R) -> R {
41+
return block(self)
42+
}
43+
}
44+

0 commit comments

Comments
 (0)