-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat_store.dart
148 lines (124 loc) · 4.25 KB
/
chat_store.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import 'package:boilerplate/core/stores/error/error_store.dart';
import 'package:boilerplate/domain/entity/message/chat_thread.dart';
import 'package:boilerplate/domain/entity/message/message.dart';
import 'package:boilerplate/domain/entity/message/message_with_time.dart';
import 'package:boilerplate/domain/usecase/message/get_all_chat_threads_usecase.dart';
import 'package:boilerplate/domain/usecase/message/send_message_usecase.dart';
import 'package:boilerplate/domain/usecase/message/update_chat_thread_usecase.dart';
import 'package:mobx/mobx.dart';
part 'chat_store.g.dart';
class ChatStore = _ChatStore with _$ChatStore;
abstract class _ChatStore with Store {
// constructor:---------------------------------------------------------------
_ChatStore(
this.errorStore,
this._sendMessageUseCase,
this._getAllChatThreadsUseCase,
this._updateChatThreadUseCase,
) {
// load all chat threads
getAllChatThreads();
// setting up disposers
_setupDisposers();
}
// use cases:-----------------------------------------------------------------
final SendMessageUseCase _sendMessageUseCase;
final GetAllChatThreadsUseCase _getAllChatThreadsUseCase;
final UpdateChatThreadUseCase _updateChatThreadUseCase;
// store for handling error messages
final ErrorStore errorStore;
// disposers:-----------------------------------------------------------------
late List<ReactionDisposer> _disposers;
void _setupDisposers() {
_disposers = [
reaction((_) => success, (_) => success = false, delay: 200),
];
}
// store variables:-----------------------------------------------------------
@observable
bool success = false;
@observable
int id = 0;
@observable
List<ChatThread> chatThreads = [];
@observable
ObservableFuture<List<ChatThread>?> chatThreadsFuture =
ObservableFuture.value(null);
@observable
ObservableFuture<Message?> sendMessageFuture = ObservableFuture.value(null);
@observable
ObservableFuture<int?> updateChatThreadFuture = ObservableFuture.value(null);
@computed
bool get isLoading =>
sendMessageFuture.status == FutureStatus.pending ||
updateChatThreadFuture.status == FutureStatus.pending;
@computed
bool get isLoadingChatThreads =>
chatThreadsFuture.status == FutureStatus.pending;
// actions:-------------------------------------------------------------------
@action
Future setChatThreadId(int newId) async {
id = newId;
}
@action
Future setChatThreads(List<ChatThread> newChatThreads) async {
chatThreads = newChatThreads;
}
@action
Future getAllChatThreads() async {
final future = _getAllChatThreadsUseCase.call();
chatThreadsFuture = ObservableFuture(future);
await future.then((value) async {
print('response message ${value}');
this.success = true;
this.chatThreads = value;
}).catchError((e) {
print(e);
this.success = false;
throw e;
});
}
@action
Future sendMessage(String content) async {
final Message message = Message(role: Role.user, content: content);
final time = DateTime.now();
final messages = chatThreads
.firstWhere((element) => element.id == id)
.messages
.map((e) => e.message)
.toList();
chatThreads
.firstWhere((element) => element.id == id)
.messages
.add(MessageWithTime(message, time));
final future = _sendMessageUseCase.call(params: [...messages, message]);
sendMessageFuture = ObservableFuture(future);
await future.then((value) async {
print('response message ${value.toMap()}');
final newChatThread =
chatThreads.firstWhere((element) => element.id == id);
newChatThread.messages.add(
MessageWithTime(
value,
DateTime.now(),
),
);
print(newChatThread);
final future = _updateChatThreadUseCase.call(
params: newChatThread,
);
updateChatThreadFuture = ObservableFuture(future);
await future.then((value) => {this.success = true});
}).catchError((e) {
print(e);
this.success = false;
throw e;
});
}
// general methods:-----------------------------------------------------------
void dispose() {
for (final d in _disposers) {
d();
}
}
}