2023-03-13 12:55:39 +01:00
|
|
|
import 'package:clock/clock.dart';
|
2022-07-11 09:06:23 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:nc_photos/app_localizations.dart';
|
2023-07-17 09:35:45 +02:00
|
|
|
import 'package:nc_photos/entity/pref.dart';
|
2022-07-11 09:06:23 +02:00
|
|
|
import 'package:nc_photos/k.dart' as k;
|
|
|
|
import 'package:nc_photos/snack_bar_manager.dart';
|
|
|
|
|
|
|
|
class DoubleTapExitHandler {
|
|
|
|
factory DoubleTapExitHandler() => _inst;
|
|
|
|
|
|
|
|
DoubleTapExitHandler._();
|
|
|
|
|
|
|
|
/// Return if this back button event should actually exit the app
|
|
|
|
bool call() {
|
|
|
|
if (!Pref().isDoubleTapExitOr()) {
|
|
|
|
return true;
|
|
|
|
}
|
2023-03-13 12:55:39 +01:00
|
|
|
final now = clock.now().toUtc();
|
2022-07-11 09:06:23 +02:00
|
|
|
_lastBackButtonAt ??= now.subtract(const Duration(days: 1));
|
|
|
|
if (now.difference(_lastBackButtonAt!) < const Duration(seconds: 5)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
_lastBackButtonAt = now;
|
|
|
|
SnackBarManager().showSnackBar(SnackBar(
|
|
|
|
content: Text(L10n.global().doubleTapExitNotification),
|
|
|
|
duration: k.snackBarDurationShort,
|
|
|
|
));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static final _inst = DoubleTapExitHandler._();
|
|
|
|
|
|
|
|
DateTime? _lastBackButtonAt;
|
|
|
|
}
|