Skip to content

Commit

Permalink
TMP
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian KOUNE committed Jun 14, 2023
1 parent 9b48f50 commit 1f3d29a
Show file tree
Hide file tree
Showing 13 changed files with 248 additions and 36 deletions.
17 changes: 9 additions & 8 deletions lib/config/routes.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:fluffychat/di/room/room_di.dart';
import 'package:fluffychat/pages/add_story/add_story.dart';
import 'package:fluffychat/pages/archive/archive.dart';
import 'package:fluffychat/pages/chat/chat.dart';
Expand Down Expand Up @@ -60,10 +61,10 @@ class AppRoutes {
widget: const ChatList(),
buildTransition: _bottomToTopTransition,
),
VWidgetWithDependency(
VWidgetWithDependencies(
path: '/contactsTab',
widget: const ContactsTab(),
di: ContactDI(),
di: [ContactDI()],
buildTransition: _bottomToTopTransition,
),
VWidget(
Expand Down Expand Up @@ -142,14 +143,14 @@ class AppRoutes {
),
],
),
VWidgetWithDependency(
di: ContactDI(),
VWidgetWithDependencies(
di: [ContactDI()],
path: '/newprivatechat',
widget: const NewPrivateChat(),
buildTransition: _rightToLeftTransition,
),
VWidgetWithDependency(
di: ContactDI(),
VWidgetWithDependencies(
di: [ContactDI(), RoomDI()],
path: '/newgroup',
widget: const NewGroup(),
buildTransition: _rightToLeftTransition,
Expand Down Expand Up @@ -200,8 +201,8 @@ class AppRoutes {
widget: const NewPrivateChat(),
buildTransition: _fadeTransition,
),
VWidgetWithDependency(
di: ContactDI(),
VWidgetWithDependencies(
di: [ContactDI(), RoomDI()],
path: '/newgroup',
widget: const NewGroup(),
buildTransition: _fadeTransition,
Expand Down
25 changes: 25 additions & 0 deletions lib/di/room/room_di.dart
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');
}
}
10 changes: 10 additions & 0 deletions lib/domain/app_state/room/create_room_failed.dart
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];
}
16 changes: 16 additions & 0 deletions lib/domain/app_state/room/create_room_success.dart
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];
}
10 changes: 10 additions & 0 deletions lib/domain/app_state/room/set_room_avatar_failed.dart
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];
}
10 changes: 10 additions & 0 deletions lib/domain/app_state/room/set_room_avatar_success.dart
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];
}
27 changes: 27 additions & 0 deletions lib/domain/model/contact/new_room_informations.dart
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,
];
}
45 changes: 45 additions & 0 deletions lib/domain/usecase/create_room_interactor.dart
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));
}
}
}
16 changes: 16 additions & 0 deletions lib/domain/usecase/set_room_avatar_interactor.dart
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));
}
}
}
37 changes: 29 additions & 8 deletions lib/pages/new_group/new_group.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'dart:async';
import 'package:dartz/dartz.dart' hide State;
import 'package:fluffychat/app_state/failure.dart';
import 'package:fluffychat/domain/app_state/contact/get_contacts_success.dart';
import 'package:fluffychat/domain/app_state/room/create_room_success.dart';
import 'package:fluffychat/pages/new_private_chat/create_room_controller.dart';
import 'package:fluffychat/presentation/model/presentation_contact.dart';
import 'package:fluffychat/mixin/comparable_presentation_contact_mixin.dart';
import 'package:fluffychat/pages/new_group/new_group_chat_info.dart';
Expand All @@ -14,6 +16,7 @@ import 'package:flutter/material.dart';

import 'package:fluffychat/pages/new_group/new_group_view.dart';
import 'package:matrix/matrix.dart';
import 'package:vrouter/vrouter.dart';

class NewGroup extends StatefulWidget {
const NewGroup({Key? key}) : super(key: key);
Expand All @@ -27,6 +30,7 @@ class NewGroupController extends State<NewGroup> with ComparablePresentationCont
final fetchContactsController = FetchContactsController();
final contactStreamController = StreamController<Either<Failure, GetContactsSuccess>>();
final groupNameTextEditingController = TextEditingController();
final createRoomStreamController = CreateRoomController();

final selectedContactsMapNotifier = ValueNotifier<Map<PresentationContact, bool>>({});
final haveSelectedContactsNotifier = ValueNotifier(false);
Expand All @@ -46,6 +50,7 @@ class NewGroupController extends State<NewGroup> with ComparablePresentationCont
listenSearchContacts();
listenGroupNameChanged();
fetchContactsController.fetchCurrentTomContacts();
listenCreateRoom();
}

@override
Expand All @@ -55,6 +60,7 @@ class NewGroupController extends State<NewGroup> with ComparablePresentationCont
searchContactsController.dispose();
fetchContactsController.dispose();
groupNameTextEditingController.dispose();
createRoomStreamController.dispose();

selectedContactsMapNotifier.dispose();
haveSelectedContactsNotifier.dispose();
Expand All @@ -69,6 +75,22 @@ class NewGroupController extends State<NewGroup> with ComparablePresentationCont
});
}

void listenCreateRoom() {
createRoomStreamController.streamController.stream.listen((event) {
Logs().d('NewGroupController::createRoom() - event: $event');
event.fold(
(failure) => {
Logs().e('NewGroupController::createRoom() - failure: $failure'),
},
(CreateRoomSuccess success) => {
Logs().d('NewGroupController::createRoom() - success: $success'),

VRouter.of(context).toSegments(['rooms', success.roomId!])
},
);
});
}

void listenSearchContacts() {
searchContactsController.lookupStreamController.stream.listen((event) {
Logs().d('NewGroupController::_fetchRemoteContacts() - event: $event');
Expand All @@ -84,8 +106,7 @@ class NewGroupController extends State<NewGroup> with ComparablePresentationCont
};
}

Iterable<PresentationContact> get contactsList
=> selectedContactsMapNotifier.value.keys;
Iterable<PresentationContact> get contactsList => selectedContactsMapNotifier.value.keys;

void selectContact(PresentationContact contact) {
final newSelectedContactsMap = Map<PresentationContact, bool>.from(selectedContactsMapNotifier.value);
Expand All @@ -105,19 +126,19 @@ class NewGroupController extends State<NewGroup> with ComparablePresentationCont

Set<PresentationContact> getAllContactsGroupChat() {
final newContactsList = {
PresentationContact(
displayName: "You",
matrixId: Matrix.of(context).client.userID,
)
};
PresentationContact(
displayName: "You",
matrixId: Matrix.of(context).client.userID,
)
};
newContactsList.addAll(getSelectedValidContacts(contactsList));
return newContactsList;
}

void moveToNewGroupInfoScreen() async {
await showDialog(
useSafeArea: false,
context: context,
context: context,
useRootNavigator: false,
builder: (context) {
return NewGroupChatInfo(
Expand Down
29 changes: 14 additions & 15 deletions lib/pages/new_group/new_group_info_controller.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:file_picker_cross/file_picker_cross.dart';
import 'package:fluffychat/domain/model/contact/new_room_informations.dart';
import 'package:fluffychat/pages/settings/settings.dart';
import 'package:fluffychat/presentation/model/presentation_contact.dart';
import 'package:fluffychat/pages/new_group/new_group.dart';
Expand All @@ -23,22 +24,20 @@ extension NewGroupInfoController on NewGroupController {

void moveToGroupChatScreen() async {
final client = Matrix.of(context).client;
final roomId = await client.createGroupChat(
groupName: groupName,
invite: getSelectedValidContacts(contactsList)
.map<String>((contact) => contact.matrixId!)
.toList(),
enableEncryption: isEnableEEEncryptionNotifier.value,
preset: isGroupPrivate
? CreateRoomPreset.privateChat
: CreateRoomPreset.publicChat,
);
createRoomStreamController.createRoom(
client,
NewRoomInformations(
groupName: groupName,
invite: getSelectedValidContacts(contactsList)
.map<String>((contact) => contact.matrixId!)
.toList(),
createRoomPreset:
isGroupPrivate ? CreateRoomPreset.privateChat : CreateRoomPreset.publicChat,
enableEncryption: isEnableEEEncryptionNotifier.value,
avatar: avatar,
));

if (roomId.isNotEmpty && avatar != null) {
await client.getRoomById(roomId)!.setAvatar(avatar!);
}

VRouter.of(context).toSegments(['rooms', roomId]);
// VRouter.of(context).toSegments(['rooms', roomId]);
}

void onGroupPrivacyChanged(bool switchValue) {
Expand Down
26 changes: 26 additions & 0 deletions lib/pages/new_private_chat/create_room_controller.dart
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();
}
}
Loading

0 comments on commit 1f3d29a

Please sign in to comment.