Skip to content

Commit

Permalink
🗃Update notification , following and followers db schema.
Browse files Browse the repository at this point in the history
1.🗃Change in db schema.
2.:children_crossing: Last notificaiton on top implementation.
TheAlphamerc/flutter_twitter_clone#26
  • Loading branch information
horizonvert1027 committed May 3, 2020
1 parent 0868ad3 commit 5ee0b5d
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 182 deletions.
3 changes: 2 additions & 1 deletion lib/helper/enum.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ enum NotificationType{
Reply,
Retweet,
Follow,
Mention
Mention,
Like
}
195 changes: 79 additions & 116 deletions lib/model/feedModel.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'package:flutter_twitter_clone/model/user.dart';

class FeedModel {
Expand All @@ -8,140 +7,104 @@ class FeedModel {
String description;
String userId;
int likeCount;
List<LikeList> likeList;
List<String> likeList;
int commentCount;
int retweetCount;
String createdAt;
String imagePath;
List<String> tags;
List<String> replyTweetKeyList;
User user;
FeedModel({
this.key,
this.description,
this.userId,
this.likeCount,
this.commentCount,
this.retweetCount,
this.createdAt,
this.imagePath,
this.likeList,
this.tags,
this.user,
this.replyTweetKeyList,
this.parentkey,
this.childRetwetkey
});
FeedModel(
{this.key,
this.description,
this.userId,
this.likeCount,
this.commentCount,
this.retweetCount,
this.createdAt,
this.imagePath,
this.likeList,
this.tags,
this.user,
this.replyTweetKeyList,
this.parentkey,
this.childRetwetkey});
toJson() {
Map<dynamic,dynamic> map;
if(likeList != null && likeList.length > 0){
map = Map.fromIterable(likeList, key: (v) =>v.key, value: (v){
var list = LikeList(key: v.key, userId: v.userId);
return list.toJson();
});
}
return {
"userId": userId,
"description": description,
"likeCount":likeCount,
"commentCount":commentCount ?? 0,
"likeCount": likeCount,
"commentCount": commentCount ?? 0,
"retweetCount": retweetCount ?? 0,
"createdAt":createdAt,
"imagePath":imagePath,
"likeList":map,
"tags":tags,
"replyTweetKeyList":replyTweetKeyList,
"user":user == null ? null : user.toJson(),
"createdAt": createdAt,
"imagePath": imagePath,
"likeList": likeList,
"tags": tags,
"replyTweetKeyList": replyTweetKeyList,
"user": user == null ? null : user.toJson(),
"parentkey": parentkey,
"childRetwetkey":childRetwetkey
"childRetwetkey": childRetwetkey
};
}
dynamic getLikeList(List<String> list){
if(list != null && list.length > 0){
var result = Map.fromIterable(list, key: (v) =>'userId', value: (v) => v[0]);
return result;
}
}

FeedModel.fromJson(Map<dynamic, dynamic> map) {
if(likeList == null){
key = map['key'];
description = map['description'];
userId = map['userId'];
// name = map['name'];
// profilePic = map['profilePic'];
likeCount = map['likeCount'];
commentCount = map['commentCount'];
retweetCount = map["retweetCount"] ?? 0;
imagePath = map['imagePath'];
createdAt = map['createdAt'];
imagePath = map['imagePath'];
// username = map['username'];
user = User.fromJson(map['user']);
parentkey = map['parentkey'];
childRetwetkey = map['childRetwetkey'];
if (map['tags'] != null) {
tags = List<String>();
map['tags'].forEach((value) {
tags.add(value);
});
}
if (map["likeList"] != null) {
likeList = List<String>();
map['likeList'].forEach((value) {
likeList.add(value);
});
likeCount = likeList.length;
} else {
likeList = [];
likeCount = 0;
}
if (map['replyTweetKeyList'] != null) {
map['replyTweetKeyList'].forEach((value) {
replyTweetKeyList = List<String>();
map['replyTweetKeyList'].forEach((value) {
replyTweetKeyList.add(value);
});
});
commentCount = replyTweetKeyList.length;
} else {
replyTweetKeyList = [];
commentCount = 0;
}
key = map['key'];
description = map['description'];
userId = map['userId'];
// name = map['name'];
// profilePic = map['profilePic'];
likeCount = map['likeCount'];
commentCount = map['commentCount'];
retweetCount = map["retweetCount"] ?? 0;
imagePath = map['imagePath'];
createdAt = map['createdAt'];
imagePath = map['imagePath'];
// username = map['username'];
user = User.fromJson(map['user']);
parentkey = map['parentkey'];
childRetwetkey = map['childRetwetkey'];
if(map['tags'] != null){
tags = List<String>();
map['tags'].forEach((value){
tags.add(value);
});
}
if(map['likeList'] != null){
map['likeList'].forEach((key,value){
if(value.containsKey('userId')){
LikeList list = LikeList(key:key,userId: value['userId']);
likeList.add(list);
}
});
likeCount = likeList.length;
}
else{
likeList = [];
likeCount = 0;
}
if(map['replyTweetKeyList'] != null){
map['replyTweetKeyList'].forEach((value){
replyTweetKeyList = List<String>();
map['replyTweetKeyList'].forEach((value){
replyTweetKeyList.add(value);
});
});
commentCount = replyTweetKeyList.length;
}
else{
replyTweetKeyList = [];
commentCount = 0;
}
}

bool get isValidTweet {
bool isValid =false;
if(description != null
&& description.isNotEmpty
&& this.user != null
&& this.user.userName != null
&& this.user.userName.isNotEmpty
){
isValid = true;
}
else{
print("Invalid Tweet found. Id:- $key");
}
return isValid;
}
}
class LikeList{
String key;
String userId;
LikeList({this.key,this.userId});
LikeList.fromJson(Map<dynamic, dynamic> map,{String key}) {
key = key;
userId = map['userId'];
}
toJson(){
return {
'userId':userId
};
bool get isValidTweet {
bool isValid = false;
if (description != null &&
description.isNotEmpty &&
this.user != null &&
this.user.userName != null &&
this.user.userName.isNotEmpty) {
isValid = true;
} else {
print("Invalid Tweet found. Id:- $key");
}
return isValid;
}
}
6 changes: 5 additions & 1 deletion lib/model/notificationModel.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
class NotificationModel {
String tweetKey;
String updatedAt;
String type;

NotificationModel({
this.tweetKey,
});

NotificationModel.fromJson(String tweetId) {
NotificationModel.fromJson(String tweetId, String updatedAt,String type) {
tweetKey = tweetId;
this.updatedAt = updatedAt;
this.type = type;
}

Map<String, dynamic> toJson() => {
Expand Down
2 changes: 1 addition & 1 deletion lib/model/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class User {
'location': location,
'createdAt': createdAt,
'followers': followersList != null ? followersList.length : null,
'following': followersList!= null ? followersList.length : null,
'following': followingList!= null ? followingList.length : null,
'userName': userName,
'webSite': webSite,
'isVerified': isVerified ?? false,
Expand Down
6 changes: 3 additions & 3 deletions lib/page/notification/notificationPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class NotificationPageBody extends StatelessWidget {
class NotificationTile extends StatelessWidget {
final FeedModel model;
const NotificationTile({Key key, this.model}) : super(key: key);
Widget _userList(BuildContext context, List<LikeList> list) {
Widget _userList(BuildContext context, List<String> list) {
// List<String> names = [];
var length = list.length;
List<Widget> avaterList = [];
Expand All @@ -122,8 +122,8 @@ class NotificationTile extends StatelessWidget {
if (list != null && list.length > 5) {
list = list.take(5).toList();
}
avaterList = list.map((x) {
return _userAvater(x.userId, state, (name) {
avaterList = list.map((userId) {
return _userAvater(userId, state, (name) {
// names.add(name);
});
}).toList();
Expand Down
7 changes: 4 additions & 3 deletions lib/state/authState.dart
Original file line number Diff line number Diff line change
Expand Up @@ -436,15 +436,16 @@ class AuthState extends AppState {
profileUserModel.followers = profileUserModel.followersList.length;
// update logged-in user's following count
userModel.following = userModel.followingList.length;

kDatabase
.child('profile')
.child(profileUserModel.userId)
.set(profileUserModel.toJson());
.child('followerList')
.set(profileUserModel.followersList);
kDatabase
.child('profile')
.child(userModel.userId)
.set(userModel.toJson());
.child('followingList')
.set(userModel.followingList);
cprint('user added to following list', event: 'add_follow');
notifyListeners();
} catch (error) {
Expand Down

0 comments on commit 5ee0b5d

Please sign in to comment.