-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommonFunctions.kt
302 lines (250 loc) · 9.09 KB
/
CommonFunctions.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package co.extentions.commonfunctions
fun AppCompatActivity.toastShort(message:String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
fun AppCompatActivity.toastLong(message:String) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show()
}
fun AppCompatActivity.getDeepChildOffset(mainParent: ViewGroup, parent: ViewParent, child: View, accumulatedOffset: Point) {
val parentGroup = parent as ViewGroup
accumulatedOffset.x += child.left
accumulatedOffset.y += child.top
if (parentGroup == mainParent) {
return
}
getDeepChildOffset(mainParent, parentGroup.parent, parentGroup, accumulatedOffset)
}
var beforeY = -1
var marginHeight = -1
var typeScroll = ""
fun toggleOnScrolled(v: View , dx: Int, dy: Int) {
if (marginHeight == -1) {
if (dy > 0 && v.visibility == View.VISIBLE) { // DOWN
v.visibility = View.GONE
typeScroll = "down"
} else if (dy < 0 && v.visibility != View.VISIBLE) { // UP
v.visibility = View.VISIBLE
typeScroll = "up"
}
if (dy != 0) {
marginHeight = v.height
beforeY = dy
}
} else { // Stop event when changed status of visible
if (typeScroll == "up") {
if (dy < beforeY && v.visibility == View.VISIBLE) {
v.visibility = View.GONE
marginHeight = -1
} else { // Otherwise
typeScroll = "bool"
}
} else if (typeScroll == "down") {
if (dy > beforeY && v.visibility != View.VISIBLE) {
v.visibility = View.VISIBLE
marginHeight = -1
} else { // Otherwise
typeScroll = "bool"
}
} else if (typeScroll == "bool") { // Otherwise
marginHeight = -1
}
}
}
fun AppCompatActivity.scrollToView(scrollViewParent: ScrollView, view: View) {
// Get deepChild Offset
val childOffset = Point()
getDeepChildOffset(scrollViewParent, view.parent, view, childOffset)
// Scroll to child.
scrollViewParent.smoothScrollTo(0, childOffset.y)
}
fun launchApp(package:String) {
val app = package
try {
startActivity(packageManager.getLaunchIntentForPackage(app))
} catch (e: Exception) {
try {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + app)))
} catch (e: Exception) {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + app)))
}
}
}
fun AppCompatActivity.runInstagram() {
launchApp("com.instagram.android")
}
fun AppCompatActivity.getDisplayWidth(): Int {
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
var widthPixel = displayMetrics.widthPixels
var displayWidth = widthPixel
return displayWidth
}
fun AppCompatActivity.hideSoftKeyboard(activity: Activity) {
try {
val inputMethodManager = activity.getSystemService(
Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager!!.hideSoftInputFromWindow(
activity.currentFocus!!.windowToken, 0)
} catch (e: Exception) {
print(e.printStackTrace())
}
}
fun AppCompatActivity.setupUI(view:View, activity: AppCompatActivity) {
if (view !is EditText) {
view.setOnTouchListener(object : View.OnTouchListener {
override fun onTouch(v: View, event: MotionEvent): Boolean {
hideSoftKeyboard(activity)
return false
}
})
}
//If a layout container, iterate over children and seed recursion.
if (view is ViewGroup) {
for (i in 0 until (view as ViewGroup).getChildCount()) {
val innerView = (view as ViewGroup).getChildAt(i)
setupUI(innerView, activity)
}
}
}
fun View.hidden () {
this.visibility = View.GONE
}
fun View.shown () {
this.visibility = View.VISIBLE
}
fun ClearEditText.disableEdit () {
this.isEnabled = false
this.isFocusable = false
this.isCursorVisible = false
this.keyListener = null
}
fun TextView.disableEdit () {
this.isEnabled = false
this.isFocusable = false
this.isCursorVisible = false
this.keyListener = null
}
fun View.setBackgroundResourceWithoutDiscard(drawable:Int) {
var bottom: Int = this.paddingBottom
var top = this.paddingTop
var right = this.paddingRight
var left = this.paddingLeft
this.setBackgroundResource(drawable)
this.setPadding(left, top, right, bottom)
}
fun View.setMargin(left: Int = -1, top: Int = -1, right: Int = -1, bottom: Int = -1) {
val params = layoutParams as ViewGroup.MarginLayoutParams
params.setMargins(if (left != -1) left else params.leftMargin, if (top != -1) top else params.topMargin, if (right != -1) right else params.rightMargin, if (bottom != -1) bottom else params.bottomMargin)
layoutParams = params
}
fun Context.DateToMilliseconds(date: String): Long {
var sdf: SimpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm")
try {
var mDate: Date = sdf.parse(date)
var timeInMilliseconds: Long = mDate.time
return timeInMilliseconds
} catch (e: ParseException) {
print(e.printStackTrace())
}
return 0
}
fun AppCompatActivity.DateToMilliseconds(date: String): Long {
var sdf: SimpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm")
try {
var mDate: Date = sdf.parse(date)
var timeInMilliseconds: Long = mDate.time
return timeInMilliseconds
} catch (e: ParseException) {
print(e.printStackTrace())
}
return 0
}
fun DateToMilliseconds(date: String): Long {
var sdf: SimpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm")
try {
var mDate: Date = sdf.parse(date)
var timeInMilliseconds: Long = mDate.time
return timeInMilliseconds
} catch (e: ParseException) {
print(e.printStackTrace())
}
return 0
}
fun Context.drawCircleCrop(v: ImageView, url: String) {
Glide.with(this)
.load(url)
.apply(RequestOptions.circleCropTransform())
.into(v)
}
fun AppCompatActivity.drawCircleCrop(v: ImageView, u: Drawable?) {
Glide.with(this)
.load(u)
.apply(RequestOptions.circleCropTransform())
.into(v)
}
fun AppCompatActivity.drawCircleCrop(v: ImageView, url: String) {
Glide.with(this)
.load(url)
.apply(RequestOptions.circleCropTransform())
.into(v)
}
fun AppCompatActivity.isVisible(view: View): Boolean {
if (view == null) {
return false
}
if (!view.isShown) {
return false
}
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
var width = displayMetrics.widthPixels
var height = displayMetrics.heightPixels
var actualPosition: Rect = Rect()
view.getGlobalVisibleRect(actualPosition)
var screen: Rect = Rect(0, 0, width, height)
return actualPosition.intersect(screen)
}
fun AppCompatActivity.isViewFullyVisible(view: View): Boolean {
if (view == null || !view.isShown)
return false
//windowRect - will hold available area where content remain visible to users
//Takes into account screen decorations (e.g. statusbar)
var windowRect: Rect = Rect()
view.getWindowVisibleDisplayFrame(windowRect)
//if there is toolBar, get his height
var actionBarHeight: Int = 0
var context: Context = view.context
if (context is AppCompatActivity && context.supportActionBar != null)
actionBarHeight = context.supportActionBar!!.height
else if (context is Activity && context.actionBar != null)
actionBarHeight = context.actionBar.height
//windowAvailableRect - takes into account toolbar height and statusbar height
var windowAvailableRect: Rect = Rect(windowRect.left, windowRect.top + actionBarHeight, windowRect.right, windowRect.bottom)
//viewRect - holds position of the view in window
//(methods as getGlobalVisibleRect, getHitRect, getDrawingRect can return different result,
// when partialy visible)
var viewRect: Rect
var viewsLocationInWindow = IntArray(2)
view.getLocationInWindow(viewsLocationInWindow)
var viewLeft = viewsLocationInWindow[0]
var viewTop = viewsLocationInWindow[1]
var viewRight = viewLeft + view.width
var viewBottom = viewTop + view.height
viewRect = Rect(viewLeft, viewTop, viewRight, viewBottom)
//return true, only if the WHOLE view is visible in window
return windowAvailableRect.contains(viewRect)
}
fun View.setLayoutWidthHeight(width: Int, height: Int) {
if (this is LinearLayout) {
val params = LinearLayout.LayoutParams(width, height)
this.layoutParams = params
} else if (this is FrameLayout) {
val params = FrameLayout.LayoutParams(width, height)
this.layoutParams = params
} else {
this.layoutParams = ViewGroup.LayoutParams(width, height)
}
}
fun AppCompatActivity.getDimenPixelSize(@DimenRes dimen:Int): Int {
return resources.getDimensionPixelSize(dimen)
}