diff --git a/lib/di/global/get_it_initializer.dart b/lib/di/global/get_it_initializer.dart index 30c3ecf948..78ccce98c0 100644 --- a/lib/di/global/get_it_initializer.dart +++ b/lib/di/global/get_it_initializer.dart @@ -41,6 +41,7 @@ import 'package:fluffychat/domain/usecase/send_file_interactor.dart'; import 'package:fluffychat/domain/usecase/send_file_on_web_interactor.dart'; import 'package:fluffychat/domain/usecase/send_image_interactor.dart'; import 'package:fluffychat/domain/usecase/send_images_interactor.dart'; +import 'package:fluffychat/domain/usecase/settings/upload_profile_interactor.dart'; import 'package:fluffychat/event/twake_event_dispatcher.dart'; import 'package:fluffychat/utils/responsive/responsive_utils.dart'; import 'package:get_it/get_it.dart'; @@ -177,5 +178,8 @@ class GetItInitializer { getIt.registerSingleton( TimelineSearchEventInteractor(), ); + getIt.registerSingleton( + UploadProfileInteractor(), + ); } } diff --git a/lib/domain/app_state/settings/upload_profile_failure.dart b/lib/domain/app_state/settings/upload_profile_failure.dart new file mode 100644 index 0000000000..884b03e4c9 --- /dev/null +++ b/lib/domain/app_state/settings/upload_profile_failure.dart @@ -0,0 +1,10 @@ +import 'package:fluffychat/app_state/failure.dart'; + +class UploadProfileFailure extends Failure { + final dynamic exception; + + const UploadProfileFailure(this.exception) : super(); + + @override + List get props => [exception]; +} diff --git a/lib/domain/app_state/settings/upload_profile_loading.dart b/lib/domain/app_state/settings/upload_profile_loading.dart new file mode 100644 index 0000000000..05eede9199 --- /dev/null +++ b/lib/domain/app_state/settings/upload_profile_loading.dart @@ -0,0 +1,8 @@ +import 'package:fluffychat/app_state/success.dart'; + +class UploadProfileLoading extends Success { + const UploadProfileLoading(); + + @override + List get props => []; +} diff --git a/lib/domain/app_state/settings/upload_profile_success.dart b/lib/domain/app_state/settings/upload_profile_success.dart new file mode 100644 index 0000000000..580f6ae630 --- /dev/null +++ b/lib/domain/app_state/settings/upload_profile_success.dart @@ -0,0 +1,19 @@ +import 'package:fluffychat/app_state/success.dart'; + +class UploadProfileInitial extends Success { + @override + List get props => []; +} + +class UploadProfileSuccess extends Success { + final Uri? avatar; + final String? displayName; + + const UploadProfileSuccess({ + this.avatar, + this.displayName, + }); + + @override + List get props => [avatar, displayName]; +} diff --git a/lib/domain/usecase/settings/upload_profile_interactor.dart b/lib/domain/usecase/settings/upload_profile_interactor.dart new file mode 100644 index 0000000000..34b8f75973 --- /dev/null +++ b/lib/domain/usecase/settings/upload_profile_interactor.dart @@ -0,0 +1,44 @@ +import 'package:dartz/dartz.dart'; +import 'package:fluffychat/app_state/failure.dart'; +import 'package:fluffychat/app_state/success.dart'; +import 'package:fluffychat/domain/app_state/settings/upload_profile_failure.dart'; +import 'package:fluffychat/domain/app_state/settings/upload_profile_loading.dart'; +import 'package:fluffychat/domain/app_state/settings/upload_profile_success.dart'; +import 'package:matrix/matrix.dart'; + +class UploadProfileInteractor { + Stream> execute({ + required Client client, + required String userId, + Uri? avatarUrl, + bool isUpdateDisPlayName = false, + String? displayName, + }) async* { + yield const Right(UploadProfileLoading()); + try { + Logs().d( + 'UploadAvatarInteractor::execute(): Uri - $avatarUrl - displayName - $displayName', + ); + if (avatarUrl != null) { + await client.setAvatarUrl( + userId, + avatarUrl, + ); + } + if (displayName != null) { + await client.setDisplayName(userId, displayName); + } + yield Right( + UploadProfileSuccess( + displayName: displayName, + avatar: avatarUrl, + ), + ); + } catch (e) { + Logs().d( + 'UploadAvatarInteractor::execute(): Exception - $e}', + ); + yield Left(UploadProfileFailure(e)); + } + } +}