Skip to content

Commit 7908a2e

Browse files
committed
Change rtl snack
1 parent 7076871 commit 7908a2e

File tree

2 files changed

+68
-209
lines changed

2 files changed

+68
-209
lines changed

app/src/main/java/ir/am3n/needtool/sample/MainActivity.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,13 @@ class MainActivity : AppCompatActivity(), LifecycleObserver {
7474
}, 1000)
7575

7676
onUI({
77-
snack(actMain, rtl = true, text = "سلام, ${device["appVersion"]}", font = SnackFont.AUTO, actionText = "بزن بریم")
77+
snack(
78+
actMain,
79+
direction = Direction.RTL,
80+
text = "Hi, heloo",
81+
font = SnackFont.AUTO,
82+
actionText = "بزن بریم"
83+
)
7884
}, 6000)
7985

8086

needtool/src/main/java/ir/am3n/needtool/SnackHelper.kt

Lines changed: 61 additions & 208 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import android.graphics.Typeface
88
import android.os.Build
99
import android.util.TypedValue
1010
import android.view.Gravity
11-
import android.view.LayoutInflater
1211
import android.view.View
13-
import android.widget.Button
1412
import android.widget.FrameLayout
1513
import android.widget.TextView
1614
import androidx.annotation.ColorInt
@@ -19,10 +17,8 @@ import androidx.annotation.FontRes
1917
import androidx.coordinatorlayout.widget.CoordinatorLayout
2018
import androidx.core.content.res.ResourcesCompat
2119
import androidx.core.view.ViewCompat
22-
import androidx.core.view.isVisible
2320
import androidx.core.view.updateLayoutParams
2421
import com.google.android.material.snackbar.Snackbar
25-
import com.google.android.material.snackbar.Snackbar.SnackbarLayout
2622

2723

2824
/**
@@ -37,192 +33,25 @@ enum class SnackFont {
3733
}
3834

3935

40-
private fun Context.rtlSnack(
41-
view: View?, text: String?, duration: Int = Snackbar.LENGTH_LONG, gravity: Int = Gravity.CENTER_HORIZONTAL or Gravity.BOTTOM,
42-
font: SnackFont = SnackFont.CALLI, fontPath: String? = null, @FontRes fontRes: Int? = null,
43-
@ColorRes textColor: Int = 0, textSizeSp: Float = 0f,
44-
@ColorRes backgroundColor: Int = 0,
45-
actionText: String? = null, @ColorRes actionTextColor: Int = 0, action: () -> Unit = {}
46-
): Snackbar? {
47-
if (view == null || text == null) return null
48-
49-
val absFontPath =
50-
if (font == SnackFont.PATH && fontPath != null) fontPath else if (font == SnackFont.AUTO) fetchFontPath(view.context) else null
51-
52-
val snackbar = Snackbar.make(view, text, duration)
53-
54-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
55-
snackbar.view.translationZ = 100.fDp2Px
56-
}
57-
58-
if (backgroundColor != 0)
59-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
60-
snackbar.view.backgroundTintList = ColorStateList.valueOf(color(backgroundColor))
61-
else
62-
snackbar.view.setBackgroundColor(color(backgroundColor))
63-
64-
if (snackbar.view.layoutParams is CoordinatorLayout.LayoutParams) {
65-
snackbar.view.updateLayoutParams<CoordinatorLayout.LayoutParams> {
66-
this.gravity = gravity
67-
}
68-
} else if (snackbar.view.layoutParams is FrameLayout.LayoutParams) {
69-
snackbar.view.updateLayoutParams<FrameLayout.LayoutParams> {
70-
this.gravity = gravity
71-
}
72-
}
73-
74-
val layout = snackbar.view as SnackbarLayout
75-
(layout.findViewById<View>(com.google.android.material.R.id.snackbar_text) as TextView).visibility = View.INVISIBLE
76-
(layout.findViewById<View>(com.google.android.material.R.id.snackbar_action) as Button).visibility = View.INVISIBLE
77-
78-
val inflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
79-
val snackView: View = inflater.inflate(R.layout.layout_rtl_snackbar, null)
80-
81-
val txt = snackView.findViewById<View>(R.id.snackbar_text) as TextView
82-
txt.text = text
83-
if (textColor != 0)
84-
txt.setTextColor(color(textColor))
85-
if (textSizeSp != 0f)
86-
txt.textSize = textSizeSp
87-
88-
89-
val btn = snackView.findViewById<View>(R.id.snackbar_action) as Button
90-
if (actionText == null) {
91-
btn.isVisible = false
92-
} else {
93-
btn.isVisible = true
94-
btn.text = actionText
95-
if (actionTextColor != 0)
96-
btn.setTextColor(
97-
when {
98-
actionTextColor != 0 -> color(actionTextColor)
99-
else -> fetchAccentColor(this) ?: Color.CYAN
100-
}
101-
)
102-
btn.setSafeOnClickListener { action.invoke() }
103-
}
104-
105-
106-
absFontPath?.let {
107-
Typeface.createFromAsset(assets, it)?.let { typeface ->
108-
txt.typeface = typeface
109-
btn.typeface = typeface
110-
}
111-
}
112-
if (fontRes != null) {
113-
ResourcesCompat.getFont(view.context, fontRes)?.let { typeface ->
114-
txt.typeface = typeface
115-
btn.typeface = typeface
116-
}
117-
}
118-
119-
120-
layout.addView(snackView, 0)
121-
snackbar.show()
122-
123-
124-
125-
return snackbar
126-
}
127-
128-
private fun rtlSnack(
129-
view: View?, text: String?, duration: Int = Snackbar.LENGTH_LONG, gravity: Int = Gravity.CENTER_HORIZONTAL or Gravity.BOTTOM,
130-
font: SnackFont = SnackFont.CALLI, fontPath: String? = null, @FontRes fontRes: Int? = null,
131-
@ColorInt textColor: Int = 0, textSizeSp: Float = 0f,
132-
@ColorInt backgroundColor: Int = 0,
133-
actionText: String? = "", @ColorInt actionTextColor: Int = 0, action: () -> Unit = {}
134-
): Snackbar? {
135-
if (view == null || text == null) return null
136-
val context = view.context
137-
138-
val absFontPath =
139-
if (font == SnackFont.PATH && fontPath != null) fontPath else if (font == SnackFont.AUTO) fetchFontPath(view.context) else null
140-
141-
val snackbar = Snackbar.make(view, text, duration)
142-
143-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
144-
snackbar.view.translationZ = 100.fDp2Px
145-
}
146-
147-
if (backgroundColor != 0)
148-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
149-
snackbar.view.backgroundTintList = ColorStateList.valueOf(backgroundColor)
150-
else
151-
snackbar.view.setBackgroundColor(backgroundColor)
152-
153-
if (snackbar.view.layoutParams is CoordinatorLayout.LayoutParams) {
154-
snackbar.view.updateLayoutParams<CoordinatorLayout.LayoutParams> {
155-
this.gravity = gravity
156-
}
157-
} else if (snackbar.view.layoutParams is FrameLayout.LayoutParams) {
158-
snackbar.view.updateLayoutParams<FrameLayout.LayoutParams> {
159-
this.gravity = gravity
160-
}
161-
}
162-
163-
val layout = snackbar.view as SnackbarLayout
164-
(layout.findViewById<View>(com.google.android.material.R.id.snackbar_text) as TextView).visibility = View.INVISIBLE
165-
(layout.findViewById<View>(com.google.android.material.R.id.snackbar_action) as Button).visibility = View.INVISIBLE
166-
167-
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
168-
val snackView: View = inflater.inflate(R.layout.layout_rtl_snackbar, null)
169-
170-
val txt = snackView.findViewById<View>(R.id.snackbar_text) as TextView
171-
txt.text = text
172-
if (textColor != 0)
173-
txt.setTextColor(textColor)
174-
if (textSizeSp != 0f)
175-
txt.textSize = textSizeSp
176-
177-
178-
val btn = snackView.findViewById<View>(R.id.snackbar_action) as Button
179-
if (actionText == null) {
180-
btn.isVisible = false
181-
} else {
182-
btn.isVisible = true
183-
btn.text = actionText
184-
if (actionTextColor != 0)
185-
btn.setTextColor(
186-
when {
187-
actionTextColor != 0 -> actionTextColor
188-
else -> fetchAccentColor(context) ?: Color.CYAN
189-
}
190-
)
191-
btn.setSafeOnClickListener { action.invoke() }
192-
}
193-
194-
195-
absFontPath?.let {
196-
Typeface.createFromAsset(context.assets, it)?.let { typeface ->
197-
txt.typeface = typeface
198-
btn.typeface = typeface
199-
}
200-
}
201-
if (fontRes != null) {
202-
ResourcesCompat.getFont(view.context, fontRes)?.let { typeface ->
203-
txt.typeface = typeface
204-
btn.typeface = typeface
205-
}
206-
}
207-
208-
209-
layout.addView(snackView, 0)
210-
snackbar.show()
211-
212-
return snackbar
213-
}
214-
36+
enum class Direction { LTR, RTL, INHERIT, LOCALE }
21537

21638
fun Context.snack(
217-
view: View?, rtl: Boolean = false, text: String?, duration: Int = Snackbar.LENGTH_LONG, gravity: Int = Gravity.CENTER_HORIZONTAL or Gravity.BOTTOM,
218-
font: SnackFont = SnackFont.CALLI, fontPath: String? = null, @FontRes fontRes: Int? = null,
219-
@ColorRes textColor: Int = 0, textSizeSp: Float = 0f,
39+
view: View?,
40+
direction: Direction = Direction.INHERIT,
41+
text: String?,
42+
duration: Int = Snackbar.LENGTH_LONG,
43+
gravity: Int = Gravity.CENTER_HORIZONTAL or Gravity.BOTTOM,
44+
font: SnackFont = SnackFont.CALLI,
45+
fontPath: String? = null,
46+
@FontRes fontRes: Int? = null,
47+
@ColorRes textColor: Int = 0,
48+
textSizeSp: Float = 0f,
22049
@ColorRes backgroundColor: Int = 0,
221-
actionText: String? = "", @ColorRes actionTextColor: Int = 0, action: () -> Unit = {}
50+
actionText: String? = "",
51+
@ColorRes actionTextColor: Int = 0,
52+
action: () -> Unit = {}
22253
): Snackbar? {
22354

224-
if (rtl) return rtlSnack(view, text, duration, gravity, font, fontPath, fontRes, textColor, textSizeSp, backgroundColor, actionText, actionTextColor, action)
225-
22655
if (view == null || text == null) return null
22756

22857
val snackbar = Snackbar.make(view, text, duration)
@@ -242,20 +71,24 @@ fun Context.snack(
24271
}
24372

24473
val snackbarView = snackbar.view
245-
if (backgroundColor != 0)
246-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
74+
if (backgroundColor != 0) {
75+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
24776
snackbarView.backgroundTintList = ColorStateList.valueOf(color(backgroundColor))
248-
else
249-
snackbarView.setBackgroundColor(color(backgroundColor))
77+
}
78+
}
25079

25180
snackbarView.findViewById<TextView?>(com.google.android.material.R.id.snackbar_text)?.apply {
252-
if (textColor != 0)
81+
if (textColor != 0) {
25382
setTextColor(color(textColor))
254-
if (textSizeSp != 0f)
83+
}
84+
if (textSizeSp != 0f) {
25585
textSize = textSizeSp
86+
}
25687
}
25788

258-
(if (font==SnackFont.PATH && fontPath!=null) fontPath else if (font==SnackFont.AUTO) fetchFontPath(view.context) else null)?.let {
89+
(if (font == SnackFont.PATH && fontPath != null) fontPath
90+
else if (font == SnackFont.AUTO) fetchFontPath(view.context)
91+
else null)?.let {
25992
Typeface.createFromAsset(assets, it)?.let { typeface ->
26093
snackbarView.findViewById<TextView?>(com.google.android.material.R.id.snackbar_text)?.typeface = typeface
26194
snackbarView.findViewById<TextView?>(com.google.android.material.R.id.snackbar_action)?.typeface = typeface
@@ -279,23 +112,35 @@ fun Context.snack(
279112
}
280113
}
281114

282-
ViewCompat.setLayoutDirection(snackbar.view, ViewCompat.LAYOUT_DIRECTION_RTL)
115+
ViewCompat.setLayoutDirection(snackbar.view, when (direction) {
116+
Direction.LTR -> ViewCompat.LAYOUT_DIRECTION_LTR
117+
Direction.RTL -> ViewCompat.LAYOUT_DIRECTION_RTL
118+
Direction.INHERIT -> ViewCompat.LAYOUT_DIRECTION_INHERIT
119+
else -> ViewCompat.LAYOUT_DIRECTION_LOCALE
120+
})
283121

284122
snackbar.show()
285123

286124
return snackbar
287125
}
288126

289127
fun snack(
290-
view: View?, rtl: Boolean = false, text: String?, duration: Int = Snackbar.LENGTH_LONG, gravity: Int = Gravity.CENTER_HORIZONTAL or Gravity.BOTTOM,
291-
font: SnackFont = SnackFont.CALLI, fontPath: String? = null, @FontRes fontRes: Int? = null,
292-
@ColorInt textColor: Int = 0, textSizeSp: Float = 0f,
128+
view: View?,
129+
direction: Direction = Direction.INHERIT,
130+
text: String?,
131+
duration: Int = Snackbar.LENGTH_LONG,
132+
gravity: Int = Gravity.CENTER_HORIZONTAL or Gravity.BOTTOM,
133+
font: SnackFont = SnackFont.CALLI,
134+
fontPath: String? = null,
135+
@FontRes fontRes: Int? = null,
136+
@ColorInt textColor: Int = 0,
137+
textSizeSp: Float = 0f,
293138
@ColorInt backgroundColor: Int = 0,
294-
actionText: String? = "", @ColorInt actionTextColor: Int = 0, action: () -> Unit = {}
139+
actionText: String? = "",
140+
@ColorInt actionTextColor: Int = 0,
141+
action: () -> Unit = {}
295142
): Snackbar? {
296143

297-
if (rtl) return rtlSnack(view, text, duration, gravity, font, fontPath, fontRes, textColor, textSizeSp, backgroundColor, actionText, actionTextColor, action)
298-
299144
if (view == null || text == null) return null
300145

301146
val snackbar = Snackbar.make(view, text, duration)
@@ -316,11 +161,11 @@ fun snack(
316161
}
317162

318163
val snackbarView = snackbar.view
319-
if (backgroundColor != 0)
320-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
164+
if (backgroundColor != 0) {
165+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
321166
snackbarView.backgroundTintList = ColorStateList.valueOf(backgroundColor)
322-
else
323-
snackbarView.setBackgroundColor(backgroundColor)
167+
}
168+
}
324169

325170
snackbarView.findViewById<TextView?>(com.google.android.material.R.id.snackbar_text)?.apply {
326171
if (textColor != 0)
@@ -329,7 +174,9 @@ fun snack(
329174
textSize = textSizeSp
330175
}
331176

332-
(if (font == SnackFont.PATH && fontPath != null) fontPath else if (font == SnackFont.AUTO) fetchFontPath(view.context) else null)?.let {
177+
(if (font == SnackFont.PATH && fontPath != null) fontPath
178+
else if (font == SnackFont.AUTO) fetchFontPath(view.context)
179+
else null)?.let {
333180
Typeface.createFromAsset(view.context.assets, it)?.let { typeface ->
334181
snackbarView.findViewById<TextView?>(com.google.android.material.R.id.snackbar_action)?.typeface = typeface
335182
snackbarView.findViewById<TextView?>(com.google.android.material.R.id.snackbar_text)?.typeface = typeface
@@ -353,7 +200,12 @@ fun snack(
353200
}
354201
}
355202

356-
ViewCompat.setLayoutDirection(snackbar.view, ViewCompat.LAYOUT_DIRECTION_RTL)
203+
ViewCompat.setLayoutDirection(snackbar.view, when (direction) {
204+
Direction.LTR -> ViewCompat.LAYOUT_DIRECTION_LTR
205+
Direction.RTL -> ViewCompat.LAYOUT_DIRECTION_RTL
206+
Direction.INHERIT -> ViewCompat.LAYOUT_DIRECTION_INHERIT
207+
else -> ViewCompat.LAYOUT_DIRECTION_LOCALE
208+
})
357209

358210
snackbar.show()
359211

@@ -366,11 +218,12 @@ private fun fetchFontPath(context: Context): String? {
366218
val list = context.assets.list("fonts/")
367219
if (list!!.isNotEmpty()) {
368220
for (file in list) {
369-
if (file.contains(".ttf"))
221+
if (file.contains(".ttf")) {
370222
return "fonts/$file"
223+
}
371224
}
372225
}
373-
} catch (t: Throwable) {
226+
} catch (_: Throwable) {
374227
return null
375228
}
376229
return null
@@ -383,7 +236,7 @@ private fun fetchAccentColor(context: Context): Int? {
383236
val color = a.getColor(0, 0)
384237
a.recycle()
385238
return color
386-
} catch (t: Throwable) {
239+
} catch (_: Throwable) {
387240
}
388241
return null
389242
}

0 commit comments

Comments
 (0)