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

Commit

Permalink
refactor(view): Adapt MvpView to use MvpRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
jh committed Feb 23, 2023
1 parent f5933ba commit 9bfb44d
Showing 1 changed file with 7 additions and 38 deletions.
45 changes: 7 additions & 38 deletions fletched/mvp/view.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
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.mvp.renderer import MvpRenderer
from fletched.routed_app import PageNotFoundView, ViewBuilder


@dataclass
class ViewConfig:
class ViewConfig(BaseModel):
route: Optional[str] = None
controls: Optional[List[ft.Control]] = None
appbar: Optional[ft.AppBar] = None
Expand All @@ -28,43 +24,16 @@ class ViewConfig:
scroll: Optional[ft.ScrollMode] = None
auto_scroll: Optional[bool] = None

class Config:
arbitrary_types_allowed = True

class MvpView(Abstract, ft.View):

class MvpView(Abstract, MvpRenderer, 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:
...
super().__init__(**self.config.dict())


class MvpViewBuilder(Abstract, ViewBuilder):
Expand Down

0 comments on commit 9bfb44d

Please sign in to comment.