Skip to content

Commit

Permalink
update chat thread name
Browse files Browse the repository at this point in the history
  • Loading branch information
Dat-TG committed Dec 29, 2023
1 parent 204062a commit 904b90e
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 6 deletions.
62 changes: 62 additions & 0 deletions lib/core/widgets/change_chat_thread_name_dialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import 'package:boilerplate/di/service_locator.dart';
import 'package:boilerplate/domain/entity/message/chat_thread.dart';
import 'package:boilerplate/presentation/chat_screen/store/chat_store.dart';
import 'package:flutter/material.dart';

class ChangeChatThreadNameDialog extends StatefulWidget {
final ChatThread chatThread;
const ChangeChatThreadNameDialog({Key? key, required this.chatThread})
: super(key: key);
@override
State<ChangeChatThreadNameDialog> createState() =>
_ChangeChatThreadNameDialogState();
}

class _ChangeChatThreadNameDialogState
extends State<ChangeChatThreadNameDialog> {
final TextEditingController _nameController = TextEditingController();
ChatStore _chatStore = getIt<ChatStore>();

@override
void initState() {
_nameController.text = widget.chatThread.subject;
super.initState();
}

@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text('Change chat thread name'),
content: TextField(
controller: _nameController,
decoration: InputDecoration(
hintText: 'Name',
),
),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('Cancel'),
),
ElevatedButton(
onPressed: () {
String newName = _nameController.text;
// Use newName as needed, e.g., save to database
print('New name: $newName');
_chatStore
.updateChatThread(widget.chatThread.copyWith(subject: newName));
Navigator.of(context).pop();
},
child: Text(
'Save',
style: TextStyle(
color: Colors.white,
),
),
),
],
);
}
}
12 changes: 12 additions & 0 deletions lib/core/widgets/chat_thread.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:boilerplate/core/widgets/change_chat_thread_name_dialog.dart';
import 'package:boilerplate/di/service_locator.dart';
import 'package:boilerplate/domain/entity/message/chat_thread.dart';
import 'package:boilerplate/presentation/chat_screen/store/chat_store.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
Expand Down Expand Up @@ -51,7 +53,17 @@ class _ChatThreadWidgetState extends State<ChatThreadWidget> {
],
elevation: 8.0,
).then((value) {
ChatThread chatThread = _chatStore.chatThreads
.firstWhere((element) => element.id == widget.id);
if (value == 'rename') {
showDialog(
context: context,
builder: (BuildContext context) {
return ChangeChatThreadNameDialog(
chatThread: chatThread,
);
},
);
// Handle edit option
} else if (value == 'delete') {
// Handle delete option
Expand Down
11 changes: 11 additions & 0 deletions lib/domain/entity/message/chat_thread.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,15 @@ class ChatThread {
"subject": subject,
"messages": List<dynamic>.from(messages.map((x) => x.toMap())),
};
ChatThread copyWith({
int? id,
String? subject,
List<MessageWithTime>? messages,
}) {
return ChatThread(
id: id ?? this.id,
subject: subject ?? this.subject,
messages: messages ?? this.messages,
);
}
}
34 changes: 31 additions & 3 deletions lib/presentation/chat_screen/store/chat_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,15 @@ abstract class _ChatStore with Store {
int id = 0;

@observable
List<ChatThread> chatThreads = [];
ObservableList<ChatThread> chatThreads = ObservableList.of([]);

@observable
ObservableFuture<List<ChatThread>?> chatThreadsFuture =
ObservableFuture.value(null);

@observable
ObservableFuture<int?> updateChatThreadFuture = ObservableFuture.value(null);

@observable
ObservableFuture<Message?> sendMessageFuture = ObservableFuture.value(null);

Expand All @@ -68,6 +71,10 @@ abstract class _ChatStore with Store {
bool get isLoadingChatThreads =>
chatThreadsFuture.status == FutureStatus.pending;

@computed
bool get isUpdatingChatThread =>
updateChatThreadFuture.status == FutureStatus.pending;

// actions:-------------------------------------------------------------------
@action
Future setChatThreadId(int newId) async {
Expand All @@ -76,7 +83,7 @@ abstract class _ChatStore with Store {

@action
Future setChatThreads(List<ChatThread> newChatThreads) async {
chatThreads = newChatThreads;
chatThreads = ObservableList.of(newChatThreads);
}

@action
Expand All @@ -87,7 +94,28 @@ abstract class _ChatStore with Store {
await future.then((value) async {
print('response message ${value}');
this.success = true;
this.chatThreads = value;
this.chatThreads = ObservableList.of(value);
}).catchError((e) {
print(e);
this.success = false;
throw e;
});
}

@action
Future updateChatThread(ChatThread chatThread) async {
int index = chatThreads.indexWhere((e) => e.id == chatThread.id);
if (index != -1) {
chatThreads[index] = chatThread;
print('Object with ID $id updated');
} else {
print('Object with ID $id not found');
}
final future = _updateChatThreadUseCase.call(params: chatThread);
updateChatThreadFuture = ObservableFuture(future);
await future.then((value) async {
print('response message ${value}');
this.success = true;
}).catchError((e) {
print(e);
this.success = false;
Expand Down
41 changes: 38 additions & 3 deletions lib/presentation/chat_screen/store/chat_store.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 904b90e

Please sign in to comment.