-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Julian KOUNE
committed
Jun 14, 2023
1 parent
9b48f50
commit 1f3d29a
Showing
13 changed files
with
248 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import 'package:fluffychat/di/base_di.dart'; | ||
import 'package:fluffychat/domain/usecase/create_room_interactor.dart'; | ||
import 'package:fluffychat/domain/usecase/set_room_avatar_interactor.dart'; | ||
import 'package:get_it/get_it.dart'; | ||
import 'package:matrix/matrix.dart'; | ||
|
||
class RoomDI extends BaseDI { | ||
@override | ||
String get scopeName => 'Room'; | ||
|
||
@override | ||
void setUp(GetIt get) { | ||
Logs().d('RoomDI::setUp()'); | ||
|
||
get.registerFactory<SetRoomAvatarInteractor>( | ||
() => SetRoomAvatarInteractor(), | ||
); | ||
|
||
get.registerFactory<CreateRoomInteractor>( | ||
() => CreateRoomInteractor(), | ||
); | ||
|
||
Logs().d('RoomDI::setUp() - done'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import 'package:fluffychat/app_state/failure.dart'; | ||
|
||
class CreateRoomFailed extends Failure { | ||
final dynamic exception; | ||
|
||
const CreateRoomFailed({required this.exception}); | ||
|
||
@override | ||
List<Object?> get props => [exception]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import 'package:fluffychat/app_state/success.dart'; | ||
|
||
enum CreateRoomSuccessEventType { | ||
createRoomSuccess, | ||
setAvatarSuccess, | ||
} | ||
|
||
class CreateRoomSuccess extends Success { | ||
final CreateRoomSuccessEventType event; | ||
final String? roomId; | ||
|
||
const CreateRoomSuccess({required this.event, required this.roomId}); | ||
|
||
@override | ||
List<Object?> get props => [event, roomId]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import 'package:fluffychat/app_state/failure.dart'; | ||
|
||
class SetRoomAvatarFailed extends Failure { | ||
final dynamic exception; | ||
|
||
const SetRoomAvatarFailed({required this.exception}); | ||
|
||
@override | ||
List<Object?> get props => [exception]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import 'package:fluffychat/app_state/success.dart'; | ||
|
||
class SetRoomAvatarSuccess extends Success { | ||
final String roomAvatarEventId; | ||
|
||
const SetRoomAvatarSuccess({required this.roomAvatarEventId}); | ||
|
||
@override | ||
List<Object?> get props => [roomAvatarEventId]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:matrix/matrix.dart'; | ||
|
||
class NewRoomInformations extends Equatable { | ||
final String? groupName; | ||
final List<String>? invite; | ||
final bool? enableEncryption; | ||
final CreateRoomPreset createRoomPreset; | ||
final MatrixFile? avatar; | ||
|
||
const NewRoomInformations({ | ||
this.groupName, | ||
this.invite, | ||
this.enableEncryption, | ||
this.createRoomPreset = CreateRoomPreset.privateChat, | ||
this.avatar, | ||
}); | ||
|
||
@override | ||
List<Object?> get props => [ | ||
groupName, | ||
invite, | ||
enableEncryption, | ||
createRoomPreset, | ||
avatar, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import 'package:dartz/dartz.dart'; | ||
import 'package:fluffychat/di/global/get_it_initializer.dart'; | ||
import 'package:fluffychat/domain/app_state/room/create_room_failed.dart'; | ||
import 'package:fluffychat/domain/app_state/room/create_room_success.dart'; | ||
import 'package:fluffychat/domain/app_state/room/set_room_avatar_failed.dart'; | ||
import 'package:fluffychat/domain/app_state/room/set_room_avatar_success.dart'; | ||
import 'package:fluffychat/domain/model/contact/new_room_informations.dart'; | ||
import 'package:fluffychat/domain/usecase/set_room_avatar_interactor.dart'; | ||
import 'package:matrix/matrix.dart'; | ||
|
||
class CreateRoomInteractor { | ||
final setRoomAvatarInteractor = getIt.get<SetRoomAvatarInteractor>(); | ||
|
||
Stream<Either<CreateRoomFailed, CreateRoomSuccess>> execute( | ||
Client matrixClient, NewRoomInformations newRoomInformations) async* { | ||
try { | ||
final roomId = await matrixClient.createGroupChat( | ||
groupName: newRoomInformations.groupName, | ||
invite: newRoomInformations.invite, | ||
enableEncryption: newRoomInformations.enableEncryption, | ||
preset: newRoomInformations.createRoomPreset, | ||
); | ||
|
||
if (roomId.isNotEmpty) { | ||
yield Right( | ||
CreateRoomSuccess(event: CreateRoomSuccessEventType.createRoomSuccess, roomId: roomId)); | ||
|
||
if (newRoomInformations.avatar != null) { | ||
await for (final Either<SetRoomAvatarFailed, SetRoomAvatarSuccess> result | ||
in setRoomAvatarInteractor.execute( | ||
matrixClient, roomId, newRoomInformations.avatar)) { | ||
if (result.isRight()) { | ||
yield Right(CreateRoomSuccess( | ||
event: CreateRoomSuccessEventType.setAvatarSuccess, roomId: roomId)); | ||
} else if (result.isLeft()) { | ||
throw result.fold((l) => l.exception, (r) => r); | ||
} | ||
} | ||
} | ||
} | ||
} catch (exception) { | ||
yield Left(CreateRoomFailed(exception: exception)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import 'package:dartz/dartz.dart'; | ||
import 'package:fluffychat/domain/app_state/room/set_room_avatar_failed.dart'; | ||
import 'package:fluffychat/domain/app_state/room/set_room_avatar_success.dart'; | ||
import 'package:matrix/matrix.dart'; | ||
|
||
class SetRoomAvatarInteractor { | ||
Stream<Either<SetRoomAvatarFailed, SetRoomAvatarSuccess>> execute(Client matrixClient, String roomId, MatrixFile? newAvatar) async* { | ||
try { | ||
final setAvatarEventId = await matrixClient.getRoomById(roomId)!.setAvatar(newAvatar); | ||
|
||
yield Right(SetRoomAvatarSuccess(roomAvatarEventId: setAvatarEventId)); | ||
} catch (exception) { | ||
yield Left(SetRoomAvatarFailed(exception: exception)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:dartz/dartz.dart'; | ||
import 'package:fluffychat/app_state/failure.dart'; | ||
import 'package:fluffychat/di/global/get_it_initializer.dart'; | ||
import 'package:fluffychat/domain/app_state/room/create_room_success.dart'; | ||
import 'package:fluffychat/domain/model/contact/new_room_informations.dart'; | ||
import 'package:fluffychat/domain/usecase/create_room_interactor.dart'; | ||
import 'package:matrix/matrix.dart'; | ||
|
||
class CreateRoomController { | ||
final _createRoomInteractor = getIt.get<CreateRoomInteractor>(); | ||
final streamController = StreamController<Either<Failure, CreateRoomSuccess>>(); | ||
|
||
void createRoom(Client matrixClient, NewRoomInformations newRoomInformations) { | ||
_createRoomInteractor | ||
.execute(matrixClient, newRoomInformations) | ||
.listen((event) { | ||
streamController.add(event); | ||
}); | ||
} | ||
|
||
void dispose() { | ||
streamController.close(); | ||
} | ||
} |
Oops, something went wrong.