PinDMs: properlly remove pinned header when no pinned DMs exist

This commit is contained in:
Mirco Wittrien 2018-12-16 20:31:13 +01:00
parent e099600d5b
commit e52d8b7e23
1 changed files with 9 additions and 13 deletions

View File

@ -19,7 +19,7 @@ class PinDMs {
getDescription () {return "Allows you to pin DMs, making them appear at the top of your DM-list.";} getDescription () {return "Allows you to pin DMs, making them appear at the top of your DM-list.";}
getVersion () {return "1.2.2";} getVersion () {return "1.2.3";}
getAuthor () {return "DevilBro";} getAuthor () {return "DevilBro";}
@ -240,7 +240,7 @@ class PinDMs {
removePinnedDM (id, dms) { removePinnedDM (id, dms) {
BDFDB.removeData(id, this, "pinnedDMs"); BDFDB.removeData(id, this, "pinnedDMs");
this.sortAndUpdate(); let existingDMs = this.sortAndUpdate();
let removepoint = null; let removepoint = null;
for (let i in dms) { for (let i in dms) {
let ele = dms[i]; let ele = dms[i];
@ -250,7 +250,7 @@ class PinDMs {
} }
} }
if (removepoint) { if (removepoint) {
let offset = BDFDB.isObjectEmpty(BDFDB.loadAllData(this, "pinnedDMs")) ? 1 : 0; let offset = existingDMs.length ? 0 : 1;
if (offset) delete dms[removepoint + offset].pinned; if (offset) delete dms[removepoint + offset].pinned;
dms.splice(removepoint-offset,1+offset); dms.splice(removepoint-offset,1+offset);
} }
@ -259,22 +259,18 @@ class PinDMs {
sortAndUpdate () { sortAndUpdate () {
let pinnedDMs = BDFDB.loadAllData(this, "pinnedDMs"); let pinnedDMs = BDFDB.loadAllData(this, "pinnedDMs");
delete pinnedDMs[""]; delete pinnedDMs[""];
let sortedDMs = [], sortDM = (id) => { let sortedDMs = [], existingDMs = [], sortDM = (id) => {
// REMOVE AFTER SOME TIME - PATCH USERID TO DMID
let DMid = this.ChannelUtils.getDMFromUserId(id);
if (DMid) {
pinnedDMs[DMid] = pinnedDMs[id];
delete pinnedDMs[id];
id = DMid;
}
if (typeof sortedDMs[pinnedDMs[id]] == "undefined") sortedDMs[pinnedDMs[id]] = id; if (typeof sortedDMs[pinnedDMs[id]] == "undefined") sortedDMs[pinnedDMs[id]] = id;
else sortDM(sortedDMs, pinnedDMs[id]+1, id); else sortDM(sortedDMs, pinnedDMs[id]+1, id);
}; };
for (let id in pinnedDMs) sortDM(id); for (let id in pinnedDMs) sortDM(id);
sortedDMs = sortedDMs.filter(n => n); sortedDMs = sortedDMs.filter(n => n);
for (let pos in sortedDMs) pinnedDMs[sortedDMs[pos]] = parseInt(pos); for (let pos in sortedDMs) {
pinnedDMs[sortedDMs[pos]] = parseInt(pos);
if (this.ChannelUtils.getChannel(sortedDMs[pos])) existingDMs.push(sortedDMs[pos]);
}
BDFDB.saveAllData(pinnedDMs, this, "pinnedDMs"); BDFDB.saveAllData(pinnedDMs, this, "pinnedDMs");
return sortedDMs; return existingDMs;
} }
forceUpdateScroller (scroller) { forceUpdateScroller (scroller) {