Skip to content

Commit

Permalink
-added login view model for handling login state.
Browse files Browse the repository at this point in the history
  • Loading branch information
FarhanRiaaz committed Jun 22, 2022
1 parent 9350711 commit 62a24a5
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 3 deletions.
3 changes: 0 additions & 3 deletions lib/data/local/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ class DBConstants {
DBConstants._();
// Store Name
static const String USER_INFO_STORE = 'userInfo';


// DB Name
static const DB_NAME = 'technologies.db';

// Fields
static const FIELD_ID = 'id';
}
Expand Down
37 changes: 37 additions & 0 deletions lib/domain/viewmodels/login_view_model.dart
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;
}
}
}
133 changes: 133 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,41 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.3.0"
build_config:
dependency: transitive
description:
name: build_config
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
build_daemon:
dependency: transitive
description:
name: build_daemon
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.0"
build_resolvers:
dependency: transitive
description:
name: build_resolvers
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.9"
build_runner:
dependency: "direct main"
description:
name: build_runner
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.11"
build_runner_core:
dependency: transitive
description:
name: build_runner_core
url: "https://pub.dartlang.org"
source: hosted
version: "7.2.3"
built_collection:
dependency: transitive
description:
Expand Down Expand Up @@ -71,6 +106,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.1"
checked_yaml:
dependency: transitive
description:
name: checked_yaml
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.1"
clock:
dependency: transitive
description:
Expand Down Expand Up @@ -177,6 +219,13 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
frontend_server_client:
dependency: transitive
description:
name: frontend_server_client
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.3"
get_it:
dependency: "direct main"
description:
Expand All @@ -191,13 +240,27 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
graphs:
dependency: transitive
description:
name: graphs
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
http:
dependency: "direct main"
description:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.13.4"
http_multi_server:
dependency: transitive
description:
name: http_multi_server
url: "https://pub.dartlang.org"
source: hosted
version: "3.2.1"
http_parser:
dependency: transitive
description:
Expand All @@ -219,13 +282,27 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.3"
io:
dependency: transitive
description:
name: io
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.3"
js:
dependency: transitive
description:
name: js
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.3"
json_annotation:
dependency: transitive
description:
name: json_annotation
url: "https://pub.dartlang.org"
source: hosted
version: "4.5.0"
lints:
dependency: transitive
description:
Expand Down Expand Up @@ -261,6 +338,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.0"
mime:
dependency: transitive
description:
name: mime
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
nested:
dependency: transitive
description:
Expand Down Expand Up @@ -345,6 +429,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.2"
pool:
dependency: transitive
description:
name: pool
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.1"
process:
dependency: transitive
description:
Expand All @@ -366,6 +457,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
pubspec_parse:
dependency: transitive
description:
name: pubspec_parse
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
sembast:
dependency: "direct main"
description:
Expand Down Expand Up @@ -429,6 +527,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
shelf:
dependency: transitive
description:
name: shelf
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.1"
shelf_web_socket:
dependency: transitive
description:
name: shelf_web_socket
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -462,6 +574,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
stream_transform:
dependency: transitive
description:
name: stream_transform
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
string_scanner:
dependency: transitive
description:
Expand Down Expand Up @@ -490,6 +609,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.8"
timing:
dependency: transitive
description:
name: timing
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
typed_data:
dependency: transitive
description:
Expand All @@ -511,6 +637,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
web_socket_channel:
dependency: transitive
description:
name: web_socket_channel
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.0"
win32:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ dependencies:
http: ^0.13.4
# A powerful Http client for Dart
dio: ^4.0.6
build_runner: ^2.1.11
# Dependency injection
get_it: ^7.2.0
injectable: ^1.5.3
Expand Down

0 comments on commit 62a24a5

Please sign in to comment.