Skip to content

Commit

Permalink
[mob][photos] Lockscreen Fixes (#2398)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashilkn authored Jul 11, 2024
2 parents b402c6a + 8117a29 commit 987cc1c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 25 deletions.
19 changes: 13 additions & 6 deletions mobile/lib/services/local_authentication_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ class LocalAuthenticationService {
) async {
if (await _isLocalAuthSupportedOnDevice()) {
AppLock.of(context)!.setEnabled(false);
final result = await requestAuthentication(context, infoMessage);
final result = await requestAuthentication(
context,
infoMessage,
isAuthenticatingForInAppChange: true,
);
AppLock.of(context)!.setEnabled(
await Configuration.instance.shouldShowLockScreen(),
);
Expand All @@ -39,15 +43,17 @@ class LocalAuthenticationService {
BuildContext context,
String? savedPin,
String? savedPassword, {
bool isOnOpeningApp = false,
bool isAuthenticatingOnAppLaunch = false,
bool isAuthenticatingForInAppChange = false,
}) async {
if (savedPassword != null) {
final result = await Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return LockScreenPassword(
isAuthenticating: true,
isOnOpeningApp: isOnOpeningApp,
isChangingLockScreenSettings: true,
isAuthenticatingForInAppChange: isAuthenticatingForInAppChange,
isAuthenticatingOnAppLaunch: isAuthenticatingOnAppLaunch,
authPass: savedPassword,
);
},
Expand All @@ -62,8 +68,9 @@ class LocalAuthenticationService {
MaterialPageRoute(
builder: (BuildContext context) {
return LockScreenPin(
isAuthenticating: true,
isOnOpeningApp: isOnOpeningApp,
isChangingLockScreenSettings: true,
isAuthenticatingForInAppChange: isAuthenticatingForInAppChange,
isAuthenticatingOnAppLaunch: isAuthenticatingOnAppLaunch,
authPin: savedPin,
);
},
Expand Down
29 changes: 20 additions & 9 deletions mobile/lib/ui/settings/lock_screen/lock_screen_password.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,24 @@ import "package:photos/utils/lock_screen_settings.dart";
class LockScreenPassword extends StatefulWidget {
const LockScreenPassword({
super.key,
this.isAuthenticating = false,
this.isOnOpeningApp = false,
this.isChangingLockScreenSettings = false,
this.isAuthenticatingOnAppLaunch = false,
this.isAuthenticatingForInAppChange = false,
this.authPass,
});

//Is false when setting a new password
final bool isAuthenticating;
final bool isOnOpeningApp;
/// [isChangingLockScreenSettings] Authentication required for changing lock screen settings.
/// Set to true when the app requires the user to authenticate before allowing
/// changes to the lock screen settings.
final bool isChangingLockScreenSettings;

/// [isAuthenticatingOnAppLaunch] Authentication required on app launch.
/// Set to true when the app requires the user to authenticate immediately upon opening.
final bool isAuthenticatingOnAppLaunch;

/// [isAuthenticatingForInAppChange] Authentication required for in-app changes (e.g., email, password).
/// Set to true when the app requires the to authenticate for sensitive actions like email, password changes.
final bool isAuthenticatingForInAppChange;
final String? authPass;
@override
State<LockScreenPassword> createState() => _LockScreenPasswordState();
Expand Down Expand Up @@ -155,7 +165,7 @@ class _LockScreenPasswordState extends State<LockScreenPassword> {
),
),
Text(
widget.isAuthenticating
widget.isChangingLockScreenSettings
? S.of(context).enterPassword
: S.of(context).setNewPassword,
textAlign: TextAlign.center,
Expand Down Expand Up @@ -201,7 +211,8 @@ class _LockScreenPasswordState extends State<LockScreenPassword> {
if (widget.authPass == base64Encode(hash)) {
await _lockscreenSetting.setInvalidAttemptCount(0);

widget.isOnOpeningApp
widget.isAuthenticatingOnAppLaunch ||
widget.isAuthenticatingForInAppChange
? Navigator.of(context).pop(true)
: Navigator.of(context).pushReplacement(
MaterialPageRoute(
Expand All @@ -210,7 +221,7 @@ class _LockScreenPasswordState extends State<LockScreenPassword> {
);
return true;
} else {
if (widget.isOnOpeningApp) {
if (widget.isAuthenticatingOnAppLaunch) {
invalidAttemptsCount++;
await _lockscreenSetting.setInvalidAttemptCount(invalidAttemptsCount);
if (invalidAttemptsCount > 4) {
Expand All @@ -224,7 +235,7 @@ class _LockScreenPasswordState extends State<LockScreenPassword> {
}

Future<void> _confirmPassword() async {
if (widget.isAuthenticating) {
if (widget.isChangingLockScreenSettings) {
await _confirmPasswordAuth(_passwordController.text);
return;
} else {
Expand Down
29 changes: 20 additions & 9 deletions mobile/lib/ui/settings/lock_screen/lock_screen_pin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,24 @@ import 'package:pinput/pinput.dart';
class LockScreenPin extends StatefulWidget {
const LockScreenPin({
super.key,
this.isAuthenticating = false,
this.isOnOpeningApp = false,
this.isChangingLockScreenSettings = false,
this.isAuthenticatingOnAppLaunch = false,
this.isAuthenticatingForInAppChange = false,
this.authPin,
});

//Is false when setting a new password
final bool isAuthenticating;
final bool isOnOpeningApp;
/// [isChangingLockScreenSettings] Authentication required for changing lock screen settings.
/// Set to true when the app requires the user to authenticate before allowing
/// changes to the lock screen settings.
final bool isChangingLockScreenSettings;

/// [isAuthenticatingOnAppLaunch] Authentication required on app launch.
/// Set to true when the app requires the user to authenticate immediately upon opening.
final bool isAuthenticatingOnAppLaunch;

/// [isAuthenticatingForInAppChange] Authentication required for in-app changes (e.g., email, password).
/// Set to true when the app requires the to authenticate for sensitive actions like email, password changes.
final bool isAuthenticatingForInAppChange;
final String? authPin;
@override
State<LockScreenPin> createState() => _LockScreenPinState();
Expand Down Expand Up @@ -62,7 +72,8 @@ class _LockScreenPinState extends State<LockScreenPin> {
if (widget.authPin == base64Encode(hash)) {
invalidAttemptsCount = 0;
await _lockscreenSetting.setInvalidAttemptCount(0);
widget.isOnOpeningApp
widget.isAuthenticatingOnAppLaunch ||
widget.isAuthenticatingForInAppChange
? Navigator.of(context).pop(true)
: Navigator.of(context).pushReplacement(
MaterialPageRoute(
Expand All @@ -81,7 +92,7 @@ class _LockScreenPinState extends State<LockScreenPin> {
isPinValid = false;
});

if (widget.isOnOpeningApp) {
if (widget.isAuthenticatingOnAppLaunch) {
invalidAttemptsCount++;
await _lockscreenSetting.setInvalidAttemptCount(invalidAttemptsCount);
if (invalidAttemptsCount > 4) {
Expand All @@ -93,7 +104,7 @@ class _LockScreenPinState extends State<LockScreenPin> {
}

Future<void> _confirmPin(String code) async {
if (widget.isAuthenticating) {
if (widget.isChangingLockScreenSettings) {
await confirmPinAuth(code);
return;
} else {
Expand Down Expand Up @@ -220,7 +231,7 @@ class _LockScreenPinState extends State<LockScreenPin> {
),
),
Text(
widget.isAuthenticating
widget.isChangingLockScreenSettings
? S.of(context).enterPin
: S.of(context).setNewPin,
style: textTheme.bodyBold,
Expand Down
4 changes: 3 additions & 1 deletion mobile/lib/utils/auth_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Future<bool> requestAuthentication(
BuildContext context,
String reason, {
bool isOpeningApp = false,
bool isAuthenticatingForInAppChange = false,
}) async {
Logger("AuthUtil").info("Requesting authentication");
await LocalAuthentication().stopAuthentication();
Expand All @@ -23,7 +24,8 @@ Future<bool> requestAuthentication(
context,
savedPin,
savedPassword,
isOnOpeningApp: isOpeningApp,
isAuthenticatingOnAppLaunch: isOpeningApp,
isAuthenticatingForInAppChange: isAuthenticatingForInAppChange,
);
} else {
return await LocalAuthentication().authenticate(
Expand Down

0 comments on commit 987cc1c

Please sign in to comment.