favoriteChat method
- ChatId id,
- ChatFavoritePosition? position
override
Marks the specified Chat as favorited for the authenticated MyUser and sets its position in the favorites list.
Implementation
@override
Future<void> favoriteChat(ChatId id, ChatFavoritePosition? position) async {
Log.debug('favoriteChat($id, $position)', '$runtimeType');
final RxChatImpl? chat = chats[id];
final ChatFavoritePosition? oldPosition = chat?.chat.value.favoritePosition;
final ChatFavoritePosition newPosition;
if (position == null) {
final List<RxChatImpl> favorites =
chats.values
.where((e) => e.chat.value.favoritePosition != null)
.toList();
favorites.sort(
(a, b) => b.chat.value.favoritePosition!.compareTo(
a.chat.value.favoritePosition!,
),
);
final double? highestFavorite =
favorites.isEmpty
? null
: favorites.first.chat.value.favoritePosition!.val;
newPosition = ChatFavoritePosition(
highestFavorite == null ? 1 : highestFavorite * 2,
);
} else {
newPosition = position;
}
chat?.chat.update((c) => c?.favoritePosition = newPosition);
paginated.emit(MapChangeNotification.updated(chat?.id, chat?.id, chat));
try {
if (id.isLocalWith(me)) {
_localMonologFavoritePosition = newPosition;
final ChatData monolog = _chat(
await _graphQlProvider.createMonologChat(),
);
id = monolog.chat.value.id;
await Future.wait([
_monologLocal.upsert(me, this.monolog = id),
_putEntry(monolog, ignoreVersion: true),
]);
} else if (id.isLocal) {
final RxChatImpl? chat = await ensureRemoteDialog(id);
if (chat != null) {
id = chat.id;
}
}
if (!id.isLocal) {
await _graphQlProvider.favoriteChat(id, newPosition);
}
} catch (e) {
if (chat?.chat.value.isMonolog == true) {
_localMonologFavoritePosition = null;
}
chat?.chat.update((c) => c?.favoritePosition = oldPosition);
paginated.emit(MapChangeNotification.updated(chat?.id, chat?.id, chat));
rethrow;
}
}