first commit
This commit is contained in:
164
client/pages/home/applications.py
Normal file
164
client/pages/home/applications.py
Normal file
@@ -0,0 +1,164 @@
|
||||
import flet as ft
|
||||
from models.applications import DBApplications
|
||||
import random
|
||||
import string
|
||||
from pages.home.application_page import ApplicationPage
|
||||
|
||||
class Applications:
|
||||
def __init__(self, page: ft.Page, dashboard):
|
||||
self.page = page
|
||||
self.dashboard = dashboard
|
||||
self.db_applications = DBApplications()
|
||||
self.user_id = self.page.client_storage.get('user_id')
|
||||
self.add_dialog = ft.AlertDialog(
|
||||
title=ft.Text("Add Application"),
|
||||
content=ft.TextField(label="Name"),
|
||||
actions=[
|
||||
ft.FilledButton(
|
||||
"Save",
|
||||
width=100,
|
||||
on_click=self.on_save_btn_click,
|
||||
bgcolor=ft.Colors.BLUE
|
||||
),
|
||||
ft.FilledButton(
|
||||
"Cancel",
|
||||
width=100,
|
||||
on_click=self.on_cancel_btn_click,
|
||||
bgcolor=ft.Colors.GREY
|
||||
),
|
||||
]
|
||||
)
|
||||
self.all_applications = self.db_applications.get_applications(self.user_id)
|
||||
self.applications_list = ft.Column(
|
||||
controls=self.create_list(self.all_applications, self.on_manage_app_btn_click, self.on_delete_app_btn_click),
|
||||
)
|
||||
|
||||
self.delete_dialog = ft.AlertDialog(
|
||||
title="Delete Application?",
|
||||
actions=[
|
||||
ft.FilledButton(
|
||||
"Yes", on_click=self.on_yes_button_click,
|
||||
width=100,
|
||||
bgcolor=ft.Colors.BLUE
|
||||
),
|
||||
ft.FilledButton(
|
||||
"No", on_click=self.on_no_button_click,
|
||||
width=100,
|
||||
bgcolor=ft.Colors.GREY
|
||||
)
|
||||
]
|
||||
)
|
||||
self.selected_application_id = None
|
||||
|
||||
def on_yes_button_click(self, e):
|
||||
self.page.close(self.delete_dialog)
|
||||
self.db_applications.delete(self.selected_application_id)
|
||||
self.selected_application_id = None
|
||||
self.all_applications = self.db_applications.get_applications(self.user_id)
|
||||
self.applications_list.controls.clear()
|
||||
self.applications_list.controls = self.create_list(self.all_applications, self.on_manage_app_btn_click, self.on_delete_app_btn_click)
|
||||
self.applications_list.update()
|
||||
|
||||
def on_no_button_click(self, e):
|
||||
self.page.close(self.delete_dialog)
|
||||
|
||||
def on_add_btn_click(self, e):
|
||||
self.page.open(self.add_dialog)
|
||||
|
||||
def on_save_btn_click(self, e):
|
||||
application_name = self.add_dialog.content.value
|
||||
self.save_new_application(application_name)
|
||||
self.add_dialog.content.value = ''
|
||||
self.page.close(self.add_dialog)
|
||||
self.all_applications = self.db_applications.get_applications(self.user_id)
|
||||
self.applications_list.controls.clear()
|
||||
self.applications_list.controls = self.create_list(self.all_applications, self.on_manage_app_btn_click, self.on_delete_app_btn_click)
|
||||
self.applications_list.update()
|
||||
|
||||
def on_cancel_btn_click(self, e):
|
||||
self.page.close(self.add_dialog)
|
||||
|
||||
def save_new_application(self, name):
|
||||
access_code = ''.join(random.choices(string.ascii_uppercase + string.digits, k=12))
|
||||
self.db_applications.insert_application(self.user_id, name, access_code)
|
||||
|
||||
def on_manage_app_btn_click(self, item):
|
||||
applications = ApplicationPage(self.page, self.dashboard, item)
|
||||
self.dashboard.placeholder.content.clean()
|
||||
self.dashboard.placeholder.content = applications.build()
|
||||
self.dashboard.placeholder.update()
|
||||
|
||||
def on_delete_app_btn_click(self, id):
|
||||
self.selected_application_id = id
|
||||
self.page.open(self.delete_dialog)
|
||||
|
||||
def create_list(self, items, on_click_handler, on_click_handler2):
|
||||
elements = []
|
||||
row = ft.Row()
|
||||
counter = 0
|
||||
for item in items:
|
||||
row.controls.append(
|
||||
ft.Container(
|
||||
ft.Row(
|
||||
[
|
||||
ft.Icon(ft.Icons.PHONE_ANDROID, size=100),
|
||||
ft.Column(
|
||||
[
|
||||
ft.Text(item["name"] if len(item['name']) < 35 else item['name'][:35]+"...", expand=True, weight=ft.FontWeight.BOLD),
|
||||
ft.Row(
|
||||
[
|
||||
ft.IconButton(
|
||||
on_click=lambda e, id=item: on_click_handler(id),
|
||||
icon = ft.Icons.EDIT,
|
||||
),
|
||||
ft.IconButton(
|
||||
on_click=lambda e, id=item['id']: on_click_handler2(id),
|
||||
icon=ft.Icons.DELETE,
|
||||
icon_color=ft.Colors.RED
|
||||
)
|
||||
]
|
||||
)
|
||||
],
|
||||
alignment=ft.MainAxisAlignment.SPACE_BETWEEN,
|
||||
expand=True
|
||||
)
|
||||
]
|
||||
),
|
||||
border_radius=10,
|
||||
border=ft.border.all(1, ft.Colors.GREY_300),
|
||||
padding=5
|
||||
)
|
||||
)
|
||||
counter += 1
|
||||
if counter % 3 == 0:
|
||||
elements.append(row)
|
||||
row = ft.Row()
|
||||
|
||||
if len(row.controls)> 0:
|
||||
elements.append(row)
|
||||
print(elements)
|
||||
return elements
|
||||
|
||||
def build(self):
|
||||
return ft.Container(
|
||||
content=ft.Column(
|
||||
[
|
||||
ft.Row(
|
||||
[
|
||||
ft.Text("Applications", weight=ft.FontWeight.BOLD, size=20),
|
||||
ft.FloatingActionButton(icon=ft.Icons.ADD, on_click=self.on_add_btn_click)
|
||||
],
|
||||
alignment=ft.MainAxisAlignment.SPACE_BETWEEN
|
||||
),
|
||||
ft.Row(
|
||||
[
|
||||
self.applications_list
|
||||
],
|
||||
alignment=ft.MainAxisAlignment.CENTER,
|
||||
)
|
||||
],
|
||||
expand=True
|
||||
),
|
||||
padding=10,
|
||||
expand=True
|
||||
)
|
||||
Reference in New Issue
Block a user