Skip to content

Commit 499e788

Browse files
committed
Use vibration in pin button
1 parent ff13a3b commit 499e788

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

app/src/main/java/com/wstxda/switchai/ui/utils/VibrationService.kt

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,30 @@ import com.wstxda.switchai.utils.Constants
1010

1111
object VibrationService {
1212

13-
fun performVibration(context: Context) {
14-
if (!isVibrationEnabled(context)) return
13+
private const val EFFECT_CLICK = 1
14+
private const val EFFECT_TICK = 2
1515

16-
val vibrator = context.getSystemService(Vibrator::class.java) ?: return
16+
fun Context.openAssistantVibration() {
17+
if (!isVibrationEnabled()) return
18+
performVibration(
19+
effectId = getEffectClick(), fallbackDuration = 40
20+
)
21+
}
22+
23+
fun Context.buttonVibration() {
24+
performVibration(
25+
effectId = getEffectTick(), fallbackDuration = 20
26+
)
27+
}
28+
29+
private fun Context.performVibration(
30+
effectId: Int,
31+
fallbackDuration: Long,
32+
) {
33+
val vibrator = getSystemService(Vibrator::class.java) ?: return
1734

1835
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
19-
val effect = VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK)
36+
val effect = VibrationEffect.createPredefined(effectId)
2037

2138
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
2239
val attrs =
@@ -25,21 +42,32 @@ object VibrationService {
2542
} else {
2643
vibrator.vibrate(effect)
2744
}
28-
2945
} else {
3046
val effect = VibrationEffect.createOneShot(
31-
40, VibrationEffect.DEFAULT_AMPLITUDE
47+
fallbackDuration, VibrationEffect.DEFAULT_AMPLITUDE
3248
)
3349
vibrator.vibrate(effect)
3450
}
3551
}
3652

37-
private fun isVibrationEnabled(context: Context): Boolean {
38-
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
39-
return prefs.getBoolean(Constants.ASSISTANT_VIBRATION_PREF_KEY, true)
53+
private fun getEffectClick(): Int {
54+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
55+
VibrationEffect.EFFECT_CLICK
56+
} else {
57+
EFFECT_CLICK
58+
}
4059
}
4160

42-
fun Context.openAssistantVibration() {
43-
performVibration(this)
61+
private fun getEffectTick(): Int {
62+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
63+
VibrationEffect.EFFECT_TICK
64+
} else {
65+
EFFECT_TICK
66+
}
67+
}
68+
69+
private fun Context.isVibrationEnabled(): Boolean {
70+
val prefs = PreferenceManager.getDefaultSharedPreferences(this)
71+
return prefs.getBoolean(Constants.ASSISTANT_VIBRATION_PREF_KEY, true)
4472
}
4573
}

app/src/main/java/com/wstxda/switchai/ui/viewholder/AssistantSelectorItemViewHolder.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.wstxda.switchai.R
77
import com.wstxda.switchai.utils.DigitalAssistantMap
88
import com.wstxda.switchai.databinding.ListItemAssistantViewBinding
99
import com.wstxda.switchai.ui.adapter.AssistantSelectorRecyclerView
10+
import com.wstxda.switchai.ui.utils.VibrationService.buttonVibration
1011

1112
class AssistantSelectorItemViewHolder(
1213
private val binding: ListItemAssistantViewBinding,
@@ -26,7 +27,10 @@ class AssistantSelectorItemViewHolder(
2627
if (item.isPinned) R.drawable.ic_pin_filled else R.drawable.ic_pin_outline
2728
)
2829

29-
binding.pinButton.setOnClickListener { onPinClicked(item.key) }
30+
binding.pinButton.setOnClickListener {
31+
onPinClicked(item.key)
32+
it.context.buttonVibration()
33+
}
3034
itemView.setOnClickListener {
3135
val context = it.context
3236
DigitalAssistantMap.assistantsMap[item.key]?.let { cls ->

0 commit comments

Comments
 (0)