91 lines
3.3 KiB
Python
91 lines
3.3 KiB
Python
class Screen2(tk.Frame):
|
|
def __init__(self, master):
|
|
tk.Frame.__init__(self, master, bg='#bcfef9')
|
|
|
|
# Load users from users.json if it exists and is not empty
|
|
self.users = self.get_users_from_file()
|
|
|
|
# Label for instructions
|
|
tk.Label(self, text="Start typing your public key to find yourself in the list", bg='#bcfef9').pack(anchor='nw')
|
|
|
|
# Entry box for user search
|
|
self.search_var = tk.StringVar()
|
|
self.search_var.trace('w', self.update_users_list)
|
|
self.search_entry = tk.Entry(self, textvariable=self.search_var, width=50)
|
|
self.search_entry.pack(anchor='nw')
|
|
|
|
# Refresh button
|
|
refresh_button = tk.Button(self, text="Refresh List", command=self.refresh_users, height=3, width=30, bg='peach puff')
|
|
refresh_button.place(relx=0.25, rely=0, anchor='nw')
|
|
|
|
# The 'Done' button to navigate to next screen
|
|
done_button = tk.Button(self, text="Done", command=lambda: master.switch_frame(Screen3), height=3, width=30, bg='peach puff')
|
|
done_button.pack(pady=10)
|
|
|
|
# Container for user list
|
|
self.container = ttk.Frame(self)
|
|
self.container.pack(fill='both', expand=True)
|
|
|
|
# Display initial list of users
|
|
self.update_users_list()
|
|
|
|
def get_users_from_file(self):
|
|
# Load users from users.json if it exists and is not empty
|
|
if os.path.exists("users.json") and os.path.getsize("users.json") > 0:
|
|
with open("users.json", 'r') as f:
|
|
try:
|
|
users = json.load(f)
|
|
except json.JSONDecodeError:
|
|
print("users.json is not valid JSON. Refreshing users...")
|
|
users = self.refresh_users()
|
|
else:
|
|
users = self.refresh_users()
|
|
return users
|
|
|
|
def refresh_users(self):
|
|
# Update users list from Scuttlebutt and display it
|
|
users = self.get_scuttlebutt_users()
|
|
self.users = users
|
|
self.update_users_list()
|
|
|
|
def get_scuttlebutt_users(self):
|
|
result = subprocess.run(['node', 'scuttlebot.js'], capture_output=True, text=True)
|
|
if result.returncode == 0:
|
|
users = json.loads(result.stdout)
|
|
# Save the users list to a JSON file
|
|
with open('users.json', 'w') as f:
|
|
json.dump(users, f)
|
|
else:
|
|
print("Command failed: " + result.stderr)
|
|
users = []
|
|
return users
|
|
|
|
def update_users_list(self, *args):
|
|
search_text = self.search_var.get().lower()
|
|
|
|
# Remove current users list
|
|
for widget in self.container.winfo_children():
|
|
widget.destroy()
|
|
|
|
# Filtered list of users
|
|
users = [user for user in self.users if search_text in user['id'].lower()]
|
|
|
|
self.display_users(users)
|
|
|
|
def display_users(self, users):
|
|
# Scrollable list of users
|
|
canvas = tk.Canvas(self.container)
|
|
scrollbar = ttk.Scrollbar(self.container, orient='vertical', command=canvas.yview, width=60)
|
|
scrollable_frame = ttk.Frame(canvas)
|
|
|
|
scrollable_frame.bind(
|
|
"<Configure>",
|
|
lambda e: canvas.configure(
|
|
scrollregion=canvas.bbox("all")
|
|
)
|
|
)
|
|
|
|
canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
|
|
canvas.configure(yscrollcommand
|
|
|