-
Notifications
You must be signed in to change notification settings - Fork 10
/
ProgressDialog.kt
176 lines (149 loc) · 5.96 KB
/
ProgressDialog.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* MIT License
*
* Copyright (c) 2018. Livin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
import android.app.Activity
import android.content.res.ColorStateList
import android.os.Build
import android.support.annotation.RequiresApi
import android.support.v7.widget.CardView
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.*
/***
* Created by Livin Mathew <mail@livinmathew.me> on 10/3/18.
*/
/**
* @param activity instance of activity to which this ProgressDialog object belongs to
**/
class ProgressDialog(private val activity: Activity) {
private val layout = RelativeLayout(activity)
private val textView = TextView(activity)
private val progressBar = ProgressBar(activity, null, android.R.attr.progressBarStyleLarge)
private val cardView = CardView(activity)
private val innerLayout = LinearLayout(activity)
private var cancelable: Boolean = true
init {
// CardView
val cardViewParams = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 275)
cardViewParams.addRule(RelativeLayout.CENTER_VERTICAL)
cardView.cardElevation = 15f
cardView.radius = 20f
cardViewParams.setMargins(dip(16f), dip(0f), dip(16f), dip(0f))
// Inner Layout
val innerLayoutParams = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT)
innerLayout.setPadding(dip(8f), dip(8f), dip(8f), dip(8f))
innerLayout.orientation = LinearLayout.HORIZONTAL
cardView.addView(innerLayout, innerLayoutParams)
// ProgressBar
val progressBarParams = RelativeLayout.LayoutParams(125, RelativeLayout.LayoutParams.MATCH_PARENT)
progressBarParams.addRule(RelativeLayout.CENTER_VERTICAL)
progressBarParams.setMargins(dip(0f), dip(0f), dip(16f), dip(0f))
innerLayout.addView(progressBar, progressBarParams)
// TextView
val textViewParams = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT)
textViewParams.addRule(RelativeLayout.CENTER_VERTICAL)
textView.setPadding(dip(16f), dip(0f), dip(0f), dip(0f))
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22f)
textView.gravity = Gravity.CENTER_VERTICAL
innerLayout.addView(textView, textViewParams)
// Layout
val layoutParams = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT)
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT)
activity.addContentView(layout, layoutParams)
layout.addView(cardView, cardViewParams)
/* If clicked anywhere on the screen except the progress dialog,
* the progress dialog must dismiss depending upon the value of cancelable
*/
layout.setOnClickListener {
if (cancelable)
dismiss()
}
/* Left empty purposefully. To detach cardview and
* its contents from layout's click listener
*/
cardView.setOnClickListener {}
dismiss()
}
/**
* @param dp size in dp
* @return size in px
**/
// Convert dp to px
private fun dip(dp: Float): Int {
val r = activity.resources
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
dp,
r.displayMetrics
).toInt()
}
/**
* @param message A string object to display on the progress dialog
* Functions same as the setMessage() in deprecated ProgressDialog class
**/
/* Set message on the progress bar. */
fun setMessage(message: String){
textView.text = message
}
/* Display progress dialog */
fun show() {
layout.visibility = View.VISIBLE
}
/* Hide progress dialog */
fun dismiss() {
layout.visibility = View.GONE
}
/**
* @param cancelable A boolean which determines if the dialog can be dismissed by the user
**/
/* Toggles value of cancelable */
fun setCancelable(cancelable: Boolean) {
this.cancelable = cancelable
}
/**
* @param color ResourceId of progress bar's color
**/
/* Sets progress bar's color */
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
fun setColor(color: Int) {
progressBar.progressTintList = ColorStateList.valueOf(color)
}
/**
* @param superOnBackPressed = {super.onBackPressed()} A block of code to be executed.
* Called when back button is pressed. Should be called in the overridden onBackPressed()
* of the activity
**/
fun onBackPressed(superOnBackPressed: () -> Unit) {
if (layout.visibility == View.VISIBLE){
if (cancelable)
dismiss()
}else
superOnBackPressed.invoke()
}
/**
* @return value of cancelable
**/
fun isCancelable(): Boolean = cancelable
}