@@ -5,15 +5,20 @@ import android.content.res.Resources
55import android.content.res.TypedArray
66import android.graphics.PorterDuff
77import android.graphics.Typeface
8+ import android.os.Build
89import android.support.annotation.NonNull
910import android.support.annotation.Nullable
1011import android.support.v4.content.res.ResourcesCompat
12+ import android.transition.Fade
13+ import android.transition.TransitionManager
1114import android.util.AttributeSet
1215import android.util.Log
1316import android.view.Gravity
1417import android.view.View
18+ import android.view.ViewGroup
1519import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
1620import android.widget.FrameLayout
21+ import android.widget.ImageView
1722import android.widget.ProgressBar
1823import android.widget.TextView
1924
@@ -24,88 +29,119 @@ class LoadingButton @JvmOverloads constructor(
2429
2530 private val TAG = " LoadingButton"
2631
27- private var progressBar: ProgressBar ? = null
28- private var tvText: TextView ? = null
29- private var array: TypedArray ? = null
32+ private lateinit var progressBar: ProgressBar
33+ private lateinit var img: ImageView
34+ private lateinit var view: View
35+ private lateinit var tvText: TextView
36+ private var array: TypedArray = context.theme.obtainStyledAttributes(attrs, R .styleable.app, defStyleAttr, 0 )
37+ private var bg: Int = R .drawable.ic_bg_blue_buttons_style
3038
3139 init {
32- array = context.theme.obtainStyledAttributes(attrs, R .styleable.app, defStyleAttr, 0 )
3340 initView()
3441 }
3542
3643 fun hideLoading () {
37- if (progressBar != null ) {
38- progressBar!! .visibility = View .INVISIBLE
39- }
40-
41- if (tvText != null ) {
42- tvText!! .visibility = View .VISIBLE
43- }
44+ view.setBackgroundResource(bg)
45+ progressBar.visibility = View .INVISIBLE
46+ img.visibility = View .INVISIBLE
47+ tvText.visibility = View .VISIBLE
4448 Log .d(TAG , " stopLoading: " )
4549 }
4650
47- fun showLoading () {
48- if (progressBar != null ) {
49- progressBar!! .visibility = View .VISIBLE
51+ fun showSuccess () {
52+ tvText.visibility = View .INVISIBLE
53+ view.setBackgroundResource(bg)
54+
55+ if (Build .VERSION .SDK_INT >= 19 ) {
56+ TransitionManager .beginDelayedTransition(view as ViewGroup , Fade (Fade .IN ))
5057 }
58+ progressBar.visibility = View .INVISIBLE
59+ img.visibility = View .VISIBLE
60+ img.setImageResource(R .drawable.ic_done_white_24dp)
61+ }
62+
63+ fun showError () {
64+ tvText.visibility = View .INVISIBLE
65+
66+ view.setBackgroundResource(R .drawable.ic_bg_red_buttons_style)
5167
52- if (tvText != null ) {
53- tvText !! .visibility = View . INVISIBLE
68+ if (Build . VERSION . SDK_INT >= 19 ) {
69+ TransitionManager .beginDelayedTransition(view as ViewGroup , Fade ( Fade . IN ))
5470 }
71+
72+ progressBar.visibility = View .INVISIBLE
73+ img.visibility = View .VISIBLE
74+ img.setImageResource(R .drawable.ic_warning)
75+ }
76+
77+ fun showLoading () {
78+ view.setBackgroundResource(bg)
79+ progressBar.visibility = View .VISIBLE
80+ tvText.visibility = View .INVISIBLE
81+ img.visibility = View .INVISIBLE
5582 Log .d(TAG , " startLoading: " )
5683 }
5784
85+ fun isLoading ():Boolean {
86+ return progressBar.isShown
87+ }
88+
5889 private fun initView () {
5990
60- val view = inflate(context, R .layout.layout_loading_button_lb, null )
91+ view = inflate(context, R .layout.layout_loading_button_lb, null )
6192 addView(view)
6293
63- val bg = array? .getResourceId(R .styleable.app_background, 0 )
94+ val bg = array.getResourceId(R .styleable.app_background, 0 )
6495
6596 if (bg != 0 ) {
66- view.setBackgroundResource(bg!! )
97+ view.setBackgroundResource(bg)
98+ this .bg = bg
6799 }
68100
69101 progressBar = view.findViewById(R .id.pb)
102+ img = view.findViewById(R .id.img)
103+
70104 tvText = view.findViewById(R .id.tvText)
71105
72- progressBar? .indeterminateDrawable?.setColorFilter(
73- array!! .getColor(R .styleable.app_progressColor, resources.getColor(R .color.white)),
106+ progressBar.indeterminateDrawable?.setColorFilter(
107+ array.getColor(R .styleable.app_progressColor, resources.getColor(R .color.white)),
74108 PorterDuff .Mode .SRC_ATOP
75109 )
76110
77- var pbSize: Int = array? .getInteger(R .styleable.app_progressBarSize, 32 )!!
111+ var pbSize: Int = array.getInteger(R .styleable.app_progressBarSize, 32 )
78112 pbSize = Math .round(pbSize * (Resources .getSystem().displayMetrics.density))
79113
80114 val pbParams = LayoutParams (WRAP_CONTENT , pbSize)
81115 pbParams.gravity = Gravity .CENTER
82- progressBar? .layoutParams = pbParams
116+ progressBar.layoutParams = pbParams
83117
84118
85119 var text = " Loading Button"
86- if (array!! .getText(R .styleable.app_text) != null ) {
87- text = array!! .getText(R .styleable.app_text).toString()
120+ if (array.getText(R .styleable.app_text) != null ) {
121+ text = array.getText(R .styleable.app_text).toString()
88122 }
89123
90- tvText!! .text = text
91- tvText!! .setTextColor(array!! .getColor(R .styleable.app_textColor, resources.getColor(R .color.white)))
124+ tvText.text = text
125+ tvText.setTextColor(array.getColor(R .styleable.app_textColor, resources.getColor(R .color.white)))
92126
93- if (array!! .getBoolean(R .styleable.app_boldText, false )) {
94- tvText!! .setTypeface(null , Typeface .BOLD )
127+ if (array.getBoolean(R .styleable.app_boldText, false )) {
128+ tvText.setTypeface(null , Typeface .BOLD )
95129 }
96130
97- var size = array!! .getDimension(R .styleable.app_textSize, 14f * Resources .getSystem().displayMetrics.density)
131+ var size = array.getDimension(R .styleable.app_textSize, 14f * Resources .getSystem().displayMetrics.density)
98132 size / = Resources .getSystem().displayMetrics.density
99133
100- tvText? .textSize = size
134+ tvText.textSize = size
101135
102- if (array!! .hasValue(R .styleable.app_customFontFamily)) {
103- val fontId = array!! .getResourceId(R .styleable.app_customFontFamily, - 1 )
136+ if (array.hasValue(R .styleable.app_customFontFamily)) {
137+ val fontId = array.getResourceId(R .styleable.app_customFontFamily, - 1 )
104138 val typeface = ResourcesCompat .getFont(context, fontId)
105- tvText?.typeface = typeface
139+ tvText.typeface = typeface
140+ } else {
141+ tvText.typeface = null
106142 }
107143
108- array? .recycle()
144+ array.recycle()
109145 }
110146
111147}
0 commit comments