+ChangeNotifier Fixes
+Signout Fixes +
This commit is contained in:
@@ -25,28 +25,16 @@ class MessagingProviders extends ChangeNotifier {
|
||||
late final Account account;
|
||||
late final Databases database;
|
||||
late final Storage storage;
|
||||
late final Realtime realtime;
|
||||
late final RealtimeSubscription? subscription;
|
||||
late model.User? user;
|
||||
|
||||
// Auth Status
|
||||
AuthStatus? status;
|
||||
late model.User? user;
|
||||
|
||||
static MessagingProviders instance = MessagingProviders();
|
||||
|
||||
MessagingProviders() {
|
||||
_init();
|
||||
_loadUser();
|
||||
subscribe();
|
||||
}
|
||||
|
||||
subscribe() {
|
||||
subscription = realtime.subscribe(
|
||||
[
|
||||
'database.${Constants.instance.databaseId}.chats',
|
||||
'database.${Constants.instance.databaseId}.conversations'
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
_init() {
|
||||
@@ -57,7 +45,6 @@ class MessagingProviders extends ChangeNotifier {
|
||||
account = Account(client);
|
||||
database = Databases(client);
|
||||
storage = Storage(client);
|
||||
realtime = Realtime(client);
|
||||
}
|
||||
|
||||
_loadUser() async {
|
||||
@@ -66,22 +53,15 @@ class MessagingProviders extends ChangeNotifier {
|
||||
if (user != null) {
|
||||
status = AuthStatus.authenticated;
|
||||
await updateLastSeen();
|
||||
_autoLogin();
|
||||
NavigationService.instance.navigateToReplacement("home");
|
||||
}
|
||||
} 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;
|
||||
|
||||
@@ -105,9 +85,10 @@ class MessagingProviders extends ChangeNotifier {
|
||||
SnackBarService.instance.showSnackBarError(e.toString());
|
||||
//
|
||||
}
|
||||
notifyListeners();
|
||||
} on Exception catch (e) {
|
||||
SnackBarService.instance.showSnackBarError(e.toString());
|
||||
} finally {
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,22 +96,20 @@ class MessagingProviders extends ChangeNotifier {
|
||||
try {
|
||||
await updateLastSeen();
|
||||
await account.deleteSession(sessionId: 'current');
|
||||
user = null;
|
||||
// user = null;
|
||||
status = AuthStatus.notAuthenticated;
|
||||
// await onSuccess();
|
||||
await NavigationService.instance.navigateToReplacement("login");
|
||||
SnackBarService.instance.showSnackBarError("logged out successfully");
|
||||
await NavigationService.instance.navigateToReplacement("login");
|
||||
} 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> Function(String _uid) onSuccess) async {
|
||||
notifyListeners();
|
||||
try {
|
||||
user = await account.create(
|
||||
userId: ID.unique(), email: _email, password: _password, name: _name);
|
||||
@@ -149,12 +128,10 @@ class MessagingProviders extends ChangeNotifier {
|
||||
SnackBarService.instance.showSnackBarError(e.toString());
|
||||
//
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> createUserInDB(
|
||||
String _documentId, String _email, String _name, String _imageURL) async {
|
||||
notifyListeners();
|
||||
try {
|
||||
await database.createDocument(
|
||||
databaseId: Constants.instance.databaseId,
|
||||
@@ -184,7 +161,6 @@ class MessagingProviders extends ChangeNotifier {
|
||||
user = null;
|
||||
//
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> updateLastSeen() async {
|
||||
@@ -196,7 +172,7 @@ class MessagingProviders extends ChangeNotifier {
|
||||
);
|
||||
}
|
||||
|
||||
Stream<User> getUser(String userId) {
|
||||
Stream<User> getUser(String userId) async* {
|
||||
//
|
||||
var _userData = database.getDocument(
|
||||
databaseId: Constants.instance.databaseId,
|
||||
@@ -204,14 +180,14 @@ class MessagingProviders extends ChangeNotifier {
|
||||
documentId: userId,
|
||||
);
|
||||
|
||||
return _userData.asStream().map((_document) {
|
||||
updateLastSeen();
|
||||
yield* _userData.asStream().map((_document) {
|
||||
return User.fromData(_document);
|
||||
});
|
||||
}
|
||||
|
||||
Stream<List<User>> getAllUsers(String _searchText) async* {
|
||||
//
|
||||
// while (true) {
|
||||
List<User> _returnValue = [];
|
||||
if (_searchText.isNotEmpty) {
|
||||
var _usersData = database.listDocuments(
|
||||
@@ -234,12 +210,10 @@ class MessagingProviders extends ChangeNotifier {
|
||||
} else {
|
||||
yield* Stream.empty();
|
||||
}
|
||||
// }
|
||||
}
|
||||
|
||||
Stream<List<Conversations>> getUserConversation(String userId) async* {
|
||||
//
|
||||
// while (true) {
|
||||
List<dynamic> _chatId = [];
|
||||
List<dynamic> _details = [];
|
||||
List<dynamic> _lastModified = [];
|
||||
@@ -265,107 +239,53 @@ class MessagingProviders extends ChangeNotifier {
|
||||
return _returnValue;
|
||||
},
|
||||
);
|
||||
// }
|
||||
}
|
||||
|
||||
Future<List<Conversations>> getUserConversations(String userId) async {
|
||||
List<dynamic> _chatId = [];
|
||||
List<dynamic> _details = [];
|
||||
List<dynamic> _lastModified = [];
|
||||
List<Conversations> _returnValue = [];
|
||||
|
||||
var _document = await database.getDocument(
|
||||
databaseId: Constants.instance.databaseId,
|
||||
collectionId: Constants.instance.convCollectionId,
|
||||
documentId: userId.trim(),
|
||||
);
|
||||
|
||||
_chatId = _document.data['chatId'];
|
||||
_details = _document.data['details'];
|
||||
_lastModified = _document.data['lastModified'];
|
||||
|
||||
for (int i = 0; i < _chatId.length; i++) {
|
||||
_returnValue.add(
|
||||
Conversations.fromData(_chatId[i], _details[i], _lastModified[i]));
|
||||
}
|
||||
_returnValue.sort((a, b) => b.lastModified!.compareTo(a.lastModified!));
|
||||
|
||||
return _returnValue;
|
||||
// .then((document) {
|
||||
// _chatId = document.data['chatId'];
|
||||
// _details = document.data['details'];
|
||||
// _lastModified = document.data['lastModified'];
|
||||
|
||||
// for (int i = 0; i < _chatId.length; i++) {
|
||||
// _returnValue.add(
|
||||
// Conversations.fromData(_chatId[i], _details[i], _lastModified[i]));
|
||||
// }
|
||||
// _returnValue.sort((a, b) => b.lastModified!.compareTo(a.lastModified!));
|
||||
|
||||
// //conversationsController.add(_returnValue);
|
||||
// }).catchError((error) {
|
||||
// _returnValue;
|
||||
// // conversationsController.addError(error);
|
||||
// });
|
||||
|
||||
// return Future.delayed(Duration(microseconds: 300));
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Stream<Chats> getMessage(String _chatId) async* {
|
||||
//
|
||||
// while (true) {
|
||||
var _chat = database.getDocument(
|
||||
databaseId: Constants.instance.databaseId,
|
||||
collectionId: Constants.instance.chatsCollectionId,
|
||||
documentId: _chatId,
|
||||
);
|
||||
|
||||
yield* _chat.asStream().map(
|
||||
(_document) {
|
||||
return Chats.fromDocument(_document);
|
||||
},
|
||||
);
|
||||
// }
|
||||
}
|
||||
|
||||
Future<String> getConversationLink(var _linkId) async {
|
||||
//
|
||||
try {
|
||||
var _conversationLinkDetail = await database.getDocument(
|
||||
var _chat = database.getDocument(
|
||||
databaseId: Constants.instance.databaseId,
|
||||
collectionId: Constants.instance.conversationLinkId,
|
||||
documentId: _linkId,
|
||||
collectionId: Constants.instance.chatsCollectionId,
|
||||
documentId: _chatId,
|
||||
);
|
||||
yield* _chat.asStream().map(
|
||||
(_document) {
|
||||
return Chats.fromDocument(_document);
|
||||
},
|
||||
);
|
||||
notifyListeners();
|
||||
return _conversationLinkDetail.data['chatId'];
|
||||
} on AppwriteException catch (e) {
|
||||
notifyListeners();
|
||||
return '';
|
||||
e.message!;
|
||||
yield* Stream.empty();
|
||||
} on Exception catch (e) {
|
||||
e.toString();
|
||||
yield* Stream.empty();
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> newMesaage(Map<String, dynamic> _chat) async {
|
||||
//
|
||||
var chatId = ID.unique();
|
||||
await database.createDocument(
|
||||
databaseId: Constants.instance.databaseId,
|
||||
collectionId: Constants.instance.chatsCollectionId,
|
||||
documentId: chatId,
|
||||
data: _chat,
|
||||
);
|
||||
// else {}
|
||||
notifyListeners();
|
||||
return chatId;
|
||||
}
|
||||
|
||||
Future<void> sendMessage(
|
||||
String chatId, Map<String, dynamic> _messages) async {
|
||||
Future<void> sendMessage(String _chatId, Map<String, dynamic> _messages,
|
||||
String _newOrUpdate) async {
|
||||
//
|
||||
await database.updateDocument(
|
||||
databaseId: Constants.instance.databaseId,
|
||||
collectionId: Constants.instance.chatsCollectionId,
|
||||
documentId: chatId,
|
||||
data: _messages,
|
||||
);
|
||||
if (_newOrUpdate == 'new') {
|
||||
await database.createDocument(
|
||||
databaseId: Constants.instance.databaseId,
|
||||
collectionId: Constants.instance.chatsCollectionId,
|
||||
documentId: _chatId,
|
||||
data: _messages,
|
||||
);
|
||||
} else if (_newOrUpdate == 'update') {
|
||||
await database.updateDocument(
|
||||
databaseId: Constants.instance.databaseId,
|
||||
collectionId: Constants.instance.chatsCollectionId,
|
||||
documentId: _chatId,
|
||||
data: _messages,
|
||||
);
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@@ -394,7 +314,6 @@ class MessagingProviders extends ChangeNotifier {
|
||||
SnackBarService.instance.showSnackBarError(e.toString());
|
||||
//
|
||||
}
|
||||
notifyListeners();
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user