141 lines
3.5 KiB
Vala
141 lines
3.5 KiB
Vala
namespace StudySystemClient {
|
|
public class ActivitiesView : ListView<ActivityCard> {
|
|
private const uint REFRESH_PERIOD_MS = 30000;
|
|
|
|
private Client client;
|
|
private Updater updater;
|
|
private Refresher refresher;
|
|
private bool pending_sort;
|
|
|
|
public ActivitiesView(Client client) {
|
|
base();
|
|
add_css_class("card-container");
|
|
this.client = client;
|
|
updater = new Updater(this, client);
|
|
refresher = new Refresher(updater, REFRESH_PERIOD_MS);
|
|
pending_sort = false;
|
|
this.map.connect(() => {
|
|
updater.refresh.begin();
|
|
refresher.start();
|
|
});
|
|
}
|
|
|
|
internal IterableBox.Iterator<Type, ActivityCard> iterator() {
|
|
return container.iterator();
|
|
}
|
|
|
|
internal void new_card(Activity activity) {
|
|
var card = new ActivityCard(activity);
|
|
card.session_logged.connect(log_session);
|
|
card.log_closed.connect(handle_pending_sort);
|
|
container.append(card);
|
|
}
|
|
|
|
internal void remove_card(ActivityCard card) {
|
|
container.remove(card);
|
|
}
|
|
|
|
internal void sort() {
|
|
if (log_in_progress())
|
|
pending_sort = true;
|
|
else
|
|
container.sort(compare_cards);
|
|
}
|
|
|
|
private async void log_session(string subject, ActivityType type,
|
|
int minutes) {
|
|
try {
|
|
yield client.log_session(subject, type, minutes);
|
|
yield updater.refresh();
|
|
} catch (ClientError e) {
|
|
stderr.printf("Error logging session: %s\n", e.message);
|
|
}
|
|
}
|
|
|
|
private void handle_pending_sort() {
|
|
if (pending_sort) {
|
|
container.sort(compare_cards);
|
|
pending_sort = false;
|
|
}
|
|
}
|
|
|
|
private bool log_in_progress() {
|
|
foreach (var card in container) {
|
|
if (card.logging)
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private static int compare_cards(ActivityCard card1,
|
|
ActivityCard card2) {
|
|
if (card1.activity.priority < card2.activity.priority)
|
|
return -1;
|
|
else if (card1.activity.priority > card2.activity.priority)
|
|
return 1;
|
|
else
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
private class Updater : IRefreshable {
|
|
private weak ActivitiesView target;
|
|
private Client client;
|
|
|
|
public Updater(ActivitiesView target, Client client) {
|
|
this.target = target;
|
|
this.client = client;
|
|
}
|
|
|
|
public async void refresh() {
|
|
if (!client.connected || target == null)
|
|
return;
|
|
try {
|
|
var activities = yield client.list_activities();
|
|
apply_update(activities);
|
|
target.sort();
|
|
} catch (ClientError e) {
|
|
stderr.printf("Error refreshing activities: %s\n",
|
|
e.message);
|
|
}
|
|
}
|
|
|
|
private void apply_update(Array<Activity> activities) {
|
|
update_existing(activities);
|
|
for (uint i = 0; i < activities.length; ++i)
|
|
target.new_card(activities.index(i));
|
|
}
|
|
|
|
private void update_existing(Array<Activity> activities) {
|
|
var to_remove = new List<ActivityCard>();
|
|
foreach (var card in target) {
|
|
if (!update_card(card, activities))
|
|
to_remove.append(card);
|
|
}
|
|
foreach (var card in to_remove)
|
|
target.remove_card(card);
|
|
}
|
|
|
|
private bool update_card(ActivityCard card,
|
|
Array<Activity> activities) {
|
|
var activity_index = find_activity(card, activities);
|
|
if (activity_index == null)
|
|
return false;
|
|
var priority = activities.index(activity_index).priority;
|
|
card.update_priority(priority);
|
|
activities._remove_index_fast(activity_index);
|
|
return true;
|
|
}
|
|
|
|
private static uint? find_activity(ActivityCard card,
|
|
Array<Activity> activities) {
|
|
for (uint i = 0; i < activities.length; ++i) {
|
|
if (activities.index(i).subject == card.activity.subject
|
|
&& activities.index(i).type == card.activity.type)
|
|
return i;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|