Skip to content

Commit 63f3e9e

Browse files
committed
feat(auth): implement viewmodels for auth flow (MVVM)
1 parent eb206d7 commit 63f3e9e

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import 'package:flutter/material.dart';
2+
3+
/// Base class to handle shared loading state
4+
class BaseViewModel extends ChangeNotifier {
5+
bool _isLoading = false;
6+
bool get isLoading => _isLoading;
7+
8+
void setLoading(bool value) {
9+
_isLoading = value;
10+
notifyListeners();
11+
}
12+
}
13+
14+
class LoginViewModel extends BaseViewModel {
15+
final emailCtrl = TextEditingController();
16+
final passCtrl = TextEditingController();
17+
bool obscurePass = true;
18+
19+
void togglePass() { obscurePass = !obscurePass; notifyListeners(); }
20+
21+
Future<bool> login() async {
22+
if (emailCtrl.text.isEmpty || passCtrl.text.isEmpty) return false;
23+
setLoading(true);
24+
// Mock API call
25+
await Future.delayed(const Duration(seconds: 1, milliseconds: 500));
26+
setLoading(false);
27+
return true; // Assume success
28+
}
29+
}
30+
31+
class RegisterViewModel extends BaseViewModel {
32+
final nameCtrl = TextEditingController();
33+
final emailCtrl = TextEditingController();
34+
final passCtrl = TextEditingController();
35+
final confirmPassCtrl = TextEditingController();
36+
bool obscurePass = true;
37+
38+
void togglePass() { obscurePass = !obscurePass; notifyListeners(); }
39+
40+
Future<bool> register() async {
41+
if (passCtrl.text != confirmPassCtrl.text) return false;
42+
setLoading(true);
43+
await Future.delayed(const Duration(seconds: 1, milliseconds: 500));
44+
setLoading(false);
45+
return true;
46+
}
47+
}
48+
49+
class ForgotPassViewModel extends BaseViewModel {
50+
final emailCtrl = TextEditingController();
51+
52+
Future<bool> sendCode() async {
53+
if (emailCtrl.text.isEmpty) return false;
54+
setLoading(true);
55+
await Future.delayed(const Duration(seconds: 1, milliseconds: 500));
56+
setLoading(false);
57+
return true;
58+
}
59+
}
60+
61+
class OtpViewModel extends BaseViewModel {
62+
List<String> digits = List.filled(6, "");
63+
64+
void updateDigit(int idx, String val) { digits[idx] = val; notifyListeners(); }
65+
66+
Future<bool> verify() async {
67+
if (digits.join().length < 6) return false;
68+
setLoading(true);
69+
await Future.delayed(const Duration(seconds: 1, milliseconds: 500));
70+
setLoading(false);
71+
return true;
72+
}
73+
}
74+
75+
class NewPassViewModel extends BaseViewModel {
76+
final passCtrl = TextEditingController();
77+
final confirmPassCtrl = TextEditingController();
78+
bool obscurePass = true;
79+
80+
void togglePass() { obscurePass = !obscurePass; notifyListeners(); }
81+
82+
Future<bool> updatePassword() async {
83+
if (passCtrl.text != confirmPassCtrl.text) return false;
84+
setLoading(true);
85+
await Future.delayed(const Duration(seconds: 1, milliseconds: 500));
86+
setLoading(false);
87+
return true;
88+
}
89+
}

0 commit comments

Comments
 (0)