Recent Conversations Page, Conversations Page implemented'
This commit is contained in:
@@ -4,6 +4,7 @@ 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/chats.dart';
|
||||
import 'package:messaging_app/models/conversations.dart';
|
||||
import 'package:messaging_app/models/users.dart';
|
||||
import 'package:messaging_app/service/navigation_service.dart';
|
||||
@@ -25,9 +26,8 @@ class MessagingProviders extends ChangeNotifier {
|
||||
late final Storage storage;
|
||||
|
||||
// Auth Status
|
||||
late AuthStatus status;
|
||||
AuthStatus? status;
|
||||
late model.User? user;
|
||||
late model.Session session;
|
||||
|
||||
static MessagingProviders instance = MessagingProviders();
|
||||
|
||||
@@ -37,7 +37,10 @@ class MessagingProviders extends ChangeNotifier {
|
||||
}
|
||||
|
||||
_init() {
|
||||
client.setEndpoint(endPoint).setProject(projectId).setSelfSigned();
|
||||
client
|
||||
.setEndpoint(Constants.instance.endPoint)
|
||||
.setProject(Constants.instance.projectId)
|
||||
.setSelfSigned();
|
||||
account = Account(client);
|
||||
database = Databases(client);
|
||||
storage = Storage(client);
|
||||
@@ -48,6 +51,7 @@ class MessagingProviders extends ChangeNotifier {
|
||||
user = await account.get();
|
||||
if (user != null) {
|
||||
status = AuthStatus.authenticated;
|
||||
await updateLastSeen();
|
||||
_autoLogin();
|
||||
}
|
||||
} catch (e) {
|
||||
@@ -69,13 +73,14 @@ class MessagingProviders extends ChangeNotifier {
|
||||
|
||||
try {
|
||||
status = AuthStatus.authenticating;
|
||||
session = await account.createEmailPasswordSession(
|
||||
await account.createEmailPasswordSession(
|
||||
email: _email, password: _password);
|
||||
|
||||
user = await account.get();
|
||||
status = AuthStatus.authenticated;
|
||||
|
||||
SnackBarService.instance.showSnackBarSuccess("Welcome! ${user!.name}");
|
||||
await updateLastSeen();
|
||||
NavigationService.instance.navigateToReplacement("home");
|
||||
} on AppwriteException catch (e) {
|
||||
SnackBarService.instance.showSnackBarError(e.message!);
|
||||
@@ -109,7 +114,7 @@ class MessagingProviders extends ChangeNotifier {
|
||||
}
|
||||
|
||||
Future<void> createUser(String _email, String _password, String _name,
|
||||
Future<void> onSuccess(String _uid)) async {
|
||||
Future<void> Function(String _uid) onSuccess) async {
|
||||
notifyListeners();
|
||||
try {
|
||||
user = await account.create(
|
||||
@@ -118,6 +123,7 @@ class MessagingProviders extends ChangeNotifier {
|
||||
status = AuthStatus.authenticated;
|
||||
await onSuccess(user!.$id);
|
||||
SnackBarService.instance.showSnackBarSuccess("Welcome! ${user!.name}");
|
||||
await updateLastSeen();
|
||||
NavigationService.instance.goBack();
|
||||
NavigationService.instance.navigateToReplacement("home");
|
||||
} on AppwriteException catch (e) {
|
||||
@@ -136,8 +142,8 @@ class MessagingProviders extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
try {
|
||||
await database.createDocument(
|
||||
databaseId: databaseId,
|
||||
collectionId: userCollectionId,
|
||||
databaseId: Constants.instance.databaseId,
|
||||
collectionId: Constants.instance.userCollectionId,
|
||||
documentId: _documentId,
|
||||
data: {
|
||||
"name": _name,
|
||||
@@ -166,18 +172,51 @@ class MessagingProviders extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> updateLastSeen() async {
|
||||
await database.updateDocument(
|
||||
databaseId: Constants.instance.databaseId,
|
||||
collectionId: Constants.instance.userCollectionId,
|
||||
documentId: user!.$id,
|
||||
data: {"lastSeen": DateTime.now().toUtc().toIso8601String()},
|
||||
);
|
||||
}
|
||||
|
||||
Stream<Users> getUser(String userId) {
|
||||
var _userData = database.getDocument(
|
||||
databaseId: databaseId,
|
||||
collectionId: userCollectionId,
|
||||
databaseId: Constants.instance.databaseId,
|
||||
collectionId: Constants.instance.userCollectionId,
|
||||
documentId: userId,
|
||||
);
|
||||
|
||||
return _userData.asStream().map((_snapshot) {
|
||||
return Users.fromData(_snapshot);
|
||||
return _userData.asStream().map((_document) {
|
||||
return Users.fromData(_document);
|
||||
});
|
||||
}
|
||||
|
||||
Stream<List<Users>> getAllUsers(String _searchText) {
|
||||
List<Users> _returnValue = [];
|
||||
if (_searchText.isNotEmpty) {
|
||||
var _usersData = database.listDocuments(
|
||||
databaseId: Constants.instance.databaseId,
|
||||
collectionId: Constants.instance.userCollectionId,
|
||||
queries: [
|
||||
Query.contains("name", _searchText),
|
||||
],
|
||||
);
|
||||
|
||||
return _usersData.asStream().map(
|
||||
(_documentList) {
|
||||
for (var _document in _documentList.documents) {
|
||||
_returnValue.add(Users.fromData(_document));
|
||||
}
|
||||
return _returnValue;
|
||||
},
|
||||
);
|
||||
} else {
|
||||
return Stream.empty();
|
||||
}
|
||||
}
|
||||
|
||||
Stream<List<Conversations>> getUserConversation(String userId) {
|
||||
//
|
||||
List<dynamic> _chatId = [];
|
||||
@@ -185,8 +224,8 @@ class MessagingProviders extends ChangeNotifier {
|
||||
List<Conversations> _returnValue = [];
|
||||
|
||||
var _conversationData = database.getDocument(
|
||||
databaseId: databaseId,
|
||||
collectionId: convCollectionId,
|
||||
databaseId: Constants.instance.databaseId,
|
||||
collectionId: Constants.instance.convCollectionId,
|
||||
documentId: userId.trim(),
|
||||
);
|
||||
|
||||
@@ -203,12 +242,24 @@ class MessagingProviders extends ChangeNotifier {
|
||||
);
|
||||
}
|
||||
|
||||
Stream<Chats> getChat(String _chatId) {
|
||||
var _chat = database.getDocument(
|
||||
databaseId: Constants.instance.databaseId,
|
||||
collectionId: Constants.instance.chatsCollectionId,
|
||||
documentId: _chatId,
|
||||
);
|
||||
|
||||
return _chat.asStream().map((_document) {
|
||||
return Chats.fromData(_document);
|
||||
});
|
||||
}
|
||||
|
||||
uploadUserImage(XFile? file, String _uid) async {
|
||||
notifyListeners();
|
||||
try {
|
||||
File _file = File(file!.path);
|
||||
var _message = await storage.createFile(
|
||||
bucketId: imageStorageBucket,
|
||||
bucketId: Constants.instance.imageStorageBucket,
|
||||
fileId: ID.unique(),
|
||||
file: InputFile.fromPath(path: _file.path, filename: file.name),
|
||||
permissions: [
|
||||
@@ -217,7 +268,7 @@ class MessagingProviders extends ChangeNotifier {
|
||||
],
|
||||
);
|
||||
|
||||
return "$endPoint/storage/buckets/${_message.bucketId}/files/${_message.$id}/view?project=$projectId";
|
||||
return "${Constants.instance.endPoint}/storage/buckets/${_message.bucketId}/files/${_message.$id}/view?project=${Constants.instance.projectId}";
|
||||
} on AppwriteException catch (e) {
|
||||
SnackBarService.instance.showSnackBarError(e.message!);
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user