feat: тактильная отдача + тумблер в настройках

This commit is contained in:
klockky
2026-05-15 07:26:27 +03:00
parent fd4f40a527
commit 5db3b2d135
6 changed files with 143 additions and 9 deletions
+83
View File
@@ -0,0 +1,83 @@
import 'package:flutter/services.dart';
import 'package:shared_preferences/shared_preferences.dart';
/// Centralized tactile feedback for Komet.
///
/// Wraps Flutter's [HapticFeedback] so the whole app speaks one tactile
/// "language": the same gesture always feels the same. Composite patterns
/// chain impacts with short delays to produce richer, more memorable
/// sensations than a single buzz.
///
/// Every call is best-effort and silent on failure — a device without a
/// vibrator (or with system haptics disabled) must never crash the UI.
class Haptics {
Haptics._();
static const String _prefKey = 'haptics_enabled';
/// Master switch. Silences every haptic app-wide when `false`.
/// Controlled by the user via Settings; persisted across launches.
static bool enabled = true;
/// Restores the saved preference. Call once during app startup,
/// before the first frame. Defaults to enabled when never set.
static Future<void> load() async {
try {
final prefs = await SharedPreferences.getInstance();
enabled = prefs.getBool(_prefKey) ?? true;
} catch (_) {
enabled = true;
}
}
/// Updates the master switch and persists it.
static Future<void> setEnabled(bool value) async {
enabled = value;
try {
final prefs = await SharedPreferences.getInstance();
await prefs.setBool(_prefKey, value);
} catch (_) {
// Persistence is best-effort; the in-memory switch still applies.
}
}
static Future<void> _fire(Future<void> Function() effect) async {
if (!enabled) return;
try {
await effect();
} catch (_) {
// Intentionally swallowed: haptics are a nicety, never a hard dependency.
}
}
/// A crisp, light tick — taps, toggles, opening panels.
static Future<void> tap() => _fire(HapticFeedback.lightImpact);
/// A firmer press — confirmations, entering a mode.
static Future<void> medium() => _fire(HapticFeedback.mediumImpact);
/// A strong thud — destructive or weighty actions.
static Future<void> heavy() => _fire(HapticFeedback.heavyImpact);
/// The subtle detent of moving between discrete options — tabs, selection.
static Future<void> selection() => _fire(HapticFeedback.selectionClick);
/// Message sent: a quick, instant tick (the "whoosh").
static Future<void> send() => tap();
/// A two-beat rising pulse — success, completion, "it landed".
static Future<void> success() async {
if (!enabled) return;
await _fire(HapticFeedback.lightImpact);
await Future.delayed(const Duration(milliseconds: 90));
await _fire(HapticFeedback.mediumImpact);
}
/// A double thud — errors, rejected or failed actions.
static Future<void> error() async {
if (!enabled) return;
await _fire(HapticFeedback.heavyImpact);
await Future.delayed(const Duration(milliseconds: 120));
await _fire(HapticFeedback.heavyImpact);
}
}
@@ -12,6 +12,7 @@ import '../calls/calls_tab.dart';
import '../contacts/contacts_tab.dart';
import '../profile/settings_tab.dart';
import '../../../backend/api.dart';
import '../../../core/utils/haptics.dart';
import '../../../backend/models/chat_folder.dart';
import '../../../backend/modules/account.dart';
import '../../../backend/modules/chats.dart';
@@ -139,6 +140,7 @@ class _ChatListScreenState extends State<ChatListScreen>
}
void _toggleSelection(String chatId) {
Haptics.selection();
setState(() {
if (_selectedChats.contains(chatId)) {
_selectedChats.remove(chatId);
@@ -729,6 +731,8 @@ class _ChatListScreenState extends State<ChatListScreen>
if (index == _currentNavIndex && !_navPageAnimController.isAnimating) {
return;
}
// Detent "click" when crossing into a different tab.
Haptics.selection();
double fromT;
if (_navPageAnimController.isAnimating) {
final t = Curves.easeOutCubic.transform(_navPageAnimController.value);
@@ -743,6 +747,7 @@ class _ChatListScreenState extends State<ChatListScreen>
}
void _toggleFab() {
Haptics.tap();
setState(() {
_isFabOpen = !_isFabOpen;
if (_isFabOpen) {
@@ -8,6 +8,7 @@ import '../../../main.dart';
import '../../../backend/api.dart';
import '../../../backend/modules/messages.dart';
import '../../../core/storage/app_database.dart';
import '../../../core/utils/haptics.dart';
import '../../../models/attachment.dart';
import '../../../backend/modules/messages.dart' show ContactCache;
import '../../widgets/message_bubble.dart';
@@ -153,6 +154,10 @@ class _ChatScreenState extends State<ChatScreen>
_hasText = false;
});
// Instant tactile "whoosh" the moment the message leaves the composer,
// not after the network round-trip — feedback must feel immediate.
Haptics.send();
_scrollToBottom();
await messagesModule.sendMessage(_myId, widget.chatId, text);
@@ -173,6 +178,7 @@ class _ChatScreenState extends State<ChatScreen>
}
} catch (e) {
debugPrint('Error sending message: $e');
Haptics.error();
} finally {
setState(() {
_isSending = false;
+44 -8
View File
@@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
import 'package:material_symbols_icons/symbols.dart';
import 'package:package_info_plus/package_info_plus.dart';
import '../../../core/storage/app_database.dart';
import '../../../core/utils/haptics.dart';
import '../../../l10n/app_localizations.dart';
import '../../../main.dart';
import '../auth/proxy_settings_sheet.dart';
@@ -30,6 +31,7 @@ class _SettingsTabState extends State<SettingsTab> {
int _versionSecretTapCount = 0;
Timer? _versionSecretTapResetTimer;
StreamSubscription? _profileUpdateSub;
bool _hapticsEnabled = Haptics.enabled;
@override
void initState() {
@@ -83,6 +85,13 @@ class _SettingsTabState extends State<SettingsTab> {
});
}
Future<void> _setHaptics(bool value) async {
await Haptics.setEnabled(value);
// Let the user *feel* the confirmation the instant they switch it on.
if (value) Haptics.success();
if (mounted) setState(() => _hapticsEnabled = value);
}
@override
Widget build(BuildContext context) {
final cs = Theme.of(context).colorScheme;
@@ -144,6 +153,12 @@ child: _buildSection(
icon: Symbols.notifications_active,
label: 'Уведомления и звук',
),
_SettingsItem(
icon: Symbols.vibration,
label: 'Тактильная отдача',
toggleValue: _hapticsEnabled,
onToggle: _setHaptics,
),
_SettingsItem(
icon: Symbols.vpn_lock,
label: 'Прокси',
@@ -450,7 +465,9 @@ child: _buildSection(
Material(
color: Colors.transparent,
child: InkWell(
onTap: item.onTap ?? () {},
onTap: item.isToggle
? () => item.onToggle!(!(item.toggleValue ?? false))
: (item.onTap ?? () {}),
borderRadius: isLast
? const BorderRadius.vertical(bottom: Radius.circular(20))
: null,
@@ -475,12 +492,18 @@ child: _buildSection(
),
),
),
Icon(
Symbols.chevron_right,
color: cs.outline,
size: 20,
weight: 400,
),
if (item.isToggle)
Switch.adaptive(
value: item.toggleValue ?? false,
onChanged: item.onToggle,
)
else
Icon(
Symbols.chevron_right,
color: cs.outline,
size: 20,
weight: 400,
),
],
),
),
@@ -505,7 +528,20 @@ class _SettingsItem {
final String label;
final VoidCallback? onTap;
const _SettingsItem({required this.icon, required this.label, this.onTap});
/// When [onToggle] is set the row renders a trailing switch instead of a
/// chevron, and [toggleValue] reflects its current state.
final bool? toggleValue;
final ValueChanged<bool>? onToggle;
const _SettingsItem({
required this.icon,
required this.label,
this.onTap,
this.toggleValue,
this.onToggle,
});
bool get isToggle => onToggle != null;
}
class _PhoneSpoiler extends StatefulWidget {
+2 -1
View File
@@ -6,6 +6,7 @@ import 'package:komet/main.dart';
import 'package:flutter/foundation.dart';
import 'package:material_symbols_icons/symbols.dart';
import '../../backend/modules/messages.dart';
import '../../core/utils/haptics.dart';
import '../../models/attachment.dart';
enum MessageType { text, attachment, voice, control }
@@ -329,7 +330,7 @@ class MessageBubble extends StatelessWidget {
return GestureDetector(
// TODO: действия с сообщением
onTap: () => print("test"),
onTap: () => Haptics.tap(),
child: Padding(
padding: EdgeInsets.only(
left: isMe ? 12 : 12,
+3
View File
@@ -11,6 +11,7 @@ import 'backend/modules/contacts.dart';
import 'backend/modules/messages.dart';
import 'core/storage/app_database.dart';
import 'core/storage/token_storage.dart';
import 'core/utils/haptics.dart';
import 'core/protocol/packet.dart';
import 'frontend/debug/fps_overlay_layer.dart';
import 'frontend/screens/auth/login_screen.dart';
@@ -44,6 +45,8 @@ void main() async {
await api.connect();
final initialLocale = await _loadInitialLocale();
await Haptics.load();
final prefs = await SharedPreferences.getInstance();
final initialFpsOverlay = prefs.getBool('dev_fps_overlay') ?? false;
runApp(