Skip to content

Commit

Permalink
Work around build issues when this is used as a dev pod
Browse files Browse the repository at this point in the history
Unfortunately I am still not quite sure what's the root cause: for some reason
MMMContainerView and MMMLayoutUtils.inverseGolden could not be found when this
pod was included as a development one. Separating them into their own sources
seems to fix the problem for now but I guess it's just a symptom of another
issue.
  • Loading branch information
aleh committed Apr 14, 2023
1 parent a26e059 commit 1ca7c11
Show file tree
Hide file tree
Showing 10 changed files with 297 additions and 283 deletions.
File renamed without changes.
23 changes: 1 addition & 22 deletions Sources/MMMCommonUIObjC/MMMCommonUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,4 @@
// Copyright (C) 2016-2020 MediaMonks. All rights reserved.
//

#import "MMMAnimations.h"
#import "MMMAutoLayoutScrollView.h"
#import "MMMCollectionView.h"
#import "MMMCommonUIMisc.h"
#import "MMMImageView.h"
#import "MMMKeyboard.h"
#import "MMMLayout.h"
#import "MMMNavigation.h"
#import "MMMNavigationStack.h"
#import "MMMPhoto.h"
#import "MMMPhotoLibraryLoadableImage.h"
#import "MMMPreferredSizeChanges.h"
#import "MMMScrollViewShadows.h"
#import "MMMShadowView.h"
#import "MMMStubView.h"
#import "MMMStubViewController.h"
#import "MMMStylesheet.h"
#import "MMMTableView.h"
#import "MMMTableViewCell.h"
#import "MMMVerticalGradientView.h"
#import "MMMViewWrappingCell.h"
#import "MMMWebView.h"
// This file is needed because Swift compatibility headers always contain `#import <MMMCommonUI/MMMCommonUI.h>` line.
4 changes: 2 additions & 2 deletions Sources/MMMCommonUIObjC/MMMCommonUIMisc.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
// Copyright (C) 2016-2020 MediaMonks. All rights reserved.
//

@import UIKit;

#if SWIFT_PACKAGE
#import "UIKit/UIKit.h"
@import MMMCommonCoreObjC;
#else
@import UIKit;
@import MMMCommonCore;
#endif

Expand Down
21 changes: 21 additions & 0 deletions Sources/MMMCommonUIObjC/MMMContainerView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Starbucks App.
// Copyright (c) 2023 MediaMonks. All rights reserved.
//

@import UIKit;

/**
* Auto Layout does not support constraints against groups of items, so this is for the cases a normal UIView is
* typically used as a container for such a group.
* Unlike UIView we have translatesAutoresizingMaskIntoConstraints set to NO already.
* Also `MMMContainerView` does not intercept touches but subviews still do.
*/
@interface MMMContainerView : UIView

- (nonnull id)init NS_DESIGNATED_INITIALIZER;

- (id)initWithCoder:(NSCoder *)aDecoder NS_UNAVAILABLE;
- (id)initWithFrame:(CGRect)frame NS_UNAVAILABLE;

@end
30 changes: 30 additions & 0 deletions Sources/MMMCommonUIObjC/MMMContainerView.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Starbucks App.
// Copyright (c) 2023 MediaMonks. All rights reserved.
//

#import "MMMContainerView.h"

@implementation MMMContainerView

- (id)init {

if (self = [super initWithFrame:CGRectZero]) {
self.translatesAutoresizingMaskIntoConstraints = NO;
}

return self;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

UIView *view = [super hitTest:point withEvent:event];

if (view == self) {
return nil;
} else {
return view;
}
}

@end
133 changes: 3 additions & 130 deletions Sources/MMMCommonUIObjC/MMMLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,113 +3,10 @@
// Copyright (C) 2016-2020 MediaMonks. All rights reserved.
//

#import <UIKit/UIKit.h>
@import UIKit;

NS_ASSUME_NONNULL_BEGIN

/**
* This is to group a few simple layout helpers.
*/
@interface MMMLayoutUtils : NSObject

/**
* A rect with the given size positioned inside of the target rect in such a way that anchor points of both rects align.
*
* Anchor points are given relative to the sizes of the corresponding rects, similar to CALayer's `anchorPoint`
* property. For example, `CGPointMake(0.5, 0.5)` represents a center of any rect; `CGPointMake(1, 0.5)` means
* the center point of the right vertical edge.
*
* Note that the origin of the rect returned is rounded to the nearest pixels (not points!).
*
* See `rectWithSize:inRect:contentMode:` for a shortcut supporting UIViewContentMode.
*/
+ (CGRect)rectWithSize:(CGSize)size anchor:(CGPoint)anchor withinRect:(CGRect)targetRect anchor:(CGPoint)targetAnchor
NS_SWIFT_NAME(rect(withSize:anchor:withinRect:anchor:));

/**
* A shortcut for the above method with anchors being the same for both source and target rect.
* (This way the resulting rect will be always inside of the target one, assuming anchors are within [0; 1] range.)
*/
+ (CGRect)rectWithSize:(CGSize)size withinRect:(CGRect)targetRect anchor:(CGPoint)anchor
NS_SWIFT_NAME(rect(withSize:withinRect:anchor:));

/**
* A frame for the `sourceRect` positioned within the `targetRect` according to standard `UIViewContentMode` flags
* related to the layout (i.e. all except `UIViewContentModeRedraw`).
*
* Note that the origin of the resulting rectangle is always rounded to the nearest pixel.
*/
+ (CGRect)rectWithSize:(CGSize)size withinRect:(CGRect)targetRect contentMode:(UIViewContentMode)contentMode
NS_SWIFT_NAME(rect(withSize:withinRect:contentMode:));

/**
* A frame of the given size with its center at the specified point (assuming the center is defined by the given anchor
* point).
*
* Note that the origin of the resulting rectangle is rounded to the nearest pixel boundary.
*/
+ (CGRect)rectWithSize:(CGSize)size atPoint:(CGPoint)center anchor:(CGPoint)anchor
NS_SWIFT_NAME(rect(withSize:atPoint:anchor:));

/** Same as rectWithSize:center:anchor: with anchor set to (0.5, 0.5). */
+ (CGRect)rectWithSize:(CGSize)size center:(CGPoint)center
NS_SWIFT_NAME(rect(withSize:center:));

@end

/**
* Suppose you need to contrain a view so its center divides its container in certain ratio different from 1:1
* (e.g. golden section):
*
* ┌─────────┐ ◆
* │ │ │
* │ │ │ a
* │┌───────┐│ │
* ─│┼ ─ ─ ─ ┼│─◆ ratio = a / b
* │└───────┘│ │
* │ │ │
* │ │ │
* │ │ │ b
* │ │ │
* │ │ │
* │ │ │
* └─────────┘ ◆
*
* You cannot put this ratio directly into the `multiplier` parameter of the corresponding NSLayoutConstraints relating
* the centers of the views, because the `multiplier` would be the ratio between the distance to the center
* of the view (`h`) and the distance to the center of the container (`H`) instead:
*
* ◆ ┌─────────┐ ◆
* │ │ │ │
* │ │ │ │ a = h
* H │ │┌───────┐│ │
* │ │├ ─ ─ ─ ┼│─◆ multiplier = h / H
* │ │└───────┘│ │ ratio = a / b = h / (2 * H - h)
* ◆─│─ ─ ─ ─ ─│ │
* │ │ │
* │ │ │ b = 2 * H - h
* │ │ │
* │ │ │
* │ │ │
* └─────────┘ ◆
*
* I.e. the `multiplier` is h / H (assuming the view is the first in the definition of the constraint),
* but the ratio we are interested would be h / (2 * H - h) if expressed in the distances to centers.
*
* If you have a desired ratio and want to get a `multiplier`, which when applied, results in the layout dividing
* the container in this ratio, then you can use this function as shortcut.
*
* Detailed calculations:
* ratio = h / (2 * H - h) ==> 2 * H * ratio - h * ratio = h ==> 2 * H * ratio / h - ratio = 1
* ==> 1 + ratio = 2 * H * ratio / h ==> (1 + ratio) / (2 * ratio) = H / h
* where H / h is the inverse of our `multiplier`, so the actual multiplier is (2 * ratio) / (1 + ratio).
*/
static
NS_SWIFT_NAME(MMMLayoutUtils.centerMultiplier(forRatio:))
inline CGFloat MMMCenterMultiplierForRatio(CGFloat ratio) {
return (2 * ratio) / (1 + ratio);
}

/**
* Auto Layout does not support relationships between empty spaces, so we need to use spacer views and set such
* constraints between them. This one is a transparent and by default hidden view which can be used as such a spacer.
Expand All @@ -125,30 +22,6 @@ inline CGFloat MMMCenterMultiplierForRatio(CGFloat ratio) {

@end

/**
* Auto Layout does not support constraints against groups of items, so this is for the cases a normal UIView is
* typically used as a container for such a group.
* Unlike UIView we have translatesAutoresizingMaskIntoConstraints set to NO already.
* Also `MMMContainerView` does not intercept touches but subviews still do.
*/
@interface MMMContainerView : UIView

- (nonnull id)init NS_DESIGNATED_INITIALIZER;

- (id)initWithCoder:(NSCoder *)aDecoder NS_UNAVAILABLE;
- (id)initWithFrame:(CGRect)frame NS_UNAVAILABLE;

@end

/** Golden ratio constant. */
extern CGFloat const MMMGolden NS_SWIFT_NAME(MMMLayoutUtils.golden);

/** 1 divided by golden ratio. */
extern CGFloat const MMMInverseGolden NS_SWIFT_NAME(MMMLayoutUtils.inverseGolden);

#define MMM_GOLDEN (MMMGolden)
#define MMM_INVERSE_GOLDEN (MMMInverseGolden)

/** General alignment flags used when it's not important which direction (vertical or horizontal) the alignment is for. */
typedef NS_ENUM(NSInteger, MMMLayoutAlignment) {
MMMLayoutAlignmentNone,
Expand Down Expand Up @@ -303,7 +176,7 @@ static inline MMMLayoutAlignment MMMLayoutAlignmentFromVerticalAlignment(MMMLayo
- (void)mmm_addConstraintsHorizontallyCenteringView:(UIView *)view
minPadding:(CGFloat)minPadding NS_SWIFT_UNAVAILABLE("");

#pragma mark - To be depcreated soon
#pragma mark - To be deprecated soon

/**
* Adds constraints anchoring the given subview within the receiver according to horizontal and vertical alignment flags.
Expand Down Expand Up @@ -465,7 +338,7 @@ extern NSDictionary<NSString *, NSNumber *> *MMMDictionaryFromUIEdgeInsets(NSStr

/**
* A container which lays out its subviews in certain direction one after another using fixed spacing between them.
* It also aligns all the items along the layout line acccoring to the given alignment settings.
* It also aligns all the items along the layout line according to the given alignment settings.
* Note that you must use setSubviews: method instead of feeding them one by one via `addSubview:`.
* This is kind of a `UIStackView` that we understand the internals of.
*/
Expand Down
Loading

0 comments on commit 1ca7c11

Please sign in to comment.