Skip to content

Commit 10d4038

Browse files
committed
Fixed show only Selected Contacts feature
1 parent e149595 commit 10d4038

File tree

3 files changed

+90
-36
lines changed

3 files changed

+90
-36
lines changed

lib/api/apis.dart

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,39 @@ class APIs{
151151
return await firestore.collection('users').doc(user.uid).set(chatUser.toJson());
152152
}
153153

154+
// For getting ID of known users from Firestore Database
155+
static Stream<QuerySnapshot<Map<String, dynamic>>> getMyUserId() {
156+
return APIs.firestore
157+
.collection('users')
158+
.doc(user.uid)
159+
.collection('my_users')
160+
.snapshots();
161+
}
162+
154163
// For getting all Users from Firestore Database
155-
static Stream<QuerySnapshot<Map<String, dynamic>>> getAllUsers(){
156-
return APIs.firestore.collection('users').where('id', isNotEqualTo: user.uid).snapshots();
164+
static Stream<QuerySnapshot<Map<String, dynamic>>> getAllUsers(List<String> userIds){
165+
log('\nUserIds: $userIds');
166+
return APIs.firestore
167+
.collection('users')
168+
.where('id',
169+
whereIn: userIds.isEmpty
170+
? ['']
171+
: userIds) //because empty list throws an error
172+
// .where('id', whereIn: userIds)
173+
//.where('id', isNotEqualTo: user.uid)
174+
.snapshots();
157175
}
158176

177+
// for adding an user to my user when first message is send
178+
static Future<void> sendFirstMessage(ChatUser chatUser, String msg, Type type) async {
179+
await firestore
180+
.collection('users')
181+
.doc(chatUser.id)
182+
.collection('my_users')
183+
.doc(user.uid)
184+
.set({}).then((value) => sendMessage(chatUser, msg, type));
185+
}
186+
159187
// For Updating User Info
160188
static Future<void> updateUserInfo()async{
161189
await firestore.collection('users').doc(auth.currentUser!.uid).update(

lib/screens/chat_screen.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,13 @@ Widget _chatInput(){
314314
//Send Messdage Button
315315
MaterialButton(onPressed: (){
316316
if(_textController.text.isNotEmpty){
317-
APIs.sendMessage(widget.user, _textController.text, Type.text);
317+
if(_list.isEmpty){
318+
// On first Message add user to my_user collection of chat
319+
APIs.sendFirstMessage(widget.user, _textController.text, Type.text);
320+
}else {
321+
// Simply send message
322+
APIs.sendMessage(widget.user, _textController.text, Type.text);
323+
}
318324
_textController.text = '';
319325
}
320326
},

lib/screens/home_screen.dart

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -110,52 +110,72 @@ class _HomeScreenState extends State<HomeScreen> {
110110
//Floating Button Icon to add new user
111111
floatingActionButton: Padding(
112112
padding: const EdgeInsets.only(bottom: 35),
113-
child: FloatingActionButton(onPressed: () async {
114-
//_addChatUserDialog();
113+
child: FloatingActionButton(onPressed: () {
114+
_addChatUserDialog();
115115
},
116116
child: const Icon(Icons.add_circle_outline),
117117
),
118118
),
119119

120120
body: StreamBuilder(
121-
stream: APIs.getAllUsers(),
122-
builder: (context, snapshot) {
121+
stream: APIs.getMyUserId(),
123122

124-
switch (snapshot.connectionState){
125-
// If Data is Loading
123+
//get id of only known users
124+
builder: (context, snapshot) {
125+
switch (snapshot.connectionState) {
126+
//if data is loading
126127
case ConnectionState.waiting:
127128
case ConnectionState.none:
128-
return const Center(child: CircularProgressIndicator(),);
129+
return const Center(child: CircularProgressIndicator());
129130

130-
// If some or all Data is loaded then show it
131+
//if some or all data is loaded then show it
131132
case ConnectionState.active:
132133
case ConnectionState.done:
133-
134-
135-
final data = snapshot.data?.docs;
136-
_list = data?.map((e) => ChatUser.fromJson(e.data())).toList() ?? [];
137-
138-
if (_list.isNotEmpty){
139-
return ListView.builder(
140-
itemCount: _isSearching ? _searchList.length : _list.length,
141-
padding: EdgeInsets.only(top: mq.height * .01),
142-
physics: const BouncingScrollPhysics(),
143-
itemBuilder: (context, index){
144-
return ChatUserCard(
145-
user: _isSearching ? _searchList[index] : _list[index],);
146-
// return Text('Name: ${list[index]}');
147-
});
148-
}else{
149-
return const Center(
150-
child: Text('No Connections Found!',
151-
style: TextStyle(
152-
fontSize: 20,
153-
),),
134+
return StreamBuilder(
135+
stream: APIs.getAllUsers(
136+
snapshot.data?.docs.map((e) => e.id).toList() ?? []),
137+
138+
//get only those user, who's ids are provided
139+
builder: (context, snapshot) {
140+
switch (snapshot.connectionState) {
141+
//if data is loading
142+
case ConnectionState.waiting:
143+
case ConnectionState.none:
144+
// return const Center(
145+
// child: CircularProgressIndicator());
146+
147+
//if some or all data is loaded then show it
148+
case ConnectionState.active:
149+
case ConnectionState.done:
150+
final data = snapshot.data?.docs;
151+
_list = data
152+
?.map((e) => ChatUser.fromJson(e.data()))
153+
.toList() ??
154+
[];
155+
156+
if (_list.isNotEmpty) {
157+
return ListView.builder(
158+
itemCount: _isSearching
159+
? _searchList.length
160+
: _list.length,
161+
padding: EdgeInsets.only(top: mq.height * .01),
162+
physics: const BouncingScrollPhysics(),
163+
itemBuilder: (context, index) {
164+
return ChatUserCard(
165+
user: _isSearching
166+
? _searchList[index]
167+
: _list[index]);
168+
});
169+
} else {
170+
return const Center(
171+
child: Text('No Connections Found!',
172+
style: TextStyle(fontSize: 20)),
173+
);
174+
}
175+
}
176+
},
154177
);
155-
}
156178
}
157-
158-
159179
},
160180
),
161181
),
@@ -184,7 +204,7 @@ class _HomeScreenState extends State<HomeScreen> {
184204
color: Colors.deepOrange,
185205
size: 28,
186206
),
187-
Text(' Add User')
207+
Text(' Add User')
188208
],
189209
),
190210

0 commit comments

Comments
 (0)