58 lines
1.9 KiB
Dart
58 lines
1.9 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:tugas_akhir_supabase/services/session_manager.dart';
|
|
|
|
/// Widget yang mendeteksi interaksi pengguna dan memperbarui timestamp aktivitas
|
|
/// Gunakan widget ini di dalam layar atau komponen yang membutuhkan deteksi interaksi
|
|
/// tetapi tidak secara otomatis terdeteksi oleh GestureDetector global di main.dart
|
|
class InteractionDetector extends StatelessWidget {
|
|
/// Widget anak yang akan dibungkus oleh detektor interaksi
|
|
final Widget child;
|
|
|
|
/// Apakah harus mencetak log debug saat interaksi terdeteksi
|
|
final bool enableLogging;
|
|
|
|
const InteractionDetector({
|
|
super.key,
|
|
required this.child,
|
|
this.enableLogging = false,
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return GestureDetector(
|
|
onTap: _updateInteraction,
|
|
onPanDown: (_) => _updateInteraction(),
|
|
onScaleStart: (_) => _updateInteraction(),
|
|
onLongPress: _updateInteraction,
|
|
behavior: HitTestBehavior.translucent,
|
|
child: Listener(
|
|
onPointerDown: (_) => _updateInteraction(),
|
|
onPointerMove: (_) => _updateInteraction(),
|
|
behavior: HitTestBehavior.translucent,
|
|
child: Focus(
|
|
onFocusChange: (hasFocus) {
|
|
if (hasFocus) _updateInteraction();
|
|
},
|
|
child: child,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/// Memperbarui timestamp interaksi pengguna terakhir
|
|
void _updateInteraction() {
|
|
if (enableLogging) {
|
|
debugPrint('InteractionDetector: User interaction detected');
|
|
}
|
|
SessionManager.updateLastUserInteraction();
|
|
}
|
|
}
|
|
|
|
/// Extension untuk memberikan metode mudah untuk memperbarui timestamp interaksi
|
|
extension InteractionUpdater on Widget {
|
|
/// Membungkus widget dengan InteractionDetector
|
|
Widget withInteractionDetection({bool enableLogging = false}) {
|
|
return InteractionDetector(enableLogging: enableLogging, child: this);
|
|
}
|
|
}
|