migrate chatty

This commit is contained in:
Salvatore Giordano
2021-05-18 17:15:37 +02:00
parent 33622093e0
commit bc655e0dc4
27 changed files with 181 additions and 159 deletions
@@ -1,7 +1,7 @@
import 'package:stream_chatter/domain/models/auth_user.dart';
abstract class AuthRepository {
Future<AuthUser> getAuthUser();
Future<AuthUser?> getAuthUser();
Future<AuthUser> signIn();
Future<void> logout();
}
@@ -1,5 +1,5 @@
import 'dart:io';
abstract class ImagePickerRepository {
Future<File> pickImage();
Future<File?> pickImage();
}
@@ -4,10 +4,17 @@ import 'package:stream_chatter/data/image_picker_repository.dart';
class ImagePickerImpl extends ImagePickerRepository {
@override
Future<File> pickImage() async {
Future<File?> pickImage() async {
final picker = ImagePicker();
final pickedFile =
await picker.getImage(source: ImageSource.gallery, maxWidth: 400);
final pickedFile = await picker.getImage(
source: ImageSource.gallery,
maxWidth: 400,
);
if (pickedFile == null) {
return null;
}
return File(pickedFile.path);
}
}
@@ -8,7 +8,7 @@ class StreamApiLocalImpl extends StreamApiRepository {
final StreamChatClient _client;
@override
Future<ChatUser> connectUser(ChatUser user, String token) async {
Future<ChatUser> connectUser(ChatUser user, String? token) async {
Map<String, dynamic> extraData = {};
if (user.image != null) {
extraData['image'] = user.image;
@@ -18,7 +18,7 @@ class StreamApiLocalImpl extends StreamApiRepository {
}
await _client.disconnect();
await _client.connectUser(
User(id: user.id, extraData: extraData),
User(id: user.id!, extraData: extraData as Map<String, Object>),
token,
);
return user;
@@ -28,12 +28,12 @@ class StreamApiLocalImpl extends StreamApiRepository {
Future<List<ChatUser>> getChatUsers() async {
final result = await _client.queryUsers();
final chatUsers = result.users
.where((element) => element.id != _client.state.user.id)
.where((element) => element.id != _client.state.user!.id)
.map(
(e) => ChatUser(
id: e.id,
name: e.name,
image: e.extraData['image'],
image: e.extraData['image'] as String?,
),
)
.toList();
@@ -47,25 +47,25 @@ class StreamApiLocalImpl extends StreamApiRepository {
@override
Future<Channel> createGroupChat(
String channelId, String name, List<String> members,
{String image}) async {
String channelId, String? name, List<String?>? members,
{String? image}) async {
final channel = _client.channel('messaging', id: channelId, extraData: {
'name': name,
'image': image,
'members': [_client.state.user.id, ...members],
'name': name!,
'image': image!,
'members': [_client.state.user!.id, ...members!],
});
await channel.watch();
return channel;
}
@override
Future<Channel> createSimpleChat(String friendId) async {
Future<Channel> createSimpleChat(String? friendId) async {
final channel = _client.channel('messaging',
id: '${_client.state.user.id.hashCode}${friendId.hashCode}',
id: '${_client.state.user!.id.hashCode}${friendId.hashCode}',
extraData: {
'members': [
friendId,
_client.state.user.id,
_client.state.user!.id,
],
});
await channel.watch();
@@ -84,6 +84,6 @@ class StreamApiLocalImpl extends StreamApiRepository {
User(id: userId),
token,
);
return _client.state.user.name != null && _client.state.user.name != userId;
return _client.state.user!.name != null && _client.state.user!.name != userId;
}
}
@@ -4,7 +4,7 @@ import 'package:stream_chatter/data/upload_storage_repository.dart';
class UploadStorageLocalImpl extends UploadStorageRepository {
@override
Future<String> uploadPhoto(File file, String path) async {
Future<String> uploadPhoto(File? file, String path) async {
return 'https://lh3.googleusercontent.com/a-/AOh14GjhqGZ-V7tNXS1pOIp9vbBij4OS9JbzxXgxgy1t=s600-k-no-rp-mo';
}
}
+9 -4
View File
@@ -7,7 +7,7 @@ class AuthImpl extends AuthRepository {
FirebaseAuth _auth = FirebaseAuth.instance;
@override
Future<AuthUser> getAuthUser() async {
Future<AuthUser?> getAuthUser() async {
final user = _auth.currentUser;
if (user != null) {
return AuthUser(user.uid);
@@ -19,16 +19,21 @@ class AuthImpl extends AuthRepository {
Future<AuthUser> signIn() async {
try {
UserCredential userCredential;
final GoogleSignInAccount googleUser = await GoogleSignIn().signIn();
final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();
if (googleUser == null) {
throw Exception('login error');
}
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
final GoogleAuthCredential googleAuthCredential =
GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
) as GoogleAuthCredential;
userCredential = await _auth.signInWithCredential(googleAuthCredential);
final user = userCredential.user;
final user = userCredential.user!;
return AuthUser(user.uid);
} catch (e) {
print(e);
@@ -13,6 +13,6 @@ class PersistentStorageImpl extends PersistentStorageRepository {
@override
Future<void> updateDarkMode(bool isDarkMode) async {
final preference = await SharedPreferences.getInstance();
return await preference.setBool(_isDarkMode, isDarkMode);
await preference.setBool(_isDarkMode, isDarkMode);
}
}
@@ -11,7 +11,7 @@ class StreamApiImpl extends StreamApiRepository {
final StreamChatClient _client;
@override
Future<ChatUser> connectUser(ChatUser user, String token) async {
Future<ChatUser> connectUser(ChatUser user, String? token) async {
Map<String, dynamic> extraData = {};
if (user.image != null) {
extraData['image'] = user.image;
@@ -21,7 +21,7 @@ class StreamApiImpl extends StreamApiRepository {
}
await _client.disconnect();
await _client.connectUser(
User(id: user.id, extraData: extraData),
User(id: user.id!, extraData: extraData as Map<String, Object>),
token,
);
return user;
@@ -31,12 +31,12 @@ class StreamApiImpl extends StreamApiRepository {
Future<List<ChatUser>> getChatUsers() async {
final result = await _client.queryUsers();
final chatUsers = result.users
.where((element) => element.id != _client.state.user.id)
.where((element) => element.id != _client.state.user!.id)
.map(
(e) => ChatUser(
id: e.id,
name: e.name,
image: e.extraData['image'],
image: e.extraData['image'] as String?,
),
)
.toList();
@@ -44,10 +44,10 @@ class StreamApiImpl extends StreamApiRepository {
}
@override
Future<String> getToken(String userId) async {
Future<String?> getToken(String userId) async {
//TODO: use your own implementation in Production
final response = await http.post(
'your_backend_url',
Uri.parse('your_backend_url'),
body: jsonEncode(<String, String>{'id': userId}),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
@@ -62,25 +62,25 @@ class StreamApiImpl extends StreamApiRepository {
}
@override
Future<Channel> createGroupChat(String id, String name, List<String> members,
{String image}) async {
Future<Channel> createGroupChat(String id, String? name, List<String?>? members,
{String? image}) async {
final channel = _client.channel('messaging', id: id, extraData: {
'name': name,
'image': image,
'members': [_client.state.user.id, ...members],
'name': name!,
'image': image!,
'members': [_client.state.user!.id, ...members!],
});
await channel.watch();
return channel;
}
@override
Future<Channel> createSimpleChat(String friendId) async {
Future<Channel> createSimpleChat(String? friendId) async {
final channel = _client.channel('messaging',
id: '${_client.state.user.id.hashCode}${friendId.hashCode}',
id: '${_client.state.user!.id.hashCode}${friendId.hashCode}',
extraData: {
'members': [
friendId,
_client.state.user.id,
_client.state.user!.id,
],
});
await channel.watch();
@@ -99,6 +99,6 @@ class StreamApiImpl extends StreamApiRepository {
User(id: userId),
token,
);
return _client.state.user.name != null && _client.state.user.name != userId;
return _client.state.user!.name != null && _client.state.user!.name != userId;
}
}
@@ -4,9 +4,9 @@ import 'package:stream_chatter/data/upload_storage_repository.dart';
class UploadStorageImpl extends UploadStorageRepository {
@override
Future<String> uploadPhoto(File file, String path) async {
Future<String> uploadPhoto(File? file, String path) async {
final ref = firebase_storage.FirebaseStorage.instance.ref(path);
final uploadTask = ref.putFile(file);
final uploadTask = ref.putFile(file!);
await uploadTask;
return await ref.getDownloadURL();
}
@@ -3,12 +3,12 @@ import 'package:stream_chat_flutter/stream_chat_flutter.dart';
abstract class StreamApiRepository {
Future<List<ChatUser>> getChatUsers();
Future<String> getToken(String userId);
Future<String?> getToken(String userId);
Future<bool> connectIfExist(String userId);
Future<ChatUser> connectUser(ChatUser user, String token);
Future<ChatUser> connectUser(ChatUser user, String? token);
Future<Channel> createGroupChat(
String channelId, String name, List<String> members,
{String image});
Future<Channel> createSimpleChat(String friendId);
String channelId, String? name, List<String?>? members,
{String? image});
Future<Channel> createSimpleChat(String? friendId);
Future<void> logout();
}
@@ -1,5 +1,5 @@
import 'dart:io';
abstract class UploadStorageRepository {
Future<String> uploadPhoto(File file, String path);
Future<String> uploadPhoto(File? file, String path);
}