import { themer } from '@nativescript-community/ui-material-core' ; import { Color , Screen , Utils , View , colorProperty } from '@nativescript/core' ; import { ActivityIndicatorBase , indeterminateProperty , maxValueProperty , valueProperty } from './index-common' ; export class ActivityIndicator extends ActivityIndicatorBase { nativeViewProtected : MDCActivityIndicator ; autoSize = true ; public createNativeView ( ) { const view = MDCActivityIndicator . new ( ) ; const color = ( themer . getAppColorScheme ( ) as MDCSemanticColorScheme ) . primaryColor ; view . cycleColors = color ? NSArray . arrayWithObject ( color ) : null ; return view ; } _onSizeChanged ( ) { if ( this . autoSize ) { this . updateStrokeRadius ( this . getMeasuredWidth ( ) , this . getMeasuredHeight ( ) ) ; } super . _onSizeChanged ( ) ; } private updateStrokeRadius ( width , height ) { const min = Math . min ( Math . min ( width , height ) , 144 ) ; const strokeWidth = min / 25 ; const scale = Screen . mainScreen . scale ; const radius = min / 2 - strokeWidth / 2 ; this . nativeViewProtected . strokeWidth = strokeWidth ; this . nativeViewProtected . radius = radius / scale ; } public onMeasure ( widthMeasureSpec , heightMeasureSpec ) { const nativeView = this . nativeViewProtected ; if ( nativeView ) { const width = Utils . layout . getMeasureSpecSize ( widthMeasureSpec ) ; const widthMode = Utils . layout . getMeasureSpecMode ( widthMeasureSpec ) ; const height = Utils . layout . getMeasureSpecSize ( heightMeasureSpec ) ; const heightMode = Utils . layout . getMeasureSpecMode ( heightMeasureSpec ) ; const horizontalPadding = this . effectivePaddingLeft + this . effectiveBorderLeftWidth + this . effectivePaddingRight + this . effectiveBorderRightWidth ; const verticalPadding = this . effectivePaddingTop + this . effectiveBorderTopWidth + this . effectivePaddingBottom + this . effectiveBorderBottomWidth ; const desiredSize = Utils . layout . measureNativeView ( nativeView , width - horizontalPadding , widthMode , height - verticalPadding , heightMode ) ; desiredSize . width = desiredSize . width + horizontalPadding ; desiredSize . height = desiredSize . height + verticalPadding ; const measureWidth = Math . max ( desiredSize . width , this . effectiveMinWidth ) ; const measureHeight = Math . max ( desiredSize . height , this . effectiveMinHeight ) ; const widthAndState = View . resolveSizeAndState ( measureWidth , width , widthMode , 0 ) ; const heightAndState = View . resolveSizeAndState ( measureHeight , height , heightMode , 0 ) ; this . setMeasuredDimension ( widthAndState , heightAndState ) ; } } [ colorProperty . setNative ] ( value ) { const color = value instanceof Color ? value . ios : value ; this . nativeViewProtected . cycleColors = color ? NSArray . arrayWithObject ( color ) : null ; } [ indeterminateProperty . setNative ] ( value ) { this . nativeViewProtected . indicatorMode = value ? MDCActivityIndicatorMode . Indeterminate : MDCActivityIndicatorMode . Determinate ; } [ valueProperty . getDefault ] ( ) { return 0 ; } [ valueProperty . setNative ] ( value ) { this . nativeViewProtected . progress = value / this . maxValue ; } [ maxValueProperty . getDefault ] ( ) { return 100 ; } [ maxValueProperty . setNative ] ( value ) { this . nativeViewProtected . progress = this . value / value ; } }