Commit 44b2d283 authored by Julian Sparber's avatar Julian Sparber

roomsettings: reload room settings when members are avaible

code clean up
parent 089b001e
This diff is collapsed.
......@@ -2,6 +2,7 @@ use app::App;
use appop::MsgPos;
use appop::RoomPanel;
use appop::AppState;
use std::thread;
use std::sync::mpsc::Receiver;
......@@ -18,6 +19,7 @@ use types::StickerGroup;
pub enum InternalCommand {
AddRoomMessage(Message, MsgPos, Option<Message>, bool, bool),
SetPanel(RoomPanel),
SetView(AppState),
NotifyClicked(Message),
SelectRoom(Room),
LoadMoreNormal,
......@@ -52,6 +54,9 @@ pub fn appop_loop(rx: Receiver<InternalCommand>) {
Ok(InternalCommand::SetPanel(st)) => {
APPOP!(room_panel, (st));
}
Ok(InternalCommand::SetView(view)) => {
APPOP!(set_state, (view));
}
Ok(InternalCommand::NotifyClicked(msg)) => {
APPOP!(notification_cliked, (msg));
}
......
......@@ -135,6 +135,10 @@ impl AppOp {
self.reload_members();
}
}
/* FIXME: update the current room settings insteat of creating a new one */
if self.room_settings.is_some() {
self.create_room_settings();
}
}
pub fn reload_members(&mut self) {
......
......@@ -5,7 +5,6 @@ use i18n::{i18n, i18n_k};
use self::gtk::prelude::*;
use cache::download_to_cache;
use appop::AppOp;
use appop::AppState;
use appop::MsgPos;
......@@ -17,7 +16,6 @@ use backend::BKCommand;
use globals;
use cache;
use widgets;
use widgets::avatar::AvatarExt;
use types::Room;
use types::Message;
......
......@@ -5,17 +5,18 @@ use self::gtk::prelude::*;
use appop::AppOp;
use appop::AppState;
use backend::BKCommand;
use fractal_api::types::Member;
use cache::download_to_cache;
use app::InternalCommand;
use std::sync::mpsc::channel;
use std::sync::mpsc::TryRecvError;
use std::sync::mpsc::{Receiver, Sender};
/* needed to request members list */
/*
use backend::BKCommand;
use fractal_api::types::Member;
use std::sync::mpsc::channel;
use std::sync::mpsc::TryRecvError;
use std::sync::mpsc::{Receiver, Sender};
*/
use widgets;
use widgets::room_settings::RoomSettings;
use widgets::AvatarExt;
impl AppOp {
pub fn show_room_settings(&mut self) {
......@@ -23,16 +24,19 @@ impl AppOp {
}
pub fn create_room_settings(&mut self) -> Option<()> {
let stack = self.ui.builder
let stack = self.ui
.builder
.get_object::<gtk::Stack>("main_content_stack")
.expect("Can't find main_content_stack in ui file.");
let stack_header = self.ui.builder
let stack_header = self.ui
.builder
.get_object::<gtk::Stack>("headerbar_stack")
.expect("Can't find headerbar_stack in ui file.");
{
let room = self.rooms.get(&self.active_room.clone()?)?;
let mut panel = widgets::RoomSettings::new(self.backend.clone(), self.uid.clone(), room.clone());
let mut panel =
widgets::RoomSettings::new(self.backend.clone(), self.uid.clone(), room.clone());
let (body, header) = panel.create()?;
/* remove old panel */
......@@ -47,12 +51,12 @@ impl AppOp {
stack_header.add_named(&header, "room-settings");
/* Headerbar */
panel.get_back_button()?.connect_clicked(clone!(stack_header, stack => move |_| {
/* we should use self.set_state(AppState::Chat);
* we could maybe create an internal command */
stack.set_visible_child_name("chat");
stack_header.set_visible_child_name("normal");
}));
let internal = self.internal.clone();
panel.get_back_button()?.connect_clicked(move |_| {
internal
.send(InternalCommand::SetView(AppState::Chat))
.unwrap()
});
self.room_settings = Some(panel);
}
......@@ -62,59 +66,25 @@ impl AppOp {
None
}
fn room_settings_show_avatar(&self, _avatar: Option<String>, edit: bool) -> Option<()> {
let container = self.ui.builder
.get_object::<gtk::Box>("room_settings_avatar_box")
.expect("Can't find room_settings_avatar_box in ui file.");
let avatar_btn = self.ui
pub fn close_room_settings(&mut self) {
let stack = self.ui
.builder
.get_object::<gtk::Stack>("main_content_stack")
.expect("Can't find main_content_stack in ui file.");
let stack_header = self.ui
.builder
.get_object::<gtk::Button>("room_settings_avatar_button")
.expect("Can't find room_settings_avatar_button in ui file.");
.get_object::<gtk::Stack>("headerbar_stack")
.expect("Can't find headerbar_stack in ui file.");
for w in container.get_children().iter() {
if w != &avatar_btn {
container.remove(w);
}
/* remove old panel */
if let Some(widget) = stack.get_child_by_name("room-settings") {
stack.remove(&widget);
}
download_to_cache(self.backend.clone(), self.uid.clone().unwrap_or_default());
let image = widgets::Avatar::avatar_new(Some(100));
image.circle(self.uid.clone().unwrap_or_default(), self.username.clone(), 100);
if edit {
let overlay = self.ui
.builder
.get_object::<gtk::Overlay>("room_settings_avatar_overlay")
.expect("Can't find room_settings_avatar_overlay in ui file.");
let overlay_box = self.ui
.builder
.get_object::<gtk::Box>("room_settings_avatar")
.expect("Can't find room_settings_avatar in ui file.");
let avatar_spinner = self.ui
.builder
.get_object::<gtk::Spinner>("room_settings_avatar_spinner")
.expect("Can't find room_settings_avatar_spinner in ui file.");
/* remove all old avatar */
for w in overlay_box.get_children().iter() {
overlay_box.remove(w);
}
overlay_box.add(&image);
overlay.show();
avatar_spinner.hide();
avatar_btn.set_sensitive(true);
/*Hack for button bug */
avatar_btn.hide();
avatar_btn.show();
} else {
avatar_btn.hide();
container.add(&image);
if let Some(widget) = stack_header.get_child_by_name("room-settings") {
stack_header.remove(&widget);
}
return None;
}
pub fn close_room_settings(&mut self) {
self.set_state(AppState::Chat);
self.room_settings = None;
}
pub fn show_new_room_avatar(&self) -> Option<()> {
......@@ -135,41 +105,47 @@ impl AppOp {
None
}
pub fn update_members_list(&self, uid: String) -> Option<()> {
self.room_settings.clone()?.update_members_list(uid);
None
}
pub fn request_members_list(&self) -> Option<()> {
let room = self.rooms.get(&self.active_room.clone()?)?;
let members: Vec<Member> = room.members.values().cloned().collect();
for (i, member) in members.iter().enumerate() {
account_settings_get_member_info(
self.backend.clone(),
member.uid.clone());
}
None
}
/*
pub fn update_members_list(&self, uid: String) -> Option<()> {
self.room_settings.clone()?.update_members_list(uid);
None
}
*/
/*
pub fn request_members_list(&self) -> Option<()> {
let room = self.rooms.get(&self.active_room.clone()?)?;
let members: Vec<Member> = room.members.values().cloned().collect();
for member in members.iter() {
account_settings_get_member_info(
self.backend.clone(),
member.uid.clone());
}
None
*/
}
/* this funtion should be moved to the backend */
pub fn account_settings_get_member_info(
backend: Sender<BKCommand>,
sender: String,
) {
let (tx, rx): (Sender<(String, String)>, Receiver<(String, String)>) = channel();
backend
.send(BKCommand::GetUserInfoAsync(sender.clone(), Some(tx)))
.unwrap();
gtk::timeout_add(100, move || match rx.try_recv() {
Err(TryRecvError::Empty) => gtk::Continue(true),
Err(TryRecvError::Disconnected) => gtk::Continue(false),
Ok((_name, _avatar)) => {
/* update UI */
//view.update_members_list(index);
//APPOP!(update_members_list, (sender));
gtk::Continue(false)
}
});
/*
pub fn account_settings_get_member_info(
backend: Sender<BKCommand>,
sender: String,
) {
let (tx, rx): (Sender<(String, String)>, Receiver<(String, String)>) = channel();
backend
.send(BKCommand::GetUserInfoAsync(sender.clone(), Some(tx)))
.unwrap();
gtk::timeout_add(100, move || match rx.try_recv() {
Err(TryRecvError::Empty) => gtk::Continue(true),
Err(TryRecvError::Disconnected) => gtk::Continue(false),
Ok((_name, _avatar)) => {
/* update UI */
//view.update_members_list(index);
//APPOP!(update_members_list, (sender));
gtk::Continue(false)
}
});
}
*/
......@@ -8,7 +8,7 @@ use appop::AppOp;
use appop::room::RoomPanel;
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum AppState {
Login,
Chat,
......@@ -75,6 +75,11 @@ impl AppOp {
if let AppState::Directory = self.state {
self.search_rooms(false);
}
/* FIXME: Find better solution to remove reference to widget */
if self.state != AppState::RoomSettings && self.room_settings.is_some() {
self.close_room_settings();
}
}
pub fn escape(&mut self, w: &gtk::ApplicationWindow) -> bool {
......
......@@ -6,13 +6,13 @@ use std::rc::Rc;
use self::gtk::prelude::*;
use backend::BKCommand;
use std::sync::mpsc::Sender;
use cache::download_to_cache;
use fractal_api::types::Room;
use std::sync::mpsc::Sender;
use types::Member;
use widgets;
use widgets::avatar::AvatarExt;
use widgets::members_list::MembersList;
use cache::download_to_cache;
#[derive(Debug, Clone)]
pub struct RoomSettings {
......@@ -24,24 +24,25 @@ pub struct RoomSettings {
}
impl RoomSettings {
pub fn new(backend: Sender<BKCommand>, uid: Option<String>, room: Room) -> RoomSettings {
pub fn new(backend: Sender<BKCommand>, uid: Option<String>, room: Room) -> RoomSettings {
let builder = gtk::Builder::new();
builder.add_from_resource("/org/gnome/Fractal/ui/room_settings.ui")
.expect("Can't load ui file: room_settings.ui");
builder
.add_from_resource("/org/gnome/Fractal/ui/room_settings.ui")
.expect("Can't load ui file: room_settings.ui");
RoomSettings {
room: room,
uid: uid,
builder: builder,
members_list: None,
backend: backend
backend: backend,
}
}
/* creates a empty list with members.len() rows, the content will be loaded when the row is
* drawn */
pub fn create(&mut self) -> Option<(gtk::Box, gtk::Box)> {
* drawn */
pub fn create(&mut self) -> Option<(gtk::Box, gtk::Box)> {
let body = self.builder
.get_object::<gtk::Box>("room_settings_box")
.expect("Can't find room_settings_box in ui file.");
......@@ -55,8 +56,7 @@ impl RoomSettings {
/*as long we don't have an avatar, room topic, members list we show a spinner */
if self.room.avatar.is_none() || self.room.topic.is_none() || self.room.members.len() < 1 {
stack.set_visible_child_name("loading")
}
else {
} else {
stack.set_visible_child_name("info")
}
......@@ -147,12 +147,19 @@ impl RoomSettings {
}));
}
fn create_file_chooser(&mut self, w: &gtk::Button) -> Option<()> {
fn create_file_chooser(&mut self, w: &gtk::Button) -> Option<()> {
let window = w.get_toplevel()?;
if let Ok(window) = window.downcast::<gtk::Window>() {
/* http://gtk-rs.org/docs/gtk/struct.FileChooser.html */
let file_chooser = gtk::FileChooserNative::new("Pick a new room avatar", Some(&window), gtk::FileChooserAction::Open, Some("Select"), None);
let result = gtk::NativeDialog::run(&file_chooser.clone().upcast::<gtk::NativeDialog>());
let file_chooser = gtk::FileChooserNative::new(
"Pick a new room avatar",
Some(&window),
gtk::FileChooserAction::Open,
Some("Select"),
None,
);
let result =
gtk::NativeDialog::run(&file_chooser.clone().upcast::<gtk::NativeDialog>());
if gtk::ResponseType::from(result) == gtk::ResponseType::Accept {
if let Some(file) = file_chooser.get_filename() {
if let Some(path) = file.to_str() {
......@@ -165,7 +172,6 @@ impl RoomSettings {
}
fn init_room_settings(&mut self) -> Option<()> {
let avatar = self.room.avatar.clone();
let name = self.room.name.clone();
let topic = self.room.topic.clone();
let mut is_room = true;
......@@ -187,7 +193,7 @@ impl RoomSettings {
Some(format!("Room · {} members", members.len()))
};
self.room_settings_show_avatar(avatar, edit);
self.room_settings_show_avatar(edit);
self.room_settings_show_room_name(name, edit);
self.room_settings_show_room_topic(topic, is_room, edit);
self.room_settings_show_room_type(description);
......@@ -215,12 +221,10 @@ impl RoomSettings {
}
pub fn close_room_settings(&mut self) {
let scroll = self
.builder
let scroll = self.builder
.get_object::<gtk::ScrolledWindow>("room_settings_scroll")
.expect("Can't find room_settings_scroll in ui file.");
let b = self
.builder
let b = self.builder
.get_object::<gtk::Frame>("room_settings_members_list")
.expect("Can't find room_settings_members_list in ui file.");
for w in b.get_children().iter() {
......@@ -236,12 +240,10 @@ impl RoomSettings {
let label = self.builder
.get_object::<gtk::Label>("room_settings_room_name")
.expect("Can't find room_settings_room_name in ui file.");
let b = self
.builder
let b = self.builder
.get_object::<gtk::Box>("room_settings_room_name_box")
.expect("Can't find room_settings_room_topic_entry in ui file.");
let entry = self
.builder
let entry = self.builder
.get_object::<gtk::Entry>("room_settings_room_name_entry")
.expect("Can't find room_settings_room_name_entry in ui file.");
let button = self.builder
......@@ -389,7 +391,7 @@ impl RoomSettings {
None
}
fn room_settings_show_avatar(&self, avatar: Option<String>, edit: bool) -> Option<()> {
fn room_settings_show_avatar(&self, edit: bool) -> Option<()> {
let container = self.builder
.get_object::<gtk::Box>("room_settings_avatar_box")
.expect("Can't find room_settings_avatar_box in ui file.");
......@@ -403,9 +405,9 @@ impl RoomSettings {
}
}
download_to_cache(self.backend.clone(), self.room.id.clone());
let image = widgets::Avatar::avatar_new(Some(100));
image.circle(self.room.id.clone(), self.room.name.clone(), 100);
let data = image.circle(self.room.id.clone(), self.room.name.clone(), 100);
download_to_cache(self.backend.clone(), self.room.id.clone(), data);
if edit {
let overlay = self.builder
......@@ -446,7 +448,7 @@ impl RoomSettings {
let room = &self.room;
let command = BKCommand::SetRoomAvatar(room.id.clone(), file.clone());
self.backend.send(command).unwrap();
self.room_settings_show_avatar(Some(file), true);
self.room_settings_show_avatar(true);
avatar_btn.set_sensitive(false);
avatar_spinner.show();
None
......
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