@@ -30,24 +30,17 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
3030 * Primary constructor.
3131 */
3232 init {
33- val isAnimateOnStart = visibility == VISIBLE && { ctx: Context , ats: AttributeSet ->
34- val a = ctx.theme.obtainStyledAttributes(ats, R .styleable.CodeView , 0 , 0 )
35-
36- try {
37- a.getBoolean(R .styleable.CodeView_animateOnStart , true )
38- } finally {
39- a.recycle()
40- }
41- }(context, attrs)
42-
43- alpha = if (isAnimateOnStart) 0f else Consts .ALPHA
44-
4533 inflate(context, R .layout.layout_code_view, this )
4634
47- if (isAnimateOnStart)
35+ if (visibility == VISIBLE && isAnimateOnStart(context, attrs)) {
36+ alpha = Const .Alpha .Invisible
37+
4838 animate()
49- .setDuration(Consts .DELAY * 5 )
50- .alpha(Consts .ALPHA )
39+ .setDuration(Const .DefaultDelay * 5 )
40+ .alpha(Const .Alpha .Initial )
41+ } else {
42+ alpha = Const .Alpha .Initial
43+ }
5144
5245 // TODO: add shadow color customization
5346 vShadowRight = findViewById(R .id.v_shadow_right)
@@ -67,7 +60,7 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
6760 getAdapter()?.highlight {
6861
6962 animate()
70- .setDuration(Consts . DELAY * 2 )
63+ .setDuration(Const . DefaultDelay * 2 )
7164 .alpha(.1f )
7265
7366 delayed {
@@ -121,7 +114,7 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
121114 /* *
122115 * View options accessor.
123116 */
124- fun getOptions (): Options ? = getAdapter()?.options
117+ fun getOptions () = getAdapter()?.options
125118 fun getOptionsOrDefault () = getOptions() ? : Options (context)
126119
127120 /* *
@@ -130,10 +123,8 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
130123 * @param options Options
131124 */
132125 fun updateOptions (options : Options ) {
133- if (getAdapter() == null )
134- setOptions(options)
135- else
136- getAdapter()!! .options = options
126+ getAdapter() ? : setOptions(options)
127+ getAdapter()?.options = options
137128 }
138129
139130 // - Adapter
@@ -169,7 +160,7 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
169160 */
170161 fun setCode (code : String ) {
171162 getAdapter() ? : prepare()
172- getAdapter()!! .updateCode(code)
163+ getAdapter()? .updateCode(code)
173164 }
174165
175166 /* *
@@ -185,7 +176,19 @@ class CodeView(context: Context, attrs: AttributeSet) : RelativeLayout(context,
185176 fun setCode (code : String , language : String ) {
186177 val options = getOptionsOrDefault()
187178 updateOptions(options.withLanguage(language))
188- getAdapter()!! .updateCode(code)
179+ getAdapter()?.updateCode(code)
180+ }
181+
182+ companion object {
183+
184+ private fun isAnimateOnStart (context : Context , attr : AttributeSet ): Boolean {
185+ context.theme.obtainStyledAttributes(attr, R .styleable.CodeView , 0 , 0 ).apply {
186+ val flag = getBoolean(R .styleable.CodeView_animateOnStart , false )
187+ recycle()
188+ return @isAnimateOnStart flag
189+ }
190+ return false
191+ }
189192 }
190193}
191194
0 commit comments