Skip to content

Commit d38053e

Browse files
committed
✨(feat) Last message can be seen at user chat list
1.:children_crossing:Last message and message time is added on chat userlist page. 3.:card_file_box: Update chat user list db schema. 2.:heavy_minus_sign: Remove unnecessary plugins. 3.:see_no_evil: update .gitignore file. 4.:fire: Remove unused comment model.
1 parent 338ba0f commit d38053e

File tree

9 files changed

+58
-107
lines changed

9 files changed

+58
-107
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,9 @@ GoogleService-Info.plist
8080
android/app/google-services.json
8181
google-services.json
8282

83+
android/.settings/org.eclipse.buildship.core.prefs
84+
android/app/.classpath
85+
android/.project
86+
android/app/.project
87+
android/app/.settings/org.eclipse.buildship.core.prefs
88+
android/app/.settings/org.eclipse.jdt.core.prefs

lib/model/chatModel.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'dart:convert';
2-
31
class ChatMessage {
42
String key;
53
String senderId;
@@ -30,6 +28,8 @@ class ChatMessage {
3028
seen: json["seen"],
3129
createdAt: json["created_at"],
3230
timeStamp:json['timeStamp'],
31+
senderName: json["senderName"],
32+
receiverId: json["receiverId"]
3333
);
3434

3535
Map<String, dynamic> toJson() => {

lib/model/commentModel.dart

Lines changed: 0 additions & 59 deletions
This file was deleted.

lib/model/fcmNotificationModel.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import 'dart:convert';
22

33
import 'package:flutter_twitter_clone/helper/enum.dart';
4-
4+
/// This model isn't used anywhere in code
5+
/// Can be removed/deleted if required
56
class FcmNotificationModel {
67
String clickAction;
78
String id;

lib/page/message/chatListPage.dart

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import 'package:flutter/material.dart';
2-
import 'package:flutter/scheduler.dart';
32
import 'package:flutter_twitter_clone/helper/constant.dart';
43
import 'package:flutter_twitter_clone/helper/theme.dart';
54
import 'package:flutter_twitter_clone/helper/utility.dart';
5+
import 'package:flutter_twitter_clone/model/chatModel.dart';
66
import 'package:flutter_twitter_clone/model/user.dart';
7-
import 'package:flutter_twitter_clone/page/common/sidebar.dart';
8-
import 'package:flutter_twitter_clone/page/message/newMessagePage.dart';
97
import 'package:flutter_twitter_clone/state/authState.dart';
108
import 'package:flutter_twitter_clone/state/chats/chatState.dart';
11-
import 'package:flutter_twitter_clone/state/notificationState.dart';
129
import 'package:flutter_twitter_clone/state/searchState.dart';
1310
import 'package:flutter_twitter_clone/widgets/customAppBar.dart';
1411
import 'package:flutter_twitter_clone/widgets/customWidgets.dart';
@@ -37,6 +34,7 @@ class _ChatListPageState extends State<ChatListPage> {
3734

3835
Widget _body() {
3936
final state = Provider.of<ChatState>(context);
37+
final searchState = Provider.of<SearchState>(context, listen: false);
4038
if (state.chatUserList == null) {
4139
return Padding(
4240
padding: EdgeInsets.symmetric(horizontal: 30),
@@ -50,7 +48,12 @@ class _ChatListPageState extends State<ChatListPage> {
5048
return ListView.separated(
5149
physics: BouncingScrollPhysics(),
5250
itemCount: state.chatUserList.length,
53-
itemBuilder: (context, index) => _userCard(state.chatUserList[index]),
51+
itemBuilder: (context, index) => _userCard(
52+
searchState.userlist.firstWhere(
53+
(x) => x.userId == state.chatUserList[index].key,
54+
orElse: () => User(userName: "Unknown"),
55+
),
56+
state.chatUserList[index]),
5457
separatorBuilder: (context, index) {
5558
return Divider(
5659
height: 0,
@@ -60,7 +63,7 @@ class _ChatListPageState extends State<ChatListPage> {
6063
}
6164
}
6265

63-
Widget _userCard(User model) {
66+
Widget _userCard(User model, ChatMessage lastMessage) {
6467
return Container(
6568
color: Colors.white,
6669
child: ListTile(
@@ -101,9 +104,14 @@ class _ChatListPageState extends State<ChatListPage> {
101104
style: onPrimaryTitleText.copyWith(color: Colors.black),
102105
),
103106
subtitle: customText(
104-
'@${model.displayName}',
107+
getLastMessage(lastMessage.message) ?? '@${model.displayName}',
105108
style: onPrimarySubTitleText.copyWith(color: Colors.black54),
106109
),
110+
trailing: lastMessage == null
111+
? SizedBox.shrink()
112+
: Text(
113+
getChatTime(lastMessage.createdAt).toString(),
114+
),
107115
),
108116
);
109117
}
@@ -127,6 +135,18 @@ class _ChatListPageState extends State<ChatListPage> {
127135
Navigator.pushNamed(context, '/DirectMessagesPage');
128136
}
129137

138+
String getLastMessage(String message) {
139+
if (message != null && message.isNotEmpty) {
140+
if (message.length > 100) {
141+
message = message.substring(0, 80) + '...';
142+
return message;
143+
} else {
144+
return message;
145+
}
146+
}
147+
return null;
148+
}
149+
130150
@override
131151
Widget build(BuildContext context) {
132152
return Scaffold(

lib/page/message/chatScreenPage.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class _ChatScreenPageState extends State<ChatScreenPage> {
9191
myMessage
9292
? SizedBox()
9393
: CircleAvatar(
94+
backgroundColor: Colors.transparent,
9495
backgroundImage: customAdvanceNetworkImage(userImage),
9596
),
9697
Expanded(

lib/state/chats/chatState.dart

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ChatState extends AppState {
1414
final FirebaseDatabase _database = FirebaseDatabase.instance;
1515

1616
List<ChatMessage> _messageList;
17-
List<User> _chatUserList;
17+
List<ChatMessage> _chatUserList;
1818
User _chatUser;
1919
String serverToken = "<FCM SERVER KEY>";
2020

@@ -40,7 +40,7 @@ class ChatState extends AppState {
4040
}
4141
}
4242

43-
List<User> get chatUserList {
43+
List<ChatMessage> get chatUserList {
4444
if (_chatUserList == null) {
4545
return null;
4646
} else {
@@ -96,12 +96,12 @@ class ChatState extends AppState {
9696
.child(userId)
9797
.once()
9898
.then((DataSnapshot snapshot) {
99-
_chatUserList = List<User>();
99+
_chatUserList = List<ChatMessage>();
100100
if (snapshot.value != null) {
101101
var map = snapshot.value;
102102
if (map != null) {
103103
map.forEach((key, value) {
104-
var model = User.fromJson(value);
104+
var model = ChatMessage.fromJson(value);
105105
model.key = key;
106106
_chatUserList.add(model);
107107
});
@@ -145,22 +145,23 @@ class ChatState extends AppState {
145145
}
146146

147147
void onMessageSubmitted(ChatMessage message, {User myUser, User secondUser}) {
148+
print(chatUser.userId);
148149
try {
149-
if (_messageList == null || _messageList.length < 1) {
150+
// if (_messageList == null || _messageList.length < 1) {
150151
_database
151152
.reference()
152153
.child('chatUsers')
153154
.child(message.senderId)
154155
.child(message.receiverId)
155-
.set(secondUser.toJson());
156+
.set(message.toJson());
156157

157158
_database
158159
.reference()
159160
.child('chatUsers')
160-
.child(secondUser.userId)
161+
.child(chatUser.userId)
161162
.child(message.senderId)
162-
.set(myUser.toJson());
163-
}
163+
.set(message.toJson());
164+
// }
164165
_database
165166
.reference()
166167
.child('chats')
@@ -228,12 +229,12 @@ class ChatState extends AppState {
228229

229230
void _onChatUserAdded(Event event) {
230231
if (_chatUserList == null) {
231-
_chatUserList = List<User>();
232+
_chatUserList = List<ChatMessage>();
232233
}
233234
if (event.snapshot.value != null) {
234235
var map = event.snapshot.value;
235236
if (map != null) {
236-
var model = User.fromJson(map);
237+
var model = ChatMessage.fromJson(map);
237238
model.key = event.snapshot.key;
238239
if (_chatUserList.length > 0 &&
239240
_chatUserList.any((x) => x.key == model.key)) {
@@ -248,9 +249,11 @@ class ChatState extends AppState {
248249
}
249250

250251
void dispose() {
251-
// messageQuery = null;
252+
var user = _chatUserList.firstWhere((x) => x.key == chatUser.userId);
253+
user.message = _messageList.first.message;
254+
user.createdAt = _messageList.first.createdAt;//;
252255
_messageList = null;
253-
// _channelName = null;
256+
notifyListeners();
254257
}
255258

256259
final FirebaseMessaging firebaseMessaging = FirebaseMessaging();

pubspec.lock

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ packages:
3030
source: hosted
3131
version: "1.5.2"
3232
async:
33-
dependency: "direct main"
33+
dependency: transitive
3434
description:
3535
name: async
3636
url: "https://pub.dartlang.org"
@@ -57,13 +57,6 @@ packages:
5757
url: "https://pub.dartlang.org"
5858
source: hosted
5959
version: "1.14.11"
60-
color:
61-
dependency: transitive
62-
description:
63-
name: color
64-
url: "https://pub.dartlang.org"
65-
source: hosted
66-
version: "2.1.1"
6760
convert:
6861
dependency: transitive
6962
description:
@@ -99,13 +92,6 @@ packages:
9992
url: "https://pub.dartlang.org"
10093
source: hosted
10194
version: "0.1.3"
102-
empty_widget:
103-
dependency: "direct main"
104-
description:
105-
name: empty_widget
106-
url: "https://pub.dartlang.org"
107-
source: hosted
108-
version: "0.0.1"
10995
firebase_analytics:
11096
dependency: "direct main"
11197
description:
@@ -188,13 +174,6 @@ packages:
188174
url: "https://pub.dartlang.org"
189175
source: hosted
190176
version: "0.7.4"
191-
flutter_native_splash:
192-
dependency: "direct main"
193-
description:
194-
name: flutter_native_splash
195-
url: "https://pub.dartlang.org"
196-
source: hosted
197-
version: "0.1.9"
198177
flutter_svg:
199178
dependency: transitive
200179
description:

pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ dependencies:
2323
# The following adds the Cupertino Icons font to your application.
2424
# Use with the CupertinoIcons class for iOS style icons.
2525
cupertino_icons: ^0.1.2
26-
empty_widget:
26+
# empty_widget:
2727
flutter_advanced_networkimage: ^0.6.0-alpha.1
2828
provider:
29-
flutter_native_splash: ^0.1.9
29+
# flutter_native_splash: ^0.1.9
3030
firebase_auth:
3131
firebase_database:
3232
firebase_analytics:
@@ -35,7 +35,7 @@ dependencies:
3535
firebase_storage:
3636
image_picker: ^0.6.2
3737
uuid: ^2.0.0
38-
async:
38+
# async:
3939
http:
4040
shared_preferences: ^0.5.1+2
4141
firebase_messaging: ^6.0.13

0 commit comments

Comments
 (0)