Skip to content

Commit

Permalink
add sizingController
Browse files Browse the repository at this point in the history
  • Loading branch information
calimarkus committed Oct 23, 2023
1 parent eaf37b7 commit e24a670
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
3 changes: 3 additions & 0 deletions JDStatusBarNotification/Private/JDSBNotificationView.m
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,9 @@ - (CGRect)pillContentRectForContentRect:(CGRect)contentRect {

// layout pill
CGFloat maxTextWidth = MAX(realTextSizeForLabel(_titleLabel).width, realTextSizeForLabel(_subtitleLabel).width);
if (_customSubview) {
maxTextWidth = MAX(maxTextWidth, [_customSubview sizeThatFits:contentRect.size].width);
}
CGFloat pillWidth = round(MAX(minimumPillWidth, MIN(maximumPillWidth, maxTextWidth + paddingX * 2)));
CGFloat pillX = round(MAX(minimumPillInset, (CGRectGetWidth(self.bounds) - pillWidth)/2.0));
CGFloat pillY = round(contentRect.origin.y + contentRect.size.height - pillHeight);
Expand Down
26 changes: 26 additions & 0 deletions JDStatusBarNotification/Public/JDStatusBarNotificationPresenter.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ typedef void (^ _Nullable JDStatusBarNotificationPresenterCompletionBlock)(JDSta

NS_ASSUME_NONNULL_BEGIN

NS_SWIFT_NAME(NotificationPresenterCustomViewSizingController)
@protocol JDStatusBarNotificationPresenterCustomViewSizingController
- (CGSize)sizeThatFits:(CGSize)size NS_SWIFT_NAME(sizeThatFits(in:));
@end

/**
* The NotificationPresenter let's you present notifications below the statusBar.
* You can customize the style (colors, fonts, etc.) and animations. It supports notch
Expand Down Expand Up @@ -242,6 +247,27 @@ NS_SWIFT_NAME(NotificationPresenter)
styleName:(NSString * _Nullable)styleName
completion:(JDStatusBarNotificationPresenterCompletionBlock)completion NS_SWIFT_NAME(present(customView:style:completion:));

/// Present a notification using a custom subview.
///
/// The `customView` will be layouted correctly according to the selected style & the current device
/// state (rotation, status bar visibility, etc.). The background will still be styled & layouted
/// according to the provided style. If your custom view requires custom touch handling,
/// make sure to set `style.canTapToHold` to `false`. Otherwise the `customView` won't
/// receive any touches, as the internal `gestureRecognizer` would receive them.
///
/// - Parameters:
/// - customView: A custom UIView to display as notification content.
/// - styleName: The name of the style. You can use styles previously added using e.g. ``addStyleNamed:prepare:``.
/// If no style can be found for the given `styleName` or it is `nil`, the default style will be used.
/// - completion: A ``JDStatusBarNotificationPresenterCompletionBlock``, which gets called once the presentation animation finishes.
///
/// - Returns: The presented UIView for further customization
///
- (UIView *)presentWithCustomView:(UIView *)customView
sizingController:(id _Nullable)sizingController
styleName:(NSString * _Nullable)styleName
completion:(JDStatusBarNotificationPresenterCompletionBlock)completion NS_SWIFT_NAME(present(customView:sizingController:style:completion:));

#pragma mark - Dismissal

/// Dismisses any currently displayed notification immediately using an animation.
Expand Down
10 changes: 10 additions & 0 deletions JDStatusBarNotification/Public/JDStatusBarNotificationPresenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ - (UIView *)presentWithTitle:(NSString *)title
- (UIView *)presentWithCustomView:(UIView *)customView
styleName:(NSString * _Nullable)styleName
completion:(JDStatusBarNotificationPresenterCompletionBlock)completion {
return [self presentWithCustomView:customView
sizingController:nil
styleName:styleName
completion:completion];
}

- (UIView *)presentWithCustomView:(UIView *)customView
sizingController:(id _Nullable)sizingController
styleName:(NSString * _Nullable)styleName
completion:(JDStatusBarNotificationPresenterCompletionBlock)completion {
JDStatusBarNotificationStyle *style = [_styleCache styleForName:styleName];
JDSBNotificationView *view = [self presentWithTitle:nil subtitle:nil style:style completion:completion];
view.customSubview = customView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@

import SwiftUI

class NotificationPresenterSizingController<Content>: NotificationPresenterCustomViewSizingController where Content: View {
let hostingController: UIHostingController<Content>

init(hostingController: UIHostingController<Content>) {
self.hostingController = hostingController
}

func sizeThatFits(in size: CGSize) -> CGSize {
return hostingController.sizeThatFits(in: size)
}
}

extension NotificationPresenter {

public func presentSwiftView(style: String? = nil,
Expand Down

0 comments on commit e24a670

Please sign in to comment.