Skip to content

Commit

Permalink
🐛 (fix) Duplicate notification appear after relogin with same account
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlphamerc committed Jul 4, 2021
1 parent 7cf4630 commit 9b69a9b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
9 changes: 8 additions & 1 deletion lib/model/notificationModel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ import 'dart:convert';
import 'package:flutter_twitter_clone/model/user.dart';

class NotificationModel {
String id;
String tweetKey;
String updatedAt;
String createdAt;
String type;
Map<String, dynamic> data;

NotificationModel(
{this.tweetKey, this.type, this.createdAt, this.updatedAt, this.data});
{this.id,
this.tweetKey,
this.type,
this.createdAt,
this.updatedAt,
this.data});

NotificationModel.fromJson(String tweetId, Map<dynamic, dynamic> map) {
this.id = tweetId;
final data = json.decode(json.encode(map["data"])) as Map<String, dynamic>;
tweetKey = tweetId;
this.updatedAt = map["updatedAt"];
Expand Down
25 changes: 13 additions & 12 deletions lib/state/notificationState.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ class NotificationState extends AppState {

List<NotificationModel> _notificationList;

addNotificationList(NotificationModel model) {
if (_notificationList == null) {
_notificationList = <NotificationModel>[];
}

if (!_notificationList.any((element) => element.id == model.id)) {
_notificationList.add(model);
}
}

List<NotificationModel> get notificationList => _notificationList;

/// [Intitilise firebase notification kDatabase]
Expand Down Expand Up @@ -52,7 +62,6 @@ class NotificationState extends AppState {
return;
}
loading = true;
_notificationList = [];
kDatabase
.child('notification')
.child(userId)
Expand All @@ -64,7 +73,7 @@ class NotificationState extends AppState {
map.forEach((tweetKey, value) {
var map = value as Map<dynamic, dynamic>;
var model = NotificationModel.fromJson(tweetKey, map);
_notificationList.add(model);
addNotificationList(model);
});
_notificationList.sort((x, y) {
if (x.updatedAt != null && y.updatedAt != null) {
Expand Down Expand Up @@ -127,10 +136,8 @@ class NotificationState extends AppState {
if (event.snapshot.value != null) {
var map = event.snapshot.value as Map<dynamic, dynamic>;
var model = NotificationModel.fromJson(event.snapshot.key, map);
if (_notificationList == null) {
_notificationList = <NotificationModel>[];
}
_notificationList.add(model);

addNotificationList(model);
// added notification to list
print("Notification added");
notifyListeners();
Expand All @@ -140,12 +147,6 @@ class NotificationState extends AppState {
/// Trigger when someone changed his like preference
void _onNotificationChanged(Event event) {
if (event.snapshot.value != null) {
var map = event.snapshot.value as Map<dynamic, dynamic>;
var model = NotificationModel.fromJson(event.snapshot.key, map);
//update notification list
// _notificationList
// .firstWhere((x) => x.tweetKey == model.tweetKey)
// .tweetKey = model.tweetKey;
notifyListeners();
print("Notification changed");
}
Expand Down

0 comments on commit 9b69a9b

Please sign in to comment.