Skip to content
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

Added ability to delete tasks #144

Open
wants to merge 12 commits into
base: refactor-project-build-system
Choose a base branch
from
5 changes: 5 additions & 0 deletions app/TaskListBox.vala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace Agenda {
public class TaskListBox : Gtk.ListBox {

public signal void task_changed (int index, Task task);
public signal void task_deleted (int index, Task task);

public TaskListBox (TaskRepositoryFile tasks) {
this.bind_model (tasks, list_box_create_widget);
Expand All @@ -38,6 +39,10 @@ namespace Agenda {
task_changed (index, task);
});

row.task_deleted.connect ((index, task) => {
task_deleted (index, task);
});

return row;
}
}
Expand Down
16 changes: 13 additions & 3 deletions app/TaskListBoxRow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
You should have received a copy of the GNU General Public License
along with Agenda. If not, see <http://www.gnu.org/licenses/>.

***/
namespace Agenda {
***/ namespace Agenda {

public class TaskListBoxRow : Gtk.ListBoxRow {

public signal void task_changed (int index, Task task);
public signal void task_deleted (int index, Task task);

public TaskListBoxRow (Task task) {

Expand All @@ -33,6 +33,7 @@ namespace Agenda {
label.set_attributes (attr_list);

var check_button = new Gtk.CheckButton ();

check_button.set_active (task.complete);
check_button.toggled.connect (() => {
task.complete = check_button.active;
Expand All @@ -45,6 +46,15 @@ namespace Agenda {
label.set_attributes (attr_list);
});

var delete_button = new Gtk.Button.from_icon_name ("edit-delete");
delete_button.halign = Gtk.Align.END;
delete_button.hexpand = true;

delete_button.clicked.connect (() => {
var index = this.get_index ();
task_deleted (index, task);
});

var grid = new Gtk.Grid ();
grid.set_margin_start (12);
grid.set_margin_end (12);
Expand All @@ -54,9 +64,9 @@ namespace Agenda {

grid.add (check_button);
grid.add (label);
grid.add (delete_button);

grid.show_all ();

this.add (grid);
}
}
Expand Down
10 changes: 9 additions & 1 deletion app/Window.vala
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ namespace Agenda {
this.get_style_context ().add_class ("rounded");
this.set_size_request (MIN_WIDTH, MIN_HEIGHT);

// Recive button events
set_events (Gdk.EventMask.BUTTON_PRESS_MASK);

var header = new Gtk.HeaderBar ();
header.show_close_button = true;
header.get_style_context ().add_class ("titlebar");
Expand Down Expand Up @@ -102,6 +105,7 @@ namespace Agenda {

task_list_box = new Agenda.TaskListBox (Application.tasks);
task_list_box.task_changed.connect (update_task);
task_list_box.task_deleted.connect (delete_task);

scrolled_window = new Gtk.ScrolledWindow (null, null);
scrolled_window.expand = true;
Expand Down Expand Up @@ -154,7 +158,7 @@ namespace Agenda {

public void append_task () {
Task task = new Task.with_attributes (
"",
new DateTime.now_local ().hash ().to_string (),
false,
task_entry.text);

Expand All @@ -167,6 +171,10 @@ namespace Agenda {
Application.tasks.update (index, task);
}

public void delete_task (int index, Task task) {
Application.tasks.remove (index, task);
}

public void restore_window_position () {
var size = Application.settings.get_value ("window-size");
var position = Application.settings.get_value ("window-position");
Expand Down
2 changes: 1 addition & 1 deletion lib/ITaskRepository.vala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ namespace Agenda {
public abstract Task? get_by_id (int id);
public abstract void add (Task task);
public abstract void update (int index, Task task);
public abstract bool remove (Task task);
public abstract bool remove (int index, Task task);
}
}
5 changes: 3 additions & 2 deletions lib/TaskRepositoryFile.vala
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace Agenda {
}

public Task? get_by_id (int index) {

if (index > this.task_list.size) {
return null;
}
Expand All @@ -67,10 +68,10 @@ namespace Agenda {
return (uint) this.task_list.size;
}

public bool remove (Task task) {
public bool remove (int index, Task task) {
var removed = this.task_list.remove (task);
if (removed) {
this.items_changed (0, 1, 0);
this.items_changed (index, 1, 0);
this.save ();
}

Expand Down
24 changes: 24 additions & 0 deletions src/Models/TaskList.vala
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,30 @@ namespace Agenda {

list_changed ();
}
/*
* Sort the tasks so finished tasks are at bottom
*/
public void sort_tasks () {
Gtk.TreeIter iter;
bool valid = get_iter_first (out iter);
Task[] tasks = {};

while (valid) {
Task task = get_task (iter);
if (task.complete) {
tasks += task;
remove (ref iter);
continue;
}
valid = iter_next (ref iter);
}

int i;
for (i = 0; i < tasks.length; i++) {
append_task (tasks[i]);
}
list_changed ();
}

public void clear_undo () {
undo_list = new TaskListHistory ();
Expand Down
13 changes: 12 additions & 1 deletion src/Widgets/TaskView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

namespace Agenda {


public class TaskView : Gtk.TreeView {

private TaskList task_list;
Expand Down Expand Up @@ -58,6 +59,8 @@ namespace Agenda {
text.ypad = 6;
text.editable = true;
text.max_width_chars = 10;
text.wrap_width = 50;
text.wrap_mode = Pango.WrapMode.WORD_CHAR;
text.ellipsize_set = true;
text.ellipsize = Pango.EllipsizeMode.END;

Expand All @@ -82,6 +85,7 @@ namespace Agenda {
set_tooltip_column (TaskList.Columns.TEXT);

text.editing_started.connect ( (editable, path) => {
debug ("Editing started");
is_editing = true;
});

Expand Down Expand Up @@ -144,10 +148,17 @@ namespace Agenda {

private void text_edited (string path, string edited_text) {
/* If the user accidentally blanks a task, abort the edit */

debug ("String: %s length: %d", edited_text, edited_text.length);
if (task_is_empty (edited_text)) {
return;
}
task_list.set_task_text (path, edited_text);
if (edited_text.length > EDITED_TEXT_MAX_LEN) {
string new_cut_text = edited_text.slice(0, EDITED_TEXT_MAX_LEN);
task_list.set_task_text (path, new_cut_text);
} else {
task_list.set_task_text (path, edited_text);
}
is_editing = false;
}
}
Expand Down
16 changes: 15 additions & 1 deletion src/Window.vala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace Agenda {

const int MIN_WIDTH = 500;
const int MIN_HEIGHT = 600;
// Limit for any edited text
const int EDITED_TEXT_MAX_LEN = 64;

const string HINT_STRING = _("Add a new task…");

Expand Down Expand Up @@ -69,6 +71,16 @@ namespace Agenda {
header.get_style_context ().add_class (Gtk.STYLE_CLASS_FLAT);
this.set_titlebar (header);

var button = new Gtk.Button.from_icon_name ("user-trash-symbolic", Gtk.IconSize.BUTTON);
button.clicked.connect (() => {
// Remove completed tasks
if (task_list != null) {
task_list.remove_completed_tasks();
}
stdout.printf ("You deleted completed tasks\n");
});
header.pack_start(button);

// Set up geometry
Gdk.Geometry geo = Gdk.Geometry ();
geo.min_width = MIN_WIDTH;
Expand Down Expand Up @@ -134,7 +146,7 @@ namespace Agenda {
task_entry.name = "TaskEntry";
task_entry.get_style_context ().add_class ("task-entry");
task_entry.placeholder_text = HINT_STRING;
task_entry.max_length = 64;
task_entry.max_length = EDITED_TEXT_MAX_LEN;
task_entry.hexpand = true;
task_entry.valign = Gtk.Align.START;
task_entry.set_icon_tooltip_text (
Expand Down Expand Up @@ -229,6 +241,8 @@ namespace Agenda {

task_list.append_task (task);
history_list.add_item (task.text);
// When adding a new task rearrange the tasks
task_list.sort_tasks ();
task_entry.text = "";
}

Expand Down