hydraveil-gui/gui/v2/ui/pages/duration_selection_page.py

105 lines
3.6 KiB
Python
Executable file

# Core
from core.services.payment_phase.payment_state import PaymentData
from gui.v2.infrastructure.take_plumbing_action import take_action
from core.controllers.profile_state.data_filters import get_plans_for_profile
from core.models.Result import Result #, ResultError
# GUI
from gui.v2.ui.styles.payment_styles import DURATION_SELECTION_STYLE, NEXT_BUTTON_STYLE
from gui.v2.ui.pages.Page import Page
from gui.v2.infrastructure.ThreadSafetyTool import ThreadSafetyTool
mainthread = ThreadSafetyTool()
from PyQt6.QtWidgets import QComboBox, QLabel, QPushButton
from PyQt6 import QtCore
class DurationSelectionPage(Page):
def __init__(self, page_stack, main_window=None, parent=None):
super().__init__("Select Duration", page_stack, main_window, parent)
self.update_status = main_window
self.create_intitial_elements()
self.button_reverse.setVisible(True)
def create_intitial_elements(self):
self.title = QLabel("Select Duration", self)
self.title.setGeometry(QtCore.QRect(530, 30, 210, 40))
self.title.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.button_reverse.clicked.connect(self.reverse)
self.combo_box = QComboBox(self)
self.combo_box.setGeometry(200, 200, 400, 60)
self.combo_box.setEditable(False)
self.combo_box.setMaxVisibleItems(4)
self.combo_box.setDuplicatesEnabled(True)
self.combo_box.setPlaceholderText("Select options")
self.combo_box.setStyleSheet(DURATION_SELECTION_STYLE)
self.next_button = QPushButton("Next", self)
self.next_button.setGeometry(350, 300, 100, 40)
self.next_button.setStyleSheet(NEXT_BUTTON_STYLE)
self.next_button.clicked.connect(self.show_next)
# Background thread launch:
self.get_data()
def get_data(self):
"""Launches background thread to get data"""
# get profile id:
profile_id = int(self.update_status.current_profile_id)
# setup singleton
self.billing_data = PaymentData(instance_id=profile_id)
# Thread SQL in background:
take_action(
action=get_plans_for_profile,
ui_caller=self.populate_with_data,
profile_id=profile_id,
)
@mainthread
def populate_with_data(self, result: Result):
if not result.valid:
self.show_a_popup(result.message)
return
self.choice_to_id = {}
self.dict_of_choices = result.data
if not self.dict_of_choices:
self.show_a_popup("There is no plan data. Go sync or remove the database and try again.")
return
if not isinstance(self.dict_of_choices, dict):
self.show_a_popup("Plan data is in an invalid format.")
return
for plan_string, plan_id in self.dict_of_choices.items():
self.combo_box.addItem(str(plan_string))
def show_next(self):
# get current selection:
current_choice = self.combo_box.currentText()
# which plan id is that:
plan_id = self.dict_of_choices[current_choice]
# save to singleton across pages:
self.billing_data.plan_id = plan_id
# go to next page:
currency_page = self.custom_window.navigator.get_cached("currency_selection")
if currency_page is not None:
self.custom_window.navigator.navigate("currency_selection")
def reverse(self):
self.custom_window.navigator.navigate("id")
# currency_page.selected_duration
# self.combo_box.addItems(
# ["1 month (€1.50)", "3 months (€4.30)", "6 months (€8)", "12 months (€16)"])