Skip to content

Commit 8be06ca

Browse files
committed
Fixing issues
1 parent 2774a15 commit 8be06ca

8 files changed

Lines changed: 70 additions & 48 deletions

File tree

example/fji_example/lib/main.dart

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'package:fji_example/firebase_options.dart';
66
import 'package:fji_example_core_flutter/model.dart';
77
import 'package:flutter/material.dart';
88
import 'package:flutter_chat_ui/flutter_chat_ui.dart';
9-
import 'package:flutter_chat_types/flutter_chat_types.dart' as types;
9+
import 'package:flutter_chat_core/flutter_chat_core.dart' as chat;
1010

1111
void main() async {
1212
WidgetsFlutterBinding.ensureInitialized();
@@ -23,26 +23,31 @@ void main() async {
2323
await auth.currentUser?.delete();
2424

2525
// This will call the `beforeUserCreated` function
26-
await auth.createUserWithEmailAndPassword(
26+
final credential = await auth.createUserWithEmailAndPassword(
2727
email: 'test@example.com',
2828
password: 'password1234',
2929
);
3030

31-
runApp(const MaterialApp(home: ChatsList()));
31+
final uid = credential.user?.uid;
32+
if (uid == null) throw 'Failed to create user';
33+
34+
runApp(MaterialApp(home: ChatsList(uid: uid)));
3235
}
3336

3437
class ChatsList extends StatelessWidget {
35-
static final _auth = FirebaseAuth.instance;
38+
final String uid;
3639

37-
const ChatsList({super.key});
40+
const ChatsList({super.key, required this.uid});
3841

3942
@override
4043
Widget build(BuildContext context) {
44+
final navigator = Navigator.of(context);
45+
4146
return Scaffold(
4247
appBar: AppBar(title: const Text('Chats')),
4348
body: FirestoreListView(
4449
query: chatsRef
45-
.whereParticipants(arrayContains: _auth.currentUser!.uid)
50+
.whereParticipants(arrayContains: uid)
4651
.orderByLastMessageTime(descending: true)
4752
.reference,
4853
emptyBuilder: (context) => const Center(child: Text('No chats')),
@@ -58,7 +63,7 @@ class ChatsList extends StatelessWidget {
5863
],
5964
),
6065
subtitle: lastMessage != null ? Text(lastMessage) : null,
61-
onTap: () => showChat(Navigator.of(context), snap.id),
66+
onTap: () => showChat(navigator, snap.id),
6267
leading: const Icon(Icons.chat),
6368
);
6469
},
@@ -73,24 +78,24 @@ class ChatsList extends StatelessWidget {
7378
void createChat(BuildContext context) async {
7479
final navigator = Navigator.of(context);
7580
final doc = chatsRef.doc();
76-
final chat = FjiChat(id: doc.id, participants: {_auth.currentUser!.uid});
81+
final chat = FjiChat(id: doc.id, participants: {uid});
7782
await doc.set(chat);
7883
showChat(navigator, doc.id);
7984
}
8085

8186
void showChat(NavigatorState navigator, String chatId) {
8287
navigator.push(
83-
MaterialPageRoute(builder: (context) => ChatScreen(chatId: chatId)),
88+
MaterialPageRoute(
89+
builder: (context) => ChatScreen(uid: uid, chatId: chatId)),
8490
);
8591
}
8692
}
8793

8894
class ChatScreen extends StatelessWidget {
89-
static final _auth = FirebaseAuth.instance;
90-
95+
final String uid;
9196
final String chatId;
9297

93-
const ChatScreen({super.key, required this.chatId});
98+
const ChatScreen({super.key, required this.uid, required this.chatId});
9499

95100
@override
96101
Widget build(BuildContext context) {
@@ -106,39 +111,49 @@ class ChatScreen extends StatelessWidget {
106111
.messages
107112
.orderByTimestamp(descending: true)
108113
.reference,
109-
builder: (context, snap, child) {
114+
builder: (context, snap, _) {
110115
if (snap.hasError) {
111116
return Center(child: Text('Error\n${snap.error}'));
112117
}
113118
if (snap.isFetching) {
114119
return const Center(child: CircularProgressIndicator());
115120
}
116121
return Chat(
117-
user: types.User(id: _auth.currentUser!.uid),
118-
messages: snap.docs
119-
.map(
120-
(snap) => types.TextMessage(
121-
id: snap.id,
122-
createdAt: snap.data().timestamp.millisecondsSinceEpoch,
123-
author: types.User(id: snap.data().author),
124-
text: snap.data().text,
125-
),
126-
)
127-
.toList(),
128-
onEndReached: () async => snap.fetchMore(),
129-
isLastPage: !snap.hasMore,
130-
onSendPressed: (partial) {
131-
final text = partial.text;
132-
if (text.isEmpty) return;
133-
// This will call the `onMessageCreated` function
134-
chatsRef.doc(chatId).messages.doc().set(
135-
FjiMessage(
136-
author: _auth.currentUser!.uid,
137-
text: text,
138-
),
139-
timestampFieldValue: FieldValue.serverTimestamp(),
140-
);
122+
currentUserId: uid,
123+
resolveUser: (id) async {
124+
final snapshot = await usersRef.doc(id).get();
125+
final data = snapshot.data;
126+
if (data == null) return null;
127+
128+
return chat.User(id: id, name: data.displayName);
141129
},
130+
chatController: chat.InMemoryChatController(
131+
messages: snap.docs
132+
.map(
133+
(e) => chat.Message.text(
134+
id: e.id,
135+
authorId: e.data().author,
136+
createdAt: e.data().timestamp,
137+
text: e.data().text,
138+
),
139+
)
140+
.toList(),
141+
),
142+
onMessageSend: (text) =>
143+
chatsRef.doc(chatId).messages.doc().set(
144+
FjiMessage(
145+
author: uid,
146+
text: text,
147+
),
148+
timestampFieldValue: FieldValue.serverTimestamp(),
149+
),
150+
builders: chat.Builders(
151+
chatAnimatedListBuilder: (context, itemBuilder) =>
152+
ChatAnimatedList(
153+
itemBuilder: itemBuilder,
154+
onEndReached: () async => snap.fetchMore(),
155+
),
156+
),
142157
);
143158
},
144159
);

example/fji_example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ dependencies:
1818
firebase_auth: ^6.1.2
1919
firebase_ui_firestore: ^2.0.1
2020
flutter_chat_ui: ^2.9.1
21-
flutter_chat_types: ^3.6.2
21+
flutter_chat_core: ^2.8.0
2222

2323
dev_dependencies:
2424
flutter_test:

example/fji_example_core/lib/model.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
1+
import 'package:meta/meta.dart';
2+
3+
@immutable
14
class FjiUser {
25
final String? displayName;
36
final String? profileImage;
47
final Set<String> fcmTokens;
58

6-
FjiUser({
9+
const FjiUser({
710
this.displayName,
811
this.profileImage,
912
this.fcmTokens = const {},
1013
});
1114
}
1215

16+
@immutable
1317
class FjiChat {
1418
final Set<String> participants;
1519
final String? lastMessage;
1620
final DateTime? lastMessageTime;
1721

18-
FjiChat({
22+
const FjiChat({
1923
required this.participants,
2024
this.lastMessage,
2125
this.lastMessageTime,
2226
});
2327
}
2428

29+
@immutable
2530
class FjiMessage {
2631
final DateTime timestamp;
2732
final String author;

example/fji_example_core/pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ environment:
77

88
dev_dependencies:
99
rexios_lints: ^16.0.0
10+
dependencies:
11+
meta: ^1.16.0

example/fji_example_core_flutter/lib/model.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const firestoreSerializable = JsonSerializable(
1313

1414
@firestoreSerializable
1515
class FjiUser extends core.FjiUser {
16-
FjiUser({
16+
const FjiUser({
1717
super.displayName,
1818
super.profileImage,
1919
super.fcmTokens,
@@ -25,7 +25,7 @@ class FjiChat extends core.FjiChat {
2525
@Id()
2626
final String id;
2727

28-
FjiChat({
28+
const FjiChat({
2929
required this.id,
3030
required super.participants,
3131
super.lastMessage,

example/fji_example_core_js_interop/lib/model.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ part 'model.g.dart';
66

77
@firestoreJsInteropSerializable
88
class FjiUser extends core.FjiUser {
9-
FjiUser({
9+
const FjiUser({
1010
super.displayName,
1111
super.profileImage,
1212
super.fcmTokens,
@@ -19,7 +19,7 @@ class FjiUser extends core.FjiUser {
1919

2020
@firestoreJsInteropSerializable
2121
class FjiChat extends core.FjiChat {
22-
FjiChat({
22+
const FjiChat({
2323
required super.participants,
2424
super.lastMessage,
2525
super.lastMessageTime,

example/fji_example_firebase/functions/src/index.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ void main() {
4242
(FirestoreEvent<QueryDocumentSnapshot> event) {
4343
// Make sure to return promises for async operations
4444
return promise(() async {
45-
final chatId = event.params['chatId'] as JSString;
46-
final chatDoc = firestore.collection('chats').doc(chatId.toDart);
45+
final chatId = event.params['chatId'];
46+
if (chatId == null) return;
47+
48+
final chatDoc = firestore.collection('chats').doc(chatId);
4749
final chatSnapshot = await chatDoc.get().toDart;
4850
final chat = FjiChat.fromJson(chatSnapshot.data().toJson());
4951

lib/src/functions/common/change.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222

23-
2423
// This is coped verbatim from the JS SDK
2524
// ignore_for_file: doc_directive_unknown
2625

2726
import 'dart:js_interop';
2827

29-
3028
/// The Cloud Functions interface for events that change state, such as
3129
/// Realtime Database or Cloud Firestore `onWrite` and `onUpdate` events.
3230
///

0 commit comments

Comments
 (0)