toggleable lists also apply status to newly added items
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-07-27 11:43:05 +02:00
parent 86a9762f39
commit e77c106813
2 changed files with 31 additions and 35 deletions

View File

@@ -169,11 +169,17 @@ class ListHandler(BaseHandler):
list_object = context.user_data["current_list"]
content_it = list_object.content.values()
done_it = [
"· " if e is None \
else "" if e \
else "" \
for e in list_object.done_dict.values()]
# distinguish the enumeration:
# either all done_dict values are None -> the list is not toggleable
# or at least one value is of type bool -> the list is toggleable and None === False
if any([type(e) == bool for e in list_object.done_dict.values()]):
done_it = [
"" if e else "" \
for e in list_object.done_dict.values()
]
else:
done_it = [". " for e in list_object.done_dict]
if content_it:
msg_content = "\n".join([f"{d} {c}" for d, c in zip(done_it, content_it)])
else:
@@ -191,7 +197,6 @@ class ListHandler(BaseHandler):
new = list_object.content
new.update({"random_key": item})
list_object.content = new
# TODO test me!
keyboard = [[InlineKeyboardButton("Add some more", callback_data="add"), InlineKeyboardButton("Back to the menu", callback_data="overview")]]
reply_markup = InlineKeyboardMarkup(keyboard)
await update.message.reply_text(f"Added {item}", reply_markup=reply_markup)
@@ -204,11 +209,10 @@ class ListHandler(BaseHandler):
await query.answer()
list_object = context.user_data["current_list"]
old = list_object.done_dict[toggle_key]
# if all None or all False (first toggle or all false) then set all dones to False
if not any(list_object.done_dict.values()):
new_done_dict = dict.fromkeys(list_object.done_dict, False)
else: new_done_dict = list_object.done_dict
old = list_object.done_dict[toggle_key] or False
# if it was previously unset (None), we can later on set it to not old = True
new_done_dict = list_object.done_dict
new_done_dict[toggle_key] = not old
list_object.done_dict = new_done_dict