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

112 lines
3.8 KiB
Vala
Raw Normal View History

2018-04-25 15:16:57 +02:00
using Gtk;
using Granite;
2018-04-26 16:05:03 +02:00
public class Tootle.AccountView : Tootle.HomeView {
2018-04-25 15:16:57 +02:00
Account account;
2018-04-25 16:30:44 +02:00
Gtk.Grid header;
Gtk.Grid header_image;
2018-04-25 15:16:57 +02:00
Granite.Widgets.Avatar avatar;
Gtk.Label display_name;
Gtk.Label username;
2018-04-28 18:27:10 +02:00
Tootle.RichLabel note;
2018-04-25 16:30:44 +02:00
Gtk.Grid counters;
2018-04-25 15:16:57 +02:00
2018-04-26 16:05:03 +02:00
public override void pre_construct () {
2018-04-25 16:30:44 +02:00
header = new Gtk.Grid ();
2018-04-25 15:16:57 +02:00
avatar = new Granite.Widgets.Avatar.with_default_icon (128);
2018-04-25 16:30:44 +02:00
avatar.hexpand = true;
avatar.margin_top = 16;
avatar.margin_bottom = 16;
header.attach (avatar, 0, 1, 1, 1);
2018-04-25 15:16:57 +02:00
2018-04-28 18:27:10 +02:00
display_name = new RichLabel ("");
2018-04-25 15:16:57 +02:00
display_name.get_style_context ().add_class (Granite.STYLE_CLASS_H2_LABEL);
2018-04-25 16:30:44 +02:00
header.attach (display_name, 0, 2, 1, 1);
2018-04-25 15:16:57 +02:00
username = new Gtk.Label ("");
username.get_style_context ().add_class (Granite.STYLE_CLASS_H3_LABEL);
2018-04-25 16:30:44 +02:00
header.attach (username, 0, 3, 1, 1);
2018-04-28 18:27:10 +02:00
note = new RichLabel ("");
2018-04-25 15:16:57 +02:00
note.set_line_wrap (true);
note.get_style_context ().add_class (Granite.STYLE_CLASS_H3_LABEL);
note.justify = Gtk.Justification.CENTER;
2018-04-25 16:30:44 +02:00
note.margin_start = 16;
note.margin_end = 16;
header.attach (note, 0, 4, 1, 1);
counters = new Gtk.Grid ();
counters.margin_top = 16;
counters.column_homogeneous = true;
header.attach (counters, 0, 5, 1, 1);
header_image = new Gtk.Grid ();
header_image.get_style_context ().add_class ("header");
header.attach (header_image, 0, 1, 1, 5);
2018-04-25 15:16:57 +02:00
view.pack_start (header, false, false, 0);
}
2018-04-26 16:05:03 +02:00
public AccountView (Account acc) {
base ("account_"+acc.id.to_string ());
2018-04-25 15:16:57 +02:00
account = acc;
2018-04-28 18:27:10 +02:00
display_name.label = "<b>%s</b>".printf (account.display_name);
2018-04-25 15:16:57 +02:00
username.label = "@" + account.acct;
note.label = Utils.escape_html (account.note);
2018-04-27 20:50:08 +02:00
Tootle.cache.load_avatar (account.avatar, avatar, 128);
2018-04-25 16:30:44 +02:00
add_counter (_("Toots"), 1, account.statuses_count);
add_counter (_("Follows"), 2, account.following_count);
add_counter (_("Followers"), 3, account.followers_count);
show_all ();
var stylesheet = ".header{background-image: url(\"%s\")}".printf (account.header);
var css_provider = Granite.Widgets.Utils.get_css_provider (stylesheet);
header_image.get_style_context ().add_provider (css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
2018-04-26 16:05:03 +02:00
request ();
2018-04-25 16:30:44 +02:00
}
2018-04-26 16:05:03 +02:00
private void add_counter (string name, int i, int64 val) {
2018-04-25 16:30:44 +02:00
var label_name = new Gtk.Label (name);
var label_val = new Gtk.Label (val.to_string ());
counters.attach (label_name, i, 1, 1, 1);
counters.attach (label_val, i, 2, 1, 1);
2018-04-25 15:16:57 +02:00
}
2018-04-26 16:05:03 +02:00
public override bool is_status_owned (Status status){
return status.account.id == account.id;
}
public override string get_url (){
2018-04-27 20:50:08 +02:00
var url = "%s/api/v1/accounts/%lld/statuses".printf (Tootle.settings.instance_url, account.id);
2018-04-26 16:05:03 +02:00
url += "?limit=25";
if (max_id > 0)
url += "&max_id=" + max_id.to_string ();
return url;
}
2018-04-28 18:27:10 +02:00
public static void open_from_id (int64 id){
var url = "%s/api/v1/accounts/%lld".printf (Tootle.settings.instance_url, id);
var msg = new Soup.Message("GET", url);
Tootle.network.queue(msg, (sess, mess) => {
try{
var root = Tootle.network.parse (mess);
var acc = Account.parse (root);
Tootle.window.open_secondary_view (new AccountView (acc));
}
catch (GLib.Error e) {
warning ("Can't update feed");
warning (e.message);
}
});
}
2018-04-25 15:16:57 +02:00
}