| 1 | import 'package:flutter_riverpod/flutter_riverpod.dart'; |
| 2 | import 'package:haptic_feedback/haptic_feedback.dart'; |
| 3 | |
| 4 | import '../providers/app_providers.dart'; |
| 5 | |
| 6 | typedef HapticVibrate = Future<void> Function(HapticsType type); |
| 7 | |
| 8 | /// The single setting-aware entry point for application haptic feedback. |
| 9 | class AppHaptics { |
| 10 | AppHaptics(this._isEnabled, {HapticVibrate? vibrate}) |
| 11 | : _vibrate = vibrate ?? Haptics.vibrate; |
| 12 | |
| 13 | final bool Function() _isEnabled; |
| 14 | final HapticVibrate _vibrate; |
| 15 | |
| 16 | Future<void> vibrate(HapticsType type) async { |
| 17 | if (!_isEnabled()) return; |
| 18 | try { |
| 19 | await _vibrate(type); |
| 20 | } catch (_) { |
| 21 | // Haptics are best-effort on unsupported devices and simulators. |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | Future<void> light() => vibrate(HapticsType.light); |
| 26 | Future<void> medium() => vibrate(HapticsType.medium); |
| 27 | Future<void> selection() => vibrate(HapticsType.selection); |
| 28 | } |
| 29 | |
| 30 | final appHapticsProvider = Provider<AppHaptics>((ref) { |
| 31 | return AppHaptics(() => ref.read(settingsProvider).hapticFeedbackEnabled); |
| 32 | }); |