Skip to content

Commit

Permalink
refactor: move some logic to today factory
Browse files Browse the repository at this point in the history
  • Loading branch information
diegopvlk committed Dec 30, 2024
1 parent c2e504f commit fe0f6ae
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
35 changes: 25 additions & 10 deletions src/todayFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,34 @@ todayItemFactory.connect('bind', (factory, listItem) => {
const name = listItem.nameLabel;
const doseAndNotes = listItem.doseAndNotes;
const checkButton = listItem.checkButton;
listItem.originalDose = item.dose;

item.doseAndNotes = doseAndNotes;
item.amountBtn = amountBtn;
item.amtSpinRow = amtSpinRow;
item.checkButton = checkButton;

name.label = item.name;

const setDoseAndNotes = () => {
doseAndNotes.label = `${item.dose} ${item.unit}`;
if (item.notes !== '') {
doseAndNotes.label += ` • ${item.notes}`;
}
};
setDoseAndNotes();

amtSpinRow.set_value(item.dose);
amtSpinRow.connect('output', row => {
item.dose = row.get_value();
setDoseAndNotes();
});

checkButton.connect('toggled', btn => {
if (!btn.active) {
item.dose = listItem.originalDose;
setDoseAndNotes();
}
amountBtn.set_visible(btn.active);
});

// activate item with space bar
const keyController = new Gtk.EventControllerKey();
keyController.connect('key-pressed', (_, keyval, keycode, state) => {
Expand All @@ -163,13 +185,6 @@ todayItemFactory.connect('bind', (factory, listItem) => {
});
row.add_controller(keyController);

name.label = item.name;
doseAndNotes.label = `${item.dose} ${item.unit}`;

if (item.notes !== '') {
doseAndNotes.label += ` • ${item.notes}`;
}

icon.icon_name = item.icon;
box.set_css_classes(['item-box', item.color]);
});
20 changes: 0 additions & 20 deletions src/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,6 @@ export const DosageWindow = GObject.registerClass(
});

this.todayItems = [];
this.todayDosesHolder = [];
this.todayModel = new Gtk.NoSelection({ model: sortedTodayModel });

this._todayList.model = this.todayModel;
Expand Down Expand Up @@ -785,39 +784,21 @@ export const DosageWindow = GObject.registerClass(
_selectTodayItems(list, position, groupCheck) {
const model = list.get_model();
const item = model.get_item(position).obj;
const doseAndNotes = item.doseAndNotes;
const amountBtn = item.amountBtn;
const amtSpinRow = item.amtSpinRow;
const checkButton = item.checkButton;

const indexToRemove = this.todayItems.indexOf(item);

let isActive = checkButton.get_active();

if (groupCheck) isActive = false;

if (!isActive) {
const d = item.dose;
if (!this.todayItems.includes(item)) {
this.todayItems.push(item);
this.todayDosesHolder.push(d);
}
} else {
const storedDose = this.todayDosesHolder[indexToRemove];
item.dose = storedDose;
doseAndNotes.label = doseAndNotes.label.replace(/^[^\s]+/, item.dose);
this.todayItems.splice(indexToRemove, 1);
this.todayDosesHolder.splice(indexToRemove, 1);
}

amtSpinRow.set_value(item.dose);
amtSpinRow.connect('output', row => {
doseAndNotes.label = doseAndNotes.label.replace(/^[^\s]+/, row.get_value());
item.dose = row.get_value();
});

checkButton.set_active(!isActive);
amountBtn.set_visible(!isActive);

const hasTodayItems = this.todayItems.length > 0;
this._updateEntryBtn(hasTodayItems);
Expand All @@ -838,7 +819,6 @@ export const DosageWindow = GObject.registerClass(
} else {
this._entryBtn.remove_css_class('suggested-action');
this.todayItems = [];
this.todayDosesHolder = [];
}
}

Expand Down

0 comments on commit fe0f6ae

Please sign in to comment.