Skip to content

Commit

Permalink
Replace assertions in slice method with alert func
Browse files Browse the repository at this point in the history
  • Loading branch information
NickEntin committed Aug 30, 2024
1 parent 42eaffe commit ac373ea
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
44 changes: 36 additions & 8 deletions Paralayout/GeometryAdditions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
//

import os
import UIKit

// MARK: - Operator Overloads
Expand Down Expand Up @@ -144,6 +145,8 @@ extension CGRect {

/// Divides the receiver in two.
///
/// The `slice` will always have a length of the specified `amount` in the perpendicular axis to the `edge`. The `remainder` will have the length remaining along that axis, clamped to zero if the `amount` is longer than the receiver's length.
///
/// - parameter from: The edge from which the amount is interpreted.
/// - parameter amount: The size of the slice (absolute).
/// - returns: A tuple (slice: A rect with a width/height of the `amount`, remainder: A rect with a width/height of
Expand All @@ -152,34 +155,42 @@ extension CGRect {
switch edge {
case .minXEdge:
// Left.
assert(amount <= width, "Cannot slice rect \(self) at edge \(edge) by \(amount)!")
if amount > width {
ParalayoutAlertForInvalidSliceDimensions()
}
return (
CGRect(x: minX, y: minY, width: amount, height: height),
CGRect(x: minX + amount, y: minY, width: width - amount, height: height)
CGRect(x: minX + amount, y: minY, width: max(width - amount, 0), height: height)
)

case .minYEdge:
// Top.
assert(amount <= height, "Cannot slice rect \(self) at edge \(edge) by \(amount)!")
if amount > height {
ParalayoutAlertForInvalidSliceDimensions()
}
return(
CGRect(x: minX, y: minY, width: width, height: amount),
CGRect(x: minX, y: minY + amount, width: width, height: height - amount)
CGRect(x: minX, y: minY + amount, width: width, height: max(height - amount, 0))
)

case .maxXEdge:
// Right.
assert(amount <= width, "Cannot slice rect \(self) at edge \(edge) by \(amount)!")
if amount > width {
ParalayoutAlertForInvalidSliceDimensions()
}
return(
CGRect(x: maxX - amount, y: minY, width: amount, height: height),
CGRect(x: minX, y: minY, width: width - amount, height: height)
CGRect(x: minX, y: minY, width: max(width - amount, 0), height: height)
)

case .maxYEdge:
// Bottom.
assert(amount <= height, "Cannot slice rect \(self) at edge \(edge) by \(amount)!")
if amount > height {
ParalayoutAlertForInvalidSliceDimensions()
}
return(
CGRect(x: minX, y: maxY - amount, width: width, height: amount),
CGRect(x: minX, y: minY, width: width, height: height - amount)
CGRect(x: minX, y: minY, width: width, height: max(height - amount, 0))
)
}
}
Expand Down Expand Up @@ -282,3 +293,20 @@ extension NSDirectionalEdgeInsets {
}

}

// MARK: -

nonisolated
private func ParalayoutAlertForInvalidSliceDimensions() {
os_log(
"%@",
log: ParalayoutLog,
type: .default,
"""
Paralayout detected a slice with an `amount` larger than the receiver's length in the specified axis. \
Set a symbolic breakpoint for \"ParalayoutAlertForInvalidSliceDimensions\" to debug. \
Call stack:
\(Thread.callStackSymbols.dropFirst(1).joined(separator: "\n"))
"""
)
}
2 changes: 0 additions & 2 deletions Paralayout/UIView+Alignment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@ extension AlignmentContext {

// MARK: -

@MainActor
private let ParalayoutLog = OSLog(subsystem: "com.squareup.Paralayout", category: "layout")

/// Triggered when an alignment method is called that uses mismatched position types, i.e. aligning a view's leading or
/// trailing edge to another view's left or right edge, or vice versa. This type of mismatch is likely to look correct
Expand Down
20 changes: 20 additions & 0 deletions ParalayoutLog.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Copyright © 2024 Block, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import os

nonisolated
internal let ParalayoutLog = OSLog(subsystem: "com.squareup.Paralayout", category: "layout")

0 comments on commit ac373ea

Please sign in to comment.