Skip to content

Commit

Permalink
TW-1400: refactor power level to a PowerLevelManager class
Browse files Browse the repository at this point in the history
  • Loading branch information
sherlockvn authored and hoangdat committed Mar 1, 2024
1 parent e318fbf commit dca5943
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
19 changes: 3 additions & 16 deletions lib/pages/new_group/new_group_chat_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:async';
import 'package:dartz/dartz.dart' hide State;
import 'package:fluffychat/app_state/failure.dart';
import 'package:fluffychat/app_state/success.dart';
import 'package:fluffychat/config/power_level_member_in_chat.dart';
import 'package:fluffychat/domain/app_state/room/create_new_group_chat_state.dart';
import 'package:fluffychat/domain/app_state/room/upload_content_state.dart';
import 'package:fluffychat/pages/new_group/new_group_chat_info_view.dart';
Expand All @@ -12,6 +11,7 @@ import 'package:fluffychat/presentation/mixins/common_media_picker_mixin.dart';
import 'package:fluffychat/presentation/mixins/single_image_picker_mixin.dart';
import 'package:fluffychat/presentation/model/presentation_contact.dart';
import 'package:fluffychat/utils/extension/build_context_extension.dart';
import 'package:fluffychat/utils/power_level_manager.dart';
import 'package:fluffychat/utils/responsive/responsive_utils.dart';
import 'package:fluffychat/widgets/matrix.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -93,6 +93,7 @@ class NewGroupChatInfoController extends State<NewGroupChatInfo>

void createNewGroup({String? urlAvatar}) {
final client = Matrix.of(context).client;
final powerLevelManager = getIt.get<PowerLevelManager>();
createNewGroupChatAction(
matrixClient: client,
createNewGroupChatRequest: CreateNewGroupChatRequest(
Expand All @@ -104,26 +105,12 @@ class NewGroupChatInfoController extends State<NewGroupChatInfo>
enableEncryption: enableEncryptionNotifier.value,
urlAvatar: urlAvatar,
powerLevelContentOverride: {
'events': _getOverridePowerLevelEventForMember(),
'events': powerLevelManager.getDefaultPowerLevelEventForMember(),
},
),
);
}

Map<String, dynamic> _getOverridePowerLevelEventForMember() {
return {
EventTypes.RoomPinnedEvents: getPowerLevelUserInChat(),
EventTypes.RoomName: getPowerLevelUserInChat(),
EventTypes.RoomAvatar: getPowerLevelUserInChat(),
EventTypes.RoomMember: getPowerLevelUserInChat(),
EventTypes.RoomTopic: getPowerLevelUserInChat(),
};
}

int getPowerLevelUserInChat() {
return DefaultPowerLevelMemberInChat.user;
}

void _handleUploadAvatarNewGroupChatOnData(
BuildContext context,
Either<Failure, Success> event,
Expand Down
26 changes: 26 additions & 0 deletions lib/utils/power_level_manager.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:fluffychat/config/default_power_level_member.dart';
import 'package:matrix/matrix.dart';

class PowerLevelManager {
static final PowerLevelManager _instance = PowerLevelManager._internal();

PowerLevelManager._internal();

factory PowerLevelManager() {
return _instance;
}

int getUserPowerLevel() {
return DefaultPowerLevelMember.user.powerLevel;
}

Map<String, dynamic> getDefaultPowerLevelEventForMember() {
return {
EventTypes.RoomPinnedEvents: getUserPowerLevel(),
EventTypes.RoomName: getUserPowerLevel(),
EventTypes.RoomAvatar: getUserPowerLevel(),
EventTypes.RoomMember: getUserPowerLevel(),
EventTypes.RoomTopic: getUserPowerLevel(),
};
}
}

0 comments on commit dca5943

Please sign in to comment.