Skip to content

Commit

Permalink
optimize signup logic (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ida631 authored Nov 12, 2024
1 parent d72d090 commit 9b93f3b
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 29 deletions.
35 changes: 32 additions & 3 deletions lib/presentation/settings/viewModels/settings_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:swiftcomp/presentation/settings/providers/feature_flag_provider.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:url_launcher/url_launcher_string.dart';
import 'package:launch_review/launch_review.dart';
import 'package:share/share.dart';


class SettingsViewModel extends ChangeNotifier {
final AuthUseCase authUseCase;
final UserUseCase userUserCase;
Expand Down Expand Up @@ -128,9 +130,36 @@ class SettingsViewModel extends ChangeNotifier {
}
}

void rateApp() {
LaunchReview.launch(
androidAppId: "com.banghuazhao.swiftcomp", iOSAppId: "1297825946");
void openAppStore() async {
if (kIsWeb) {
// Handle the web case if necessary, such as showing an error or a message.
print("App store link is not supported on web.");
return;
}

final Uri androidUrl = Uri.parse('https://play.google.com/store/apps/details?id=com.banghuazhao.swiftcomp');
final Uri iOSUrl = Uri.parse('https://apps.apple.com/app/id1297825946');

final Uri url = Platform.isAndroid ? androidUrl : iOSUrl;

if (await canLaunchUrl(url)) {
await launchUrl(url, mode: LaunchMode.externalApplication);
} else {
throw 'Could not open the app store URL';
}
}

void rateApp() async {
try {
await LaunchReview.launch(
androidAppId: "com.banghuazhao.swiftcomp",
iOSAppId: "1297825946",
writeReview: true,
);
} catch (e) {
// Use fallback to open the app store directly if `LaunchReview` fails
openAppStore();
}
}

Future<void> shareApp(BuildContext context) async {
Expand Down
19 changes: 19 additions & 0 deletions lib/presentation/settings/viewModels/signup_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class SignupViewModel extends ChangeNotifier {
String? _errorMessage;

String? get errorMessage => _errorMessage;
String? _loginErrorMessage;
String? get loginErrorMessage => _loginErrorMessage;

Future<User?> signup(String email, String password, String verificationCode,
{String? name}) async {
Expand Down Expand Up @@ -72,4 +74,21 @@ class SignupViewModel extends ChangeNotifier {
_isLoading = value;
notifyListeners();
}

Future<String?> login(String email,String password) async {
_isLoading = true;
_loginErrorMessage = null;
notifyListeners();

try {
final accessToken = await authUseCase.login(email, password);
return accessToken; // Successful login returns the access token
} catch (e) {
_loginErrorMessage = 'Login failed: ${e.toString()}';
return null;
} finally {
_isLoading = false;
notifyListeners();
}
}
}
21 changes: 12 additions & 9 deletions lib/presentation/settings/views/forget_password_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ForgetPasswordPage extends StatefulWidget {
class _ForgetPasswordPageState extends State<ForgetPasswordPage> {
final TextEditingController _emailController = TextEditingController();
final TextEditingController _confirmationCodeController = TextEditingController();
final TextEditingController _newPasswordController = TextEditingController();
final TextEditingController _confirmPasswordController = TextEditingController();
final _formKey = GlobalKey<FormState>();
bool isEmailValid = false;
Expand All @@ -24,7 +25,10 @@ class _ForgetPasswordPageState extends State<ForgetPasswordPage> {
@override
void initState() {
super.initState();
_emailController.addListener(_validateEmail);
_emailController.addListener(_validateEmail);//these functions to automatically run whenever the text in the associated input field changes
_newPasswordController.addListener(checkConfirmInput); //these functions to automatically run whenever the text in the associated input field changes
_confirmPasswordController.addListener(checkConfirmInput);
_confirmationCodeController.addListener(checkConfirmInput);
}

void _validateEmail() {
Expand Down Expand Up @@ -90,6 +94,7 @@ class _ForgetPasswordPageState extends State<ForgetPasswordPage> {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextFormField(
controller: _newPasswordController,
obscureText: viewModel.obscureTextNewPassword,
decoration: InputDecoration(
labelText: "New Password",
Expand Down Expand Up @@ -161,7 +166,7 @@ class _ForgetPasswordPageState extends State<ForgetPasswordPage> {
if (value == null || value.isEmpty) {
return 'Please confirm your password';
}
if (value != newPassword) {
if (value != _newPasswordController.text) {
return "The passwords do not match";
}
return null;
Expand Down Expand Up @@ -201,7 +206,6 @@ class _ForgetPasswordPageState extends State<ForgetPasswordPage> {
return null;
},
onChanged: (value) {
confirmCode = value;
checkConfirmInput();
},
),
Expand All @@ -214,7 +218,7 @@ class _ForgetPasswordPageState extends State<ForgetPasswordPage> {
enable: confirmEnable,
onPressed: () async {
await viewModel.confirmResetPassword(
_emailController.text, newPassword, confirmCode);
_emailController.text, _newPasswordController.text , _confirmationCodeController.text);
if (viewModel.errorMessage.isNotEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
Expand Down Expand Up @@ -286,11 +290,10 @@ class _ForgetPasswordPageState extends State<ForgetPasswordPage> {
void checkConfirmInput() {
setState(() {
confirmEnable = _emailController.text.isNotEmpty &&
(newPassword?.isNotEmpty ?? false) &&
(newPassword?.length ?? 0) >= 6 &&
_confirmPasswordController.text == newPassword &&
(confirmCode?.isNotEmpty ?? false) &&
(confirmCode?.length ?? 0) == 6;
(isEmailValid) &&
(_newPasswordController.text.isNotEmpty && _newPasswordController.text.length >= 6) &&
(_confirmPasswordController.text == _newPasswordController.text) &&
(_confirmationCodeController.text.isNotEmpty && _confirmationCodeController.text.length == 6);
});
}
}
10 changes: 3 additions & 7 deletions lib/presentation/settings/views/login_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class _LoginPageState extends State<NewLoginPage> {
);
},
child: Text(
'Reset Password',
'Forgot Password',
style: TextStyle(color: Colors.white, fontSize: 16),
),
),
Expand Down Expand Up @@ -259,12 +259,8 @@ class _LoginPageState extends State<NewLoginPage> {
MaterialPageRoute(builder: (context) => SignupPage()),
);

if (result != null && result is User) {
print(result.email);
setState(() {
email = result.email;
_emailController.text = email;
});
if (result == "sign up success") {
Navigator.pop(context, "Log in Success");
}
}
}
7 changes: 6 additions & 1 deletion lib/presentation/settings/views/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter_progress_hud/flutter_progress_hud.dart';
import 'package:swiftcomp/presentation/settings/views/user_profile_page.dart';
import 'package:launch_review/launch_review.dart';
import '../viewModels/settings_view_model.dart';
import 'feature_flag_page.dart';
import 'login_page.dart';
Expand Down Expand Up @@ -85,8 +86,12 @@ class _SettingsPageState extends State<SettingsPage> {
MoreRow(
title: "Rate this App",
leadingIcon: Icons.thumb_up_rounded,
onTap: viewModel.rateApp,
onTap: () {
print("Rate App button tapped"); // Debug print
viewModel.rateApp();
},
),

MoreRow(
title: "Share this App",
leadingIcon: Icons.share_rounded,
Expand Down
32 changes: 25 additions & 7 deletions lib/presentation/settings/views/sigup_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,39 @@ class _SignupFormState extends State<SignupForm> {
setState(() => isLoading = false);

if (user != null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("Sign-up complete! Welcome aboard!"),
duration: Duration(seconds: 5),
),
);
Navigator.pop(context, user);
// Attempt to login after successful sign-up
String? token = await viewModel.login(email, password);
if (token != null) {
// Successful login
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("Sign-up complete! You have been logged in."),
duration: Duration(seconds: 3),
),
);

// Navigate to the home screen
Navigator.pop(context, "sign up success");
} else {
// Sign-up succeeded, but login failed
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("Sign-up successful but login failed. Please try logging in manually."),
),
);
}
} else if (viewModel.errorMessage != null) {
// Sign-up failed
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(viewModel.errorMessage!)),
);
}
}
}




@override
Widget build(BuildContext context) {
final viewModel = Provider.of<SignupViewModel>(context);
Expand Down
2 changes: 1 addition & 1 deletion lib/presentation/settings/views/user_profile_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class UserProfilePage extends StatelessWidget {
},
style: ElevatedButton.styleFrom(
minimumSize: Size(MediaQuery.of(context).size.width * 0.5, 50),
backgroundColor: Colors.orange,
backgroundColor: Colors.orangeAccent,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies:
flutter_staggered_grid_view: ^0.4.1
device_info: ^2.0.3
package_info_plus: ^6.0.0
url_launcher: ^6.3.0
url_launcher: ^6.0.20
launch_review: ^3.0.1
share: ^2.0.4
linalg: ^0.4.0
Expand Down

0 comments on commit 9b93f3b

Please sign in to comment.