Refactor: Move logic for saving box order to PrefsBoxOrderListBox

Move the logic for saving the box order represented by a
`PrefsBoxOrderListBox` (and its `PrefsBoxOrderItemRows`) to the
`PrefsBoxOrderListBox` class.
This makes the code cleaner and allows for easy reuse of the logic in
the future.
This commit is contained in:
Julian 2021-07-05 07:46:08 +02:00
parent 28fb67ad4d
commit bd69f816eb
Signed by: julian
GPG Key ID: 094C2AC34192FA11
2 changed files with 21 additions and 28 deletions

View File

@ -99,37 +99,12 @@ var PrefsBoxOrderItemRow = GObject.registerClass({
} }
} }
/// Finally save the box orders to settings. /// Finally save the box order(/s) to settings.
const settings = ExtensionUtils.getSettings(); ownListBox.saveBoxOrderToSettings();
let updatedBoxOrder = [ ];
for (let potentialListBoxRow of ownListBox) {
// Only process PrefsBoxOrderItemRows.
if (potentialListBoxRow.constructor.$gtype.name !== "PrefsBoxOrderItemRow") {
continue;
}
const item = potentialListBoxRow.item;
updatedBoxOrder.push(item);
}
settings.set_strv(ownListBox.boxOrder, updatedBoxOrder);
// If the list boxes of `this` and the drop value were different, // If the list boxes of `this` and the drop value were different,
// save an updated box order for the list were the drop value was in // save an updated box order for the list were the drop value was in
// as well. // as well.
if (ownListBox !== valueListBox) { if (ownListBox !== valueListBox) valueListBox.saveBoxOrderToSettings();
let updatedBoxOrder = [ ];
for (let potentialListBoxRow of valueListBox) {
// Only process PrefsBoxOrderItemRows.
if (potentialListBoxRow.constructor.$gtype.name !== "PrefsBoxOrderItemRow") {
continue;
}
const item = potentialListBoxRow.item;
updatedBoxOrder.push(item);
}
settings.set_strv(valueListBox.boxOrder, updatedBoxOrder);
}
}); });
this.add_controller(dropTarget); this.add_controller(dropTarget);
} }

View File

@ -37,6 +37,24 @@ var PrefsBoxOrderListBox = GObject.registerClass({
_init(params = {}, boxOrder) { _init(params = {}, boxOrder) {
super._init(params); super._init(params);
this._settings = ExtensionUtils.getSettings();
this.boxOrder = boxOrder; this.boxOrder = boxOrder;
} }
/**
* Saves the box order represented by `this` (and its
* `PrefsBoxOrderItemRows`) to settings.
*/
saveBoxOrderToSettings() {
let currentBoxOrder = [ ];
for (let potentialPrefsBoxOrderItemRow of this) {
// Only process PrefsBoxOrderItemRows.
if (potentialPrefsBoxOrderItemRow.constructor.$gtype.name !== "PrefsBoxOrderItemRow") continue;
const item = potentialPrefsBoxOrderItemRow.item;
currentBoxOrder.push(item);
}
this._settings.set_strv(this.boxOrder, currentBoxOrder);
}
}); });