@@ 27,7 27,6 @@ bool SMSRecordInterface::Add(const SMSRecord &rec)
}
uint32_t contactID = contactRec->ID;
// Search for a thread with specified contactID
- uint32_t threadID = 0;
ThreadRecordInterface threadInterface(smsDB, contactsDB);
auto threadRec =
threadInterface.GetLimitOffsetByField(0, 1, ThreadRecordField::ContactID, std::to_string(contactID).c_str());
@@ 49,11 48,11 @@ bool SMSRecordInterface::Add(const SMSRecord &rec)
return false;
}
}
- threadID = (*threadRec)[0].ID;
+ auto thread = (*threadRec)[0];
// Create SMS
- if (!smsDB->sms.add(SMSTableRow{{.ID = rec.ID},
- .threadID = threadID,
+ if (!smsDB->sms.add(SMSTableRow{{.ID = DB_ID_NONE}, // the entry is not yet in the DB
+ .threadID = thread.ID,
.contactID = contactID,
.date = rec.date,
.dateSent = rec.dateSent,
@@ 69,7 68,6 @@ bool SMSRecordInterface::Add(const SMSRecord &rec)
// TODO: error check
// Update thread
- auto thread = (*threadRec)[0];
thread.snippet = rec.body.substr(0, rec.body.length() >= snippetLength ? snippetLength : rec.body.length());
thread.date = rec.date;
thread.type = rec.type;
@@ 161,14 159,32 @@ bool SMSRecordInterface::Update(const SMSRecord &recUpdated)
return false;
}
- return smsDB->sms.update(SMSTableRow{{.ID = recUpdated.ID},
- .threadID = recCurrent.threadID,
- .contactID = recCurrent.contactID,
- .date = recUpdated.date,
- .dateSent = recUpdated.dateSent,
- .errorCode = recUpdated.errorCode,
- .body = recUpdated.body,
- .type = recUpdated.type});
+ smsDB->sms.update(SMSTableRow{{.ID = recUpdated.ID},
+ .threadID = recCurrent.threadID,
+ .contactID = recCurrent.contactID,
+ .date = recUpdated.date,
+ .dateSent = recUpdated.dateSent,
+ .errorCode = recUpdated.errorCode,
+ .body = recUpdated.body,
+ .type = recUpdated.type});
+
+ // Update messages read count if necessary
+ ThreadRecordInterface threadInterface(smsDB, contactsDB);
+ auto thread = threadInterface.GetByID(recCurrent.threadID);
+
+ // update thread details with the latest sms from given thread
+ auto latest =
+ smsDB->sms.getLimitOffsetByField(0, 1, SMSTableFields::ThreadID, std::to_string(thread.dbID).c_str())[0];
+ // check if there is need to change thread summary
+ if (latest.ID == recCurrent.ID) { // there is no == operator overloaded
+ thread.snippet = recUpdated.body.substr(0, recUpdated.body.length() >= snippetLength ? snippetLength : rec.body.length());
+ thread.date = recUpdated.date;
+ thread.type = recUpdated.type;
+ }
+
+ threadInterface.Update(thread);
+
+ return true;
}
bool SMSRecordInterface::RemoveByID(uint32_t id)
@@ 195,6 211,18 @@ bool SMSRecordInterface::RemoveByID(uint32_t id)
threadInterface.RemoveByID(sms.threadID);
}
else {
+ // update thread details with the latest sms from given thread
+ auto twoLatest =
+ smsDB->sms.GetLimitOffsetByField(0, 2, SMSTableFields::ThreadID, std::to_string(threadRec.dbID).c_str());
+ // check if there is need to change thread summary
+ if (twoLatest[0].ID == sms.ID) { // there is no == operator overloaded
+ // if deleting the newest sms, refresh thread details with next sms in the column
+ auto newLatest = twoLatest[1];
+ threadRec.snippet = newLatest.body.substr(
+ 0, newLatest.body.length() >= snippetLength ? snippetLength : newLatest.body.length());
+ threadRec.date = newLatest.date;
+ threadRec.type = newLatest.type;
+ }
// Update msg count
threadRec.msgCount--;
threadInterface.Update(threadRec);