81 lines
3.0 KiB
Python
81 lines
3.0 KiB
Python
import json
|
|
import tkinter as tk
|
|
from app.http import request_builder
|
|
from app.views.chat import ChatWindow
|
|
from app.views.user_frame import UserFrame
|
|
|
|
|
|
class MainPageView(tk.Frame):
|
|
"""
|
|
This is the view from which the user will open a chat with anyone which is registered in the system. Each chat is
|
|
shown as a box with a photo, the username and the email.
|
|
"""
|
|
def __init__(self, master=None):
|
|
super().__init__(master)
|
|
self.form_frame = None
|
|
self.define_http_params()
|
|
self.create_widgets()
|
|
|
|
def define_http_params(self):
|
|
self.url = "http://localhost:8000/users/get_all"
|
|
self.headers = {"Content-Type": "application/json"}
|
|
self.method = "GET"
|
|
self.params = None
|
|
self.body = None
|
|
|
|
def create_widgets(self):
|
|
"""Takes care of the creation of the UI"""
|
|
# Create a main frame to center the user frame and button frame
|
|
main_frame = tk.Frame(self.master)
|
|
main_frame.pack(expand=True)
|
|
|
|
# Create a frame to hold the user frames
|
|
user_frame_container = tk.Frame(main_frame)
|
|
user_frame_container.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
|
|
|
|
users_data = self.get_user_list()
|
|
users_data = [user for user in users_data if user[1] != self.master.current_logged_user['username']]
|
|
|
|
# Display users dynamically with two users per row
|
|
for i in range(0, len(users_data)):
|
|
user_data_slice = users_data[i]
|
|
|
|
# Calculate the row and column for the UserFrame in the grid
|
|
row = i // 2
|
|
column = i % 2
|
|
|
|
user_frame_instance = UserFrame(user_frame_container, user_data_slice, self.master.current_logged_user,
|
|
self.open_chat)
|
|
user_frame_instance.grid(row=row, column=column, padx=10, pady=10)
|
|
|
|
# Create a frame to hold the buttons
|
|
button_frame = tk.Frame(main_frame)
|
|
button_frame.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
|
|
|
|
# Create buttons in the button frame
|
|
logout_button = tk.Button(button_frame, text="Logout", command=self.master.change_to_initial_view)
|
|
|
|
# Pack buttons in the button frame
|
|
logout_button.pack(padx=5, pady=5)
|
|
|
|
def return_to_initial_view(self):
|
|
self.master.change_to_initial_view()
|
|
|
|
def open_chat(self, user_data):
|
|
"""Opens a chat in a different window"""
|
|
# Pass both the clicked user data and the current logged user info to ChatWindow
|
|
chat_window = ChatWindow(self, user_data, self.master.current_logged_user)
|
|
chat_window.mainloop() # Main loop for the new window
|
|
|
|
def get_user_list(self):
|
|
request_builder.set_url(self.url)
|
|
request_builder.set_headers(self.headers)
|
|
request_builder.set_method(self.method)
|
|
request_builder.set_body(self.body)
|
|
request_builder.set_params(self.params)
|
|
|
|
json_string = request_builder.build().make_request().text
|
|
data = json.loads(json_string)
|
|
|
|
return data.get('users', [])
|