[Cards] Implement stroke variant themer
MODIFY
MaterialComponents.podspec
MaterialComponents.podspec
@@ -264,6 +264,13 @@ Pod::Spec.new do |mdc|
extension.dependency "MaterialComponents/Cards"
extension.dependency "MaterialComponents/schemes/Color"
end
+ component.subspec "CardThemer" do |extension|
+ extension.ios.deployment_target = '8.0'
+ extension.public_header_files = "components/Cards/src/#{extension.base_name}/*.h"
+ extension.source_files = "components/Cards/src/#{extension.base_name}/*.{h,m}", "components/Slider/src/#{extension.base_name}/private/*.{h,m}"
+ extension.dependency "MaterialComponents/Cards"
+ extension.dependency "MaterialComponents/Cards+Extensions/ColorThemer"
+ end
end
mdc.subspec "Chips" do |component|

MODIFY
components/Cards/BUILD
components/Cards/BUILD
@@ -37,6 +37,18 @@ mdc_public_objc_library(
],
)
+mdc_objc_library(
+ name = "CardThemer",
+ srcs = native.glob(["src/CardThemer/*.m"]),
+ hdrs = native.glob(["src/CardThemer/*.h"]),
+ includes = ["src/CardThemer"],
+ deps = [
+ ":Cards",
+ ":ColorThemer",
+ ],
+ visibility = ["//visibility:public"],
+)
+
mdc_objc_library(
name = "ColorThemer",
srcs = native.glob(["src/ColorThemer/*.m"]),

MODIFY
components/Cards/examples/CardExampleViewController.swift
components/Cards/examples/CardExampleViewController.swift
@@ -41,7 +41,9 @@ class CardExampleViewController: UIViewController {
shapeLayer.path = bezierPath.cgPath
imageView.layer.mask = shapeLayer
- MDCCardsColorThemer.applySemanticColorScheme(colorScheme, to: card)
+ let cardScheme = MDCCardScheme();
+ cardScheme.colorScheme = MDCSemanticColorScheme()
+ MDCCardThemer.applyScheme(cardScheme, to: card)
}
override func didReceiveMemoryWarning() {

ADD
components/Cards/src/CardThemer/MDCCardScheme.h
+/*
+ Copyright 2018-present the Material Components for iOS authors. All Rights Reserved.
+
+ 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 "MaterialCards.h"
+#import "MaterialColorScheme.h"
+
+#import <Foundation/Foundation.h>
+
+/** Defines a readonly immutable interface for cards style data to be applied by a themer. */
+@protocol MDCCardScheming
+
+/** The color scheme to apply to cards. */
+@property(nonnull, readonly, nonatomic) id <MDCColorScheming> colorScheme;
+
+@end
+
+/** Defines the cards style data that will be applied to a card by a themer. */
+@interface MDCCardScheme : NSObject <MDCCardScheming>
+
+// Redeclare protocol properties as readwrite
+@property(nonnull, readwrite, nonatomic) MDCSemanticColorScheme *colorScheme;
+
+@end
+

ADD
components/Cards/src/CardThemer/MDCCardScheme.m
+/*
+ Copyright 2018-present the Material Components for iOS authors. All Rights Reserved.
+
+ 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 "MDCCardScheme.h"
+
+@implementation MDCCardScheme
+
+- (instancetype)init {
+ self = [super init];
+ if (self) {
+ _colorScheme = [[MDCSemanticColorScheme alloc] init];
+ }
+ return self;
+}
+@end
+

ADD
components/Cards/src/CardThemer/MDCCardThemer.h
+/*
+ Copyright 2018-present the Material Components for iOS authors. All Rights Reserved.
+
+ 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 "MDCCardScheme.h"
+
+#import "MaterialCards.h"
+
+#import <Foundation/Foundation.h>
+
+/** Applies material style to MDCCard objects. */
+@interface MDCCardThemer : NSObject
+
+/**
+ Applies the material card style using the card scheme data.
+
+ @param scheme The card style data that should be used to change the @c card.
+ @param card A MDCCard instance to apply the @c scheme
+ */
++ (void)applyScheme:(nonnull id<MDCCardScheming>)scheme
+ toCard:(nonnull MDCCard *)card;
+
+/**
+ Applies the material card style using the card scheme data.
+
+ @param scheme The card style data that should be used to change the @c card.
+ @param cardCell A MDCCardCollectionCell instance to apply the @c scheme
+ */
++ (void)applyScheme:(nonnull id<MDCCardScheming>)scheme
+ toCardCell:(nonnull MDCCardCollectionCell *)cardCell;
+
+/**
+ Applies the material outlined card style using the card scheme data.
+
+ @param scheme The card style data that should be used to change the @c card.
+ @param card A MDCCard instance to apply the @c scheme
+ */
++ (void)applyOutlinedVariantWithScheme:(nonnull id<MDCCardScheming>)scheme
+ toCard:(nonnull MDCCard *)card;
+
+/**
+ Applies the material outlined card style using the card scheme data.
+
+ @param scheme The card style data that should be used to change the @c card.
+ @param cardCell A MDCCardCollectionCell instance to apply the @c scheme
+ */
++ (void)applyOutlinedVariantWithScheme:(nonnull id<MDCCardScheming>)scheme
+ toCardCell:(nonnull MDCCardCollectionCell *)cardCell;
+
+@end
+

ADD
components/Cards/src/CardThemer/MDCCardThemer.m
+/*
+ Copyright 2018-present the Material Components for iOS authors. All Rights Reserved.
+
+ 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 "MDCCardThemer.h"
+
+#import "MaterialCards+ColorThemer.h"
+
+static const MDCShadowElevation kElevation = 0.f;
+static const MDCShadowElevation kSelectedElevation = 4.f;
+static const MDCShadowElevation kHighlightedElevation = 8.f;
+static const CGFloat kBorderWidth = 1.f;
+
+@implementation MDCCardThemer
+
++ (void)applyScheme:(nonnull id<MDCCardScheming>)scheme
+ toCard:(nonnull MDCCard *)card {
+ [MDCCardsColorThemer applySemanticColorScheme:scheme.colorScheme toCard:card];
+}
+
++ (void)applyScheme:(nonnull id<MDCCardScheming>)scheme
+ toCardCell:(nonnull MDCCardCollectionCell *)cardCell {
+ [MDCCardsColorThemer applySemanticColorScheme:scheme.colorScheme toCardCell:cardCell];
+}
+
++ (void)applyOutlinedVariantWithScheme:(nonnull id<MDCCardScheming>)scheme
+ toCard:(nonnull MDCCard *)card {
+ NSUInteger maximumStateValue =
+ UIControlStateNormal | UIControlStateSelected | UIControlStateHighlighted |
+ UIControlStateDisabled;
+ for (NSUInteger state = 0; state <= maximumStateValue; ++state) {
+ [card setShadowElevation:kElevation forState:state];
+ [card setBorderWidth:kBorderWidth forState:state];
+ }
+ [card setShadowElevation:kHighlightedElevation forState:UIControlStateHighlighted];
+ [MDCCardsColorThemer applyOutlinedVariantWithColorScheme:scheme.colorScheme toCard:card];
+}
+
++ (void)applyOutlinedVariantWithScheme:(nonnull id<MDCCardScheming>)scheme
+ toCardCell:(nonnull MDCCardCollectionCell *)cardCell {
+ [cardCell setShadowElevation:kElevation forState:MDCCardCellStateNormal];
+ [cardCell setShadowElevation:kSelectedElevation forState:MDCCardCellStateSelected];
+ [cardCell setShadowElevation:kHighlightedElevation forState:MDCCardCellStateHighlighted];
+ [cardCell setBorderWidth:kBorderWidth forState:MDCCardCellStateNormal];
+ [cardCell setBorderWidth:kBorderWidth forState:MDCCardCellStateSelected];
+ [cardCell setBorderWidth:kBorderWidth forState:MDCCardCellStateHighlighted];
+ [MDCCardsColorThemer applyOutlinedVariantWithColorScheme:scheme.colorScheme toCardCell:cardCell];
+}
+
+@end
+

ADD
components/Cards/src/CardThemer/MaterialCards+CardThemer.h
+/*
+ Copyright 2018-present the Material Components for iOS authors. All Rights Reserved.
+
+ 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 "MDCCardScheme.h"
+#import "MDCCardThemer.h"
+
+

MODIFY
components/Cards/src/ColorThemer/MDCCardsColorThemer.m
components/Cards/src/ColorThemer/MDCCardsColorThemer.m
#import "MDCCardsColorThemer.h"
+static const CGFloat kStrokeVariantBorderOpacity = 0.37f;
+
@implementation MDCCardsColorThemer
+ (void)applySemanticColorScheme:(nonnull id<MDCColorScheming>)colorScheme
cardCell.backgroundColor = colorScheme.surfaceColor;
}
++ (void)applyOutlinedVariantWithColorScheme:(nonnull id<MDCColorScheming>)colorScheme
+ toCard:(nonnull MDCCard *)card {
+ NSUInteger maximumStateValue =
+ UIControlStateNormal | UIControlStateSelected | UIControlStateHighlighted |
+ UIControlStateDisabled;
+ for (NSUInteger state = 0; state <= maximumStateValue; ++state) {
+ [card setBorderColor:nil forState:state];
+ }
+
+ card.backgroundColor = colorScheme.surfaceColor;
+ UIColor *borderColor =
+ [colorScheme.onSurfaceColor colorWithAlphaComponent:kStrokeVariantBorderOpacity];
+ [card setBorderColor:borderColor forState:UIControlStateNormal];
+}
+
++ (void)applyOutlinedVariantWithColorScheme:(id<MDCColorScheming>)colorScheme
+ toCardCell:(MDCCardCollectionCell *)cardCell {
+ NSUInteger maximumStateValue =
+ UIControlStateNormal | UIControlStateSelected | UIControlStateHighlighted |
+ UIControlStateDisabled;
+ for (NSUInteger state = 0; state <= maximumStateValue; ++state) {
+ [cardCell setBorderColor:nil forState:state];
+ }
+
+ cardCell.backgroundColor = colorScheme.surfaceColor;
+ UIColor *borderColor =
+ [colorScheme.onSurfaceColor colorWithAlphaComponent:kStrokeVariantBorderOpacity];
+ [cardCell setBorderColor:borderColor forState:MDCCardCellStateNormal];
+}
+
@end

[Cards] Implement stroke variant themer (#3485)