Skip to content

Commit

Permalink
feat: add button to show all/less history
Browse files Browse the repository at this point in the history
  • Loading branch information
diegopvlk committed Dec 26, 2024
1 parent b9d67c5 commit 81ea69a
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 29 deletions.
5 changes: 4 additions & 1 deletion data/styles/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,12 @@ listview > header label {
padding-bottom: 20px;
background-color: transparent;
}
.dosage-list:not(.history) {
.dosage-list {
padding-bottom: 75px;
}
.list-no-extra-padding-bottom {
padding-bottom: 20px;
}
list.boxed-list-separate > row.expander row.header {
border-top-left-radius: var(--list-border-radius);
border-top-right-radius: var(--list-border-radius);
Expand Down
18 changes: 18 additions & 0 deletions data/ui/window.blp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,24 @@ template $DosageWindow: Adw.ApplicationWindow {
icon-name: 'history-empty-symbolic';
visible: false;
}

[overlay]
Box {
halign: center;
valign: end;

Button toogleHistAmountBtn {
halign: center;
valign: end;
margin-bottom: 6;
clicked => $_toggleHistoryAmount();

styles [
'pill',
'floating'
]
}
}
};
}

Expand Down
39 changes: 21 additions & 18 deletions src/medDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,15 +513,28 @@ export function openMedicationDialog(DosageWindow, list, position, mode) {
const it = list.get_model().get_item(position);
const [, pos] = historyLS.find(it);

historyLS.remove(pos);
const updItem = updatedItem.obj;

historyLS.insert_sorted(updatedItem, (a, b) => {
const dateA = a.obj.taken[0];
const dateB = b.obj.taken[0];
if (dateA < dateB) return 1;
else if (dateA > dateB) return -1;
else return 0;
});
if (
updItem.taken[0] !== tempTaken0 ||
updItem.taken[1] !== tempTaken1 ||
updItem.dose !== item.dose
) {
historyLS.remove(pos);

historyLS.insert_sorted(updatedItem, (a, b) => {
const dateA = a.obj.taken[0];
const dateB = b.obj.taken[0];
if (dateA < dateB) return 1;
else if (dateA > dateB) return -1;
else return 0;
});

DosageWindow._updateJsonFile('history', historyLS);
DosageWindow._setShowHistoryAmount();
} else {
return;
}

list.scroll_to(Math.max(0, position - 1), Gtk.ListScrollFlags.FOCUS, null);

Expand All @@ -545,16 +558,6 @@ export function openMedicationDialog(DosageWindow, list, position, mode) {
DosageWindow._checkInventory();
}
}

const updItem = updatedItem.obj;

if (
updItem.taken[0] !== tempTaken0 ||
updItem.taken[1] !== tempTaken1 ||
updItem.dose !== item.dose
) {
DosageWindow._updateJsonFile('history', historyLS);
}
}

function addSingleItemToHistory() {
Expand Down
79 changes: 69 additions & 10 deletions src/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const DosageWindow = GObject.registerClass(
Template: 'resource:///io/github/diegopvlk/Dosage/ui/window.ui',
InternalChildren: [
'todayList',
'toogleHistAmountBtn',
'historyList',
'treatmentsList',
'treatmentsPage',
Expand Down Expand Up @@ -70,6 +71,7 @@ export const DosageWindow = GObject.registerClass(
settings.connect('changed::clear-old-hist', (sett, key) => {
if (sett.get_boolean(key) === true) {
this._clearOldHistoryEntries();
this._setShowHistoryAmount();
this._updateJsonFile('history', historyLS);
}
});
Expand Down Expand Up @@ -139,6 +141,7 @@ export const DosageWindow = GObject.registerClass(
}

#clockTick() {
this.today = new Date();
let lastDate = new Date().setHours(0, 0, 0, 0);

const tick = () => {
Expand Down Expand Up @@ -292,6 +295,49 @@ export const DosageWindow = GObject.registerClass(
}
}

_setShowHistoryAmount() {
if (this.histQuery) return;

const moreThan30 = historyLS.n_items > 30;

this._toogleHistAmountBtn.sensitive = moreThan30;
this._toogleHistAmountBtn.visible = moreThan30;
this._toogleHistAmountBtn.label = _('Show all');

if (moreThan30) {
this._historyList.remove_css_class('list-no-extra-padding-bottom');
} else {
this._historyList.add_css_class('list-no-extra-padding-bottom');
}

if (!this.showAllHist && moreThan30) {
const dates = new Set();
let count = 0;

this.historyFilter.set_filter_func(item => {
const itemDate = new Date(item.obj.taken[0]).setHours(0, 0, 0, 0);

if (!dates.has(itemDate)) {
dates.add(itemDate);
count++;
}

return count <= 7;
});
} else if (this.showAllHist) {
this._toogleHistAmountBtn.label = _('Show less');
this.historyFilter.set_filter_func(null);
} else {
this._toogleHistAmountBtn.visible = false;
this.historyFilter.set_filter_func(null);
}
}

_toggleHistoryAmount() {
this.showAllHist = !this.showAllHist;
this._setShowHistoryAmount();
}

async _loadHistory(historyJson) {
try {
if (!historyLS.get_item(0)) {
Expand Down Expand Up @@ -327,6 +373,7 @@ export const DosageWindow = GObject.registerClass(
});

this._historyList.model = this.noSelectionModel;
this._setShowHistoryAmount();

this._headerBarSpinner.set_visible(false);
});
Expand Down Expand Up @@ -419,12 +466,18 @@ export const DosageWindow = GObject.registerClass(
const query = removeDiacritics(this._searchEntry.text.trim());

if (!query) {
this.historyFilter.set_filter_func(null);
this.histQuery = false;
this.showAllHist = false;
this._setShowHistoryAmount();
this._emptyHistory.visible = false;
this._historyList.scroll_to(0, null, null);
return;
} else {
this.histQuery = true;
}

this._toogleHistAmountBtn.visible = false;

this.historyFilter.set_filter_func(item => {
const itemName = removeDiacritics(item.obj.name);
return itemName.includes(query);
Expand Down Expand Up @@ -535,6 +588,11 @@ export const DosageWindow = GObject.registerClass(
for (const date of dateKeys.slice(0, 30)) {
const itemsToAdd = itemsHolder[date];
historyLS.splice(0, 0, itemsToAdd);
historyLS.sort((a, b) => {
const dtA = new Date(a.obj.taken[0]).setHours(0, 0, 0, 0);
const dtB = new Date(b.obj.taken[0]).setHours(0, 0, 0, 0);
return dtA === dtB ? 0 : dtA < dtB ? 1 : -1;
});
}

return true;
Expand Down Expand Up @@ -1070,23 +1128,24 @@ export const DosageWindow = GObject.registerClass(
}

_updateEverything(skipHistUp, notifAction) {
this._setShowHistoryAmount();
this._updateCycleAndLastUp();
this._updateJsonFile('treatments', treatmentsLS);
this._loadToday();
this._updateEntryBtn(false);
this._checkInventory(notifAction);
if (skipHistUp) {
const pos = Math.max(0, skipHistUp[1] - 1);
this._treatmentsList.scroll_to(pos, Gtk.ListScrollFlags.FOCUS, null);
} else {
this._updateJsonFile('history', historyLS);
}

// reload-ish of treatments list
// necessary for updating low stock and cycle date labels
this._treatmentsList.model = new Gtk.NoSelection({
model: treatmentsLS,
});
this._treatmentsList.visible = false;
this._treatmentsList.visible = true;

if (!skipHistUp) {
this._updateJsonFile('history', historyLS);
} else if (skipHistUp[1] >= 0) {
const pos = Math.max(0, skipHistUp[1] - 1);
this._treatmentsList.scroll_to(pos, Gtk.ListScrollFlags.FOCUS, null);
}
}

_openMedDialog(list, position, mode) {
Expand Down

0 comments on commit 81ea69a

Please sign in to comment.