-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
-added login view model for handling login state.
- Loading branch information
1 parent
9350711
commit 62a24a5
Showing
4 changed files
with
171 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import 'package:technologies_login_example/di/components/service_locator.dart'; | ||
import 'package:technologies_login_example/domain/enums/view_state.dart'; | ||
import 'package:technologies_login_example/domain/models/login/login_request.dart'; | ||
import 'package:technologies_login_example/domain/models/login/login_response.dart'; | ||
import 'package:technologies_login_example/domain/repositories/user_repository.dart'; | ||
import 'package:technologies_login_example/domain/viewmodels/base_view_model.dart'; | ||
|
||
class LoginViewModel extends BaseViewModel { | ||
final UserRepository _userRepository = getIt<UserRepository>(); | ||
late User? user; | ||
|
||
Future<bool> login(String email, String password) async { | ||
setState(ViewState.BUSY); | ||
var success = await _userRepository | ||
.loginUser(LoginRequest(email: email, password: password)); | ||
setState(ViewState.IDLE); | ||
if (success.data?.user?.id != null) { | ||
_userRepository.insertUser(success.data!.user!); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
Future<bool> isUserLoggedIn() async { | ||
setState(ViewState.BUSY); | ||
var success = await _userRepository.getUser(); | ||
setState(ViewState.IDLE); | ||
if (success.first.id != null) { | ||
user = success.first; | ||
return true; | ||
} else { | ||
user = User(); | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters