From c421da9a4893a2d0505d0b3058590a78358c2f0c Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Sun, 31 Jan 2021 11:23:33 -0800 Subject: [PATCH] Haptic feedback & double-tap settings --- Localizations/Localizable.strings | 2 + .../Utilities/AppPreferences.swift | 12 +++++ Views/SwiftUI/PreferencesView.swift | 4 ++ Views/UIKit/Content Views/StatusView.swift | 52 ++++++++++++++++++- 4 files changed, 68 insertions(+), 2 deletions(-) diff --git a/Localizations/Localizable.strings b/Localizations/Localizable.strings index 9005f6b..15ab08e 100644 --- a/Localizations/Localizable.strings +++ b/Localizations/Localizable.strings @@ -157,6 +157,8 @@ "preferences.position.remember-position" = "Remember position"; "preferences.position.sync-position" = "Sync position with web and other devices"; "preferences.position.newest" = "Load newest"; +"preferences.require-double-tap-to-reblog" = "Require double tap to reblog"; +"preferences.require-double-tap-to-favorite" = "Require double tap to favorite"; "preferences.show-reblog-and-favorite-counts" = "Show reblog and favorite counts"; "preferences.status-word" = "Status word"; "filters.active" = "Active"; diff --git a/ServiceLayer/Sources/ServiceLayer/Utilities/AppPreferences.swift b/ServiceLayer/Sources/ServiceLayer/Utilities/AppPreferences.swift index 9acede2..02b8a84 100644 --- a/ServiceLayer/Sources/ServiceLayer/Utilities/AppPreferences.swift +++ b/ServiceLayer/Sources/ServiceLayer/Utilities/AppPreferences.swift @@ -157,11 +157,23 @@ public extension AppPreferences { get { self[.showReblogAndFavoriteCounts] ?? false } set { self[.showReblogAndFavoriteCounts] = newValue } } + + var requireDoubleTapToReblog: Bool { + get { self[.requireDoubleTapToReblog] ?? false } + set { self[.requireDoubleTapToReblog] = newValue } + } + + var requireDoubleTapToFavorite: Bool { + get { self[.requireDoubleTapToFavorite] ?? false } + set { self[.requireDoubleTapToFavorite] = newValue } + } } private extension AppPreferences { enum Item: String { case statusWord + case requireDoubleTapToReblog + case requireDoubleTapToFavorite case useSystemReduceMotionForMedia case animateAvatars case animateHeaders diff --git a/Views/SwiftUI/PreferencesView.swift b/Views/SwiftUI/PreferencesView.swift index 4b3f122..89cf71a 100644 --- a/Views/SwiftUI/PreferencesView.swift +++ b/Views/SwiftUI/PreferencesView.swift @@ -69,6 +69,10 @@ struct PreferencesView: View { Text(option.localizedStringKey).tag(option) } } + Toggle("preferences.require-double-tap-to-reblog", + isOn: $identityContext.appPreferences.requireDoubleTapToReblog) + Toggle("preferences.require-double-tap-to-favorite", + isOn: $identityContext.appPreferences.requireDoubleTapToFavorite) if accessibilityReduceMotion { Toggle("preferences.media.use-system-reduce-motion", isOn: $identityContext.appPreferences.useSystemReduceMotionForMedia) diff --git a/Views/UIKit/Content Views/StatusView.swift b/Views/UIKit/Content Views/StatusView.swift index 09ee40c..e0871dc 100644 --- a/Views/UIKit/Content Views/StatusView.swift +++ b/Views/UIKit/Content Views/StatusView.swift @@ -245,13 +245,29 @@ private extension StatusView { for: .touchUpInside) reblogButton.addAction( - UIAction { [weak self] _ in self?.statusConfiguration.viewModel.toggleReblogged() }, + UIAction { [weak self] _ in + guard let self = self, + !self.statusConfiguration.viewModel.identityContext.appPreferences.requireDoubleTapToReblog + else { return } + + self.reblog() + }, for: .touchUpInside) + reblogButton.addTarget(self, action: #selector(reblogButtonDoubleTap(sender:event:)), for: .touchDownRepeat) + favoriteButton.addAction( - UIAction { [weak self] _ in self?.statusConfiguration.viewModel.toggleFavorited() }, + UIAction { [weak self] _ in + guard let self = self, + !self.statusConfiguration.viewModel.identityContext.appPreferences.requireDoubleTapToFavorite + else { return } + + self.favorite() + }, for: .touchUpInside) + favoriteButton.addTarget(self, action: #selector(favoriteButtonDoubleTap(sender:event:)), for: .touchDownRepeat) + shareButton.addAction( UIAction { [weak self] _ in self?.statusConfiguration.viewModel.shareStatus() }, for: .touchUpInside) @@ -534,6 +550,38 @@ private extension StatusView { menuButton.setImage(UIImage(systemName: "ellipsis", withConfiguration: UIImage.SymbolConfiguration(scale: scale)), for: .normal) } + + @objc func reblogButtonDoubleTap(sender: UIButton, event: UIEvent) { + guard + statusConfiguration.viewModel.identityContext.appPreferences.requireDoubleTapToReblog, + event.allTouches?.first?.tapCount == 2 else { + return + } + + reblog() + } + + @objc func favoriteButtonDoubleTap(sender: UIButton, event: UIEvent) { + guard + statusConfiguration.viewModel.identityContext.appPreferences.requireDoubleTapToFavorite, + event.allTouches?.first?.tapCount == 2 else { + return + } + + favorite() + } + + func reblog() { + UIImpactFeedbackGenerator(style: .medium).impactOccurred() + + statusConfiguration.viewModel.toggleReblogged() + } + + func favorite() { + UIImpactFeedbackGenerator(style: .medium).impactOccurred() + + statusConfiguration.viewModel.toggleFavorited() + } } private extension UIButton {