import { dynamicElevationOffsetProperty , elevationProperty , getRippleColor , rippleColorProperty , shapeProperty , themer } from '@nativescript-community/ui-material-core' ; import { createStateListAnimator , getAttrColor , getColorStateList , isPostLollipop } from '@nativescript-community/ui-material-core/android/utils' ; import { Color , Length , backgroundInternalProperty } from '@nativescript/core' ; import { CardViewBase } from './cardview-common' ; let MDCCardView ; const DEFAULT_STROKE_VALUE = - 1 ; function initMDCCardView ( ) { if ( ! MDCCardView ) { MDCCardView = com . google . android . material . card . MaterialCardView ; } } interface ViewOutlineProvider extends android . view . ViewOutlineProvider { new ( context ) : ViewOutlineProvider ; } let ViewOutlineProvider ; function initializeOutlineProvider ( ) { if ( ViewOutlineProvider ) { return ; } @ NativeClass class OutlineProvider extends android . view . ViewOutlineProvider { constructor ( private owner ) { super ( ) ; return global . __native ( this ) ; } getOutline ( view , outline ) { const cardView = this . owner && this . owner . get ( ) ; if ( cardView ) { outline . setRoundRect ( 0 , 0 , view . getWidth ( ) , view . getHeight ( ) , cardView . _borderRadius - cardView . _strokeWidth ) ; } } } ViewOutlineProvider = OutlineProvider as any ; } export class CardView extends CardViewBase { nativeViewProtected : com . google . android . material . card . MaterialCardView ; _strokeWidth = 0 ; _borderRadius = 0 ; getDefaultElevation ( ) { if ( isPostLollipop ( ) ) { return 1 ; } return 0 ; } getDefaultDynamicElevationOffset ( ) { if ( isPostLollipop ( ) ) { return 5 ; } return 0 ; } public createNativeView ( ) { initMDCCardView ( ) ; initializeOutlineProvider ( ) ; const view = new MDCCardView ( this . _context ) ; if ( isPostLollipop ( ) ) { createStateListAnimator ( this , view ) ; } view . setClickable ( this . isUserInteractionEnabled ) ; return view ; } getCardRippleColor ( ) { if ( this . rippleColor ) { return getRippleColor ( this . rippleColor ) ; } return getRippleColor ( themer . getAccentColor ( ) ) ; } [ backgroundInternalProperty . setNative ] ( value ) { if ( this . nativeViewProtected ) { if ( value instanceof android . graphics . drawable . Drawable ) { this . nativeViewProtected . setBackgroundDrawable ( value ) ; } else { if ( this . _strokeWidth !== value . borderTopWidth ) { this . _strokeWidth = value . borderTopWidth ; this . _strokeWidth = value . borderTopWidth ; } this . nativeViewProtected . setStrokeWidth ( this . _strokeWidth ) ; if ( value . color ) { this . nativeViewProtected . setCardBackgroundColor ( value . color . android ) ; } if ( value . borderTopColor && value . borderTopColor . android !== DEFAULT_STROKE_VALUE ) { this . nativeViewProtected . setStrokeColor ( value . borderTopColor . android ) ; } if ( this . _borderRadius !== value . borderTopLeftRadius ) { this . _borderRadius = value . borderTopLeftRadius ; this . nativeViewProtected . setRadius ( this . _borderRadius ) ; } } } } defaultAppearanceModel ; [ shapeProperty . setNative ] ( shape ) { const appearanceModel = themer . getShape ( shape ) ; if ( ! shape ) { if ( this . defaultAppearanceModel ) { this . nativeViewProtected . setShapeAppearanceModel ( this . defaultAppearanceModel ) ; } } else { if ( ! this . defaultAppearanceModel ) { this . defaultAppearanceModel = this . nativeViewProtected . getShapeAppearanceModel ( ) ; } this . nativeViewProtected . setShapeAppearanceModel ( appearanceModel ) ; } } [ elevationProperty . setNative ] ( value ) { if ( ! this . nativeViewProtected ) { return ; } if ( isPostLollipop ( ) ) { createStateListAnimator ( this , this . nativeViewProtected ) ; } else { const newValue = Length . toDevicePixels ( typeof value === 'string' ? Length . parse ( value ) : value , 0 ) ; androidx . core . view . ViewCompat . setElevation ( this . nativeViewProtected , newValue ) ; } } [ dynamicElevationOffsetProperty . setNative ] ( value ) { if ( ! this . nativeViewProtected ) { return ; } if ( isPostLollipop ( ) ) { createStateListAnimator ( this , this . nativeViewProtected ) ; } else { const newValue = Length . toDevicePixels ( typeof value === 'string' ? Length . parse ( value ) : value , 0 ) ; androidx . core . view . ViewCompat . setTranslationZ ( this . nativeViewProtected , newValue ) ; } } [ rippleColorProperty . setNative ] ( color ) { const rippleColor = color ? color . android : - 1 ; ( this . nativeViewProtected as any ) . setRippleColor ( getColorStateList ( rippleColor ) ) ; } }