Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
bitfriend committed Sep 19, 2024
1 parent cc22c3e commit db3667d
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 151 deletions.
52 changes: 16 additions & 36 deletions app/lib/common/providers/room_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,12 @@ final parentIdsProvider =
try {
// FIXME: we should get only the parent Ids from the underlying SDK
final relations = await ref.watch(spaceRelationsProvider(roomId).future);
if (relations == null) {
return [];
}

if (relations == null) return [];
// Collect all parents: mainParent and otherParents
List<String> allParents = [];
final mainParent = relations.mainParent();
if (mainParent != null) {
allParents.add(mainParent.roomId().toString());
}
allParents
.addAll(relations.otherParents().map((p) => p.roomId().toString()));
List<String> allParents =
relations.mainParent().let((p0) => [p0.roomId().toString()]) ?? [];
final others = relations.otherParents().map((p) => p.roomId().toString());
allParents.addAll(others);
return allParents;
} catch (e) {
_log.warning('Failed to load parent ids for $roomId: $e');
Expand All @@ -155,9 +149,7 @@ final parentIdsProvider =
final roomDisplayNameProvider =
FutureProvider.family<String?, String>((ref, roomId) async {
final room = await ref.watch(maybeRoomProvider(roomId).future);
if (room == null) {
return null;
}
if (room == null) return null;
return (await room.displayName()).text();
});

Expand All @@ -167,10 +159,7 @@ final roomAvatarProvider =
final sdk = await ref.watch(sdkProvider.future);
final thumbsize = sdk.api.newThumbSize(48, 48);
final room = await ref.watch(maybeRoomProvider(roomId).future);
if (room == null || !room.hasAvatar()) {
return null;
}

if (room == null || !room.hasAvatar()) return null;
final avatar = (await room.avatar(thumbsize)).data();
if (avatar != null) {
return MemoryImage(Uint8List.fromList(avatar.asTypedList()));
Expand All @@ -195,9 +184,7 @@ final parentAvatarInfosProvider =
final joinRulesAllowedRoomsProvider = FutureProvider.autoDispose
.family<List<String>, String>((ref, roomId) async {
final room = await ref.watch(maybeRoomProvider(roomId).future);
if (room == null) {
return [];
}
if (room == null) return [];
return room.restrictedRoomIdsStr().map((e) => e.toDartString()).toList();
});

Expand All @@ -206,9 +193,7 @@ final joinRulesAllowedRoomsProvider = FutureProvider.autoDispose
final roomMembershipProvider = FutureProvider.family<Member?, String>(
(ref, roomId) async {
final room = await ref.watch(maybeRoomProvider(roomId).future);
if (room == null || !room.isJoined()) {
return null;
}
if (room == null || !room.isJoined()) return null;
return await room.getMyMembership();
},
);
Expand All @@ -217,20 +202,16 @@ final roomMembershipProvider = FutureProvider.family<Member?, String>(
final roomNotificationStatusProvider =
FutureProvider.autoDispose.family<String?, String>((ref, roomId) async {
final room = await ref.watch(maybeRoomProvider(roomId).future);
if (room == null) {
return null;
}
return room.notificationMode();
if (room == null) return null;
return await room.notificationMode();
});

/// Get the default RoomNotificationsStatus for this room type
final roomDefaultNotificationStatusProvider =
FutureProvider.autoDispose.family<String?, String>((ref, roomId) async {
final room = await ref.watch(maybeRoomProvider(roomId).future);
if (room == null) {
return null;
}
return room.defaultNotificationMode();
if (room == null) return null;
return await room.defaultNotificationMode();
});

/// Get the default RoomNotificationsStatus for this room type
Expand Down Expand Up @@ -264,7 +245,8 @@ final membershipStatusStr =
final memberDisplayNameProvider =
FutureProvider.autoDispose.family<String?, MemberInfo>((ref, query) async {
try {
return ref.watch(_memberProfileProvider(query)).valueOrNull?.displayName();
final profile = ref.watch(_memberProfileProvider(query)).valueOrNull;
return profile?.displayName();
} on RoomNotFound {
return null;
}
Expand Down Expand Up @@ -306,9 +288,7 @@ final memberAvatarInfoProvider =
final membersIdsProvider =
FutureProvider.family<List<String>, String>((ref, roomIdOrAlias) async {
final room = await ref.watch(maybeRoomProvider(roomIdOrAlias).future);
if (room == null) {
return [];
}
if (room == null) return [];
final members = await room.activeMembersIds();
return asDartStringList(members);
});
Expand Down
63 changes: 25 additions & 38 deletions app/lib/common/providers/space_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ final otherSpacesForInviteMembersProvider = FutureProvider.autoDispose
final spaceProvider =
FutureProvider.family<Space, String>((ref, spaceId) async {
final maybeSpace = await ref.watch(maybeSpaceProvider(spaceId).future);
if (maybeSpace != null) {
return maybeSpace;
}
throw 'Space not found';
if (maybeSpace == null) throw 'Space not found';
return maybeSpace;
});

final spaceIsBookmarkedProvider =
Expand All @@ -74,9 +72,7 @@ final maybeSpaceProvider =
final maybeSpaceInfoProvider =
FutureProvider.autoDispose.family<SpaceItem?, String>((ref, spaceId) async {
final space = await ref.watch(maybeSpaceProvider(spaceId).future);
if (space == null || !space.isJoined()) {
return null;
}
if (space == null || !space.isJoined()) return null;
final avatarInfo = ref.watch(roomAvatarInfoProvider(spaceId));
final membership = await space.getMyMembership();
return SpaceItem(
Expand All @@ -95,9 +91,7 @@ final selectedSpaceIdProvider =
/// gives current context space details based on id, will throw null if id is null
final selectedSpaceDetailsProvider = Provider.autoDispose<SpaceItem?>((ref) {
final selectedSpaceId = ref.watch(selectedSpaceIdProvider);
if (selectedSpaceId == null) {
return null;
}
if (selectedSpaceId == null) return null;
return ref.watch(briefSpaceItemProvider(selectedSpaceId));
});

Expand Down Expand Up @@ -145,9 +139,7 @@ final hasSpaceWithPermissionProvider =
final spaces = ref.watch(spacesProvider);
for (final element in spaces) {
final membership = await element.getMyMembership();
if (membership.canString(permission)) {
return true;
}
if (membership.canString(permission)) return true;
}
// none found
return false;
Expand All @@ -164,9 +156,8 @@ final _spaceIdAndNames =
List<_SpaceIdAndName> items = [];
for (final space in spaces) {
final roomId = space.getRoomIdStr();
items.add(
(roomId, await ref.watch(roomDisplayNameProvider(roomId).future)),
);
final dispName = await ref.watch(roomDisplayNameProvider(roomId).future);
items.add((roomId, dispName));
}
return items;
});
Expand Down Expand Up @@ -224,9 +215,7 @@ final briefSpaceItemProvider =
final spaceInvitedMembersProvider = FutureProvider.autoDispose
.family<List<Member>, String>((ref, roomIdOrAlias) async {
final space = await ref.watch(spaceProvider(roomIdOrAlias).future);
if (!space.isJoined()) {
return [];
}
if (!space.isJoined()) return [];
final members = await space.invitedMembers();
return members.toList();
});
Expand All @@ -237,27 +226,23 @@ final spaceInvitedMembersProvider = FutureProvider.autoDispose
final spaceRelationsOverviewProvider =
FutureProvider.family<SpaceRelationsOverview, String>((ref, spaceId) async {
final relatedSpaces = await ref.watch(spaceRelationsProvider(spaceId).future);
if (relatedSpaces == null) {
throw SpaceNotFound;
}
if (relatedSpaces == null) throw SpaceNotFound;
bool hasMore = false;
final List<String> knownSubspaces = [];
final List<String> knownChats = [];
final List<String> suggested = [];
for (final related in relatedSpaces.children()) {
String targetType = related.targetType();
final roomId = related.roomId().toString();
if (related.suggested()) {
suggested.add(roomId);
}
if (related.suggested()) suggested.add(roomId);

final room = ref.watch(maybeRoomProvider(roomId)).valueOrNull;
if (room == null || !room.isJoined()) {
// we don’t know this room or are not in it
hasMore = true;
continue;
}

if (targetType == 'ChatRoom') {
if (related.targetType() == 'ChatRoom') {
// we know this as a chat room
knownChats.add(roomId);
} else {
Expand Down Expand Up @@ -324,9 +309,7 @@ final spaceRemoteRelationsProvider =
FutureProvider.family<List<SpaceHierarchyRoomInfo>, String>(
(ref, spaceId) async {
final relatedSpaces = await ref.watch(spaceRelationsProvider(spaceId).future);
if (relatedSpaces == null) {
return [];
}
if (relatedSpaces == null) return [];
return (await relatedSpaces.queryHierarchy()).toList();
});

Expand Down Expand Up @@ -367,10 +350,12 @@ final suggestedChatsProvider =
await ref.watch(spaceRemoteRelationsProvider(spaceId).future);
// filter out the known rooms
final remoteRooms = roomHierarchy
.where((r) =>
!r.isSpace() &&
suggestedRooms.contains(r.roomIdStr()) &&
!toIgnore.contains(r.roomIdStr()),)
.where(
(r) =>
!r.isSpace() &&
suggestedRooms.contains(r.roomIdStr()) &&
!toIgnore.contains(r.roomIdStr()),
)
.toList();
return (localRooms, remoteRooms);
} on SpaceNotFound {
Expand All @@ -395,10 +380,12 @@ final suggestedSpacesProvider =
await ref.watch(spaceRemoteRelationsProvider(spaceId).future);
// filter out the known rooms
final remoteRooms = roomHierarchy
.where((r) =>
r.isSpace() &&
suggestedRooms.contains(r.roomIdStr()) &&
!toIgnore.contains(r.roomIdStr()),)
.where(
(r) =>
r.isSpace() &&
suggestedRooms.contains(r.roomIdStr()) &&
!toIgnore.contains(r.roomIdStr()),
)
.toList();
return (localRooms, remoteRooms);
} on SpaceNotFound {
Expand Down
5 changes: 3 additions & 2 deletions app/lib/common/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,9 @@ Future<void> uploadAvatar(
try {
if (!context.mounted) return;
EasyLoading.show(status: L10n.of(context).avatarUploading);
final file = result.files.first;
if (file.path != null) await room.uploadAvatar(file.path!);
final filePath = result.files.first.path;
if (filePath == null) throw 'avatar path not available';
await room.uploadAvatar(filePath);
// close loading
EasyLoading.dismiss();
} catch (e, s) {
Expand Down
20 changes: 8 additions & 12 deletions app/lib/common/widgets/acter_icon_picker/acter_icon_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,14 @@ class _ActerIconWidgetState extends State<ActerIconWidget> {
Widget _buildIconUI() {
return ValueListenableBuilder<Color>(
valueListenable: color,
builder: (context, colorData, child) {
return ValueListenableBuilder<ActerIcon>(
valueListenable: icon,
builder: (context, acterIcon, child) {
return Icon(
acterIcon.data,
size: widget.iconSize ?? 100,
color: colorData,
);
},
);
},
builder: (context, colorData, child) => ValueListenableBuilder<ActerIcon>(
valueListenable: icon,
builder: (context, acterIcon, child) => Icon(
acterIcon.data,
size: widget.iconSize ?? 100,
color: colorData,
),
),
);
}
}
14 changes: 6 additions & 8 deletions app/lib/common/widgets/edit_html_description_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import 'package:acter/common/toolkit/buttons/primary_action_button.dart';
import 'package:acter/common/widgets/html_editor.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

void showEditHtmlDescriptionBottomSheet({
required BuildContext context,
Expand Down Expand Up @@ -56,13 +56,11 @@ class _EditHtmlDescriptionSheetState
@override
void initState() {
super.initState();
final document = widget.descriptionHtmlValue != null
? ActerDocumentHelpers.fromHtml(
widget.descriptionHtmlValue!,
)
: ActerDocumentHelpers.fromMarkdown(
widget.descriptionMarkdownValue ?? '',
);
final html = widget.descriptionHtmlValue;
final markdown = widget.descriptionMarkdownValue ?? '';
final document = html != null
? ActerDocumentHelpers.fromHtml(html)
: ActerDocumentHelpers.fromMarkdown(markdown);
textEditorState = EditorState(document: document);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,29 +216,30 @@ class AttachmentSelectionOptions extends StatelessWidget {
AttachmentType type,
OnAttachmentSelected handleFileUpload,
) {
final size = MediaQuery.of(context).size;
if (selectedFiles != null && selectedFiles.isNotEmpty) {
context.isLargeScreen
? showAdaptiveDialog(
context: context,
builder: (context) => Dialog(
insetPadding: const EdgeInsets.all(8),
child: ConstrainedBox(
constraints: BoxConstraints(
maxWidth: size.width * 0.5,
maxHeight: size.height * 0.5,
),
child: _FileWidget(selectedFiles, type, handleFileUpload),
),
),
)
: showModalBottomSheet(
context: context,
builder: (context) => Padding(
padding: const EdgeInsets.all(8.0),
child: _FileWidget(selectedFiles, type, handleFileUpload),
),
);
if (selectedFiles == null || selectedFiles.isEmpty) return;
if (context.isLargeScreen) {
final size = MediaQuery.of(context).size;
showAdaptiveDialog(
context: context,
builder: (context) => Dialog(
insetPadding: const EdgeInsets.all(8),
child: ConstrainedBox(
constraints: BoxConstraints(
maxWidth: size.width * 0.5,
maxHeight: size.height * 0.5,
),
child: _FileWidget(selectedFiles, type, handleFileUpload),
),
),
);
} else {
showModalBottomSheet(
context: context,
builder: (context) => Padding(
padding: const EdgeInsets.all(8.0),
child: _FileWidget(selectedFiles, type, handleFileUpload),
),
);
}
}
}
Expand Down
Loading

0 comments on commit db3667d

Please sign in to comment.