Permalink
Browse files

Example 61: Removing duplicate code for chat window creation.

We pull the code that creates a chat window if it doesn't exist into its own method. The calling code is now
much easier to follow.
  • Loading branch information...
1 parent f5d824b commit d569f3a9203afebb678bb57a539d51827718fb50 @buchuki committed Jul 25, 2013
Showing with 8 additions and 6 deletions.
  1. +8 −6 orkiv/__main__.py
View
14 orkiv/__main__.py
@@ -134,20 +134,22 @@ def show_buddy_list(self):
buddy_list_item.deselect()
self.add_widget(self.buddy_list)
- def show_buddy_chat(self, jabber_id):
- self.remove_widget(self.buddy_list)
+ def get_chat_window(self, jabber_id):
if jabber_id not in self.chat_windows:
self.chat_windows[jabber_id] = ChatWindow(jabber_id=jabber_id)
- self.add_widget(self.chat_windows[jabber_id])
+ return self.chat_windows[jabber_id]
+
+ def show_buddy_chat(self, jabber_id):
+ self.remove_widget(self.buddy_list)
+ self.add_widget(self.get_chat_window(jabber_id))
def handle_xmpp_message(self, message):
if message['type'] not in ['normal', 'chat']:
return
jabber_id = message['from'].bare
- if jabber_id not in self.chat_windows:
- self.chat_windows[jabber_id] = ChatWindow(jabber_id=jabber_id)
- self.chat_windows[jabber_id].chat_log_label.text += "(%s) %s: %s\n" % (
+ chat_window = self.get_chat_window(jabber_id)
+ chat_window.chat_log_label.text += "(%s) %s: %s\n" % (
datetime.datetime.now().strftime("%Y-%m-%d %H:%M"),
jabber_id,
message['body'])

0 comments on commit d569f3a

Please sign in to comment.