Extract ActivitesView updating logic into own class
This commit is contained in:
parent
5697cf0652
commit
e81a47d83f
@ -1,8 +1,9 @@
|
|||||||
namespace StudySystemClient {
|
namespace StudySystemClient {
|
||||||
public class ActivitiesView : ListView<ActivityCard>, IRefreshable {
|
public class ActivitiesView : ListView<ActivityCard> {
|
||||||
private const uint REFRESH_PERIOD_MS = 30000;
|
private const uint REFRESH_PERIOD_MS = 30000;
|
||||||
|
|
||||||
private Client client;
|
private Client client;
|
||||||
|
private Updater updater;
|
||||||
private Refresher refresher;
|
private Refresher refresher;
|
||||||
private bool pending_sort;
|
private bool pending_sort;
|
||||||
|
|
||||||
@ -10,31 +11,42 @@ namespace StudySystemClient {
|
|||||||
base();
|
base();
|
||||||
add_css_class("card-container");
|
add_css_class("card-container");
|
||||||
this.client = client;
|
this.client = client;
|
||||||
refresher = new Refresher(this, REFRESH_PERIOD_MS);
|
updater = new Updater(this, client);
|
||||||
|
refresher = new Refresher(updater, REFRESH_PERIOD_MS);
|
||||||
pending_sort = false;
|
pending_sort = false;
|
||||||
this.map.connect(() => {
|
this.map.connect(() => {
|
||||||
|
updater.refresh.begin();
|
||||||
refresher.start();
|
refresher.start();
|
||||||
refresh.begin();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void refresh() {
|
internal IterableBox.Iterator<Type, ActivityCard> iterator() {
|
||||||
if (!client.connected)
|
return container.iterator();
|
||||||
return;
|
|
||||||
try {
|
|
||||||
var activities = yield client.list_activities();
|
|
||||||
update_cards(activities);
|
|
||||||
} catch (ClientError e) {
|
|
||||||
stderr.printf("Error refreshing activities: %s\n",
|
|
||||||
e.message);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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,
|
private async void log_session(string subject, ActivityType type,
|
||||||
int minutes) {
|
int minutes) {
|
||||||
try {
|
try {
|
||||||
yield client.log_session(subject, type, minutes);
|
yield client.log_session(subject, type, minutes);
|
||||||
yield refresh();
|
yield updater.refresh();
|
||||||
} catch (ClientError e) {
|
} catch (ClientError e) {
|
||||||
stderr.printf("Error logging session: %s\n", e.message);
|
stderr.printf("Error logging session: %s\n", e.message);
|
||||||
}
|
}
|
||||||
@ -47,54 +59,6 @@ namespace StudySystemClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void update_cards(Array<Activity> activities) {
|
|
||||||
update_existing_cards(activities);
|
|
||||||
for (uint i = 0; i < activities.length; ++i)
|
|
||||||
create_card(activities.index(i));
|
|
||||||
if (log_in_progress())
|
|
||||||
pending_sort = true;
|
|
||||||
else
|
|
||||||
container.sort(compare_cards);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void update_existing_cards(Array<Activity> activities) {
|
|
||||||
var to_remove = new List<ActivityCard>();
|
|
||||||
foreach (var card in container) {
|
|
||||||
if (!update_existing_card(card, activities))
|
|
||||||
to_remove.append(card);
|
|
||||||
}
|
|
||||||
foreach (var card in to_remove)
|
|
||||||
container.remove(card);
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool update_existing_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 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void create_card(Activity activity) {
|
|
||||||
var card = new ActivityCard(activity);
|
|
||||||
card.session_logged.connect(log_session);
|
|
||||||
card.log_closed.connect(handle_pending_sort);
|
|
||||||
container.append(card);
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool log_in_progress() {
|
private bool log_in_progress() {
|
||||||
foreach (var card in container) {
|
foreach (var card in container) {
|
||||||
if (card.logging)
|
if (card.logging)
|
||||||
@ -113,4 +77,64 @@ namespace StudySystemClient {
|
|||||||
return 0;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user