From 9a576d70e43ff7a5dd2fd255a0d682d1d335323f Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Sun, 6 Jan 2019 15:26:54 -0600 Subject: [PATCH] Changed config to MVP --- src/controller/mainController.py | 7 ++- src/interactors/__init__.py | 3 +- src/interactors/configuration.py | 51 ++++++++++++++++ src/presenters/__init__.py | 3 +- src/presenters/configuration.py | 66 ++++++++++++++++++++ src/views/__init__.py | 1 + src/views/dialogs/__init__.py | 0 src/views/dialogs/audioRecorder.py | 36 +++++++++++ src/views/dialogs/configuration.py | 98 ++++++++++++++++++++++++++++++ 9 files changed, 260 insertions(+), 5 deletions(-) create mode 100644 src/interactors/configuration.py create mode 100644 src/presenters/configuration.py create mode 100644 src/views/__init__.py create mode 100644 src/views/dialogs/__init__.py create mode 100644 src/views/dialogs/audioRecorder.py create mode 100644 src/views/dialogs/configuration.py diff --git a/src/controller/mainController.py b/src/controller/mainController.py index 9216693..f4c9b60 100644 --- a/src/controller/mainController.py +++ b/src/controller/mainController.py @@ -8,6 +8,9 @@ import logging import wx import widgetUtils import output +import presenters +import interactors +import views from vk_api.exceptions import LoginRequired, VkApiError from requests.exceptions import ConnectionError from pubsub import pub @@ -325,9 +328,7 @@ class Controller(object): def configuration(self, *args, **kwargs): """ Opens the global settings dialogue.""" - d = configuration.configuration(self.session) - if d.response == widgetUtils.OK: - d.save_configuration() + presenter = presenters.configurationPresenter(session=self.session, view=views.configurationDialog(title=_("Preferences")), interactor=interactors.configurationInteractor()) def new_timeline(self, *args, **kwargs): b = self.get_current_buffer() diff --git a/src/interactors/__init__.py b/src/interactors/__init__.py index 57cd2eb..011323e 100644 --- a/src/interactors/__init__.py +++ b/src/interactors/__init__.py @@ -1 +1,2 @@ -from . audioRecorder import * \ No newline at end of file +from . audioRecorder import * +from .configuration import * \ No newline at end of file diff --git a/src/interactors/configuration.py b/src/interactors/configuration.py new file mode 100644 index 0000000..dfa1e53 --- /dev/null +++ b/src/interactors/configuration.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals +import widgetUtils +from pubsub import pub +from . import base + +class configurationInteractor(base.baseInteractor): + + def create_tab(self, tab): + getattr(self.view, "create_"+tab)() + + def set_setting(self, tab, setting, value): + self.view.set_value(tab, setting, value) + + def install(self, *args, **kwargs): + super(configurationInteractor, self).install(*args, **kwargs) + pub.subscribe(self.create_tab, self.modulename+"_create_tab") + pub.subscribe(self.set_setting, self.modulename+"_set") + + def uninstall(self): + super(configurationInteractor, self).uninstall() + pub.unsubscribe(self.create_tab, self.modulename+"_create_tab") + pub.unsubscribe(self.set_setting, self.modulename+"_set") + + def start(self): + self.view.realize() + result = self.view.get_response() + if result == widgetUtils.OK: + self.on_save_settings() + + def on_save_settings(self, *args, **kwargs): + self.presenter.update_setting(section="buffers", setting="count_for_video_buffers", value=self.view.get_value("general", "video_buffers_count")) + self.presenter.update_setting(section="general", setting="load_images", value=self.view.get_value("general", "load_images")) + update_channel = self.presenter.get_update_channel_type(self.view.get_value("general", "update_channel")) + if update_channel != self.presenter.session.settings["general"]["update_channel"]: + if update_channel == "stable": + self.presenter.update_setting(section="general", setting="update_channel", value=update_channel) + elif update_channel == "weekly": + dialog = self.view.weekly_channel() + if dialog == widgetUtils.YES: + self.presenter.update_setting(section="general", setting="update_channel", value=update_channel) + elif update_channel == "alpha": + dialog = self.view.alpha_channel() + if dialog == widgetUtils.YES: + self.presenter.update_setting(section="general", setting="update_channel", value=update_channel) + self.presenter.update_setting(section="chat", setting="notify_online", value=self.view.get_value("chat", "notify_online")) + self.presenter.update_setting(section="chat", setting="notify_offline", value=self.view.get_value("chat", "notify_offline")) + self.presenter.update_setting(section="chat", setting="open_unread_conversations", value=self.view.get_value("chat", "open_unread_conversations")) + self.presenter.update_setting(section="chat", setting="automove_to_conversations", value=self.view.get_value("chat", "automove_to_conversations")) + self.presenter.update_setting(section="chat", setting="notifications", value=self.presenter.get_notification_type(self.view.get_value("chat", "notifications"))) + self.presenter.save_settings_file() \ No newline at end of file diff --git a/src/presenters/__init__.py b/src/presenters/__init__.py index 3ed3c0b..e83f10b 100644 --- a/src/presenters/__init__.py +++ b/src/presenters/__init__.py @@ -1,2 +1,3 @@ # -*- coding: utf-8 -*- -from .audioRecorder import * \ No newline at end of file +from .audioRecorder import * +from .configuration import * \ No newline at end of file diff --git a/src/presenters/configuration.py b/src/presenters/configuration.py new file mode 100644 index 0000000..6e541d0 --- /dev/null +++ b/src/presenters/configuration.py @@ -0,0 +1,66 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals +from pubsub import pub +from interactors import configuration as interactor + +class configurationPresenter(object): + + def __init__(self, session, view, interactor): + self.interactor = interactor + self.session = session + self.view = view + self.interactor.install(view=view, presenter=self, modulename="configuration") + self.create_config() + self.interactor.start() + self.interactor.uninstall() + + def get_notification_label(self, value): + if value == "native": + return _("Native") + else: + return _("Custom") + + def get_update_channel_label(self, value): + if value == "stable": + return _("Stable") + elif value == "weekly": + return _("Weekly") + else: + return _("Alpha") + + def get_notification_type(self, value): + if value == _("Native"): + return "native" + else: + return "custom" + + def get_update_channel_type(self, value): + if value == _("Stable"): + return "stable" + elif value == _("Weekly"): + return "weekly" + else: + return "alpha" + + def create_config(self): + pub.sendMessage("configuration_create_tab", tab="general") + pub.sendMessage("configuration_set", tab="general", setting="wall_buffer_count", value=self.session.settings["buffers"]["count_for_wall_buffers"]) + pub.sendMessage("configuration_set", tab="general", setting="video_buffers_count", value=self.session.settings["buffers"]["count_for_video_buffers"]) + pub.sendMessage("configuration_set", tab="general", setting="load_images", value=self.session.settings["general"]["load_images"]) + pub.sendMessage("configuration_set", tab="general", setting="update_channel", value=self.get_update_channel_label(self.session.settings["general"]["update_channel"])) + pub.sendMessage("configuration_create_tab", tab="chat") + pub.sendMessage("configuration_set", tab="chat", setting="notify_online", value=self.session.settings["chat"]["notify_online"]) + pub.sendMessage("configuration_set", tab="chat", setting="notify_offline", value=self.session.settings["chat"]["notify_offline"]) + pub.sendMessage("configuration_set", tab="chat", setting="open_unread_conversations", value=self.session.settings["chat"]["open_unread_conversations"]) + pub.sendMessage("configuration_set", tab="chat", setting="automove_to_conversations", value=self.session.settings["chat"]["automove_to_conversations"]) + pub.sendMessage("configuration_set", tab="chat", setting="notifications", value=self.get_notification_label(self.session.settings["chat"]["notifications"])) + + def update_setting(self, section, setting, value): + if section not in self.session.settings: + raise AttributeError("The configuration section is not present in the spec file.") + if setting not in self.session.settings[section]: + raise AttributeError("The setting you specified is not present in the config file.") + self.session.settings[section][setting] = value + + def save_settings_file(self): + self.session.settings.write() \ No newline at end of file diff --git a/src/views/__init__.py b/src/views/__init__.py new file mode 100644 index 0000000..2dec1a3 --- /dev/null +++ b/src/views/__init__.py @@ -0,0 +1 @@ +from .dialogs.configuration import * \ No newline at end of file diff --git a/src/views/dialogs/__init__.py b/src/views/dialogs/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/views/dialogs/audioRecorder.py b/src/views/dialogs/audioRecorder.py new file mode 100644 index 0000000..5c05e07 --- /dev/null +++ b/src/views/dialogs/audioRecorder.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals +import wx +import widgetUtils + +class audioRecorderDialog(widgetUtils.BaseDialog): + def __init__(self): + super(audioRecorderDialog, self).__init__(None, title=_("Record voice message")) + panel = wx.Panel(self) + sizer = wx.BoxSizer(wx.VERTICAL) + self.play = wx.Button(panel, -1, _("&Play")) + self.play.Disable() + self.record = wx.Button(panel, -1, _("&Record")) + self.record.SetFocus() + self.discard = wx.Button(panel, -1, _("&Discard")) + self.discard.Disable() + self.ok = wx.Button(panel, wx.ID_OK, _("&Add")) + cancel = wx.Button(panel, wx.ID_CANCEL, _("&Cancel")) + btnSizer = wx.BoxSizer(wx.HORIZONTAL) + btnSizer2 = wx.BoxSizer(wx.HORIZONTAL) + btnSizer.Add(self.play, 0, wx.ALL, 5) + btnSizer.Add(self.record, 0, wx.ALL, 5) + btnSizer2.Add(self.ok, 0, wx.ALL, 5) + btnSizer2.Add(cancel, 0, wx.ALL, 5) + sizer.Add(btnSizer, 0, wx.ALL, 5) + sizer.Add(btnSizer2, 0, wx.ALL, 5) + panel.SetSizer(sizer) + self.SetClientSize(sizer.CalcMin()) + + def enable_control(self, control): + if hasattr(self, control): + getattr(self, control).Enable() + + def disable_control(self, control): + if hasattr(self, control): + getattr(self, control).Disable() \ No newline at end of file diff --git a/src/views/dialogs/configuration.py b/src/views/dialogs/configuration.py new file mode 100644 index 0000000..b262751 --- /dev/null +++ b/src/views/dialogs/configuration.py @@ -0,0 +1,98 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals +import wx +import widgetUtils + +class general(wx.Panel, widgetUtils.BaseDialog): + def __init__(self, panel): + super(general, self).__init__(panel) + sizer = wx.BoxSizer(wx.VERTICAL) + lbl1 = wx.StaticText(self, wx.NewId(), _("Number of items to load for newsfeed and wall buffers (maximun 100)")) + self.wall_buffer_count = wx.SpinCtrl(self, wx.NewId()) + self.wall_buffer_count.SetRange(1, 100) + box1 = wx.BoxSizer(wx.HORIZONTAL) + box1.Add(lbl1, 0, wx.ALL, 5) + box1.Add(self.wall_buffer_count, 0, wx.ALL, 5) + sizer.Add(box1, 0, wx.ALL, 5) + lbl3 = wx.StaticText(self, wx.NewId(), _("Number of items to load in video buffers (maximun 200)")) + self.video_buffers_count = wx.SpinCtrl(self, wx.NewId()) + self.video_buffers_count.SetRange(1, 200) + box3 = wx.BoxSizer(wx.HORIZONTAL) + box3.Add(lbl3, 0, wx.ALL, 5) + box3.Add(self.video_buffers_count, 0, wx.ALL, 5) + sizer.Add(box3, 0, wx.ALL, 5) + self.load_images = wx.CheckBox(self, wx.NewId(), _("Load images in posts")) + sizer.Add(self.load_images, 0, wx.ALL, 5) + lbl4 = wx.StaticText(self, wx.NewId(), _("Update channel")) + self.update_channel = wx.ComboBox(self, wx.NewId(), choices=[_("Stable"), _("Alpha")], value=_("Native"), style=wx.CB_READONLY) + box4 = wx.BoxSizer(wx.HORIZONTAL) + box4.Add(lbl4, 0, wx.ALL, 5) + box4.Add(self.update_channel, 0, wx.ALL, 5) + sizer.Add(box4, 0, wx.ALL, 5) + self.SetSizer(sizer) + +class chat(wx.Panel, widgetUtils.BaseDialog): + def __init__(self, panel): + super(chat, self).__init__(panel) + sizer = wx.BoxSizer(wx.VERTICAL) + self.notify_online = wx.CheckBox(self, wx.NewId(), _("Show notifications when users are online")) + sizer.Add(self.notify_online, 0, wx.ALL, 5) + self.notify_offline = wx.CheckBox(self, wx.NewId(), _("Show notifications when users are offline")) + sizer.Add(self.notify_offline, 0, wx.ALL, 5) + self.open_unread_conversations = wx.CheckBox(self, wx.NewId(), _("Open unread conversations at startup")) + sizer.Add(self.open_unread_conversations, 0, wx.ALL, 5) + self.automove_to_conversations = wx.CheckBox(self, wx.NewId(), _("Move focus to new conversations")) + sizer.Add(self.automove_to_conversations, 0, wx.ALL, 5) + lbl = wx.StaticText(self, wx.NewId(), _("Notification type")) + self.notifications = wx.ComboBox(self, wx.NewId(), choices=[_("Native"), _("Custom"),], value=_("Native"), style=wx.CB_READONLY) + nbox = wx.BoxSizer(wx.HORIZONTAL) + nbox.Add(lbl, 0, wx.ALL, 5) + nbox.Add(self.notifications, 0, wx.ALL, 5) + sizer.Add(nbox, 0, wx.ALL, 5) + self.SetSizer(sizer) + +class configurationDialog(widgetUtils.BaseDialog): + + def __init__(self, title): + super(configurationDialog, self).__init__(None, -1, title=title) + self.panel = wx.Panel(self) + self.sizer = wx.BoxSizer(wx.VERTICAL) + self.notebook = wx.Notebook(self.panel) + + def create_general(self): + self.general = general(self.notebook) + self.notebook.AddPage(self.general, _("General")) + self.general.SetFocus() + + def create_chat(self): + self.chat = chat(self.notebook) + self.notebook.AddPage(self.chat, _("Chat settings")) + + + def realize(self): + self.sizer.Add(self.notebook, 0, wx.ALL, 5) + ok_cancel_box = wx.BoxSizer(wx.HORIZONTAL) + ok = wx.Button(self.panel, wx.ID_OK, _("Save")) + ok.SetDefault() + cancel = wx.Button(self.panel, wx.ID_CANCEL, _("Close")) + self.SetEscapeId(cancel.GetId()) + ok_cancel_box.Add(ok, 0, wx.ALL, 5) + ok_cancel_box.Add(cancel, 0, wx.ALL, 5) + self.sizer.Add(ok_cancel_box, 0, wx.ALL, 5) + self.panel.SetSizer(self.sizer) + self.SetClientSize(self.sizer.CalcMin()) + + def get_value(self, panel, key): + p = getattr(self, panel) + return getattr(p, key).GetValue() + + def set_value(self, panel, key, value): + p = getattr(self, panel) + control = getattr(p, key) + getattr(control, "SetValue")(value) + + def alpha_channel(self): + return wx.MessageDialog(self, _("The alpha channel contains bleeding edge changes introduced to Socializer. A new alpha update is generated every time there are new changes in the project. Take into account that updates are generated automatically and may fail at any time due to errors in the build process. Use alpha channels when you are sure you want to try the latest changes and contribute with reports to fix bugs. Never use alpha channel updates for everyday use. Do you want to continue?"), _("Attention"), style=wx.ICON_QUESTION|wx.YES_NO).ShowModal() + + def weekly_channel(self): + return wx.MessageDialog(self, _("The weekly channel generates an update automatically every week by building the source code present in the project. This version is used to test features added to the next stable version. Do you want to continue?"), _("Attention"), style=wx.ICON_QUESTION|wx.YES_NO).ShowModal() \ No newline at end of file