import { cssProperty , rippleColorProperty , themer } from '@nativescript-community/ui-material-core' ; import { getColorStateList , getEnabledColorStateList , state } from '@nativescript-community/ui-material-core/android/utils' ; import { CoercibleProperty , Color , Property , View , backgroundColorProperty , backgroundInternalProperty , colorProperty } from '@nativescript/core' ; import { stepSizeProperty , thumbColorProperty , trackBackgroundColorProperty , trackFillColorProperty } from './cssproperties' ; let ASlider ; export function sliderGetEnabledColorStateList ( color , alpha = 255 ) { if ( ! color ) { return null ; } return getEnabledColorStateList ( color . android , new Color ( alpha , 158 , 158 , 158 ) . android ) ; } export const valueProperty = new CoercibleProperty < Slider , number > ( { name : 'value' , defaultValue : 0 , coerceValue : ( target , value ) => { value = Math . max ( value , target . minValue ) ; value = Math . min ( value , target . maxValue ) ; return value ; } , valueConverter : ( v ) => parseFloat ( v ) } ) ; /**
 * Represents the observable property backing the minValue property of each Slider instance.
 */ export const minValueProperty = new Property < Slider , number > ( { name : 'minValue' , defaultValue : 0 , valueChanged : ( target , oldValue , newValue ) => { maxValueProperty . coerce ( target ) ; valueProperty . coerce ( target ) ; } , valueConverter : ( v ) => parseFloat ( v ) } ) ; /**
 * Represents the observable property backing the maxValue property of each Slider instance.
 */ export const maxValueProperty = new CoercibleProperty < Slider , number > ( { name : 'maxValue' , defaultValue : 100 , coerceValue : ( target , value ) => { const minValue = target . minValue ; if ( value < minValue ) { value = minValue ; } return value ; } , valueChanged : ( target , oldValue , newValue ) => valueProperty . coerce ( target ) , valueConverter : ( v ) => parseFloat ( v ) } ) ; export class Slider extends View { nativeViewProtected : com . google . android . material . slider . Slider ; @ cssProperty rippleColor : Color ; @ cssProperty trackBackgroundColor : Color ; @ cssProperty trackFillColor : Color ; @ cssProperty thumbColor : Color ; @ cssProperty stepSize : number ; @ cssProperty elevation : number ; _supressNativeValue : boolean ; public value : number ; public minValue : number ; public maxValue : number ; listener : com . google . android . material . slider . Slider . OnChangeListener ; constructor ( ) { super ( ) ; this . color = themer . getPrimaryColor ( ) as Color ; } createNativeView ( ) { if ( ! ASlider ) { ASlider = com . google . android . material . slider . Slider ; } const result = new ASlider ( this . _context ) ; result . setLabelBehavior ( 2 ) ; return result ; } initNativeView ( ) { super . initNativeView ( ) ; const nativeView = this . nativeViewProtected ; this . listener = new com . google . android . material . slider . Slider . OnChangeListener ( { onValueChange : ( param0 , value , fromUser ) => { if ( fromUser ) { valueProperty . nativeValueChange ( this as any , value ) ; } } } ) ; nativeView . addOnChangeListener ( this . listener ) ; nativeView . setValueFrom ( this . minValue ) ; nativeView . setValueTo ( this . maxValue ) ; } disposeNativeView ( ) { if ( this . listener ) { this . nativeViewProtected . removeOnChangeListener ( this . listener ) ; this . listener = null ; } super . disposeNativeView ( ) ; } /**
     * There is no minValue in Android. We simulate this by subtracting the minValue from the native value and maxValue.
     * We need this method to call native setMax and setProgress methods when minValue property is changed,
     * without handling the native value changed callback.
     */ [ colorProperty . setNative ] ( color ) { if ( color ) { this . nativeViewProtected . setTrackTintList ( sliderGetEnabledColorStateList ( color ) ) ; if ( ! this . trackBackgroundColor ) { this . trackBackgroundColor = new Color ( 61.2 , color . r , color . g , color . b ) ; } } else { this . nativeViewProtected . setTrackTintList ( null ) ; if ( ! this . trackBackgroundColor ) { this . trackBackgroundColor = null ; } } if ( ! this . thumbColor ) { this . thumbColor = color ; } else { this [ thumbColorProperty . setNative ] ( this . thumbColor ) ; } } [ stepSizeProperty . getDefault ] ( ) { return 0 ; } [ stepSizeProperty . setNative ] ( value ) { this . nativeViewProtected . setStepSize ( value ) ; } [ valueProperty . setNative ] ( value ) { this . nativeViewProtected . setValueFrom ( this . minValue ) ; this . nativeViewProtected . setValueTo ( this . maxValue ) ; this . nativeViewProtected . setValue ( value ) ; } [ minValueProperty . setNative ] ( value ) { this . nativeViewProtected . setValueFrom ( value ) ; } [ maxValueProperty . getDefault ] ( ) { return 100 ; } [ maxValueProperty . setNative ] ( value ) { this . nativeViewProtected . setValueTo ( value ) ; } [ backgroundColorProperty . setNative ] ( value ) { this [ trackBackgroundColorProperty . setNative ] ( value ) ; } [ backgroundInternalProperty . setNative ] ( value ) { } [ rippleColorProperty . setNative ] ( color ) { this . nativeViewProtected . setHaloTintList ( color ? getColorStateList ( color . android ) : null ) ; } [ thumbColorProperty . setNative ] ( color ) { this . nativeViewProtected . setThumbTintList ( sliderGetEnabledColorStateList ( color ) ) ; if ( ! this . rippleColor ) { this . rippleColor = color ; } else { this [ rippleColorProperty . setNative ] ( this . rippleColor ) ; } } [ trackBackgroundColorProperty . setNative ] ( color ) { this . nativeViewProtected . setTrackInactiveTintList ( sliderGetEnabledColorStateList ( color , 61.2 ) ) ; } [ trackFillColorProperty . setNative ] ( color ) { this . nativeViewProtected . setTrackActiveTintList ( sliderGetEnabledColorStateList ( color ) ) ; } } valueProperty . register ( Slider ) ; minValueProperty . register ( Slider ) ; maxValueProperty . register ( Slider ) ;