-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathViewExt.kt
127 lines (104 loc) · 2.99 KB
/
ViewExt.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
/* Extentions view level */
iimport android.graphics.Typeface
import android.support.design.widget.Snackbar
import android.support.v4.content.ContextCompat
import android.support.v7.widget.DefaultItemAnimator
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import android.view.View
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import id.ac.unpad.profolio.R
fun View.visible(
) {
visibility = View.VISIBLE
}
fun View.gone(
) {
visibility = View.GONE
}
fun View.invisible(
) {
visibility = View.INVISIBLE
}
fun View.showSnackbarWithCustomColor(
message: String,
textColor: Int, backgroundColor: Int,
duration: Int
) {
val finalMessage = if (TextUtils.isEmpty(message)) {
Const.SERVER_ERROR_MESSAGE_DEFAULT
} else {
message
}
val finalDuration = if (duration == 0) {
Const.SNACKBAR_TIMER_SHOWING_DEFAULT
} else {
duration
}
val finalTextColor = if (textColor == 0) {
ContextCompat.getColor(this.context, R.color.mainWhite)
} else {
textColor
}
val finalBackgroundColor = if (textColor == 0) {
ContextCompat.getColor(this.context, R.color.greyBackgroundDefault)
} else {
backgroundColor
}
val snackView = Snackbar.make(this, finalMessage, finalDuration)
snackView.setActionTextColor(finalTextColor)
snackView.view.setBackgroundColor(finalBackgroundColor)
snackView.show()
}
fun View.showSnackbarDefault(
message: String,
duration: Int
) {
val finalMessage = if (TextUtils.isEmpty(message)) {
Const.SERVER_ERROR_MESSAGE_DEFAULT
} else {
message
}
val finalDuration = if (duration == 0) {
Const.SNACKBAR_TIMER_SHOWING_DEFAULT
} else {
duration
}
Snackbar.make(this, finalMessage, finalDuration).show()
}
fun View.setCustomFont(
fontName: String
): Typeface = Typeface
.createFromAsset(this.context.assets, "fonts/$fontName")
fun RecyclerView.verticalListStyle(
) {
layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
setHasFixedSize(true)
itemAnimator = DefaultItemAnimator()
setItemViewCacheSize(30)
isDrawingCacheEnabled = true
drawingCacheQuality = View.DRAWING_CACHE_QUALITY_HIGH
}
fun RecyclerView.horizontalListStyle(
) {
layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
setHasFixedSize(true)
itemAnimator = DefaultItemAnimator()
setItemViewCacheSize(30)
isDrawingCacheEnabled = true
drawingCacheQuality = View.DRAWING_CACHE_QUALITY_HIGH
}
fun View.onShowWithSlide() {
this.animate()
.translationY(0f)
.setInterpolator(DecelerateInterpolator(2f))
.start()
}
fun View.onHideWithSlide() {
this.animate()
.translationY((this.height).toFloat())
.setInterpolator(AccelerateInterpolator(2f))
.start()
}