Add Android TV admin unlock and focus-driven controls
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../../data/services/sync_service.dart';
|
||||
@@ -6,6 +9,7 @@ import '../../data/services/sound_service.dart';
|
||||
import '../../core/enums.dart';
|
||||
import '../../core/sacred_tokens.dart';
|
||||
import '../../providers.dart';
|
||||
import '../admin/admin_screen.dart';
|
||||
import 'main_screen.dart';
|
||||
import 'adzan_screen.dart';
|
||||
import 'iqomah_screen.dart';
|
||||
@@ -23,14 +27,40 @@ class HomeView extends ConsumerStatefulWidget {
|
||||
}
|
||||
|
||||
class _HomeViewState extends ConsumerState<HomeView> {
|
||||
static const List<LogicalKeyboardKey> _adminUnlockSequence = [
|
||||
LogicalKeyboardKey.arrowUp,
|
||||
LogicalKeyboardKey.arrowUp,
|
||||
LogicalKeyboardKey.arrowDown,
|
||||
LogicalKeyboardKey.arrowDown,
|
||||
LogicalKeyboardKey.arrowLeft,
|
||||
LogicalKeyboardKey.arrowRight,
|
||||
LogicalKeyboardKey.arrowLeft,
|
||||
LogicalKeyboardKey.arrowRight,
|
||||
LogicalKeyboardKey.select,
|
||||
];
|
||||
|
||||
final FocusNode _homeFocusNode = FocusNode(debugLabel: 'home_tv_root');
|
||||
final List<LogicalKeyboardKey> _recentKeys = [];
|
||||
Timer? _comboResetTimer;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
_checkAutoSync();
|
||||
if (mounted) {
|
||||
_homeFocusNode.requestFocus();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_comboResetTimer?.cancel();
|
||||
_homeFocusNode.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _checkAutoSync() async {
|
||||
final schedule = ref.read(todayScheduleProvider);
|
||||
if (schedule == null) {
|
||||
@@ -45,6 +75,66 @@ class _HomeViewState extends ConsumerState<HomeView> {
|
||||
}
|
||||
}
|
||||
|
||||
KeyEventResult _handleTvKey(FocusNode node, KeyEvent event) {
|
||||
if (event is! KeyDownEvent) return KeyEventResult.ignored;
|
||||
|
||||
final key = event.logicalKey;
|
||||
if (!_isComboKey(key)) {
|
||||
_resetCombo();
|
||||
return KeyEventResult.ignored;
|
||||
}
|
||||
|
||||
_comboResetTimer?.cancel();
|
||||
_comboResetTimer = Timer(const Duration(seconds: 3), _resetCombo);
|
||||
|
||||
_recentKeys.add(key);
|
||||
if (_recentKeys.length > _adminUnlockSequence.length) {
|
||||
_recentKeys.removeAt(0);
|
||||
}
|
||||
|
||||
if (_matchesUnlockSequence()) {
|
||||
_resetCombo();
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
||||
if (!mounted) return;
|
||||
await Navigator.of(context).push(
|
||||
MaterialPageRoute(builder: (_) => const AdminScreen()),
|
||||
);
|
||||
if (mounted) {
|
||||
_homeFocusNode.requestFocus();
|
||||
}
|
||||
});
|
||||
return KeyEventResult.handled;
|
||||
}
|
||||
|
||||
return KeyEventResult.ignored;
|
||||
}
|
||||
|
||||
bool _isComboKey(LogicalKeyboardKey key) {
|
||||
return key == LogicalKeyboardKey.arrowUp ||
|
||||
key == LogicalKeyboardKey.arrowDown ||
|
||||
key == LogicalKeyboardKey.arrowLeft ||
|
||||
key == LogicalKeyboardKey.arrowRight ||
|
||||
key == LogicalKeyboardKey.select ||
|
||||
key == LogicalKeyboardKey.enter;
|
||||
}
|
||||
|
||||
bool _matchesUnlockSequence() {
|
||||
if (_recentKeys.length != _adminUnlockSequence.length) return false;
|
||||
|
||||
for (var i = 0; i < _adminUnlockSequence.length; i++) {
|
||||
final current = _recentKeys[i] == LogicalKeyboardKey.enter
|
||||
? LogicalKeyboardKey.select
|
||||
: _recentKeys[i];
|
||||
if (current != _adminUnlockSequence[i]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void _resetCombo() {
|
||||
_comboResetTimer?.cancel();
|
||||
_recentKeys.clear();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// Audio trigger listener
|
||||
@@ -101,44 +191,49 @@ class _HomeViewState extends ConsumerState<HomeView> {
|
||||
|
||||
final isSimulating = ref.watch(mockTimeOffsetProvider) != Duration.zero;
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: SacredColors.background,
|
||||
body: Stack(
|
||||
children: [
|
||||
AnimatedSwitcher(
|
||||
duration: const Duration(milliseconds: 800),
|
||||
transitionBuilder: (child, animation) {
|
||||
return FadeTransition(opacity: animation, child: child);
|
||||
},
|
||||
child: screen,
|
||||
),
|
||||
|
||||
if (isSimulating)
|
||||
Positioned(
|
||||
right: 64,
|
||||
bottom: 64,
|
||||
child: ElevatedButton.icon(
|
||||
onPressed: () {
|
||||
ref.read(mockTimeOffsetProvider.notifier).state = Duration.zero;
|
||||
},
|
||||
icon: const Icon(Icons.cancel, color: Colors.white),
|
||||
label: const Text(
|
||||
'BATALKAN SIMULASI',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
letterSpacing: 2,
|
||||
color: Colors.white,
|
||||
return Focus(
|
||||
autofocus: true,
|
||||
focusNode: _homeFocusNode,
|
||||
onKeyEvent: _handleTvKey,
|
||||
child: Scaffold(
|
||||
backgroundColor: SacredColors.background,
|
||||
body: Stack(
|
||||
children: [
|
||||
AnimatedSwitcher(
|
||||
duration: const Duration(milliseconds: 800),
|
||||
transitionBuilder: (child, animation) {
|
||||
return FadeTransition(opacity: animation, child: child);
|
||||
},
|
||||
child: screen,
|
||||
),
|
||||
|
||||
if (isSimulating)
|
||||
Positioned(
|
||||
right: 64,
|
||||
bottom: 64,
|
||||
child: ElevatedButton.icon(
|
||||
onPressed: () {
|
||||
ref.read(mockTimeOffsetProvider.notifier).state = Duration.zero;
|
||||
},
|
||||
icon: const Icon(Icons.cancel, color: Colors.white),
|
||||
label: const Text(
|
||||
'BATALKAN SIMULASI',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
letterSpacing: 2,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.red.shade800,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
|
||||
elevation: 10,
|
||||
),
|
||||
),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.red.shade800,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
|
||||
elevation: 10,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user