Commit e55bc350 authored by Daniel García Moreno's avatar Daniel García Moreno

stickers: Purchase sticker group working

The purchase is working but the sticker popover refresh doesn't work
well, the stickers doesn't update just after the purchase.

There's also other problem when there's a lot of stickers in a group we
try to load all at once and that's a problem, we should load one by one
or something like that to avoid huge resource consumption.
parent b6f81380
......@@ -316,6 +316,10 @@ impl Backend {
let r = stickers::send(self, room, &sticker);
bkerror!(r, tx, BKResponse::StickersError);
}
Ok(BKCommand::PurchaseSticker(group)) => {
let r = stickers::purchase(self, &group);
bkerror!(r, tx, BKResponse::StickersError);
}
// Internal commands
Ok(BKCommand::ShutDown) => {
......
......@@ -13,6 +13,7 @@ use error::Error;
use backend::types::Backend;
use backend::types::BKResponse;
use backend::types::BKCommand;
use types::StickerGroup;
use types::Sticker;
use self::serde_json::Value as JsonValue;
......@@ -113,3 +114,35 @@ pub fn send(bk: &Backend, roomid: String, sticker: &Sticker) -> Result<(), Error
Ok(())
}
pub fn purchase(bk: &Backend, group: &StickerGroup) -> Result<(), Error> {
let widget = bk.data.lock().unwrap().sticker_widget.clone();
let widget_id = match widget {
None => {
let id = get_sticker_widget_id(bk)?;
bk.data.lock().unwrap().sticker_widget = Some(id.clone());
id
},
Some(id) => id.clone(),
};
let asset = group.asset.clone();
let data = vec![
("asset_type", asset.clone()),
("widget_id", widget_id.clone()),
("widget_type", "m.stickerpicker".to_string()),
];
let url = bk.vurl("widgets/purchase_asset", data)?;
let tx = bk.tx.clone();
let itx = bk.internal_tx.clone();
get!(&url,
|_| {
if let Some(t) = itx {
t.send(BKCommand::ListStickers).unwrap();
}
},
|err| { tx.send(BKResponse::StickersError(err)).unwrap() }
);
Ok(())
}
......@@ -59,6 +59,7 @@ pub enum BKCommand {
Invite(String, String),
ListStickers,
SendSticker(String, Sticker),
PurchaseSticker(StickerGroup),
}
#[derive(Debug)]
......
......@@ -19,6 +19,7 @@ pub struct Sticker {
#[derive(Serialize, Deserialize)]
pub struct StickerGroup {
pub name: String,
pub asset: String,
pub description: String,
pub price: i64,
pub purchased: bool,
......@@ -32,6 +33,7 @@ impl StickerGroup {
let d = &js["data"];
let purchased = js["purchased"].as_bool().unwrap_or_default();
let asset = js["asset_type"].as_str().unwrap_or_default().to_string();
let name = d["name"].as_str().unwrap_or_default().to_string();
let description = d["description"].as_str().unwrap_or_default().to_string();
let price = d["price"].as_i64().unwrap_or_default();
......@@ -53,6 +55,7 @@ impl StickerGroup {
StickerGroup {
name,
asset,
description,
price,
purchased,
......
......@@ -12,6 +12,7 @@ use types::Message;
use types::Room;
use types::Member;
use types::Sticker;
use types::StickerGroup;
#[derive(Debug)]
......@@ -23,6 +24,7 @@ pub enum InternalCommand {
LoadMoreNormal,
RemoveInv(String),
SendSticker(Sticker),
PurchaseSticker(StickerGroup),
ToInvite(Member),
RmInvite(String),
......@@ -62,6 +64,9 @@ pub fn appop_loop(rx: Receiver<InternalCommand>) {
Ok(InternalCommand::SendSticker(sticker)) => {
APPOP!(send_sticker, (sticker));
}
Ok(InternalCommand::PurchaseSticker(group)) => {
APPOP!(purchase_sticker, (group));
}
Err(_) => {
break;
}
......
......@@ -10,6 +10,7 @@ use self::gtk::StackExt;
use self::gtk::WidgetExt;
use self::gtk::LabelExt;
use self::gtk::BoxExt;
use self::gtk::ButtonExt;
use self::gdk_pixbuf::Pixbuf;
use self::gtk::ImageExt;
......@@ -76,6 +77,14 @@ impl AppOp {
let img = builder.get_object::<gtk::Image>("thumb")
.expect("Can't find thumb in ui file.");
self.sticker_thumbnail(sticker.thumbnail.clone(), &img);
let btn = builder.get_object::<gtk::Button>("btn")
.expect("Can't find btn in ui file.");
let group = sticker.clone();
let internal = self.internal.clone();
btn.connect_clicked(move |_| {
let command = InternalCommand::PurchaseSticker(group.clone());
internal.send(command).unwrap();
});
}
container.remove(&bx);
......@@ -143,7 +152,6 @@ impl AppOp {
for (i, img) in sticker.stickers.iter().enumerate() {
if i > 0 && i % 5 == 0 {
println!("container");
bx = gtk::Box::new(gtk::Orientation::Horizontal, 6);
bx.set_homogeneous(true);
content.pack_start(&bx, true, true, 6);
......@@ -193,4 +201,9 @@ impl AppOp {
self.add_tmp_room_message(msg);
}
pub fn purchase_sticker(&self, group: StickerGroup) {
self.backend.send(BKCommand::PurchaseSticker(group)).unwrap();
self.stickers_loading(true);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment