tootle-linux-client/src/Views/HomeView.vala

96 lines
2.6 KiB
Vala
Raw Normal View History

2018-04-14 14:09:06 +02:00
using Gtk;
using Gdk;
public class Tootle.HomeView : Tootle.AbstractView {
private string timeline;
2018-04-26 16:05:03 +02:00
public HomeView (string timeline = "home") {
2018-05-05 17:38:01 +02:00
base ();
2018-04-14 14:09:06 +02:00
this.timeline = timeline;
2018-04-14 19:53:09 +02:00
2018-04-18 11:13:22 +02:00
view.remove.connect (on_remove);
2018-04-27 20:50:08 +02:00
Tootle.accounts.switched.connect(on_account_changed);
2018-05-05 17:38:01 +02:00
Tootle.app.refresh.connect(on_refresh);
2018-04-14 14:09:06 +02:00
2018-05-05 17:38:01 +02:00
request ();
2018-04-14 14:09:06 +02:00
}
public override string get_icon () {
return "user-home-symbolic";
}
public override string get_name () {
2018-05-09 23:46:24 +02:00
return _("Home");
2018-04-14 14:09:06 +02:00
}
2018-04-26 16:05:03 +02:00
public virtual bool is_status_owned (Status status){
return false;
}
2018-05-18 23:14:12 +02:00
public void prepend (ref Status status){
2018-04-18 11:13:22 +02:00
var separator = new Gtk.Separator (Gtk.Orientation.HORIZONTAL);
separator.show ();
2018-05-18 23:14:12 +02:00
var widget = new StatusWidget (ref status);
2018-04-18 11:13:22 +02:00
widget.separator = separator;
2018-04-26 16:05:03 +02:00
widget.button_press_event.connect(widget.open);
if (!is_status_owned (status))
widget.avatar.button_press_event.connect(widget.on_avatar_clicked);
2018-04-18 11:13:22 +02:00
view.pack_start(separator, false, false, 0);
2018-04-16 22:36:57 +02:00
view.pack_start(widget, false, false, 0);
2018-04-14 14:09:06 +02:00
}
2018-04-18 11:13:22 +02:00
public virtual void on_remove (Widget widget){
if (!(widget is StatusWidget))
return;
2018-04-26 16:05:03 +02:00
//TODO: empty state
2018-04-18 11:13:22 +02:00
}
2018-05-05 17:38:01 +02:00
public virtual string get_url () {
2018-05-14 16:43:10 +02:00
var url = "%s/api/v1/timelines/%s?limit=25".printf (Tootle.settings.instance_url, this.timeline);
2018-04-26 16:05:03 +02:00
if (max_id > 0)
url += "&max_id=" + max_id.to_string ();
return url;
}
2018-05-05 17:38:01 +02:00
public virtual void request (){
2018-04-26 16:05:03 +02:00
var msg = new Soup.Message("GET", get_url ());
2018-04-27 20:50:08 +02:00
Tootle.network.queue(msg, (sess, mess) => {
2018-04-14 14:09:06 +02:00
try{
2018-04-27 20:50:08 +02:00
Tootle.network.parse_array (mess).foreach_element ((array, i, node) => {
2018-04-14 14:09:06 +02:00
var object = node.get_object ();
if (object != null){
var status = Status.parse(object);
2018-04-26 16:05:03 +02:00
max_id = status.id;
2018-05-18 23:14:12 +02:00
prepend (ref status);
2018-04-14 14:09:06 +02:00
}
});
}
catch (GLib.Error e) {
warning ("Can't update feed");
warning (e.message);
}
});
}
2018-04-26 16:05:03 +02:00
2018-05-03 10:11:31 +02:00
public virtual void on_refresh (){
clear ();
request ();
}
2018-04-26 16:05:03 +02:00
public virtual void on_account_changed (Account? account){
if(account == null)
return;
2018-05-05 17:38:01 +02:00
on_refresh ();
2018-04-26 16:05:03 +02:00
}
public override void bottom_reached (){
request ();
}
2018-04-14 14:09:06 +02:00
}