Skip to content

Commit

Permalink
VENOM-302: Refactor UserInfoWidget to mvvm
Browse files Browse the repository at this point in the history
  • Loading branch information
naxuroqa committed Apr 8, 2018
1 parent 922b718 commit 9337ce5
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 96 deletions.
5 changes: 3 additions & 2 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ venom_source = files(
'ui/NodeWidget.vala',
'ui/PeerEntry.vala',
'ui/SettingsWidget.vala',
'ui/UserInfoWidget.vala',
'ui/UITools.vala',
'ui/WelcomeWidget.vala',
'view/AddContactWidget.vala',
Expand All @@ -80,13 +79,15 @@ venom_source = files(
'view/FileTransferEntry.vala',
'view/FriendInfoWidget.vala',
'view/MessageWidget.vala',
'view/UserInfoWidget.vala',
'viewmodel/AddContactViewModel.vala',
'viewmodel/ConferenceInfoViewModel.vala',
'viewmodel/ContactListEntryViewModel.vala',
'viewmodel/CreateGroupchatViewModel.vala',
'viewmodel/FileTransferEntryViewModel.vala',
'viewmodel/FriendInfoViewModel.vala',
'viewmodel/MessageViewModel.vala'
'viewmodel/MessageViewModel.vala',
'viewmodel/UserInfoViewModel.vala'
)

venom_ui_resources = gnome.compile_resources( 'venom_ui_resources', 'ui/venom.gresource.xml',
Expand Down
86 changes: 0 additions & 86 deletions src/ui/UserInfoWidget.vala

This file was deleted.

24 changes: 16 additions & 8 deletions src/ui/user_info_widget.ui
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.21.0 -->
<!-- Generated with glade 3.20.4 -->
<interface domain="venom">
<requires lib="gtk+" version="3.20"/>
<object class="GtkSizeGroup"/>
Expand Down Expand Up @@ -402,6 +402,9 @@
</child>
</object>
</child>
<style>
<class name="frame"/>
</style>
</object>
<packing>
<property name="expand">False</property>
Expand All @@ -410,21 +413,26 @@
</packing>
</child>
<child>
<object class="GtkImage" id="image_qr_code">
<object class="GtkButton">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="no_show_all">True</property>
<property name="icon_name">emblem-shared-symbolic</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="no_show_all">True</property>
<property name="icon_name">view-more-symbolic</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<style>
<class name="frame"/>
</style>
</object>
<packing>
<property name="expand">False</property>
Expand Down
60 changes: 60 additions & 0 deletions src/view/UserInfoWidget.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* UserInfoWidget.vala
*
* Copyright (C) 2013-2018 Venom authors and contributors
*
* This file is part of Venom.
*
* Venom is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Venom is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Venom. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Venom {
[GtkTemplate(ui = "/im/tox/venom/ui/user_info_widget.ui")]
public class UserInfoWidget : Gtk.Box {
[GtkChild] private Gtk.Entry entry_username;
[GtkChild] private Gtk.Entry entry_statusmessage;
[GtkChild] private Gtk.Image image_userimage;
[GtkChild] private Gtk.Label label_id;
[GtkChild] private Gtk.FileChooserButton filechooser;
[GtkChild] private Gtk.Button apply;

private ILogger logger;
private UserInfoViewModel view_model;

private string _filename;
public string filename {
get { return _filename = filechooser.get_filename(); }
set { filechooser.set_filename(value); }
}

public UserInfoWidget(ILogger logger, UserInfo user_info) {
logger.d("UserInfoWidget created.");
this.logger = logger;
this.view_model = new UserInfoViewModel(logger, user_info);

view_model.bind_property("username", entry_username, "text", GLib.BindingFlags.SYNC_CREATE | GLib.BindingFlags.BIDIRECTIONAL);
view_model.bind_property("statusmessage", entry_statusmessage, "text", GLib.BindingFlags.SYNC_CREATE | GLib.BindingFlags.BIDIRECTIONAL);
view_model.bind_property("userimage", image_userimage, "pixbuf", GLib.BindingFlags.SYNC_CREATE | GLib.BindingFlags.BIDIRECTIONAL);
view_model.bind_property("tox-id", label_id, "label", GLib.BindingFlags.SYNC_CREATE);
view_model.bind_property("filename", this, "filename", GLib.BindingFlags.SYNC_CREATE);

filechooser.file_set.connect(view_model.on_file_selected);
apply.clicked.connect(view_model.on_apply_clicked);
}

~UserInfoWidget() {
logger.d("UserInfoWidget destroyed.");
}
}
}
67 changes: 67 additions & 0 deletions src/viewmodel/UserInfoViewModel.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* UserInfoViewModel.vala
*
* Copyright (C) 2018 Venom authors and contributors
*
* This file is part of Venom.
*
* Venom is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Venom is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Venom. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Venom {
public class UserInfoViewModel : GLib.Object {
public string username { get; set; }
public string statusmessage { get; set; }
public Gdk.Pixbuf userimage { get; set; }
public string tox_id { get; set; }
public Gdk.Pixbuf tox_qr_code { get; set; }
public string filename { get; set; }

private ILogger logger;
private UserInfo user_info;

public UserInfoViewModel(ILogger logger, UserInfo user_info) {
logger.d("UserInfoViewModel created.");
this.logger = logger;
this.user_info = user_info;

username = user_info.get_name();
statusmessage = user_info.get_status_message();
userimage = user_info.get_image();
tox_id = user_info.get_tox_id();
filename = "";
}

public void on_apply_clicked() {
logger.d("on_apply_clicked.");
user_info.set_name(username);
user_info.set_status_message(statusmessage);
user_info.set_image(userimage);
user_info.info_changed(this);
}

public void on_file_selected() {
logger.d("on_file_selected.");
try {
userimage = new Gdk.Pixbuf.from_file_at_scale(filename, 100, 100, true);
} catch (Error e) {
logger.e("Could not read file: " + e.message);
}
}

~UserInfoViewModel() {
logger.d("UserInfoViewModel destroyed.");
}
}
}

0 comments on commit 9337ce5

Please sign in to comment.