Implement reblogging and favoriting

This commit is contained in:
bleakgrey 2018-04-16 21:22:42 +03:00
parent 03323f073c
commit 2d0a38e957
4 changed files with 100 additions and 12 deletions

View File

@ -7,15 +7,19 @@ public class Tootle.Status{
public int64 reblogs_count;
public int64 favourites_count;
public bool reblogged;
public bool favorited;
public string avatar;
public string acct;
public Status(int64 id) {
this.id = id;
this.reblogged = false;
this.favorited = false;
}
public static string escape_html(string content){
//debug(content);
return content
.replace("<br>", "\n")
.replace("</br>", "")
@ -43,6 +47,11 @@ public class Tootle.Status{
status.favourites_count = obj.get_int_member ("favourites_count");
status.content = escape_html ( obj.get_string_member ("content"));
if(obj.has_member ("reblogged"))
status.reblogged = obj.get_boolean_member ("reblogged");
if(obj.has_member ("favourited"))
status.favorited = obj.get_boolean_member ("favourited");
var acc = obj.get_object_member ("account");
status.avatar = acc.get_string_member ("avatar");
status.acct = acc.get_string_member ("acct");

View File

@ -45,9 +45,9 @@ public class Tootle.NetManager : GLib.Object{
}
public static Json.Object parse(Soup.Message msg) throws GLib.Error{
stdout.printf ("Status Code: %u\n", msg.status_code);
stdout.printf ("Message length: %lld\n", msg.response_body.length);
stdout.printf ("Data: \n%s\n", (string) msg.response_body.data);
// stdout.printf ("Status Code: %u\n", msg.status_code);
// stdout.printf ("Message length: %lld\n", msg.response_body.length);
//stdout.printf ("Data: \n%s\n", (string) msg.response_body.data);
var parser = new Json.Parser ();
parser.load_from_data ((string) msg.response_body.flatten ().data, -1);
@ -55,9 +55,9 @@ public class Tootle.NetManager : GLib.Object{
}
public static Json.Array parse_array(Soup.Message msg) throws GLib.Error{
// stdout.printf ("Status Code: %u\n", msg.status_code);
// stdout.printf ("Message length: %lld\n", msg.response_body.length);
// stdout.printf ("Data: \n%s\n", (string) msg.response_body.data);
// stdout.printf ("Status Code: %u\n", msg.status_code);
// stdout.printf ("Message length: %lld\n", msg.response_body.length);
// stdout.printf ("Data: \n%s\n", (string) msg.response_body.data);
var parser = new Json.Parser ();
parser.load_from_data ((string) msg.response_body.flatten ().data, -1);

View File

@ -45,7 +45,7 @@ public class Tootle.HomeView : Tootle.AbstractView {
}
public void prepend(Status status){ //TODO: clear all on account switch
var widget = new StatusWidget();
var widget = new StatusWidget(status);
widget.rebind (status);
view.pack_end (widget, false, false, 0);
}

View File

@ -3,6 +3,8 @@ using Granite;
public class Tootle.StatusWidget : Gtk.Grid {
public Status status;
public Granite.Widgets.Avatar avatar;
Gtk.Label user;
Gtk.Label content;
@ -10,6 +12,9 @@ public class Tootle.StatusWidget : Gtk.Grid {
Gtk.Box counters;
Gtk.Label reblogs;
Gtk.Label favorites;
Gtk.ToggleButton reblog;
Gtk.ToggleButton favorite;
construct {
margin = 6;
@ -33,12 +38,24 @@ public class Tootle.StatusWidget : Gtk.Grid {
reblogs = new Gtk.Label ("0");
favorites = new Gtk.Label ("0");
reblog = get_action_button ();
reblog.toggled.connect (() => {
if (reblog.sensitive)
toggle_reblog ();
});
favorite = get_action_button (false);
favorite.toggled.connect (() => {
if (favorite.sensitive)
toggle_fav ();
});
counters = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 6); //TODO: currently useless
counters.margin_top = 6;
counters.margin_bottom = 12;
counters.add(new Gtk.Image.from_icon_name ("media-playlist-repeat-symbolic", Gtk.IconSize.SMALL_TOOLBAR));
counters.add(reblog);
counters.add(reblogs);
counters.add(new Gtk.Image.from_icon_name ("user-bookmarks-symbolic", Gtk.IconSize.SMALL_TOOLBAR));
counters.add(favorite);
counters.add(favorites);
counters.show_all ();
@ -49,18 +66,80 @@ public class Tootle.StatusWidget : Gtk.Grid {
show_all(); //TODO: display conversations
}
public StatusWidget () {
public StatusWidget (Status status) {
this.status = status;
get_style_context ().add_class ("status");
}
public void rebind (Status status){
public void rebind (Status status = this.status){
user.label = "<b>"+status.acct+"</b>";
content.label = status.content;
reblogs.label = status.reblogs_count.to_string ();
favorites.label = status.favourites_count.to_string ();
reblog.active = status.reblogged;
reblog.sensitive = true;
favorite.active = status.favorited;
favorite.sensitive = true;
CacheManager.instance.load_avatar (status.avatar, this.avatar);
}
private Gtk.ToggleButton get_action_button (bool reblog = true){
var path = "edit-undo-symbolic";
if (!reblog)
path = "help-about-symbolic";
var icon = new Gtk.Image.from_icon_name (path, Gtk.IconSize.SMALL_TOOLBAR);
var button = new Gtk.ToggleButton ();
button.add (icon);
return button;
}
public void toggle_reblog(){
var state = reblog.get_active ();
var action = "reblog";
if (!state)
action = "unreblog";
var msg = new Soup.Message("POST", Settings.instance.instance_url + "/api/v1/statuses/" + status.id.to_string () + "/" + action);
msg.finished.connect(() => {
status.reblogged = state;
reblog.sensitive = false;
favorite.sensitive = false;
if(state)
status.reblogs_count += 1;
else
status.reblogs_count -= 1;
rebind ();
});
NetManager.instance.queue(msg, (sess, mess) => {
//NetManager.parse (msg);
});
}
public void toggle_fav(){
var state = favorite.get_active ();
var action = "favourite";
if (!state)
action = "unfavourite";
var msg = new Soup.Message("POST", Settings.instance.instance_url + "/api/v1/statuses/" + status.id.to_string () + "/" + action);
msg.finished.connect(() => {
status.favorited = state;
reblog.sensitive = false;
favorite.sensitive = false;
if(state)
status.favourites_count += 1;
else
status.favourites_count -= 1;
rebind ();
});
NetManager.instance.queue(msg, (sess, mess) => {
//NetManager.parse (msg);
});
}
}