forked from AppFlowy-IO/AppFlowy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
521 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
frontend/appflowy_flutter/lib/plugins/ai_chat/application/chat_message_selector_bloc.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import 'package:appflowy/plugins/ai_chat/application/chat_entity.dart'; | ||
import 'package:appflowy/plugins/document/application/document_data_pb_extension.dart'; | ||
import 'package:appflowy/shared/markdown_to_document.dart'; | ||
import 'package:appflowy/workspace/application/view/prelude.dart'; | ||
import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:flutter_chat_core/flutter_chat_core.dart'; | ||
import 'package:freezed_annotation/freezed_annotation.dart'; | ||
|
||
part 'chat_message_selector_bloc.freezed.dart'; | ||
|
||
class ChatMessageSelectorBloc | ||
extends Bloc<ChatMessageSelectorEvent, ChatMessageSelectorState> { | ||
ChatMessageSelectorBloc({required this.parentViewId}) | ||
: super(const ChatMessageSelectorState()) { | ||
on<ChatMessageSelectorEvent>( | ||
(event, emit) { | ||
event.when( | ||
toggleSelectingMessages: () { | ||
if (state.isSelectingMessages) { | ||
// Clear selected messages when exiting selection mode | ||
return emit( | ||
state.copyWith( | ||
selectedMessages: [], | ||
isSelectingMessages: false, | ||
), | ||
); | ||
} | ||
|
||
emit(state.copyWith(isSelectingMessages: true)); | ||
}, | ||
toggleSelectMessage: (Message message) { | ||
if (state.selectedMessages.contains(message)) { | ||
emit( | ||
state.copyWith( | ||
selectedMessages: state.selectedMessages | ||
.where((m) => m != message) | ||
.toList(), | ||
), | ||
); | ||
} else { | ||
emit( | ||
state.copyWith( | ||
selectedMessages: [...state.selectedMessages, message], | ||
), | ||
); | ||
} | ||
}, | ||
selectAllMessages: (List<Message> messages) { | ||
final filtered = messages.where(isAIMessage).toList(); | ||
emit(state.copyWith(selectedMessages: filtered)); | ||
}, | ||
unselectAllMessages: () { | ||
emit(state.copyWith(selectedMessages: const [])); | ||
}, | ||
saveAsPage: () { | ||
String completeMessage = ''; | ||
for (final message in state.selectedMessages) { | ||
if (message is TextMessage) { | ||
completeMessage += '${message.text}\n\n'; | ||
} | ||
} | ||
|
||
if (completeMessage.isEmpty) { | ||
return; | ||
} | ||
|
||
final document = customMarkdownToDocument(completeMessage); | ||
final initialBytes = | ||
DocumentDataPBFromTo.fromDocument(document)?.writeToBuffer(); | ||
if (initialBytes != null) { | ||
ViewBackendService.createView( | ||
// TODO: Better name for the document? | ||
name: 'Test document', | ||
layoutType: ViewLayoutPB.Document, | ||
parentViewId: parentViewId, | ||
initialDataBytes: DocumentDataPBFromTo.fromDocument(document) | ||
?.writeToBuffer(), | ||
// TODO: Consider the location of this document? | ||
); | ||
} | ||
|
||
// Reset state when saving as page | ||
emit(const ChatMessageSelectorState()); | ||
}, | ||
); | ||
}, | ||
); | ||
} | ||
|
||
final String parentViewId; | ||
|
||
bool isMessageSelected(String messageId) => | ||
state.selectedMessages.any((m) => m.id == messageId); | ||
|
||
bool isAIMessage(Message message) { | ||
return message.author.id == aiResponseUserId || | ||
message.author.id == systemUserId || | ||
message.author.id.startsWith("streamId:"); | ||
} | ||
} | ||
|
||
@freezed | ||
class ChatMessageSelectorEvent with _$ChatMessageSelectorEvent { | ||
const factory ChatMessageSelectorEvent.toggleSelectingMessages() = | ||
_ToggleSelectingMessages; | ||
const factory ChatMessageSelectorEvent.toggleSelectMessage(Message message) = | ||
_ToggleSelectMessage; | ||
const factory ChatMessageSelectorEvent.selectAllMessages( | ||
List<Message> messages, | ||
) = _SelectAllMessages; | ||
const factory ChatMessageSelectorEvent.unselectAllMessages() = | ||
_UnselectAllMessages; | ||
const factory ChatMessageSelectorEvent.saveAsPage() = _SaveAsPage; | ||
} | ||
|
||
@freezed | ||
class ChatMessageSelectorState with _$ChatMessageSelectorState { | ||
const factory ChatMessageSelectorState({ | ||
@Default(false) bool isSelectingMessages, | ||
@Default([]) List<Message> selectedMessages, | ||
}) = _ChatMessageSelectorState; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.