Skip to content

Commit

Permalink
Merge pull request #141 from klinker-apps/fix/unread-count
Browse files Browse the repository at this point in the history
Fix unread count...
  • Loading branch information
Serubin authored May 25, 2020
2 parents 4510eb7 + 96e2988 commit 14cf35c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
24 changes: 10 additions & 14 deletions src/components/Conversations/Conversations.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@

<script>
import Vue from 'vue';
import { i18n } from '@/utils';
import Hash from 'object-hash';
import { Util, Api, SessionCache, TimeUtils } from '@/utils';
import { Util, Api, i18n, SessionCache, TimeUtils } from '@/utils';
import ConversationItem from './ConversationItem.vue';
import DayLabel from './DayLabel.vue';
import Spinner from '@/components/Spinner.vue';
import unreadCountMixin from '@/mixins/unreadCountMixin.js';
import joypixels from 'emoji-toolkit';
export default {
name: 'Conversations',
components: {
ConversationItem,
DayLabel,
Spinner
},
mixins: [ unreadCountMixin ],
props: ['small', 'index', 'folderId', 'folderName'],
data () {
Expand Down Expand Up @@ -197,8 +197,6 @@ export default {
this.unFilteredAllConversations = response;
}
let unreadCount = 0;
const updatedConversations = [];
const cache = [];
Expand All @@ -223,10 +221,6 @@ export default {
}
}
// Update unread count
if (!item.read && (!this.index || this.index == "index_public_unarchived"))
unreadCount++;
updatedConversations.push(item);
// Save to contact cache
Expand All @@ -252,7 +246,7 @@ export default {
// Set unread, only on unarchived public index
if (!this.index || this.index == "index_public_unarchived")
this.$store.commit('unread_count', unreadCount);
this.updateUnreadCount();
if (!this.small) {
this.$store.commit("loading", false);
Expand Down Expand Up @@ -281,10 +275,12 @@ export default {
return false;
}
// Increment unread, only on unarchived public index
// Update unread, only on unarchived public index
// This check is probably not totally necessary, but it prevents
// unnecessarily calling updateUnreadCount
if (conv.read != event_obj.read && !event_obj.read
&& (!this.index || this.index == "index_public_unarchived"))
this.$store.commit('increment_unread_count'); // Increment unread
this.updateUnreadCount();
// Generate new snippet
Expand Down Expand Up @@ -354,9 +350,9 @@ export default {
if(!conv || !conv_index)
return false;
// Decrement unread, only on unarchived public index
// Update unread, only on unarchived public index
if (!conv.read)
this.$store.commit('decrement_unread_count');
this.updateUnreadCount();
conv.read = true;
conv.hash = Hash(conv);
Expand Down
11 changes: 11 additions & 0 deletions src/mixins/unreadCountMixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import store from '@/store';

export default {
methods: {
updateUnreadCount () {
const conversations = store.state.session_conversations.index_public_unarchived;
const newUnreadCount = conversations.reduce((count, conv) => count + (conv.read ? 0 : 1), 0);
store.commit('unread_count', newUnreadCount);
},
},
};
2 changes: 0 additions & 2 deletions src/store/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ export const mutations = {
loading: (state, loading) => state.loading = loading,
hotkey_navigation: (state, hotkey_navigation) => state.hotkey_navigation = hotkey_navigation,
unread_count: (state, unread_count) => state.unread_count = unread_count,
increment_unread_count: (state) => state.unread_count++,
decrement_unread_count: (state) => state.unread_count--,
full_theme: (state, full_theme) => state.full_theme = full_theme,
sidebar_open: (state, sidebar_open) => state.sidebar_open = sidebar_open,
account_id: (state, account_id) => state.account_id = account_id,
Expand Down
5 changes: 4 additions & 1 deletion src/utils/cache_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default class SessionCache {

static putMessages (messages, conversation_id) {
let sessionMessages = SessionCache.getAllMessages();
sessionMessages[conversation_id] = messages;
sessionMessages[conversation_id] = JSON.parse(JSON.stringify(messages));

store.commit('session_messages', sessionMessages);
}
Expand Down Expand Up @@ -225,6 +225,7 @@ export default class SessionCache {
if (conversations != null) {
for (let i = 0; i < conversations.length; i++) {
if (conversations[i].device_id == message.conversation_id) {
conversations[i].read = message.read;
conversations[i].timestamp = message.timestamp;
conversations[i].snippet = message.mime_type.indexOf("text") > -1 ? message.data : "";
this.putConversations(this.resortConversations(conversations), 'index_public_unarchived');
Expand All @@ -237,6 +238,7 @@ export default class SessionCache {
if (conversations != null) {
for (let i = 0; i < conversations.length; i++) {
if (conversations[i].device_id == message.conversation_id) {
conversations[i].read = message.read;
conversations[i].timestamp = message.timestamp;
conversations[i].snippet = message.mime_type.indexOf("text") > -1 ? message.data : "";
this.putConversations(this.resortConversations(conversations), 'index_archived');
Expand All @@ -249,6 +251,7 @@ export default class SessionCache {
if (conversations != null) {
for (let i = 0; i < conversations.length; i++) {
if (conversations[i].device_id == message.conversation_id) {
conversations[i].read = message.read;
conversations[i].timestamp = message.timestamp;
conversations[i].snippet = message.mime_type.indexOf("text") > -1 ? message.data : "";
this.putConversations(this.resortConversations(conversations), 'index_private');
Expand Down

0 comments on commit 14cf35c

Please sign in to comment.