Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

Commit

Permalink
refactor(project): fixes and adaptations
Browse files Browse the repository at this point in the history
  • Loading branch information
jh committed Feb 15, 2023
1 parent e712e90 commit d3c85c7
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 128 deletions.
20 changes: 1 addition & 19 deletions fletched/__init__.py
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
7 changes: 7 additions & 0 deletions fletched/mvp/__init__.py
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
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from abstractcp import Abstract, abstract_class_property
from fletched.mvp_utils.error import ErrorMessage
from fletched.mvp_utils.observable import Observable
from pydantic import BaseModel, ValidationError

from fletched.routed_app.app import RoutedApp
from fletched.mvp.error import ErrorMessage
from fletched.mvp.observable import Observable
from fletched.routed_app import RoutedApp


class MvpDataSource(Abstract, Observable):
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions fletched/mvp_utils/presenter.py → fletched/mvp/presenter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dataclasses import dataclass

from fletched.mvp_utils.datasource import MvpDataSource
from fletched.mvp_utils.protocols import MvpViewProtocol
from fletched.mvp.datasource import MvpDataSource
from fletched.mvp.protocols import MvpViewProtocol


@dataclass
Expand Down
File renamed without changes.
91 changes: 91 additions & 0 deletions fletched/mvp/view.py
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
7 changes: 0 additions & 7 deletions fletched/mvp_utils/__init__.py

This file was deleted.

74 changes: 0 additions & 74 deletions fletched/mvp_utils/view.py

This file was deleted.

2 changes: 1 addition & 1 deletion fletched/routed_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from fletched.routed_app.auth import group_required, login_required
from fletched.routed_app.routing import route
from fletched.routed_app.state import CustomAppState
from fletched.routed_app.view_builder import MvpViewBuilder, ViewBuilder
from fletched.routed_app.view_builder import ViewBuilder
23 changes: 1 addition & 22 deletions fletched/routed_app/view_builder.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from abc import ABC, abstractmethod
from typing import Any, Callable, Type
from typing import Any, Callable

import flet as ft

from fletched.mvp_utils import MvpDataSource, MvpPresenter, MvpView


class ViewBuilder(ABC):
route: str | None = None
Expand Down Expand Up @@ -49,22 +47,3 @@ def _build_unauthorized_view(self) -> ft.View:
vertical_alignment=ft.MainAxisAlignment.CENTER,
horizontal_alignment=ft.CrossAxisAlignment.CENTER,
)


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:
self.data_source = self.data_source_class(
app=self.app, route_params=route_params
)
self.view: ft.View = self.view_class(route=self.route)
self.presenter = self.presenter_class(
data_source=self.data_source,
view=self.view,
)
self.presenter.build()

return self.view

0 comments on commit d3c85c7

Please sign in to comment.