This repository has been archived by the owner on Jul 10, 2023. It is now read-only.
generated from iron3oxide/python-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(project): fixes and adaptations
- Loading branch information
jh
committed
Feb 15, 2023
1 parent
e712e90
commit d3c85c7
Showing
13 changed files
with
106 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1 @@ | ||
from fletched.mvp_utils import ( | ||
ErrorMessage, | ||
MvpDataSource, | ||
MvpModel, | ||
MvpPresenter, | ||
MvpPresenterProtocol, | ||
MvpView, | ||
MvpViewProtocol, | ||
Observable, | ||
) | ||
from fletched.routed_app import ( | ||
CustomAppState, | ||
MvpViewBuilder, | ||
RoutedApp, | ||
ViewBuilder, | ||
group_required, | ||
login_required, | ||
route, | ||
) | ||
from fletched import mvp, routed_app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from fletched.mvp.datasource import MvpDataSource | ||
from fletched.mvp.error import ErrorMessage | ||
from fletched.mvp.model import MvpModel | ||
from fletched.mvp.observable import Observable | ||
from fletched.mvp.presenter import MvpPresenter | ||
from fletched.mvp.protocols import MvpPresenterProtocol, MvpViewProtocol | ||
from fletched.mvp.view import MvpView, MvpViewBuilder, ViewConfig |
6 changes: 3 additions & 3 deletions
6
fletched/mvp_utils/datasource.py → fletched/mvp/datasource.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
from abc import abstractmethod | ||
from dataclasses import asdict, dataclass | ||
from typing import List, Optional, Type | ||
|
||
import flet as ft | ||
from abstractcp import Abstract, abstract_class_property | ||
from pydantic import BaseModel | ||
|
||
from fletched.mvp.datasource import MvpDataSource | ||
from fletched.mvp.error import ErrorMessage | ||
from fletched.mvp.presenter import MvpPresenter | ||
from fletched.mvp.protocols import MvpPresenterProtocol | ||
from fletched.routed_app import ViewBuilder | ||
|
||
|
||
@dataclass | ||
class ViewConfig: | ||
route: Optional[str] = None | ||
controls: Optional[List[ft.Control]] = None | ||
appbar: Optional[ft.AppBar] = None | ||
floating_action_button: Optional[ft.FloatingActionButton] = None | ||
navigation_bar: Optional[ft.NavigationBar] = None | ||
vertical_alignment: ft.MainAxisAlignment = ft.MainAxisAlignment.NONE | ||
horizontal_alignment: ft.CrossAxisAlignment = ft.CrossAxisAlignment.NONE | ||
spacing: ft.OptionalNumber = None | ||
padding: ft.PaddingValue = None | ||
bgcolor: Optional[str] = None | ||
scroll: Optional[ft.ScrollMode] = None | ||
auto_scroll: Optional[bool] = None | ||
|
||
|
||
class MvpView(Abstract, ft.View): | ||
ref_map = abstract_class_property(dict[str, ft.Ref]) | ||
config = abstract_class_property(ViewConfig) | ||
|
||
def __init__(self) -> None: | ||
super().__init__(**asdict(self.config)) | ||
|
||
def render(self, model: BaseModel) -> None: | ||
page: ft.Page | None = None | ||
model_map = model.dict() | ||
|
||
for variable_name, ref in self.ref_map.items(): | ||
|
||
model_field_content = model_map[variable_name] | ||
control_attribute_name = "value" | ||
if not hasattr(ref.current, control_attribute_name): | ||
control_attribute_name = "text" | ||
if isinstance(model_field_content, ErrorMessage): | ||
control_attribute_name = "error_text" | ||
model_field_content = model_field_content.message | ||
|
||
control_attribute_content = getattr(ref.current, control_attribute_name) | ||
|
||
if model_field_content == control_attribute_content: | ||
continue | ||
setattr(ref.current, control_attribute_name, model_field_content) | ||
|
||
if not page: | ||
page = ref.current.page | ||
|
||
if page: | ||
page.update() | ||
|
||
@abstractmethod | ||
def build(self, presenter: MvpPresenterProtocol) -> None: | ||
... | ||
|
||
|
||
class MvpViewBuilder(ViewBuilder): | ||
data_source_class: Type[MvpDataSource] | ||
view_class: Type[MvpView] | ||
presenter_class: Type[MvpPresenter] | ||
|
||
def build_view(self, route_params: dict[str, str]) -> ft.View: | ||
|
||
if not hasattr(self, "data_source"): | ||
self.data_source = self.data_source_class( | ||
app=self.app, route_params=route_params | ||
) | ||
|
||
self.view_class.config.route = self.route | ||
self.view: ft.View = self.view_class() | ||
self.presenter = self.presenter_class( | ||
data_source=self.data_source, | ||
view=self.view, | ||
) | ||
self.presenter.build() | ||
self.view.render(self.data_source.current_model) | ||
|
||
return self.view |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters