Skip to content

Commit 4e87808

Browse files
committed
💡 (doc) Add code documentation of chatstate.
1 parent 03641fe commit 4e87808

File tree

2 files changed

+54
-28
lines changed

2 files changed

+54
-28
lines changed

lib/page/feed/composeTweet/state/composeTweetState.dart

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class ComposeTweetState extends ChangeNotifier {
2424
}
2525

2626
/// Display/Hide userlist on the basis of username availability in description
27+
/// To display userlist in compose screen two condion is required
28+
/// First is value of `status` should be true
29+
/// Second value of `hideUserList` should be false
2730
bool get displayUserList {
2831
RegExp regExp = new RegExp(usernameRegex);
2932
var status = regExp.hasMatch(description);
@@ -34,12 +37,18 @@ class ComposeTweetState extends ChangeNotifier {
3437
}
3538
}
3639

37-
/// Hide userlist when a username is selected from userlist
40+
/// Hide userlist when a user select a username from userlist
3841
void onUserSelected() {
3942
hideUserList = true;
4043
notifyListeners();
4144
}
4245

46+
/// This method will trigger every time when user writes tweet description.
47+
/// `hideUserList` is set to false to reset user list show flag.
48+
/// If description is not empty and its lenth is lesser then 280 characters
49+
/// then value of `enableSubmitButton` is set to true.
50+
///
51+
/// `enableSubmitButton` is responsible to enable/disable tweet submit button
4352
void onDescriptionChanged(String text, SearchState searchState) {
4453
description = text;
4554
hideUserList = false;
@@ -54,7 +63,7 @@ class ComposeTweetState extends ChangeNotifier {
5463
enableSubmitButton = true;
5564
var last = text.substring(text.length - 1, text.length);
5665

57-
/// Regex to search last username from description
66+
/// Regex to search last username available from description
5867
/// Ex. `Hello @john do you know @ricky`
5968
/// In above description reegex is serch for last username ie. `@ricky`.
6069
@@ -88,8 +97,25 @@ class ComposeTweetState extends ChangeNotifier {
8897
return description;
8998
}
9099

91-
/// Fecth FCM server key from firebase Remote config
100+
/// Fetch FCM server key from firebase Remote config
101+
/// FCM server key is stored in firebase remote config
102+
/// you have to add server key in firebase remote config
103+
/// To fetch this key go to project setting in firebase
104+
/// Click on `cloud messaging` tab
105+
/// Copy server key from `Project credentials`
106+
/// Now goto `Remote Congig` section in fireabse
107+
/// Add [FcmServerKey] as paramerter key and below json in Default vslue
108+
/// ``` json
109+
/// {
110+
/// "key": "FCM server key here"
111+
/// } ```
112+
/// For more detail visit:- https://github.com/TheAlphamerc/flutter_twitter_clone/issues/28#issue-611695533
113+
/// For package detail check:- https://pub.dev/packages/firebase_remote_config#-readme-tab-
92114
Future<Null> getFCMServerKey() async {
115+
/// If FCM server key is already fetched then no need to fetch it again.
116+
if(serverToken != null && serverToken.isNotEmpty){
117+
return Future.value(null);
118+
}
93119
final RemoteConfig remoteConfig = await RemoteConfig.instance;
94120
await remoteConfig.fetch(expiration: const Duration(hours: 5));
95121
await remoteConfig.activateFetched();
@@ -98,16 +124,17 @@ class ComposeTweetState extends ChangeNotifier {
98124
serverToken = jsonDecode(data)["key"];
99125
}
100126
}
101-
127+
/// Fecth FCM server key from firebase Remote config
128+
/// send notification to user once fcmToken is retrieved from firebase
102129
Future<void> sendNotification(FeedModel model, SearchState state) async {
103130
final usernameRegex = r"(@\w*[a-zA-Z1-9])";
104131
RegExp regExp = new RegExp(usernameRegex);
105132
var status = regExp.hasMatch(description);
133+
/// Check if username is availeble in description or not
106134
if (status) {
107-
/// Fecth FCM server key from firebase Remote config
108-
/// send notification to user once fcmToken is retrieved from firebase
135+
/// Get FCM server key from firebase remote config
109136
getFCMServerKey().then((val) async {
110-
/// Reset userlist
137+
/// Reset userlist
111138
state.filterByUsername("");
112139

113140
/// Search all username from description

lib/state/chats/chatState.dart

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import 'package:flutter_twitter_clone/state/appState.dart';
1111

1212
class ChatState extends AppState {
1313
bool setIsChatScreenOpen;
14-
final FirebaseDatabase _database = FirebaseDatabase.instance;
14+
// final FirebaseDatabase _database = FirebaseDatabase.instance;
1515

1616
List<ChatMessage> _messageList;
1717
List<ChatMessage> _chatUserList;
@@ -53,21 +53,21 @@ class ChatState extends AppState {
5353
if (_channelName == null) {
5454
getChannelName(userId, myId);
5555
}
56-
_database
57-
.reference()
56+
kDatabase
5857
.child("chatUsers")
5958
.child(myId)
6059
.onChildAdded
6160
.listen(_onChatUserAdded);
6261
if (messageQuery == null || _channelName != getChannelName(userId, myId)) {
63-
messageQuery = _database.reference().child("chats").child(_channelName);
62+
messageQuery = kDatabase.child("chats").child(_channelName);
6463
messageQuery.onChildAdded.listen(_onMessageAdded);
6564
messageQuery.onChildChanged.listen(_onMessageChanged);
6665
}
6766
}
6867

68+
/// Fecth FCM server key from firebase Remote config
6969
/// FCM server key is stored in firebase remote config
70-
/// you have to save server key in firebase remote config
70+
/// you have to add server key in firebase remote config
7171
/// To fetch this key go to project setting in firebase
7272
/// Click on `cloud messaging` tab
7373
/// Copy server key from `Project credentials`
@@ -77,7 +77,8 @@ class ChatState extends AppState {
7777
/// {
7878
/// "key": "FCM server key here"
7979
/// } ```
80-
/// For more detail visit:- https://pub.dev/packages/firebase_remote_config#-readme-tab-
80+
/// For more detail visit:- https://github.com/TheAlphamerc/flutter_twitter_clone/issues/28#issue-611695533
81+
/// For package detail check:- https://pub.dev/packages/firebase_remote_config#-readme-tab-
8182
void getFCMServerKey() async {
8283
final RemoteConfig remoteConfig = await RemoteConfig.instance;
8384
await remoteConfig.fetch(expiration: const Duration(hours: 5));
@@ -88,10 +89,10 @@ class ChatState extends AppState {
8889
}
8990
}
9091

92+
/// Fetch users list to who have ever engaged in chat message with logged-in user
9193
void getUserchatList(String userId) {
9294
try {
93-
final databaseReference = FirebaseDatabase.instance.reference();
94-
databaseReference
95+
kDatabase
9596
.child('chatUsers')
9697
.child(userId)
9798
.once()
@@ -116,10 +117,12 @@ class ChatState extends AppState {
116117
}
117118
}
118119

120+
/// Fetch chat all chat messages
121+
/// `_channelName` is used as primary key for chat message table
122+
/// `_channelName` is created from by combining first 5 letters from user ids of two users
119123
void getchatDetailAsync() async {
120124
try {
121-
final databaseReference = FirebaseDatabase.instance.reference();
122-
databaseReference
125+
kDatabase
123126
.child('chats')
124127
.child(_channelName)
125128
.once()
@@ -148,26 +151,19 @@ class ChatState extends AppState {
148151
print(chatUser.userId);
149152
try {
150153
// if (_messageList == null || _messageList.length < 1) {
151-
_database
152-
.reference()
154+
kDatabase
153155
.child('chatUsers')
154156
.child(message.senderId)
155157
.child(message.receiverId)
156158
.set(message.toJson());
157159

158-
_database
159-
.reference()
160+
kDatabase
160161
.child('chatUsers')
161162
.child(chatUser.userId)
162163
.child(message.senderId)
163164
.set(message.toJson());
164-
// }
165-
_database
166-
.reference()
167-
.child('chats')
168-
.child(_channelName)
169-
.push()
170-
.set(message.toJson());
165+
166+
kDatabase.child('chats').child(_channelName).push().set(message.toJson());
171167
sendAndRetrieveMessage(message);
172168
logEvent('send_message');
173169
} catch (error) {
@@ -256,6 +252,9 @@ class ChatState extends AppState {
256252
_messageList = null;
257253
notifyListeners();
258254
}
255+
/// [Warning] do not call super.dispose() from this method
256+
/// If this method called here it will dipose chat state data
257+
// super.dispose();
259258
}
260259

261260
final FirebaseMessaging firebaseMessaging = FirebaseMessaging();

0 commit comments

Comments
 (0)