-
-
Notifications
You must be signed in to change notification settings - Fork 38k
Add support for reordering Shopping List Items via Drag and Drop #41585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
8d161c1
ab5d3b1
3840eeb
d66ecf9
35e7b36
915451a
7615119
33535e9
8f0071c
d54b60d
a759d01
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| WS_TYPE_SHOPPING_LIST_ADD_ITEM = "shopping_list/items/add" | ||
| WS_TYPE_SHOPPING_LIST_UPDATE_ITEM = "shopping_list/items/update" | ||
| WS_TYPE_SHOPPING_LIST_CLEAR_ITEMS = "shopping_list/items/clear" | ||
| WS_TYPE_SHOPPING_LIST_REORDER_ITEMS = "shopping_list/items/reorder" | ||
|
|
||
| SCHEMA_WEBSOCKET_ITEMS = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend( | ||
| {vol.Required("type"): WS_TYPE_SHOPPING_LIST_ITEMS} | ||
|
|
@@ -53,6 +54,13 @@ | |
| {vol.Required("type"): WS_TYPE_SHOPPING_LIST_CLEAR_ITEMS} | ||
| ) | ||
|
|
||
| SCHEMA_WEBSOCKET_REORDER_ITEMS = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend( | ||
| { | ||
| vol.Required("type"): WS_TYPE_SHOPPING_LIST_REORDER_ITEMS, | ||
| vol.Required("item_ids"): [str], | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| async def async_setup(hass, config): | ||
| """Initialize the shopping list.""" | ||
|
|
@@ -128,6 +136,12 @@ async def complete_item_service(call): | |
| SCHEMA_WEBSOCKET_CLEAR_ITEMS, | ||
| ) | ||
|
|
||
| hass.components.websocket_api.async_register_command( | ||
| WS_TYPE_SHOPPING_LIST_REORDER_ITEMS, | ||
| websocket_handle_reorder, | ||
| SCHEMA_WEBSOCKET_REORDER_ITEMS, | ||
| ) | ||
|
|
||
| return True | ||
|
|
||
|
|
||
|
|
@@ -166,6 +180,31 @@ def async_clear_completed(self): | |
| self.items = [itm for itm in self.items if not itm["complete"]] | ||
| self.hass.async_add_job(self.save) | ||
|
|
||
| @callback | ||
| def async_reorder(self, item_ids): | ||
| """Reorder items.""" | ||
| # The array for sorted items. | ||
| new_items = [] | ||
| all_items_mapping = {item["id"]: item for item in self.items} | ||
| # Append items by the order of passed in array. | ||
| for item_id in item_ids: | ||
| if item_id not in all_items_mapping: | ||
| raise KeyError | ||
| new_items.append(all_items_mapping[item_id]) | ||
| # Remove the item from mapping after it's appended in the result array. | ||
| del all_items_mapping[item_id] | ||
| # Append the rest of the items | ||
| for key in all_items_mapping: | ||
| # All the unchecked items must be passed in the item_ids array, | ||
| # so all items left in the mapping should be checked items. | ||
| if all_items_mapping[key]["complete"] is False: | ||
| raise vol.Invalid( | ||
| "The item ids array doesn't contain all the unchecked shopping list items." | ||
| ) | ||
| new_items.append(all_items_mapping[key]) | ||
| self.items = new_items | ||
| self.hass.async_add_job(self.save) | ||
|
|
||
| async def async_load(self): | ||
| """Load items.""" | ||
|
|
||
|
|
@@ -281,3 +320,23 @@ def websocket_handle_clear(hass, connection, msg): | |
| hass.data[DOMAIN].async_clear_completed() | ||
| hass.bus.async_fire(EVENT, {"action": "clear"}) | ||
| connection.send_message(websocket_api.result_message(msg["id"])) | ||
|
|
||
|
|
||
| @callback | ||
| def websocket_handle_reorder(hass, connection, msg): | ||
|
balloob marked this conversation as resolved.
|
||
| """Handle reordering shopping_list items.""" | ||
| msg_id = msg.pop("id") | ||
| try: | ||
| hass.data[DOMAIN].async_reorder(msg.pop("item_ids")) | ||
| hass.bus.async_fire(EVENT, {"action": "reorder"}) | ||
| connection.send_message(websocket_api.result_message(msg_id)) | ||
|
ShaneQi marked this conversation as resolved.
Outdated
|
||
| except KeyError: | ||
| connection.send_message( | ||
| websocket_api.error_message( | ||
| msg_id, "item_not_found", "One or more item id(s) not found." | ||
| ) | ||
| ) | ||
|
ShaneQi marked this conversation as resolved.
Outdated
|
||
| except vol.Invalid as err: | ||
| connection.send_message( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed this and the above 2 issues. |
||
| websocket_api.error_message(msg_id, "bad_request", f"{err}") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the websocket_api error
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use async_add_executor_job
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed