Skip to content

Commit

Permalink
Made custom alert dialog for checkbox and written its complete test c…
Browse files Browse the repository at this point in the history
…ases
  • Loading branch information
MohitMaulekhi committed Dec 23, 2024
1 parent 1177aff commit 982c099
Show file tree
Hide file tree
Showing 7 changed files with 378 additions and 99 deletions.
2 changes: 1 addition & 1 deletion lang/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,6 @@
"Organisation on different server, logout and scan qr again": "異なるサーバー上の組織です。ログアウトして、QRコードを再びスキャンしてください。",
"Post was deleted if you had the rights!": "権限がある場合、投稿は削除されました!",
"Members": " メンバー ",
"Remember me": "私を覚えてますか",
"Remember me": "ログイン情報を保存",
"Login directly with": "直接ログイン"
}
20 changes: 14 additions & 6 deletions lib/view_model/pre_auth_view_models/login_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,9 @@ class LoginViewModel extends BaseModel {
result.data!['login'] as Map<String, dynamic>,
);
userConfig.updateUser(loggedInUser);
await secureStorage.write(key: "userEmail", value: this.email.text);
await secureStorage.write(
key: "userPassword",
value: this.password.text,
);
}
},
apiCallSuccessUpdateUI: () {
apiCallSuccessUpdateUI: () async {
// if user has not already joined any organization.
if (userConfig.currentUser.joinedOrganizations!.isEmpty) {
navigationService.removeAllAndPush(
Expand All @@ -178,6 +173,19 @@ class LoginViewModel extends BaseModel {
arguments: MainScreenArgs(mainScreenIndex: 0, fromSignUp: false),
);
}
try {
await secureStorage.write(
key: "userEmail",
value: this.email.text,
);
await secureStorage.write(
key: "userPassword",
value: this.password.text,
);
} catch (e) {
// Handle secure storage write failure
print("Failed to save credentials: $e");

Check warning on line 187 in lib/view_model/pre_auth_view_models/login_view_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/view_model/pre_auth_view_models/login_view_model.dart#L187

Added line #L187 was not covered by tests
}
},
onActionException: (e) async {
print('here');
Expand Down
19 changes: 13 additions & 6 deletions lib/view_model/pre_auth_view_models/signup_details_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,6 @@ class SignupDetailsViewModel extends BaseModel {
final User signedInUser = User.fromJson(
result.data!['signUp'] as Map<String, dynamic>,
);

await secureStorage.write(key: "userEmail", value: this.email.text);
await secureStorage.write(
key: "userPassword",
value: this.password.text,
);
final bool userSaved = await userConfig.updateUser(signedInUser);
final bool tokenRefreshed = await graphqlConfig.getToken() as bool;

Expand Down Expand Up @@ -208,6 +202,19 @@ class SignupDetailsViewModel extends BaseModel {
Routes.splashScreen,
);
}
try {
await secureStorage.write(
key: "userEmail",
value: this.email.text,
);
await secureStorage.write(
key: "userPassword",
value: this.password.text,
);
} catch (e) {
// Handle secure storage write failure
print("Failed to save credentials: $e");

Check warning on line 216 in lib/view_model/pre_auth_view_models/signup_details_view_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/view_model/pre_auth_view_models/signup_details_view_model.dart#L216

Added line #L216 was not covered by tests
}
}
}
},
Expand Down
106 changes: 21 additions & 85 deletions lib/views/after_auth_screens/app_settings/app_settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import 'package:talawa/services/size_config.dart';
import 'package:talawa/utils/app_localization.dart';
import 'package:talawa/view_model/after_auth_view_models/settings_view_models/app_setting_view_model.dart';
import 'package:talawa/views/base_view.dart';
import 'package:talawa/widgets/custom_alert_dialog_with_checkbox.dart';
import 'package:talawa/widgets/lang_switch.dart';
import 'package:talawa/widgets/raised_round_edge_button.dart';
import 'package:talawa/widgets/theme_switch.dart';

/// Widget representing the App Settings page.
Expand All @@ -24,9 +24,6 @@ class AppSettingsPage extends StatefulWidget {
}

class _AppSettingsPageState extends State<AppSettingsPage> {
/// This is used to check if to remember user credentials after logout.
bool _rememberMe = true;

/// Secure local storage instance.
final secureStorage = const FlutterSecureStorage();

Expand Down Expand Up @@ -298,88 +295,27 @@ class _AppSettingsPageState extends State<AppSettingsPage> {
? showDialog(
context: context,
builder: (context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return AlertDialog(
surfaceTintColor: Theme.of(context)
.colorScheme
.secondaryContainer,
title: Text(
AppLocalizations.of(context)!
.strictTranslate('Confirmation'),
style: Theme.of(context)
.textTheme
.headlineSmall!
.copyWith(fontWeight: FontWeight.w800),
),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
AppLocalizations.of(context)!.strictTranslate(
'Are you sure you want to logout?',
),
),
Row(
children: [
Text(
AppLocalizations.of(context)!
.strictTranslate("Remember me"),
),
Checkbox(
value: _rememberMe,
onChanged: (bool? val) {
setState(() {
_rememberMe = val!;
});
},
),
],
),
],
),
buttonPadding: EdgeInsets.symmetric(
horizontal: SizeConfig.screenWidth! * 0.05,
vertical: SizeConfig.screenHeight! * 0.05,
),
actions: [
RaisedRoundedButton(
key: const Key('Close'),
onTap: () {
navigationService.pop();
},
buttonLabel: AppLocalizations.of(context)!
.strictTranslate('Close'),
textColor: Colors.white,
backgroundColor: const Color(0xFF008A37),
width: SizeConfig.screenWidth! * 0.2,
height: SizeConfig.screenHeight! * 0.06,
),
RaisedRoundedButton(
key: const Key('Logout'),
onTap: () async {
await model.logout();
if (!_rememberMe) {
await secureStorage.delete(
key: "userEmail",
);
await secureStorage.delete(
key: "userPassword",
);
}
},
buttonLabel: AppLocalizations.of(context)!
.strictTranslate('Logout'),
textColor: const Color(0xFF008A37),
backgroundColor:
Theme.of(context).colorScheme.tertiary,
width: SizeConfig.screenWidth! * 0.2,
height: SizeConfig.screenHeight! * 0.06,
),
],
);
return CustomAlertDialogWithCheckbox(
success: (checkBoxVal) async {
await model.logout();
if (checkBoxVal != null && checkBoxVal == false) {
try {
await secureStorage.delete(

Check warning on line 303 in lib/views/after_auth_screens/app_settings/app_settings_page.dart

View check run for this annotation

Codecov / codecov/patch

lib/views/after_auth_screens/app_settings/app_settings_page.dart#L303

Added line #L303 was not covered by tests
key: "userEmail",
);
await secureStorage.delete(

Check warning on line 306 in lib/views/after_auth_screens/app_settings/app_settings_page.dart

View check run for this annotation

Codecov / codecov/patch

lib/views/after_auth_screens/app_settings/app_settings_page.dart#L306

Added line #L306 was not covered by tests
key: "userPassword",
);
} catch (e) {
print("Unable to delete stored value : $e");

Check warning on line 310 in lib/views/after_auth_screens/app_settings/app_settings_page.dart

View check run for this annotation

Codecov / codecov/patch

lib/views/after_auth_screens/app_settings/app_settings_page.dart#L310

Added line #L310 was not covered by tests
}
}
},
reverse: true,
dialogSubTitle: "Are you sure you want to logout?",
checkboxLabel: "Remember me",
successText: "Logout",
initialCheckboxValue: true,
);
},
)
Expand Down
1 change: 0 additions & 1 deletion lib/views/pre_auth_screens/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class _LoginState extends State<Login> {
return BaseView<LoginViewModel>(
onModelReady: (model) {
model.initialize();
model.fetchPrevUser();
},
builder: (context, model, child) {
return Scaffold(
Expand Down
147 changes: 147 additions & 0 deletions lib/widgets/custom_alert_dialog_with_checkbox.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import 'package:flutter/material.dart';
import 'package:talawa/locator.dart';
import 'package:talawa/services/size_config.dart';
import 'package:talawa/utils/app_localization.dart';
import 'package:talawa/widgets/raised_round_edge_button.dart';

/// This Class returns CustomAlertDialogWithCheckbox widget . Custom Alert Dialog with check box is a popup.
///
/// that appeared in the screen showing Alert message along with the event buttons.
/// This extends stateful widget that means it cannot change its state during the
/// runtime of a application.
/// This has a check box which wil rewuire user input to change.
class CustomAlertDialogWithCheckbox extends StatefulWidget {
const CustomAlertDialogWithCheckbox({
super.key,
this.successText = 'Confirm',
this.dialogTitle = 'Confirmation',
this.reverse = false,
this.secondaryButtonText = 'Close',
this.secondaryButtonTap,
required this.success,
required this.dialogSubTitle,
required this.checkboxLabel,
this.initialCheckboxValue = false,
this.onCheckboxChanged,
});

/// Indicates whether the order of action buttons should be reversed.
final bool reverse;

/// Function triggered upon tapping the primary action button.
final Function(bool?) success;

/// Function triggered upon tapping the secondary action button.
final Function()? secondaryButtonTap;

/// Text displayed on the primary action button.
final String successText;

/// Title displayed in the dialog (default value is 'Confirmation').
final String dialogTitle;

/// Subtitle or message content of the dialog.
final String dialogSubTitle;

/// Text displayed on the secondary action button (default value is 'Close').
final String secondaryButtonText;

/// Label for the checkbox.
final String checkboxLabel;

/// Initial value for the checkbox.
final bool initialCheckboxValue;

/// Callback for checkbox value change.
final ValueChanged<bool>? onCheckboxChanged;

@override
_CustomAlertDialogWithCheckboxState createState() =>
_CustomAlertDialogWithCheckboxState();
}

class _CustomAlertDialogWithCheckboxState
extends State<CustomAlertDialogWithCheckbox> {
late bool _checkboxValue;

@override
void initState() {
super.initState();
_checkboxValue = widget.initialCheckboxValue;
}

@override
Widget build(BuildContext context) {
final List<Widget> actions = [
RaisedRoundedButton(
key: Key(widget.secondaryButtonText),
onTap: () {
widget.secondaryButtonTap != null
? widget.secondaryButtonTap!()
: navigationService.pop();
},
buttonLabel: AppLocalizations.of(context)!
.strictTranslate(widget.secondaryButtonText),
textColor: Colors.white,
backgroundColor: const Color(0xFF008A37),
width: SizeConfig.screenWidth! * 0.2,
height: SizeConfig.screenHeight! * 0.06,
),
RaisedRoundedButton(
key: Key(widget.successText),
onTap: () => widget.success(_checkboxValue),
buttonLabel:
AppLocalizations.of(context)!.strictTranslate(widget.successText),
textColor: const Color(0xFF008A37),
backgroundColor: Theme.of(context).colorScheme.tertiary,
width: SizeConfig.screenWidth! * 0.2,
height: SizeConfig.screenHeight! * 0.06,
),
];

return AlertDialog(
surfaceTintColor: Theme.of(context).colorScheme.secondaryContainer,
title: Text(
AppLocalizations.of(context)!.strictTranslate(widget.dialogTitle),
style: Theme.of(context)
.textTheme
.headlineSmall!
.copyWith(fontWeight: FontWeight.w800),
),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
AppLocalizations.of(context)!
.strictTranslate(widget.dialogSubTitle),
),
Row(
children: [
Text(
AppLocalizations.of(context)!
.strictTranslate(widget.checkboxLabel),
),
Checkbox(
value: _checkboxValue,
onChanged: (bool? val) {
setState(() {
_checkboxValue = val!;
});
if (widget.onCheckboxChanged != null) {
widget.onCheckboxChanged?.call(_checkboxValue);

Check warning on line 132 in lib/widgets/custom_alert_dialog_with_checkbox.dart

View check run for this annotation

Codecov / codecov/patch

lib/widgets/custom_alert_dialog_with_checkbox.dart#L132

Added line #L132 was not covered by tests
}
},
),
],
),
],
),
buttonPadding: EdgeInsets.symmetric(
horizontal: SizeConfig.screenWidth! * 0.05,
vertical: SizeConfig.screenHeight! * 0.05,
),
actions: widget.reverse ? actions.reversed.toList() : actions,
);
}
}
Loading

0 comments on commit 982c099

Please sign in to comment.