Skip to content

Commit bb77b35

Browse files
Provide haptic feedback without delay
1 parent 6e006db commit bb77b35

4 files changed

Lines changed: 34 additions & 32 deletions

File tree

app/src/legacy/kotlin/app/fyreplace/fyreplace/legacy/extensions/ViewExt.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package app.fyreplace.fyreplace.legacy.extensions
22

33
import android.content.Context
4+
import android.os.Build
5+
import android.view.HapticFeedbackConstants
46
import android.view.View
57
import android.view.inputmethod.InputMethodManager
68
import androidx.core.graphics.Insets
@@ -38,3 +40,29 @@ fun View.hideSoftInput() {
3840
context.getSystemService(InputMethodManager::class.java)
3941
?.hideSoftInputFromWindow(windowToken, 0)
4042
}
43+
44+
fun View.provideHapticFeedback(type: HapticType = HapticType.SIMPLE) {
45+
performHapticFeedback(
46+
when {
47+
Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE -> when (type) {
48+
HapticType.TOGGLE_ON -> HapticFeedbackConstants.TOGGLE_ON
49+
HapticType.TOGGLE_OFF -> HapticFeedbackConstants.TOGGLE_OFF
50+
else -> HapticFeedbackConstants.CONFIRM
51+
}
52+
53+
Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> HapticFeedbackConstants.CONFIRM
54+
else -> HapticFeedbackConstants.VIRTUAL_KEY
55+
}
56+
)
57+
}
58+
59+
fun View.provideHapticFeedback(positive: Boolean) = provideHapticFeedback(
60+
if (positive) HapticType.TOGGLE_ON
61+
else HapticType.TOGGLE_OFF
62+
)
63+
64+
enum class HapticType {
65+
TOGGLE_ON,
66+
TOGGLE_OFF,
67+
SIMPLE
68+
}
Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package app.fyreplace.fyreplace.legacy.ui
22

3-
import android.os.Build
4-
import android.view.HapticFeedbackConstants
53
import android.view.View
64
import androidx.annotation.StringRes
5+
import app.fyreplace.fyreplace.legacy.extensions.HapticType
6+
import app.fyreplace.fyreplace.legacy.extensions.provideHapticFeedback
77
import com.google.android.material.snackbar.BaseTransientBottomBar
88
import com.google.android.material.snackbar.Snackbar
99

@@ -22,30 +22,4 @@ interface BasePresenter : ContextHolder {
2222
rootView.provideHapticFeedback(haptics)
2323
}
2424
}
25-
26-
fun View.provideHapticFeedback(type: HapticType = HapticType.SIMPLE) {
27-
performHapticFeedback(
28-
when {
29-
Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE -> when (type) {
30-
HapticType.TOGGLE_ON -> HapticFeedbackConstants.TOGGLE_ON
31-
HapticType.TOGGLE_OFF -> HapticFeedbackConstants.TOGGLE_OFF
32-
else -> HapticFeedbackConstants.CONFIRM
33-
}
34-
35-
Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> HapticFeedbackConstants.CONFIRM
36-
else -> HapticFeedbackConstants.VIRTUAL_KEY
37-
}
38-
)
39-
}
40-
41-
fun View.provideHapticFeedback(positive: Boolean) = provideHapticFeedback(
42-
if (positive) HapticType.TOGGLE_ON
43-
else HapticType.TOGGLE_OFF
44-
)
45-
46-
enum class HapticType {
47-
TOGGLE_ON,
48-
TOGGLE_OFF,
49-
SIMPLE
50-
}
5125
}

app/src/legacy/kotlin/app/fyreplace/fyreplace/legacy/ui/adapters/FeedAdapter.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import app.fyreplace.fyreplace.databinding.ItemFeedPostTextBinding
1212
import app.fyreplace.fyreplace.legacy.events.EventsManager
1313
import app.fyreplace.fyreplace.legacy.events.RemoteNotificationWasReceivedEvent
1414
import app.fyreplace.fyreplace.legacy.extensions.firstChapter
15+
import app.fyreplace.fyreplace.legacy.extensions.provideHapticFeedback
1516
import app.fyreplace.fyreplace.legacy.ui.adapters.holders.PreviewHolder
1617
import app.fyreplace.protos.Post
1718
import kotlinx.coroutines.MainScope
@@ -115,6 +116,8 @@ class FeedAdapter(
115116
fun onUpClicked(view: View) = vote(view)
116117

117118
private fun vote(button: View) {
119+
button.provideHapticFeedback(positive = button == up)
120+
118121
if (!isVoting) scope.launch {
119122
button.isActivated = true
120123
delay(500)

app/src/legacy/kotlin/app/fyreplace/fyreplace/legacy/ui/fragments/FeedFragment.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,7 @@ class FeedFragment :
9191
}
9292

9393
override fun onPostVoted(view: View, position: Int, spread: Boolean) {
94-
view.provideHapticFeedback(positive = spread)
95-
launch {
96-
vm.vote(position, spread)
97-
}
94+
launch { vm.vote(position, spread) }
9895
}
9996

10097
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {

0 commit comments

Comments
 (0)