Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add largeTitleDisplayMode to PaymentContext #849

Merged
merged 8 commits into from
Dec 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
* Minimum supported Xcode version is now 9.0
* `AddressBook` framework support has been removed.
* `STPRedirectContext` will no longer retain itself for the duration of the redirect, you must explicitly maintain a reference to it yourself.
* `STPPaymentConfiguration.requiredShippingAddress` now is a set of `STPContactField` objects instead of a `PKAddressField` bitmask.
* `STPPaymentConfiguration.requiredShippingAddress` now is a set of `STPContactField` objects instead of a `PKAddressField` bitmask.
* See MIGRATING.md for more information on any of the previously mentioned breaking API changes.
* Pre-built view controllers now layout properly on iPhone X in landscape orientation, respecting `safeAreaInsets`.

* `STPPaymentContext` now has a `largeTitleDisplayMode` property, which you can use to control the title display mode in the navigation bar of our pre-built view controllers.

## 11.5.0 2017-11-09
* Adds a new helper method to `STPSourceParams` for creating reusable Alipay sources. [#811](https://github.com/stripe/stripe-ios/pull/811)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class BrowseProductsViewController: UITableViewController {
super.viewDidLoad()
self.navigationItem.title = "Emoji Apparel"
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.view.backgroundColor = .white
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the transition when PaymentContext pushes a small title view controller onto a large title nav controller (there's a black flash without this line).

self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "Products", style: .plain, target: nil, action: nil)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Settings", style: .plain, target: self, action: #selector(showSettings))
}
Expand Down
4 changes: 4 additions & 0 deletions Stripe.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@
8BD87B931EFB1C1E00269C2B /* STPSourceVerification+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 8BD87B911EFB1C1E00269C2B /* STPSourceVerification+Private.h */; };
8BD87B951EFB1CB100269C2B /* STPSourceVerificationTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 8BD87B941EFB1CB100269C2B /* STPSourceVerificationTest.m */; };
8BE5AE8B1EF8905B0081A33C /* STPCardParamsTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 8BE5AE8A1EF8905B0081A33C /* STPCardParamsTest.m */; };
C1054F911FE197AE0033C87E /* STPPaymentContextSnapshotTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C1054F901FE197AE0033C87E /* STPPaymentContextSnapshotTests.m */; };
C1080F491CBECF7B007B2D89 /* STPAddress.h in Headers */ = {isa = PBXBuildFile; fileRef = C1080F471CBECF7B007B2D89 /* STPAddress.h */; settings = {ATTRIBUTES = (Public, ); }; };
C1080F4A1CBECF7B007B2D89 /* STPAddress.m in Sources */ = {isa = PBXBuildFile; fileRef = C1080F481CBECF7B007B2D89 /* STPAddress.m */; };
C1080F4C1CBED48A007B2D89 /* STPAddressTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C1080F4B1CBED48A007B2D89 /* STPAddressTests.m */; };
Expand Down Expand Up @@ -993,6 +994,7 @@
8BD87B911EFB1C1E00269C2B /* STPSourceVerification+Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "STPSourceVerification+Private.h"; sourceTree = "<group>"; };
8BD87B941EFB1CB100269C2B /* STPSourceVerificationTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = STPSourceVerificationTest.m; sourceTree = "<group>"; };
8BE5AE8A1EF8905B0081A33C /* STPCardParamsTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = STPCardParamsTest.m; sourceTree = "<group>"; };
C1054F901FE197AE0033C87E /* STPPaymentContextSnapshotTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = STPPaymentContextSnapshotTests.m; sourceTree = "<group>"; };
C1080F471CBECF7B007B2D89 /* STPAddress.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = STPAddress.h; path = PublicHeaders/STPAddress.h; sourceTree = "<group>"; };
C1080F481CBECF7B007B2D89 /* STPAddress.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = STPAddress.m; sourceTree = "<group>"; };
C1080F4B1CBED48A007B2D89 /* STPAddressTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = STPAddressTests.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1485,6 +1487,7 @@
F1B980931DB550E60075332E /* STPPaymentMethodsViewControllerLocalizationTests.m */,
C1EF04491DD2396200FBF452 /* STPShippingAddressViewControllerLocalizationTests.m */,
C1EF044A1DD2396200FBF452 /* STPShippingMethodsViewControllerLocalizationTests.m */,
C1054F901FE197AE0033C87E /* STPPaymentContextSnapshotTests.m */,
);
name = Snapshot;
sourceTree = "<group>";
Expand Down Expand Up @@ -2646,6 +2649,7 @@
F148ABFB1D5E88C70014FD92 /* STPTestUtils.m in Sources */,
8BB97F081F26645B0095122A /* NSDictionary+StripeTest.m in Sources */,
045A62AB1B8E7259000165CE /* STPPaymentCardTextFieldTest.m in Sources */,
C1054F911FE197AE0033C87E /* STPPaymentContextSnapshotTests.m in Sources */,
C127110A1DBA7E490087840D /* STPAddressViewModelTest.m in Sources */,
C17D24EE1E37DBAC005CB188 /* STPSourceTest.m in Sources */,
C1E4F8061EBBEB0F00E611F5 /* STPCustomerContextTest.m in Sources */,
Expand Down
15 changes: 15 additions & 0 deletions Stripe/PublicHeaders/STPPaymentContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,21 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic, assign) UIModalPresentationStyle modalPresentationStyle;

/**
The mode to use when displaying the title of the navigation bar in all view
controllers presented by the context. The default value is `automatic`,
which causes the title to use the same styling as the previously displayed
navigation item (if the view controller is pushed onto the `hostViewController`).

If the `prefersLargeTitles` property of the `hostViewController`'s navigation bar
is false, this property has no effect and the navigation item's title is always
displayed as a small title.

If the view controller is presented modally, `automatic` and
`never` always result in a navigation bar with a small title.
*/
@property (nonatomic, assign) UINavigationItemLargeTitleDisplayMode largeTitleDisplayMode NS_AVAILABLE_IOS(11_0);

/**
A view that will be placed as the footer of the payment methods selection
view controller.
Expand Down
61 changes: 50 additions & 11 deletions Stripe/STPPaymentContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ - (instancetype)initWithAPIAdapter:(id<STPBackendAPIAdapter>)apiAdapter
_paymentCountry = @"US";
_paymentAmountModel = [[STPPaymentContextAmountModel alloc] initWithAmount:0];
_modalPresentationStyle = UIModalPresentationFullScreen;
if (@available(iOS 11, *)) {
_largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAutomatic;
}
_state = STPPaymentContextStateNone;
[self retryLoading];
}
Expand Down Expand Up @@ -157,6 +160,11 @@ - (BOOL)loading {
return !self.loadingPromise.completed;
}

// Disable transition animations in tests
- (BOOL)transitionAnimationsEnabled {
return NSClassFromString(@"XCTest") == nil;
}

- (void)setHostViewController:(UIViewController *)hostViewController {
NSCAssert(_hostViewController == nil, @"You cannot change the hostViewController on an STPPaymentContext after it's already been set.");
_hostViewController = hostViewController;
Expand Down Expand Up @@ -287,11 +295,19 @@ - (void)presentPaymentMethodsViewControllerWithNewState:(STPPaymentContextState)
paymentMethodsViewController.prefilledInformation = self.prefilledInformation;
paymentMethodsViewController.paymentMethodsViewControllerFooterView = self.paymentMethodsViewControllerFooterView;
paymentMethodsViewController.addCardViewControllerFooterView = self.addCardViewControllerFooterView;
if (@available(iOS 11, *)) {
paymentMethodsViewController.navigationItem.largeTitleDisplayMode = self.largeTitleDisplayMode;
}

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:paymentMethodsViewController];
navigationController.navigationBar.stp_theme = self.theme;
if (@available(iOS 11, *)) {
navigationController.navigationBar.prefersLargeTitles = YES;
}
navigationController.modalPresentationStyle = self.modalPresentationStyle;
[self.hostViewController presentViewController:navigationController animated:YES completion:nil];
[self.hostViewController presentViewController:navigationController
animated:[self transitionAnimationsEnabled]
completion:nil];
}
}];
}
Expand All @@ -316,8 +332,12 @@ - (void)pushPaymentMethodsViewController {
paymentMethodsViewController.prefilledInformation = self.prefilledInformation;
paymentMethodsViewController.paymentMethodsViewControllerFooterView = self.paymentMethodsViewControllerFooterView;
paymentMethodsViewController.addCardViewControllerFooterView = self.addCardViewControllerFooterView;

[navigationController pushViewController:paymentMethodsViewController animated:YES];
if (@available(iOS 11, *)) {
paymentMethodsViewController.navigationItem.largeTitleDisplayMode = self.largeTitleDisplayMode;
}

[navigationController pushViewController:paymentMethodsViewController
animated:[self transitionAnimationsEnabled]];
}
}];
}
Expand Down Expand Up @@ -360,7 +380,8 @@ - (void)appropriatelyDismissPaymentMethodsViewController:(STPPaymentMethodsViewC
completion:(STPVoidBlock)completion {
if ([viewController stp_isAtRootOfNavigationController]) {
// if we're the root of the navigation controller, we've been presented modally.
[viewController.presentingViewController dismissViewControllerAnimated:YES completion:^{
[viewController.presentingViewController dismissViewControllerAnimated:[self transitionAnimationsEnabled]
completion:^{
self.paymentMethodsViewController = nil;
if (completion) {
completion();
Expand All @@ -373,7 +394,9 @@ - (void)appropriatelyDismissPaymentMethodsViewController:(STPPaymentMethodsViewC
if ([self.hostViewController isKindOfClass:[UINavigationController class]]) {
destinationViewController = self.originalTopViewController;
}
[viewController.navigationController stp_popToViewController:destinationViewController animated:YES completion:^{
[viewController.navigationController stp_popToViewController:destinationViewController
animated:[self transitionAnimationsEnabled]
completion:^{
self.paymentMethodsViewController = nil;
if (completion) {
completion();
Expand All @@ -397,10 +420,18 @@ - (void)presentShippingViewControllerWithNewState:(STPPaymentContextState)state
self.state = state;

STPShippingAddressViewController *addressViewController = [[STPShippingAddressViewController alloc] initWithPaymentContext:self];
if (@available(iOS 11, *)) {
addressViewController.navigationItem.largeTitleDisplayMode = self.largeTitleDisplayMode;
}
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:addressViewController];
navigationController.navigationBar.stp_theme = self.theme;
if (@available(iOS 11, *)) {
navigationController.navigationBar.prefersLargeTitles = YES;
}
navigationController.modalPresentationStyle = self.modalPresentationStyle;
[self.hostViewController presentViewController:navigationController animated:YES completion:nil];
[self.hostViewController presentViewController:navigationController
animated:[self transitionAnimationsEnabled]
completion:nil];
}
}];
}
Expand All @@ -421,7 +452,11 @@ - (void)pushShippingViewController {
self.state = STPPaymentContextStateShowingRequestedViewController;

STPShippingAddressViewController *addressViewController = [[STPShippingAddressViewController alloc] initWithPaymentContext:self];
[navigationController pushViewController:addressViewController animated:YES];
if (@available(iOS 11, *)) {
addressViewController.navigationItem.largeTitleDisplayMode = self.largeTitleDisplayMode;
}
[navigationController pushViewController:addressViewController
animated:[self transitionAnimationsEnabled]];
}
}];
}
Expand Down Expand Up @@ -480,7 +515,8 @@ - (void)appropriatelyDismissViewController:(UIViewController *)viewController
completion:(STPVoidBlock)completion {
if ([viewController stp_isAtRootOfNavigationController]) {
// if we're the root of the navigation controller, we've been presented modally.
[viewController.presentingViewController dismissViewControllerAnimated:YES completion:^{
[viewController.presentingViewController dismissViewControllerAnimated:[self transitionAnimationsEnabled]
completion:^{
if (completion) {
completion();
}
Expand All @@ -492,7 +528,9 @@ - (void)appropriatelyDismissViewController:(UIViewController *)viewController
if ([self.hostViewController isKindOfClass:[UINavigationController class]]) {
destinationViewController = self.originalTopViewController;
}
[viewController.navigationController stp_popToViewController:destinationViewController animated:YES completion:^{
[viewController.navigationController stp_popToViewController:destinationViewController
animated:[self transitionAnimationsEnabled]
completion:^{
if (completion) {
completion();
}
Expand Down Expand Up @@ -613,13 +651,14 @@ - (void)requestPayment {
onPaymentAuthorization:paymentHandler
onTokenCreation:applePayTokenHandler
onFinish:^(STPPaymentStatus status, NSError * _Nullable error) {
[self.hostViewController dismissViewControllerAnimated:YES completion:^{
[self.hostViewController dismissViewControllerAnimated:[self transitionAnimationsEnabled]
completion:^{
[self didFinishWithStatus:status
error:error];
}];
}];
[self.hostViewController presentViewController:paymentAuthVC
animated:YES
animated:[self transitionAnimationsEnabled]
completion:nil];
}
}] onFailure:^(NSError *error) {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions Tests/Tests/FBSnapshotTestCase+STPViewControllerLoading.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,24 @@
#import <FBSnapshotTestCase/FBSnapshotTestCase.h>

@interface FBSnapshotTestCase (STPViewControllerLoading)

/**
Embeds the given controller in a navigation controller, prepares it for
snapshot testing and returns the view controller's view.
*/
- (UIView *)stp_preparedAndSizedViewForSnapshotTestFromViewController:(UIViewController *)viewController;

/**
Returns a navigation controller initialized with the given root view controller
and prepares it for snapshot testing (adding it to a UIWindow and loading views)
*/
- (UINavigationController *)stp_navigationControllerForSnapshotTestWithRootVC:(UIViewController *)viewController;

/**
Returns a view for snapshot testing from the topViewController of the given
navigation controller, making necessary layout adjustments for
`STPCoreScrollViewController`.
*/
- (UIView *)stp_preparedAndSizedViewForSnapshotTestFromNavigationController:(UINavigationController *)navController;

@end
18 changes: 17 additions & 1 deletion Tests/Tests/FBSnapshotTestCase+STPViewControllerLoading.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,29 @@
#import "STPCoreScrollViewController+Private.h"

@implementation FBSnapshotTestCase (STPViewControllerLoading)

- (UIView *)stp_preparedAndSizedViewForSnapshotTestFromViewController:(UIViewController *)viewController {
UINavigationController *navController = [self stp_navigationControllerForSnapshotTestWithRootVC:viewController];
return [self stp_preparedAndSizedViewForSnapshotTestFromNavigationController:navController];
}

- (UINavigationController *)stp_navigationControllerForSnapshotTestWithRootVC:(UIViewController *)viewController {
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
UIWindow *testWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
testWindow.rootViewController = navController;
testWindow.hidden = NO;

// Test that loaded properly + loads them on first call
// Test that views loaded properly + loads them on first call
XCTAssertNotNil(navController.view);
XCTAssertNotNil(viewController.view);

return navController;
}

- (UIView *)stp_preparedAndSizedViewForSnapshotTestFromNavigationController:(UINavigationController *)navController {
UIViewController *viewController = [navController topViewController];

// Test that views loaded properly + loads them on first call
XCTAssertNotNil(navController.view);
XCTAssertNotNil(viewController.view);

Expand Down
Loading