diff --git a/scripts/time_entry.gd b/scripts/time_entry.gd index 858fa3b..3a01f41 100644 --- a/scripts/time_entry.gd +++ b/scripts/time_entry.gd @@ -47,6 +47,18 @@ static func time_to_period(time_in_secs: int) -> String: return "%02d:%02d:%02d" % [hours, minutes, seconds] +static func period_to_time(period_string: String) -> int: + var period := period_string.split(":") + if period.size() < 3: + return -1 + var hours := int(period[0]) + var minutes := int(period[1]) + var seconds := int(period[2]) + var time := seconds + (minutes * 60) + (hours * 60 * 60) + return time + + + func to_csv_line() -> PoolStringArray: return PoolStringArray([ name, diff --git a/scripts/time_stamp.gd b/scripts/time_stamp.gd index 53f53eb..fc00915 100644 --- a/scripts/time_stamp.gd +++ b/scripts/time_stamp.gd @@ -50,5 +50,24 @@ func from_string(time_string: String) -> TimeStamp: return from_dict(time) +func from_unix_time(unix_time: int) -> TimeStamp: + var time := Time.get_datetime_dict_from_unix_time(unix_time) + return from_dict(time) + + +func equals(other) -> bool: + return ( + other.year == year and \ + other.month == month and \ + other.day == day and \ + other.weekday == weekday and \ + other.hour == hour and \ + other.minute == minute and \ + other.second == second and \ + other.unix == unix + ) + + func _to_string() -> String: return Time.get_datetime_string_from_datetime_dict(to_dict(), false) + diff --git a/ui/tasks_list.gd b/ui/tasks_list.gd index 9a261a2..3bdabd4 100644 --- a/ui/tasks_list.gd +++ b/ui/tasks_list.gd @@ -5,6 +5,8 @@ enum COL{ TIME } +const META_KEY = "time_entry" + var config: ConfigManager = preload("res://config_manager.tres") var _timer := Timer.new() @@ -19,9 +21,43 @@ func _ready() -> void: _timer.connect("timeout", self, "_on_timer_timeout") # warning-ignore:return_value_discarded config.connect("time_sheet_loaded", self, "populate_entries") + # warning-ignore:return_value_discarded + connect("item_edited", self, "on_Tree_item_edited") populate_entries() +func on_Tree_item_edited(): + var tree_item := get_edited() + var time_entry := tree_item.get_meta(META_KEY) as TimeEntry + if time_entry == null: + return + + var edited_start_time_str := tree_item.get_text(COL.TEXT) + var edited_start_time := TimeStamp.new().from_string(edited_start_time_str) + if edited_start_time.year == 1970: + _update_from_time_entry(time_entry, tree_item) + return + + var edited_duration_str := tree_item.get_text(COL.TIME) + var edited_duration := TimeEntry.period_to_time(edited_duration_str) + if edited_duration == -1: + _update_from_time_entry(time_entry, tree_item) + return + + var edited_end_time_unix := edited_start_time.unix + edited_duration + var edited_end_time := TimeStamp.new().from_unix_time(edited_end_time_unix) + + if edited_start_time.equals(time_entry.start_time) \ + and edited_end_time.equals(time_entry.end_time): + _update_from_time_entry(time_entry, tree_item) + return + + time_entry.start_time = edited_start_time + time_entry.end_time = edited_end_time + config.timesheet.save() + + + func populate_entries() -> void: clear() var tree_items_root := create_item() @@ -54,16 +90,29 @@ func _populate_from_entry(tree_item_root: TreeItem, time_entry_item_root: TimeEn var time_entry_item := entry_item as TimeEntryTreeItem var item := create_item(tree_item_root) var time_entry := time_entry_item.time_entry - item.set_text(COL.TEXT, time_entry.start_time.to_string()) - item.set_metadata(COL.TEXT, time_entry_item.time_entry.name) - item.set_text(COL.TIME, time_entry_item.get_period()) - if time_entry.is_closed == false: - var texture := preload("res://assets/stop_small.svg") - item.add_button(COL.TIME, texture) + item.set_meta(META_KEY, time_entry) + item.set_metadata(COL.TEXT, time_entry.name) + item.set_editable(COL.TEXT, true) + item.set_editable(COL.TIME, true) + + _update_from_time_entry(time_entry, item) # warning-ignore:return_value_discarded time_entry_item.connect("end_time_updated", self, "_on_time_entry_changed_update_item", [time_entry_item, item]) +func _update_from_time_entry(time_entry: TimeEntry, item: TreeItem) -> void: + + item.set_text(COL.TEXT, time_entry.start_time.to_string()) + item.set_text(COL.TIME, time_entry.get_period()) + if time_entry.is_closed == false: + if item.get_button_count(COL.TIME) < 1: + var texture := preload("res://assets/stop_small.svg") + item.add_button(COL.TIME, texture, 0) + else: + if item.get_button_count(COL.TIME) > 0: + item.erase_button(COL.TIME, 0) + + func _on_time_entry_changed_update_item(time_entry_item: TimeEntryTreeItem, item: TreeItem) -> void: item.set_text(COL.TIME, time_entry_item.get_period())