Commit 84c53526 authored by Karunarathna K.M.D.Y.K's avatar Karunarathna K.M.D.Y.K

Merge branch 'IT19978666' into 'master'

Added providers

See merge request !8
parents 19921fbe 9768cd8a
This source diff could not be displayed because it is too large. You can view the blob instead.
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:govimithura/models/error_model.dart';
import 'package:govimithura/utils/utils.dart';
import '../models/auth_model.dart';
import '../models/user_model.dart';
import '../services/auth_service.dart';
import '../services/user_service.dart';
class AuthenticationProvider extends ChangeNotifier {
AuthService authService = AuthService();
UserModel userModel = UserModel();
AuthModel authModel = AuthModel();
bool isOpen = false;
Future<bool> login() async {
bool success = await authService.login(authModel).then((value) => value);
if (success) {
await getCurentUserModel();
}
return success;
}
Future<bool> register() async {
userModel.authModel = authModel;
return await authService.register(userModel.authModel!).then(
(value) {
if (value != null) {
userModel.authModel = value;
UserService.addUser(userModel);
return true;
} else {
return false;
}
},
);
}
Future<void> signOut() async {
await authService.signOut();
}
bool isLoggedIn() {
return authService.isLoggedIn();
}
Future<UserModel?> getCurentUserModel() async {
if (authService.isLoggedIn()) {
return await UserService.getCurentUser().then((value) {
if (value != null) {
Map<String, dynamic> user = value.docs.first.data();
authModel.email = user['email'];
authModel.userType = user['user_type'];
authModel.uid = user['uid'];
userModel.authModel = authModel;
userModel.firstName = user['first_name'];
userModel.lastName = user['last_name'];
userModel.profilePic = user['profilePic'];
notifyListeners();
return userModel;
}
return null;
});
}
return null;
}
Future<bool> updateSingleField(String key, String value) async {
return await UserService.updateSingleField(key, value)
.then((success) async {
if (success) {
if (key == 'profilePic') {
getCurrentUser()!.updatePhotoURL(value);
}
if (key == 'first_name' || key == 'last_name') {
await getCurentUserModel();
getCurrentUser()!.updateDisplayName(
"${userModel.firstName} ${userModel.lastName}");
notifyListeners();
}
return true;
} else {
return false;
}
});
}
Future<bool> forgetPassword(BuildContext context) async {
var response = AuthService.forgetPassword(authModel.email);
if (response != null) {
return true;
} else {
Utils.showSnackBar(context, ErrorModel.errorMessage);
return false;
}
}
User? getCurrentUser() {
return FirebaseAuth.instance.currentUser;
}
setFirstName(String firstName) {
userModel.setFirstName = firstName;
notifyListeners();
}
setLastName(String lastName) {
userModel.setLastName = lastName;
notifyListeners();
}
setPassWord(String password) {
authModel.password = password;
notifyListeners();
}
setConfirmPassword(String confirmPassword) {
authModel.confirmPassword = confirmPassword;
notifyListeners();
}
setEmail(String email) {
authModel.email = email;
notifyListeners();
}
setIsOpen(bool isOpen) {
this.isOpen = isOpen;
notifyListeners();
}
}
import 'package:flutter/material.dart';
import 'package:govimithura/models/entity_model.dart';
import 'package:govimithura/models/error_model.dart';
import 'package:govimithura/services/disease_service.dart';
import 'package:govimithura/utils/utils.dart';
class DiseaseProvider extends ChangeNotifier {
EntityModel leafEntity = EntityModel();
EntityModel diseaseEntity = EntityModel();
Future<void> getLeafsById(int id, int diseaseId, BuildContext context) async {
await DiseaseService.getLeafsById(id).then((value) {
if (value != null) {
if (value.docs.isNotEmpty) {
leafEntity = EntityModel.fromMap(value.docs.first.data());
getDiseaseById(value.docs.first.id, diseaseId, context);
}
} else {
Utils.showSnackBar(context, ErrorModel.errorMessage);
}
});
notifyListeners();
}
Future<void> getDiseaseById(
String leafRef, int id, BuildContext context) async {
await DiseaseService.getDiseaseById(leafRef, id).then((value) {
if (value != null) {
if (value.docs.isNotEmpty) {
diseaseEntity = EntityModel.fromMap(value.docs.first.data());
}
} else {
Utils.showSnackBar(context, ErrorModel.errorMessage);
}
});
notifyListeners();
}
}
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:govimithura/models/error_model.dart';
import 'package:govimithura/providers/img_util_provider.dart';
import 'package:govimithura/services/storage_service.dart';
import 'package:govimithura/utils/utils.dart';
class StorageProvider extends ChangeNotifier {
final ImageUtilProvider? pImage;
StorageProvider({this.pImage});
Future<String?> uploadImage(BuildContext context) async {
if (pImage!.imagePath != null) {
return await StorageService.uploadImage(File(pImage!.imagePath!)).then(
(value) {
if (value != null) {
pImage!.setImageUrl(value);
return value;
} else {
Utils.showSnackBar(context, ErrorModel.errorMessage);
}
return null;
},
);
}
return null;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment