Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented MVVM on update_profile_page_view.dart #759

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/locator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:talawa/view_models/page_view_model/add_event_page_view_model.dar
import 'package:talawa/view_models/page_view_model/create_organization_page_viewModel.dart';
import 'package:talawa/view_models/page_view_model/events_page_view_model.dart';
import 'package:talawa/view_models/page_view_model/join_organization_view_model.dart';
import 'package:talawa/view_models/page_view_model/update_profile_page_viewModel.dart';
import 'package:talawa/view_models/page_view_model/profile_page_viewModel.dart';
import 'package:talawa/view_models/swtich_org_vm.dart';
import 'services/api_.dart';
Expand All @@ -16,7 +17,10 @@ GetIt locator = GetIt.instance;
void setupLocator() {
locator.registerLazySingleton(() => API());
locator.registerFactory(() => GraphQLConfiguration());

//Register ViewModels
locator.registerFactory(() => JoinOrgnizationViewModel());
locator.registerFactory(() => UpdateProfilePageViewModel());
locator.registerFactory(() => EventPageViewModel());
locator.registerFactory(() => AddEventPageViewModel());
locator.registerFactory(() => CreateOrganizationViewModel());
Expand Down
2 changes: 1 addition & 1 deletion lib/router.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'package:flutter/material.dart';
import 'package:talawa/routing_constants.dart';
import 'package:talawa/views/pages/_pages.dart';
import 'package:talawa/views/pages/home_page.dart';
import 'package:talawa/views/pages/login_signup/login_page.dart';
import 'package:talawa/views/pages/login_signup/register_page.dart';
import 'package:talawa/views/pages/login_signup/set_url_page.dart';
import 'package:talawa/views/pages/newsfeed/add_post.dart';
import 'package:talawa/views/pages/newsfeed/news_article.dart';
import 'package:talawa/views/pages/organization/Create%20Organization/create_organization_view.dart';
import 'package:talawa/views/pages/organization/join_organization.dart';
import 'package:talawa/views/pages/organization/profile_page.dart';
import 'package:talawa/views/pages/organization/switch_org_page.dart';

Expand Down
132 changes: 132 additions & 0 deletions lib/view_models/page_view_model/update_profile_page_viewModel.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:image_picker/image_picker.dart';
import 'package:persistent_bottom_nav_bar/persistent-tab-view.dart';
import 'package:talawa/controllers/auth_controller.dart';
import 'package:graphql/utilities.dart' show multipartFileFrom;
import 'package:talawa/enums/exception_type.dart';
import 'package:talawa/enums/image_from.dart';
import 'package:talawa/enums/viewstate.dart';
import 'package:talawa/services/exception.dart';
import 'package:talawa/services/queries_.dart';
import 'package:talawa/utils/custom_toast.dart';
import 'package:talawa/utils/gql_client.dart';
import 'package:talawa/view_models/base_model.dart';
import 'package:talawa/view_models/vm_register.dart';
import 'package:talawa/views/pages/organization/profile_page.dart';

import '../../locator.dart';

class UpdateProfilePageViewModel extends BaseModel {
List _userDetails = [];
File _profileImage;
final Queries _updateProfileQuery = Queries();
RegisterViewModel _userModel = RegisterViewModel();
final AuthController _authController = AuthController();
GraphQLConfiguration graphQLConfiguration = locator<GraphQLConfiguration>();
BuildContext _viewContext;

List get userDetails => _userDetails;
File get userProfileImage => _profileImage;
RegisterViewModel get userModel => _userModel;

initialise(List userDetailsFromWidget, BuildContext context) {
_viewContext = context;
_userDetails = userDetailsFromWidget;
}

setUserFirstName(String firstName) {
_userModel.firstName = firstName;
}

setUserEmail(String email) {
_userModel.email = email;
}

setUserLastname(String lastname) {
_userModel.firstName = lastname;
}

setProfileImage(File file) {
_profileImage = file;
notifyListeners();
}

//get image from camera and gallery based on the enum passed
imgFrom({From pickFrom = From.none}) async {
File pickImageFile;
if (pickFrom != From.none) {
final PickedFile selectedImage = await ImagePicker().getImage(
source: pickFrom == From.camera
? ImageSource.camera
: ImageSource.gallery);
pickImageFile = File(selectedImage.path);
_profileImage = pickImageFile;
notifyListeners();
}
}

//Function called when the user update without the image
updateProfile() async {
setState(ViewState.busy);

final GraphQLClient _client = graphQLConfiguration.authClient();
QueryResult result;
Map<String, dynamic> _variables = {};

if (_profileImage != null) {
final img = await multipartFileFrom(_profileImage);
_variables = {
'file': img,
"firstName": _userModel.firstName,
"lastName": _userModel.lastName,
"email": _userDetails[0]['email'] == _userModel.email
? null
: _userModel.email,
};
} else {
_variables = {
"firstName": _userModel.firstName,
"lastName": _userModel.lastName,
"email": _userDetails[0]['email'] == _userModel.email
? null
: _userModel.email,
};
}

result = await _client.mutate(
MutationOptions(
documentNode: gql(_updateProfileQuery.updateUserProfile()),
variables: _variables,
),
);

if (result.hasException) {
final ExceptionType exceptionType = retrieveExceptionType(result);
if (exceptionType == ExceptionType.accesstokenException) {
_authController.getNewToken();
return updateProfile();
} else {
setState(ViewState.idle);
if (result.exception.clientException != null) {
CustomToast.exceptionToast(
msg: result.exception.clientException.message);
} else {
CustomToast.exceptionToast(
msg: result.exception.graphqlErrors.first.message);
}
return;
}
}
if (!result.loading) {
setState(ViewState.idle);
CustomToast.sucessToast(msg: 'Profile Updated');
Navigator.of(_viewContext).popUntil(ModalRoute.withName("/"));
pushNewScreen(
_viewContext,
screen: const ProfilePage(),
);
}
}
}
2 changes: 1 addition & 1 deletion lib/views/pages/_pages.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export 'home_page.dart';
export 'login_signup/login_page.dart';
export 'organization/join_organization.dart';
export 'organization/join_organization_view.dart';
export 'organization/profile_page.dart';
2 changes: 1 addition & 1 deletion lib/views/pages/login_signup/register_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import 'package:talawa/view_models/vm_register.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:talawa/services/preferences.dart';
import 'package:talawa/model/token.dart';
import 'package:talawa/views/pages/organization/join_organization.dart';
import 'package:talawa/views/pages/organization/join_organization_view.dart';
import 'package:graphql/utilities.dart' show multipartFileFrom;

//pubspec packages are called here
Expand Down
6 changes: 3 additions & 3 deletions lib/views/pages/organization/profile_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import 'package:talawa/utils/ui_scaling.dart';
import 'package:talawa/utils/uidata.dart';
import 'package:talawa/view_models/page_view_model/profile_page_viewModel.dart';
import 'package:talawa/views/base_view.dart';
import 'package:talawa/views/pages/organization/join_organization.dart';
import 'package:talawa/views/pages/organization/update_profile_page.dart';
import 'package:talawa/views/pages/organization/join_organization_view.dart';
import 'package:talawa/views/pages/organization/update_profile_page_view.dart';
import 'package:talawa/views/widgets/about_tile.dart';
import 'package:persistent_bottom_nav_bar/persistent-tab-view.dart';
import 'package:talawa/views/pages/organization/organization_settings.dart';
Expand Down Expand Up @@ -128,7 +128,7 @@ class ProfilePage extends StatelessWidget {
onTap: () {
pushNewScreen(
context,
screen: UpdateProfilePage(
screen: UpdateProfileView(
userDetails: model.userDetails,
),
);
Expand Down
Loading