diff --git a/Contact.cpp b/Contact.cpp index cf24c9cde5def7619b2f5b42dd8c62cf6593e499..6415b335b6c0b462a4ec6bb0aa7d2c2875739eb6 100644 --- a/Contact.cpp +++ b/Contact.cpp @@ -32,7 +32,8 @@ using namespace ViewModel; Contact::Contact(String^ name, String^ ringID, - String^ GUID) + String^ GUID, + unsigned int unreadmessages) { name_ = name; ringID_ = ringID; @@ -68,10 +69,14 @@ Contact::Contact(String^ name, } } }); - //conversation_ = ref new Conversation(); notificationNewMessage_ = Windows::UI::Xaml::Visibility::Collapsed; - unreadMessages_ = 0; // not saved on disk yet (TO DO) + unreadMessages_ = unreadmessages; // not saved on disk yet (TO DO) + + if(unreadMessages_) { + notificationNewMessage = Windows::UI::Xaml::Visibility::Visible; + PropertyChanged(this, ref new PropertyChangedEventArgs("unreadMessages")); + } /* connect to delegate */ ContactsViewModel::instance->notifyNewConversationMessage += ref new NotifyNewConversationMessage([&] () { @@ -82,6 +87,7 @@ Contact::Contact(String^ name, PropertyChanged(this, ref new PropertyChangedEventArgs("unreadMessages")); notificationNewMessage = Windows::UI::Xaml::Visibility::Collapsed; unreadMessages_ = 0; + ContactsViewModel::instance->saveContactsToFile(); } }); } @@ -91,7 +97,6 @@ Contact::addNotifyNewConversationMessage() { notificationNewMessage = Windows::UI::Xaml::Visibility::Visible; unreadMessages_++; - RingDebug::instance->print(Utils::toString(unreadMessages_.ToString())); } void @@ -113,6 +118,7 @@ Contact::ToJsonObject() contactObject->SetNamedValue(nameKey, JsonValue::CreateStringValue(name_)); contactObject->SetNamedValue(ringIDKey, JsonValue::CreateStringValue(ringID_)); contactObject->SetNamedValue(GUIDKey, JsonValue::CreateStringValue(GUID_)); + contactObject->SetNamedValue(unreadMessagesKey, JsonValue::CreateNumberValue(unreadMessages_)); JsonObject^ jsonObject = ref new JsonObject(); jsonObject->SetNamedValue(contactKey, contactObject); diff --git a/Contact.h b/Contact.h index fc8fdae6709654a6d2b78ae956e3028582d35981..71547784151981ac9adff78ddc897109b7c5e020 100644 --- a/Contact.h +++ b/Contact.h @@ -26,6 +26,7 @@ using namespace Windows::UI::Xaml::Data; String^ nameKey = "name"; String^ ringIDKey = "ringid"; String^ GUIDKey = "guid"; +String^ unreadMessagesKey = "unreadmessages"; String^ contactKey = "contact"; String^ contactListKey = "contactlist"; @@ -35,7 +36,7 @@ ref class Conversation; public ref class Contact sealed : public INotifyPropertyChanged { public: - Contact(String^ name, String^ ringID, String^ GUID); + Contact(String^ name, String^ ringID, String^ GUID, unsigned int unreadmessages); JsonObject^ ToJsonObject(); virtual event PropertyChangedEventHandler^ PropertyChanged; diff --git a/ContactsViewModel.cpp b/ContactsViewModel.cpp index d42a84bf31e699a82490d05ab6b7f6ef09158e75..fb1c8d90a1f597f75e3895e0d4cd203b621bb713 100644 --- a/ContactsViewModel.cpp +++ b/ContactsViewModel.cpp @@ -58,8 +58,10 @@ ContactsViewModel::ContactsViewModel() if (contact->ringID_ == from && isNotSelected) { // increment contact's unread message count contact->addNotifyNewConversationMessage(); - // update the xaml for all + // update the xaml for all contacts notifyNewConversationMessage(); + // save to disk + saveContactsToFile(); } }); @@ -80,7 +82,7 @@ ContactsViewModel::addNewContact(String^ name, String^ ringId) { auto trimedName = Utils::Trim(name); if (contactsList_ && !findContactByName(trimedName)) { - Contact^ contact = ref new Contact(trimedName, trimedName, nullptr); + Contact^ contact = ref new Contact(trimedName, trimedName, nullptr, 0); contactsList_->Append(contact); saveContactsToFile(); return contact; @@ -158,10 +160,11 @@ ContactsViewModel::Stringify() void ContactsViewModel::Destringify(String^ data) { - JsonObject^ jsonObject = JsonObject::Parse(data); - String^ name; - String^ ringid; - String^ guid; + JsonObject^ jsonObject = JsonObject::Parse(data); + String^ name; + String^ ringid; + String^ guid; + unsigned int unreadmessages; JsonArray^ contactlist = jsonObject->GetNamedArray(contactListKey, ref new JsonArray()); for (unsigned int i = 0; i < contactlist->Size; i++) { @@ -170,11 +173,12 @@ ContactsViewModel::Destringify(String^ data) JsonObject^ jsonContactObject = contact->GetObject(); JsonObject^ contactObject = jsonContactObject->GetNamedObject(contactKey, nullptr); if (contactObject != nullptr) { - name = contactObject->GetNamedString(nameKey, ""); - ringid = contactObject->GetNamedString(ringIDKey, ""); - guid = contactObject->GetNamedString(GUIDKey, ""); + name = contactObject->GetNamedString(nameKey); + ringid = contactObject->GetNamedString(ringIDKey); + guid = contactObject->GetNamedString(GUIDKey); + unreadmessages = static_cast<uint16_t>(contactObject->GetNamedNumber(unreadMessagesKey)); } - contactsList_->Append(ref new Contact(name, ringid, guid)); + contactsList_->Append(ref new Contact(name, ringid, guid, unreadmessages)); } } }