Skip to content

Commit

Permalink
VENOM-302: Refactor ContactListWidget to mvvm
Browse files Browse the repository at this point in the history
  • Loading branch information
naxuroqa committed Apr 8, 2018
1 parent 99d3021 commit f2f9a73
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 57 deletions.
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ venom_source = files(
'viewmodel/AddContactViewModel.vala',
'viewmodel/ConferenceInfoViewModel.vala',
'viewmodel/ContactListEntryViewModel.vala',
'viewmodel/ContactListViewModel.vala',
'viewmodel/CreateGroupchatViewModel.vala',
'viewmodel/FileTransferEntryViewModel.vala',
'viewmodel/FriendInfoViewModel.vala',
Expand Down
71 changes: 14 additions & 57 deletions src/view/ContactListWidget.vala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* ContactListWidget.vala
*
* Copyright (C) 2017-2018 Venom authors and contributors
* Copyright (C) 2017-2018 Venom authors and contributors
*
* This file is part of Venom.
*
Expand All @@ -25,58 +25,28 @@ namespace Venom {
private ILogger logger;
private ContactListWidgetCallback callback;
private UserInfo user_info;
private ContactListViewModel view_model;

[GtkChild]
private Gtk.Label username;
[GtkChild]
private Gtk.Label statusmessage;
[GtkChild]
private Gtk.Image userimage;
[GtkChild]
private Gtk.ListBox contact_list;
[GtkChild]
private Gtk.Image image_status;
[GtkChild] private Gtk.Label username;
[GtkChild] private Gtk.Label statusmessage;
[GtkChild] private Gtk.Image userimage;
[GtkChild] private Gtk.ListBox contact_list;
[GtkChild] private Gtk.Image image_status;

public ContactListWidget(ILogger logger, ObservableList<IContact> contacts, ContactListWidgetCallback callback, UserInfo user_info) {
logger.d("ContactListWidget created.");
this.logger = logger;
this.callback = callback;
this.user_info = user_info;
this.view_model = new ContactListViewModel(logger, contacts, callback, user_info);

refresh_user_info(this);
user_info.info_changed.connect(refresh_user_info);
view_model.bind_property("username", username, "label", GLib.BindingFlags.SYNC_CREATE);
view_model.bind_property("statusmessage", statusmessage, "label", GLib.BindingFlags.SYNC_CREATE);
view_model.bind_property("userimage", userimage, "pixbuf", GLib.BindingFlags.SYNC_CREATE);
view_model.bind_property("image-status", image_status, "icon-name", GLib.BindingFlags.SYNC_CREATE);

contact_list.bind_model(new ObservableListModel<IContact>(contacts), create_entry);
contact_list.row_activated.connect(on_row_activated);
}

private void refresh_user_info(GLib.Object sender) {
username.label = user_info.get_name();
statusmessage.label = user_info.get_status_message();
var userimage_pixbuf = scalePixbuf(user_info.get_image());
if (userimage_pixbuf != null) {
userimage.set_from_pixbuf(userimage_pixbuf);
}
image_status.set_from_icon_name(get_resource_from_status(user_info.get_user_status()), Gtk.IconSize.INVALID);
}

private string get_resource_from_status(UserStatus status) {
switch (status) {
case UserStatus.ONLINE:
return R.icons.online;
case UserStatus.AWAY:
return R.icons.idle;
case UserStatus.BUSY:
return R.icons.busy;
}
return R.icons.offline;
}

private Gdk.Pixbuf ? scalePixbuf(Gdk.Pixbuf ? pixbuf) {
if (pixbuf == null) {
return null;
}
return pixbuf.scale_simple(44, 44, Gdk.InterpType.BILINEAR);
contact_list.bind_model(view_model.get_list_model(), create_entry);
contact_list.row_activated.connect(view_model.on_row_activated);
}

private Gtk.Widget create_entry(GLib.Object object) {
Expand All @@ -87,21 +57,8 @@ namespace Venom {
return new ContactListRequestEntry(logger, c);
}

private void on_row_activated(Gtk.ListBoxRow row) {
if (row is IContactListEntry) {
var entry = row as IContactListEntry;
callback.on_contact_selected(entry.get_contact());
} else {
logger.e("ContactListWidget wrong type selected.");
}
}

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

public interface ContactListWidgetCallback : GLib.Object {
public abstract void on_contact_selected(IContact contact);
}
}
95 changes: 95 additions & 0 deletions src/viewmodel/ContactListViewModel.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* ContactListViewModel.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 ContactListViewModel : GLib.Object {
private ILogger logger;
private ContactListWidgetCallback callback;
private UserInfo user_info;
private ObservableList<IContact> contacts;

public string username { get; set; }
public string statusmessage { get; set; }
public Gdk.Pixbuf userimage { get; set; }
public string image_status { get; set; }

public ContactListViewModel(ILogger logger, ObservableList<IContact> contacts, ContactListWidgetCallback callback, UserInfo user_info) {
logger.d("ContactListViewModel created.");
this.logger = logger;
this.callback = callback;
this.user_info = user_info;
this.contacts = contacts;

refresh_user_info(this);
user_info.info_changed.connect(refresh_user_info);
}

public ListModel get_list_model() {
return new ObservableListModel<IContact>(contacts);
}

public void on_row_activated(Gtk.ListBoxRow row) {
if (row is IContactListEntry) {
var entry = row as IContactListEntry;
callback.on_contact_selected(entry.get_contact());
} else {
logger.e("ContactListViewModel wrong type selected.");
}
}

private void refresh_user_info(GLib.Object sender) {
username = user_info.get_name();
statusmessage = user_info.get_status_message();
var userimage_pixbuf = scalePixbuf(user_info.get_image());
if (userimage_pixbuf != null) {
userimage = userimage_pixbuf;
}
image_status = get_resource_from_status(user_info.get_user_status());
}

private string get_resource_from_status(UserStatus status) {
switch (status) {
case UserStatus.ONLINE:
return R.icons.online;
case UserStatus.AWAY:
return R.icons.idle;
case UserStatus.BUSY:
return R.icons.busy;
}
return R.icons.offline;
}

private Gdk.Pixbuf ? scalePixbuf(Gdk.Pixbuf ? pixbuf) {
if (pixbuf == null) {
return null;
}
return pixbuf.scale_simple(44, 44, Gdk.InterpType.BILINEAR);
}

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

public interface ContactListWidgetCallback : GLib.Object {
public abstract void on_contact_selected(IContact contact);
}
}

0 comments on commit f2f9a73

Please sign in to comment.