Skip to content

Commit 0e1faa3

Browse files
Start using haptics
1 parent 4f6891f commit 0e1faa3

File tree

5 files changed

+54
-12
lines changed

5 files changed

+54
-12
lines changed

app/src/legacy/kotlin/app/fyreplace/fyreplace/legacy/ui/BasePresenter.kt

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package app.fyreplace.fyreplace.legacy.ui
22

3+
import android.os.Build
4+
import android.view.HapticFeedbackConstants
35
import android.view.View
46
import androidx.annotation.StringRes
57
import com.google.android.material.snackbar.BaseTransientBottomBar
@@ -10,9 +12,40 @@ interface BasePresenter : ContextHolder {
1012

1113
fun showBasicSnackbar(
1214
@StringRes message: Int,
13-
@BaseTransientBottomBar.Duration duration: Int = Snackbar.LENGTH_LONG
15+
@BaseTransientBottomBar.Duration duration: Int = Snackbar.LENGTH_LONG,
16+
haptics: HapticType? = null
1417
) {
1518
val rootView = rootView ?: return
1619
Snackbar.make(rootView, rootView.context.getString(message), duration).show()
20+
21+
if (haptics != null) {
22+
rootView.provideHapticFeedback(haptics)
23+
}
24+
}
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
1750
}
1851
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class FeedFragment :
105105
launch {
106106
vm.vote(position, spread)
107107
adapter.remove(position)
108+
view.provideHapticFeedback(positive = spread)
108109
}
109110
}
110111

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import app.fyreplace.fyreplace.legacy.extensions.mainActivity
2626
import app.fyreplace.fyreplace.legacy.extensions.makePreview
2727
import app.fyreplace.fyreplace.legacy.extensions.makeShareIntent
2828
import app.fyreplace.fyreplace.legacy.grpc.p
29+
import app.fyreplace.fyreplace.legacy.ui.BasePresenter
2930
import app.fyreplace.fyreplace.legacy.ui.PrimaryActionStyle
3031
import app.fyreplace.fyreplace.legacy.ui.adapters.PostAdapter
3132
import app.fyreplace.fyreplace.legacy.ui.adapters.holders.ItemHolder
@@ -315,17 +316,16 @@ class PostFragment :
315316
if (subscribed) PostWasSubscribedToEvent(preview)
316317
else PostWasUnsubscribedFromEvent(preview)
317318
)
318-
showBasicSnackbar(
319-
if (subscribed) R.string.post_snackbar_subscribed
320-
else R.string.post_snackbar_unsubscribed,
321-
Snackbar.LENGTH_SHORT
322-
)
319+
bd.root.provideHapticFeedback(positive = subscribed)
323320
}
324321
}
325322

326323
private suspend fun report() {
327324
vm.report()
328-
showBasicSnackbar(R.string.post_report_success_message)
325+
showBasicSnackbar(
326+
R.string.post_report_success_message,
327+
haptics = BasePresenter.HapticType.SIMPLE
328+
)
329329
}
330330

331331
private suspend fun delete() {
@@ -336,7 +336,10 @@ class PostFragment :
336336

337337
private suspend fun reportComment(comment: Comment) {
338338
vm.reportComment(comment.id)
339-
showBasicSnackbar(R.string.post_comment_report_success_message)
339+
showBasicSnackbar(
340+
R.string.post_comment_report_success_message,
341+
haptics = BasePresenter.HapticType.SIMPLE
342+
)
340343
}
341344

342345
private suspend fun deleteComment(position: Int, comment: Comment) {

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import app.fyreplace.fyreplace.legacy.extensions.getUsername
2222
import app.fyreplace.fyreplace.legacy.extensions.setAvatar
2323
import app.fyreplace.fyreplace.legacy.extensions.setLinkifiedText
2424
import app.fyreplace.fyreplace.legacy.extensions.setupTransitions
25+
import app.fyreplace.fyreplace.legacy.ui.BasePresenter
2526
import app.fyreplace.fyreplace.legacy.ui.FailureHandler
2627
import app.fyreplace.fyreplace.legacy.viewmodels.CentralViewModel
2728
import app.fyreplace.fyreplace.legacy.viewmodels.Sentence
@@ -165,7 +166,10 @@ class UserFragment : DialogFragment(), FailureHandler {
165166
private fun report() = showChoiceAlert(R.string.user_report_title, null) {
166167
launch {
167168
vm.report()
168-
showBasicSnackbar(R.string.user_report_success_message)
169+
showBasicSnackbar(
170+
R.string.user_report_success_message,
171+
haptics = BasePresenter.HapticType.SIMPLE
172+
)
169173
}
170174
}
171175

@@ -194,7 +198,10 @@ class UserFragment : DialogFragment(), FailureHandler {
194198
}
195199

196200
private fun finishBan() {
197-
showBasicSnackbar(R.string.user_ban_success_message)
201+
showBasicSnackbar(
202+
R.string.user_ban_success_message,
203+
haptics = BasePresenter.HapticType.SIMPLE
204+
)
198205
bd.toolbar.menu.findItem(R.id.ban).isVisible = false
199206
em.post(UserWasBannedEvent(args.profile.v.toBuilder().setIsBanned(true).build()))
200207
}

app/src/legacy/res/values/strings.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,6 @@
192192
<string name="post_comment_delete_title">Delete this comment?</string>
193193
<string name="post_comment_delete_message">This action cannot be undone.</string>
194194
<string name="post_write_comment">Add comment</string>
195-
<string name="post_snackbar_subscribed">Subscribed to new comments</string>
196-
<string name="post_snackbar_unsubscribed">Unsubscribed from new comments</string>
197195
<string name="post_snackbar_comment_created">New comment from %1$s</string>
198196
<string name="post_snackbar_comment_created_action">See</string>
199197
<string name="post_error_not_found_title">Post not found</string>

0 commit comments

Comments
 (0)