ente/lib/ui/tools/lock_screen.dart

118 lines
3.5 KiB
Dart
Raw Normal View History

2021-03-21 06:27:42 +00:00
import 'package:flutter/material.dart';
2021-03-21 08:32:10 +00:00
import 'package:logging/logging.dart';
2023-04-07 05:58:38 +00:00
import "package:photos/generated/l10n.dart";
2022-09-05 09:51:39 +00:00
import 'package:photos/ui/common/gradient_button.dart';
2022-07-01 14:39:02 +00:00
import 'package:photos/ui/tools/app_lock.dart';
2021-03-21 06:27:42 +00:00
import 'package:photos/utils/auth_util.dart';
class LockScreen extends StatefulWidget {
const LockScreen({Key? key}) : super(key: key);
2021-03-21 06:27:42 +00:00
@override
2022-07-03 09:45:00 +00:00
State<LockScreen> createState() => _LockScreenState();
2021-03-21 06:27:42 +00:00
}
class _LockScreenState extends State<LockScreen> with WidgetsBindingObserver {
2021-03-21 08:32:10 +00:00
final _logger = Logger("LockScreen");
bool _isShowingLockScreen = false;
bool _hasPlacedAppInBackground = false;
bool _hasAuthenticationFailed = false;
2021-03-21 08:32:10 +00:00
@override
void initState() {
_logger.info("initState");
2021-03-21 08:32:10 +00:00
_showLockScreen();
WidgetsBinding.instance.addObserver(this);
2021-03-21 08:32:10 +00:00
super.initState();
}
2021-03-21 06:27:42 +00:00
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
2022-06-06 06:19:20 +00:00
child: Column(
2022-06-11 08:23:52 +00:00
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Stack(
alignment: Alignment.center,
children: [
Opacity(
opacity: 0.2,
child: Image.asset('assets/loading_photos_background.png'),
2022-06-11 08:23:52 +00:00
),
SizedBox(
2023-05-10 03:42:02 +00:00
width: 180,
2022-09-05 09:51:39 +00:00
child: GradientButton(
2023-04-07 05:58:38 +00:00
text: S.of(context).unlock,
2022-09-05 09:51:39 +00:00
iconData: Icons.lock_open_outlined,
2022-06-11 08:23:52 +00:00
onTap: () async {
_showLockScreen();
},
2022-06-06 06:19:20 +00:00
),
2022-06-11 08:23:52 +00:00
),
],
),
],
),
2021-03-21 06:27:42 +00:00
),
);
}
2021-03-21 08:32:10 +00:00
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
_logger.info(state.toString());
if (state == AppLifecycleState.resumed) {
// This is triggered either when the lock screen is dismissed or when
// the app is brought to foreground
_hasPlacedAppInBackground = false;
if (!_hasAuthenticationFailed) {
// Show the lock screen again only if the app is resuming from the
// background, and not when the lock screen was explicitly dismissed
_showLockScreen();
} else {
_hasAuthenticationFailed = false; // Reset failure state
}
} else if (state == AppLifecycleState.paused ||
state == AppLifecycleState.inactive) {
// This is triggered either when the lock screen pops up or when
// the app is pushed to background
if (!_isShowingLockScreen) {
_hasPlacedAppInBackground = true;
_hasAuthenticationFailed = false; // reset failure state
}
}
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
2021-03-21 08:32:10 +00:00
Future<void> _showLockScreen() async {
_logger.info("Showing lock screen");
2021-03-21 08:32:10 +00:00
try {
_isShowingLockScreen = true;
final result = await requestAuthentication(
context,
2023-04-07 05:58:38 +00:00
S.of(context).authToViewYourMemories,
2022-06-11 08:23:52 +00:00
);
_isShowingLockScreen = false;
2021-03-21 08:32:10 +00:00
if (result) {
AppLock.of(context)!.didUnlock();
} else {
_logger.info("Dismissed");
if (!_hasPlacedAppInBackground) {
// Treat this as a failure only if user did not explicitly
// put the app in background
_hasAuthenticationFailed = true;
_logger.info("Authentication failed");
}
2021-03-21 08:32:10 +00:00
}
2021-08-09 08:21:18 +00:00
} catch (e, s) {
_logger.severe(e, s);
2021-03-21 08:32:10 +00:00
}
}
2021-03-21 06:27:42 +00:00
}