233 lines
6.5 KiB
Dart
233 lines
6.5 KiB
Dart
import 'dart:io';
|
|
import 'package:appwrite/appwrite.dart';
|
|
import 'package:appwrite/models.dart' as model;
|
|
import 'package:flutter/material.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
import 'package:messaging_app/components/constants.dart';
|
|
import 'package:messaging_app/models/conversations.dart';
|
|
import 'package:messaging_app/models/users.dart';
|
|
import 'package:messaging_app/service/navigation_service.dart';
|
|
import 'package:messaging_app/service/snackbar_service.dart';
|
|
|
|
enum AuthStatus {
|
|
notAuthenticated,
|
|
authenticated,
|
|
authenticating,
|
|
userNotFound,
|
|
error,
|
|
}
|
|
|
|
class MessagingProviders extends ChangeNotifier {
|
|
//
|
|
Client client = Client();
|
|
late final Account account;
|
|
late final Databases database;
|
|
late final Storage storage;
|
|
|
|
// Auth Status
|
|
late AuthStatus status;
|
|
late model.User? user;
|
|
late model.Session session;
|
|
|
|
static MessagingProviders instance = MessagingProviders();
|
|
|
|
MessagingProviders() {
|
|
_init();
|
|
_loadUser();
|
|
}
|
|
|
|
_init() {
|
|
client.setEndpoint(endPoint).setProject(projectId).setSelfSigned();
|
|
account = Account(client);
|
|
database = Databases(client);
|
|
storage = Storage(client);
|
|
}
|
|
|
|
_loadUser() async {
|
|
try {
|
|
user = await account.get();
|
|
if (user != null) {
|
|
status = AuthStatus.authenticated;
|
|
_autoLogin();
|
|
}
|
|
} catch (e) {
|
|
status = AuthStatus.notAuthenticated;
|
|
} finally {
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
void _autoLogin() {
|
|
NavigationService.instance.navigateToReplacement("home");
|
|
}
|
|
|
|
void login(String _email, String _password) async {
|
|
//
|
|
notifyListeners();
|
|
try {
|
|
status != AuthStatus.error;
|
|
|
|
try {
|
|
status = AuthStatus.authenticating;
|
|
session = await account.createEmailPasswordSession(
|
|
email: _email, password: _password);
|
|
|
|
user = await account.get();
|
|
status = AuthStatus.authenticated;
|
|
|
|
SnackBarService.instance.showSnackBarSuccess("Welcome! ${user!.name}");
|
|
NavigationService.instance.navigateToReplacement("home");
|
|
} on AppwriteException catch (e) {
|
|
SnackBarService.instance.showSnackBarError(e.message!);
|
|
status = AuthStatus.error;
|
|
user = null;
|
|
//
|
|
} on Exception catch (e) {
|
|
SnackBarService.instance.showSnackBarError(e.toString());
|
|
//
|
|
}
|
|
notifyListeners();
|
|
} on Exception catch (e) {
|
|
SnackBarService.instance.showSnackBarError(e.toString());
|
|
}
|
|
}
|
|
|
|
void logout() async {
|
|
try {
|
|
await account.deleteSession(sessionId: 'current');
|
|
user = null;
|
|
status = AuthStatus.notAuthenticated;
|
|
// await onSuccess();
|
|
await NavigationService.instance.navigateToReplacement("login");
|
|
SnackBarService.instance.showSnackBarError("Logged out Successfully");
|
|
} on AppwriteException catch (e) {
|
|
SnackBarService.instance.showSnackBarError(e.message!);
|
|
} on Exception catch (e) {
|
|
SnackBarService.instance.showSnackBarError(e.toString());
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> createUser(String _email, String _password, String _name,
|
|
Future<void> onSuccess(String _uid)) async {
|
|
notifyListeners();
|
|
try {
|
|
user = await account.create(
|
|
userId: ID.unique(), email: _email, password: _password, name: _name);
|
|
|
|
status = AuthStatus.authenticated;
|
|
await onSuccess(user!.$id);
|
|
SnackBarService.instance.showSnackBarSuccess("Welcome! ${user!.name}");
|
|
NavigationService.instance.goBack();
|
|
NavigationService.instance.navigateToReplacement("home");
|
|
} on AppwriteException catch (e) {
|
|
status = AuthStatus.error;
|
|
user = null;
|
|
SnackBarService.instance.showSnackBarError(e.message!);
|
|
} on Exception catch (e) {
|
|
SnackBarService.instance.showSnackBarError(e.toString());
|
|
//
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> createUserInDB(
|
|
String _documentId, String _email, String _name, String _imageURL) async {
|
|
notifyListeners();
|
|
try {
|
|
await database.createDocument(
|
|
databaseId: databaseId,
|
|
collectionId: userCollectionId,
|
|
documentId: _documentId,
|
|
data: {
|
|
"name": _name,
|
|
"email": _email,
|
|
"lastSeen": DateTime.now().toUtc().toIso8601String(),
|
|
"image": _imageURL
|
|
},
|
|
permissions: [
|
|
Permission.read(Role.any()),
|
|
//Permission.update(Role.user(_documentId)),
|
|
],
|
|
);
|
|
|
|
status = AuthStatus.authenticated;
|
|
} on AppwriteException catch (e) {
|
|
SnackBarService.instance.showSnackBarError(e.message!);
|
|
status = AuthStatus.error;
|
|
user = null;
|
|
//
|
|
} on Exception catch (e) {
|
|
SnackBarService.instance.showSnackBarError(e.toString());
|
|
status = AuthStatus.error;
|
|
user = null;
|
|
//
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
Stream<Users> getUser(String userId) {
|
|
var _userData = database.getDocument(
|
|
databaseId: databaseId,
|
|
collectionId: userCollectionId,
|
|
documentId: userId,
|
|
);
|
|
|
|
return _userData.asStream().map((_snapshot) {
|
|
return Users.fromData(_snapshot);
|
|
});
|
|
}
|
|
|
|
Stream<List<Conversations>> getUserConversation(String userId) {
|
|
//
|
|
List<dynamic> _chatId = [];
|
|
List<dynamic> _details = [];
|
|
List<Conversations> _returnValue = [];
|
|
|
|
var _conversationData = database.getDocument(
|
|
databaseId: databaseId,
|
|
collectionId: convCollectionId,
|
|
documentId: userId.trim(),
|
|
);
|
|
|
|
return _conversationData.asStream().map(
|
|
(_snapshot) {
|
|
_chatId = _snapshot.data['chatId'];
|
|
_details = _snapshot.data['details'];
|
|
|
|
for (int i = 0; i < _chatId.length; i++) {
|
|
_returnValue.add(Conversations.fromData(_chatId[i], _details[i]));
|
|
}
|
|
return _returnValue;
|
|
},
|
|
);
|
|
}
|
|
|
|
uploadUserImage(XFile? file, String _uid) async {
|
|
notifyListeners();
|
|
try {
|
|
File _file = File(file!.path);
|
|
var _message = await storage.createFile(
|
|
bucketId: imageStorageBucket,
|
|
fileId: ID.unique(),
|
|
file: InputFile.fromPath(path: _file.path, filename: file.name),
|
|
permissions: [
|
|
Permission.read(Role.any()),
|
|
// Permission.update(Role.user(_uid))
|
|
],
|
|
);
|
|
|
|
return "$endPoint/storage/buckets/${_message.bucketId}/files/${_message.$id}/view?project=$projectId";
|
|
} on AppwriteException catch (e) {
|
|
SnackBarService.instance.showSnackBarError(e.message!);
|
|
//
|
|
} on Exception catch (e) {
|
|
status = AuthStatus.error;
|
|
user = null;
|
|
SnackBarService.instance.showSnackBarError(e.toString());
|
|
//
|
|
}
|
|
notifyListeners();
|
|
}
|
|
}
|