Files

125 lines
4.2 KiB
Python

import flet as ft
from app.layout import main_layout
from app.pages.article_detail import article_detail_page
from app.pages.login import login_page
from app.pages.articles import articles_page
from app.pages.courses import courses_page
from app.pages.my_courses import my_courses_page
from app.pages.progress import progress_page
from app.pages.profile import profile_page
from app.state import state
from app.widgets.adaptive_menu import get_selected_index_from_route
async def main(page: ft.Page):
page.title = "LMS Frontend"
# register font
page.fonts = {
"NotoSansThai": "assets/fonts/NotoSansThai-Regular.ttf",
"NotoSansThaiMedium": "assets/fonts/NotoSansThai-Medium.ttf",
"NotoSansThaiBold": "assets/fonts/NotoSansThai-Bold.ttf",
}
page.theme = ft.Theme(font_family="NotoSansThai")
# GLOBAL CONTAINER (รองรับ Persistent Layout + Dynamic Content)
main_container = ft.Container(expand=True, padding=10)
# ROUTES MAP
routes = {
"/articles": articles_page,
"/articles/{id}": article_detail_page,
"/courses": courses_page,
"/my_courses": my_courses_page,
"/progress": progress_page,
"/profile": profile_page,
}
# ROUTING
async def route_change(e):
print(">>> route_change:", page.route)
# กัน user แอบเข้า /main โดยไม่ login
if page.route != "/login" and not state.access_token:
await page.push_route("/login")
return
# LOGIN
if page.route == "/login":
page.views.clear()
page.views.append(
ft.View(
route="/login",
controls=[
login_page(page, on_login_success=go_articles)
],
horizontal_alignment=ft.CrossAxisAlignment.CENTER,
vertical_alignment=ft.MainAxisAlignment.CENTER,
)
)
# MAIN
else:
# สร้าง view ครั้งเดียว
if not page.views or page.views[-1].route != "/main":
page.views.clear()
page.views.append(
ft.View(
route="/main",
controls=[
main_layout(page, main_container)
],
)
)
# เปลี่ยน content อย่างเดียว
# จัดการ Dynamic Route ก่อน เช่น ARTICLE DETAIL
if page.route.startswith("/articles/"):
try:
article_id = int(page.route.split('/')[-1])
main_container.content = article_detail_page(page, article_id)
except ValueError:
main_container.content = ft.Text('Invalid article ID')
# จัดการ Static Routes ที่เหลือ
elif page.route in routes:
main_container.content = routes[page.route](page)
else:
main_container.content = ft.Text("404 NOT FOUND")
# หลังจาก set main_container.content แล้ว ให้อับเดตเมนูที่เลือก รองรับกรณี Back/Forward
if hasattr(page, "_menu_control"):
page._menu_control.selected_index = get_selected_index_from_route(page.route)
page.update()
# LOGIN SUCCESS
async def go_articles():
print(">>> GO ARTICLES")
await page.push_route("/articles")
page.on_route_change = route_change
def on_resize(e):
width = page.width if page.width and page.width > 0 else 1024
new_mode = "mobile" if width < 600 else "desktop"
if getattr(page, "_layout_mode", None) == new_mode:
return
print(f">>> Layout mode changed: {page._layout_mode} -> {new_mode}")
if page.views and page.views[-1].route == "/main":
page.views[-1].controls[0] = main_layout(page, main_container)
page.update()
page.on_resize = on_resize
# start app
await page.push_route("/login")
ft.run(main)