diff --git a/lib/controllers/client_controller.dart b/lib/controllers/client_controller.dart index fd84f5d..f1ff6a6 100644 --- a/lib/controllers/client_controller.dart +++ b/lib/controllers/client_controller.dart @@ -231,10 +231,10 @@ class ClientController extends AsyncNotifier { Future paginate(PaginateRequest request) async => Paginate.fromJson(await _sendCommand("paginate", request.toJson())); - Future getProfile(String userId) async => Profile.fromJsonWithCatch({ - ...(await _sendCommand("get_profile", {"user_id": userId})), - "id": userId, - }); + Future getProfile(String userId) async { + final json = await _sendCommand("get_profile", {"user_id": userId}); + return Profile.fromJsonWithCatch({...json, "id": userId}); + } Future reportEvent(ReportRequest request) => _sendCommand("report_event", request.toJson()); diff --git a/lib/controllers/profile_controller.dart b/lib/controllers/profile_controller.dart index 6131034..9aa0e09 100644 --- a/lib/controllers/profile_controller.dart +++ b/lib/controllers/profile_controller.dart @@ -12,8 +12,6 @@ class ProfileController extends AsyncNotifier { return client.getProfile(userId); } - static final provider = - AsyncNotifierProvider.family( - ProfileController.new, - ); + static final provider = AsyncNotifierProvider.family + .autoDispose(ProfileController.new); } diff --git a/lib/controllers/room_chat_controller.dart b/lib/controllers/room_chat_controller.dart index d8fea0c..cf6209e 100644 --- a/lib/controllers/room_chat_controller.dart +++ b/lib/controllers/room_chat_controller.dart @@ -44,16 +44,11 @@ class RoomChatController extends AsyncNotifier> { loadOlder(); } - return IMap.fromValues( - keyMapper: (id) => 9999999 + (id ?? 0), - values: room.sticky, - ) - .addAll(room.timeline) - .toEntryIList(compare: (a, b) => (b?.key ?? 0).compareTo(a?.key ?? 0)) + return room.timeline + .toValueIList(sort: true, compare: (a, b) => (a ?? 0).compareTo(b ?? 0)) + .addAll(room.sticky) .map((entry) { - final foundEvent = entry.value == null - ? null - : room.events[entry.value!]; + final foundEvent = entry == null ? null : room.events[entry]; final editedEvent = foundEvent == null || foundEvent.lastEditRowId == 0 diff --git a/lib/models/content/membership.dart b/lib/models/content/membership.dart index aa5a36d..dbbd123 100644 --- a/lib/models/content/membership.dart +++ b/lib/models/content/membership.dart @@ -7,8 +7,16 @@ part "membership.g.dart"; @freezed abstract class MembershipContent extends Content with _$MembershipContent { MembershipContent._(); + + static String? displaynameFromJson(String? displayName) => + displayName?.isEmpty == true ? null : displayName; + factory MembershipContent({ - @JsonKey(name: "displayname") required String? displayName, + @JsonKey( + name: "displayname", + fromJson: MembershipContent.displaynameFromJson, + ) + required String? displayName, @JsonKey(name: "membership") required MembershipStatus status, Uri? avatarUrl, String? reason, diff --git a/lib/models/profile.dart b/lib/models/profile.dart index 6ba2656..6ae1e94 100644 --- a/lib/models/profile.dart +++ b/lib/models/profile.dart @@ -1,26 +1,32 @@ import "package:fast_immutable_collections/fast_immutable_collections.dart"; import "package:freezed_annotation/freezed_annotation.dart"; +import "package:nexus/models/content/membership.dart"; part "profile.freezed.dart"; part "profile.g.dart"; -Object? readPronouns(Map map, _) => - map["m.pronouns"] ?? map["io.fsky.nyx.pronouns"]; - -Object? readTimezone(Map map, _) => - map["m.tz"] ?? map["us.cloke.msc4175.tz"]; - @freezed abstract class Profile with _$Profile { + static Object? readPronouns(Map map, _) => + map["m.pronouns"] ?? map["io.fsky.nyx.pronouns"]; + + static Object? readTimezone(Map map, _) => + map["m.tz"] ?? map["us.cloke.msc4175.tz"]; + const factory Profile({ required String id, String? parseError, Uri? avatarUrl, - @JsonKey(name: "displayname") String? displayName, - @JsonKey(readValue: readTimezone, name: "m.tz") String? timezone, + @JsonKey( + name: "displayname", + fromJson: MembershipContent.displaynameFromJson, + ) + String? displayName, + + @JsonKey(readValue: Profile.readTimezone, name: "m.tz") String? timezone, @Default(IList.empty()) - @JsonKey(readValue: readPronouns, name: "io.fsky.nyx.pronouns") + @JsonKey(readValue: Profile.readPronouns, name: "io.fsky.nyx.pronouns") IList pronouns, }) = _Profile; diff --git a/lib/widgets/room_chat.dart b/lib/widgets/room_chat.dart index 733c5ba..87bc90d 100644 --- a/lib/widgets/room_chat.dart +++ b/lib/widgets/room_chat.dart @@ -78,7 +78,10 @@ class RoomChat extends HookConsumerWidget { final client = ref.watch(ClientController.provider.notifier); final listController = useRef(ListController()); - final scrollController = useScrollController(); + final scrollController = useScrollController( + initialScrollOffset: 99999, + keys: [roomId], + ); useEffect(() { Future listener() async { @@ -90,9 +93,9 @@ class RoomChat extends HookConsumerWidget { if (room == null) return; if (scrollController.position.pixels == 0) { - await client.markRead(room); - } else { if (room.hasMore) await notifier.loadOlder(); + } else { + await client.markRead(room); } } @@ -323,7 +326,6 @@ class RoomChat extends HookConsumerWidget { } final controllerData = ref.watch(controllerProvider); - return Scaffold( appBar: RoomAppbar( roomId: roomId, @@ -345,12 +347,19 @@ class RoomChat extends HookConsumerWidget { child: switch (controllerData) { AsyncData(:final value) || AsyncLoading(:final value?) => CustomScrollView( - reverse: true, controller: scrollController, slivers: [ - SliverPadding( - padding: EdgeInsetsGeometry.only( - bottom: composerSize.value, + SliverToBoxAdapter( + child: Padding( + padding: EdgeInsets.symmetric(vertical: 36), + child: Center( + child: ElevatedButton( + onPressed: controllerData is AsyncLoading + ? null + : notifier.loadOlder, + child: Text("Load More"), + ), + ), ), ), @@ -359,7 +368,7 @@ class RoomChat extends HookConsumerWidget { itemCount: value.length, itemBuilder: (_, index) { final event = value[index]; - final previousEvent = value.getOrNull(index + 1); + final previousEvent = value.getOrNull(index - 1); return FlashWrapper( EventRenderer( event, @@ -401,17 +410,9 @@ class RoomChat extends HookConsumerWidget { }, ), - SliverToBoxAdapter( - child: Padding( - padding: EdgeInsets.symmetric(vertical: 36), - child: Center( - child: controllerData is AsyncLoading - ? Loading() - : ElevatedButton( - onPressed: notifier.loadOlder, - child: Text("Load More"), - ), - ), + SliverPadding( + padding: EdgeInsetsGeometry.only( + bottom: composerSize.value, ), ), ],