Skip to content

Commit

Permalink
remove BaseToolbox dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
lkzhao committed Jan 11, 2024
1 parent 8b9b255 commit e1a48c8
Show file tree
Hide file tree
Showing 20 changed files with 141 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
{
"object": {
"pins": [
{
"package": "BaseToolbox",
"repositoryURL": "https://github.com/lkzhao/BaseToolbox",
"state": {
"branch": null,
"revision": "62fe235533582054af1d7f13de714c76a72cf12f",
"version": "0.4.1"
}
},
{
"package": "Kingfisher",
"repositoryURL": "https://github.com/onevcat/Kingfisher",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import UIComponent
import UIKit
import BaseToolbox

struct ImageData {
let url: URL
Expand Down Expand Up @@ -46,10 +45,10 @@ class AsyncImageViewController: ComponentViewController {
for (index, image) in images.enumerated() {
AsyncImage(image.url)
.size(width: .fill, height: .aspectPercentage(image.size.height / image.size.width))
.tappableView {
.tappableView { [weak self] in
let detailVC = AsyncImageDetailViewController()
detailVC.image = image
$0.parentViewController?.navigationController?.pushViewController(detailVC, animated: true)
self?.navigationController?.pushViewController(detailVC, animated: true)
}
.previewBackgroundColor(.systemBackground.withAlphaComponent(0.7))
.previewProvider {
Expand Down Expand Up @@ -90,8 +89,8 @@ class AsyncImageDetailViewController: ComponentViewController {
VStack {
AsyncImage(image.url)
.size(width: .fill, height: .aspectPercentage(image.size.height / image.size.width))
.tappableView {
$0.parentViewController?.navigationController?.popViewController(animated: true)
.tappableView { [weak self] in
self?.navigationController?.popViewController(animated: true)
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions Examples/UIComponentExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,17 @@ struct ExampleItem: ComponentBuilder {
}
}
}

extension UIView {
var parentViewController: UIViewController? {
var responder: UIResponder? = self
while responder is UIView {
responder = responder!.next
}
return responder as? UIViewController
}

func present(_ viewController: UIViewController, completion: (() -> Void)? = nil) {
parentViewController?.present(viewController, animated: true, completion: completion)
}
}
3 changes: 1 addition & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ let package = Package(
)
],
dependencies: [
.package(url: "https://github.com/lkzhao/BaseToolbox", from: "0.4.0")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "UIComponent",
dependencies: ["BaseToolbox"]
dependencies: []
),
.testTarget(
name: "UIComponentTests",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Created by Luke Zhao on 8/24/20.

@_implementationOnly import BaseToolbox

import UIKit

/// Implementation for `FlexColumn` & `FlexRow`
Expand Down
2 changes: 1 addition & 1 deletion Sources/UIComponent/Components/Layout/Inset/Insets.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Created by Luke Zhao on 8/23/20.

@_implementationOnly import BaseToolbox

import UIKit

public struct Insets: Component {
Expand Down
2 changes: 1 addition & 1 deletion Sources/UIComponent/Components/Layout/Other/Badge.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Created by y H on 2021/7/25.

@_implementationOnly import BaseToolbox

import UIKit

/// # Badge Component
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Created by Luke Zhao on 8/22/20.

@_implementationOnly import BaseToolbox

import UIKit

public protocol StackRenderNode: RenderNode, BaseLayoutProtocol {
Expand Down
2 changes: 1 addition & 1 deletion Sources/UIComponent/Components/Layout/Stack/ZStack.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Created by Luke Zhao on 8/26/20.

@_implementationOnly import BaseToolbox

import UIKit

public struct ZStack: Component {
Expand Down
10 changes: 5 additions & 5 deletions Sources/UIComponent/Components/View/PrimaryMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ public class PrimaryMenu: UIControl {
public override func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
let config = configuration ?? PrimaryMenuConfiguration.default
config.didTap?(self)
return UIContextMenuConfiguration(actionProvider: { [menu] suggested in
let menuConfiguration = UIContextMenuConfiguration(actionProvider: { [menu] suggested in
return menu
}).then {
if #available(iOS 16.0, *) {
$0.preferredMenuElementOrder = self.preferredMenuElementOrder
}
})
if #available(iOS 16.0, *) {
menuConfiguration.preferredMenuElementOrder = self.preferredMenuElementOrder
}
return menuConfiguration
}

public override func contextMenuInteraction(_ interaction: UIContextMenuInteraction, willDisplayMenuFor configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionAnimating?) {
Expand Down
10 changes: 6 additions & 4 deletions Sources/UIComponent/Components/View/TappableView.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Created by Luke Zhao on 6/8/21.

import UIKit
@_implementationOnly import BaseToolbox


public struct TappableViewConfiguration {
public static var `default` = TappableViewConfiguration(onHighlightChanged: nil, didTap: nil)
Expand All @@ -22,9 +22,11 @@ open class TappableView: ComponentView {
public var configuration: TappableViewConfiguration?

public private(set) lazy var tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didTap))
public private(set) lazy var doubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didDoubleTap)).then {
$0.numberOfTapsRequired = 2
}
public private(set) lazy var doubleTapGestureRecognizer: UITapGestureRecognizer = {
let gesture = UITapGestureRecognizer(target: self, action: #selector(didDoubleTap))
gesture.numberOfTapsRequired = 2
return gesture
}()
public private(set) lazy var longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(didLongPress))
#if !os(tvOS)
public private(set) lazy var contextMenuInteraction = UIContextMenuInteraction(delegate: self)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Created by Luke Zhao on 8/27/20.

@_implementationOnly import BaseToolbox

import UIKit

public protocol ComponentReloadDelegate: AnyObject {
Expand Down
6 changes: 3 additions & 3 deletions Sources/UIComponent/Core/Model/Animator.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Created by Luke Zhao on 2017-07-19.

@_implementationOnly import BaseToolbox

import UIKit

open class Animator {
Expand Down Expand Up @@ -58,8 +58,8 @@ open class Animator {
view: UIView,
frame: CGRect
) {
if view.bounds.size != frame.bounds.size {
view.bounds.size = frame.bounds.size
if view.bounds.size != frame.size {
view.bounds.size = frame.size
}
if view.center != frame.center {
view.center = frame.center
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ extension Component {
public func roundedCorner() -> UpdateComponent<Self> {
ModifierComponent(content: self) { node in
node.update { view in
view.cornerRadius = min(node.size.width, node.size.height) / 2
view.layer.cornerRadius = min(node.size.width, node.size.height) / 2
}
}
}
Expand Down Expand Up @@ -198,16 +198,16 @@ extension Component {
Insets(child: self, insets: UIEdgeInsets(top: top, left: left, bottom: bottom, right: right))
}
public func inset(top: CGFloat, rest: CGFloat) -> some Component {
Insets(child: self, insets: UIEdgeInsets(top: top, rest: rest))
Insets(child: self, insets: UIEdgeInsets(top: top, left: rest, bottom: rest, right: rest))
}
public func inset(left: CGFloat, rest: CGFloat) -> some Component {
Insets(child: self, insets: UIEdgeInsets(left: left, rest: rest))
Insets(child: self, insets: UIEdgeInsets(top: rest, left: left, bottom: rest, right: rest))
}
public func inset(bottom: CGFloat, rest: CGFloat) -> some Component {
Insets(child: self, insets: UIEdgeInsets(bottom: bottom, rest: rest))
Insets(child: self, insets: UIEdgeInsets(top: rest, left: rest, bottom: bottom, right: rest))
}
public func inset(right: CGFloat, rest: CGFloat) -> some Component {
Insets(child: self, insets: UIEdgeInsets(right: right, rest: rest))
Insets(child: self, insets: UIEdgeInsets(top: rest, left: rest, bottom: rest, right: right))
}
public func inset(_ insets: UIEdgeInsets) -> some Component {
Insets(child: self, insets: insets)
Expand Down
5 changes: 3 additions & 2 deletions Sources/UIComponent/Core/Model/Constraint.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Created by Luke Zhao on 8/22/20.

@_implementationOnly import BaseToolbox

import UIKit

public struct Constraint {
public var minSize: CGSize
public var maxSize: CGSize
public var isTight: Bool { minSize == maxSize }

public init(minSize: CGSize = .minSize, maxSize: CGSize = .infinity) {
public init(minSize: CGSize = .constraintMinSize,
maxSize: CGSize = .constraintMaxSize) {
self.minSize = minSize
self.maxSize = maxSize
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/UIComponent/Core/Model/RenderNode/RenderNode.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Created by Luke Zhao on 8/22/20.

import UIKit
@_implementationOnly import BaseToolbox


public enum ReuseStrategy {
case automatic, noReuse
Expand Down Expand Up @@ -106,8 +106,8 @@ extension RenderNode {

extension RenderNode {
public func frame(at index: Int) -> CGRect? {
guard let size = children.get(index)?.size, let position = positions.get(index) else { return nil }
return CGRect(origin: position, size: size)
guard children.count > index, positions.count > index, index >= 0 else { return nil }
return CGRect(origin: positions[index], size: children[index].size)
}

public func frame(id: String) -> CGRect? {
Expand Down
2 changes: 1 addition & 1 deletion Sources/UIComponent/Core/Model/RenderNode/Renderable.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Created by Luke Zhao on 8/22/20.

import UIKit
@_implementationOnly import BaseToolbox


public protocol Renderable {
var id: String { get }
Expand Down
9 changes: 0 additions & 9 deletions Sources/UIComponent/Extensions/CGSize.swift

This file was deleted.

89 changes: 89 additions & 0 deletions Sources/UIComponent/Extensions/CoreGraphics.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Created by Luke Zhao on 10/17/21.


import UIKit

extension CGSize {
static public let constraintMaxSize: CGSize = CGSize(width: CGFloat.infinity, height: CGFloat.infinity)

static public let constraintMinSize: CGSize = CGSize(width: -CGFloat.infinity, height: -CGFloat.infinity)
}

extension CGPoint {
@inlinable public func distance(_ point: CGPoint) -> CGFloat {
hypot(point.x - x, point.y - y)
}

@inlinable public static func + (left: CGPoint, right: CGPoint) -> CGPoint {
CGPoint(x: left.x + right.x, y: left.y + right.y)
}

@inlinable public static func - (left: CGPoint, right: CGPoint) -> CGPoint {
CGPoint(x: left.x - right.x, y: left.y - right.y)
}

@inlinable public static func += (left: inout CGPoint, right: CGPoint) {
left.x += right.x
left.y += right.y
}
}

extension CGSize {
@inlinable public static func * (left: CGSize, right: CGFloat) -> CGSize {
CGSize(width: left.width * right, height: left.height * right)
}

public func inset(by insets: UIEdgeInsets) -> CGSize {
CGSize(width: width - insets.left - insets.right, height: height - insets.top - insets.bottom)
}
}

extension CGRect {
@inlinable var center: CGPoint {
CGPoint(x: midX, y: midY)
}

@inlinable var bounds: CGRect {
CGRect(origin: .zero, size: size)
}

@inlinable public static func + (left: CGRect, right: CGPoint) -> CGRect {
CGRect(origin: left.origin + right, size: left.size)
}

@inlinable public static func - (left: CGRect, right: CGPoint) -> CGRect {
CGRect(origin: left.origin - right, size: left.size)
}
}

extension Comparable {
public func clamp(_ minValue: Self, _ maxValue: Self) -> Self {
self < minValue ? minValue : (self > maxValue ? maxValue : self)
}
}

extension UIEdgeInsets {
static public prefix func - (inset: UIEdgeInsets) -> UIEdgeInsets {
UIEdgeInsets(top: -inset.top, left: -inset.left, bottom: -inset.bottom, right: -inset.right)
}
}

extension Collection {
/// Finds such index N that predicate is true for all elements up to
/// but not including the index N, and is false for all elements
/// starting with index N.
/// Behavior is undefined if there is no such N.
public func binarySearch(predicate: (Iterator.Element) -> Bool) -> Index {
var low = startIndex
var high = endIndex
while low != high {
let mid = index(low, offsetBy: distance(from: low, to: high) / 2)
if predicate(self[mid]) {
low = index(after: mid)
} else {
high = mid
}
}
return low
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Created by Luke on 4/16/17.

@_implementationOnly import BaseToolbox

import UIKit

extension UIScrollView {
Expand Down

0 comments on commit e1a48c8

Please sign in to comment.