osam 5.3.2 copy "osam: ^5.3.2" to clipboard
osam: ^5.3.2 copied to clipboard

discontinued
outdated

State management library inspired SOLID prenciples.

OSAM #

GitHub Logo

Osam - it is complex solution for fast and quality mobile developing.

First we will talk about State.

The data structure which representing current data in runtime can be called as State.

Example:

class AppState {
  final counter = Counter();
}
class Counter {
  var value = 0;
}

This code means that we have a something like tree of data in our app.

Recommendation: Create a one AppState in main function instead of a lot of states.

The properties can be listenable. You can wrap it by BaseValue. BaseValue have ValueStream - Bundle with value and it's stream and just a value. Every time you set new value in BaseValue, valueStream throw new value for all listeners aka Hot Stream( StreamController ).

Example:

class Counter {
 var number = BaseValue(0);
}

void someFun(){
 // value: 
 Counter().number.value;
 // stream
 Counter().number.valueStream.listen((v){
   // new values appears here.
 });
}

Notice: following dart feature about mutable collections, please use change method from BaseValue.

Example:

class Store {
 var employees = BaseValue(<Employee>[]);
}

void someFun(){
 Store().employees.change((v) => v.add(Employee()));
}

Second we talk about is UseCases and UseCaseContainer.

The purpose of UseCaseContainer is provide 'UseCases'. UseCase could be everything, but I recommend to follow several rules:

  1. UseCase have access to AppState or appState's sub states.
  2. State or states defined in UseCase must be lib private (underscored "var _state = SomeState()").
  3. Only UseCase can change States.
  4. All repositories you need must be provided via constructor in to UseCase.
  5. UseCase should depends of interfaces, not implementations of repositories.

You can call a UseCaseContainer from Presenter and UseCase and get exact useCase you want.

Example:

class MyPresenter extends Presenter<UiState>{
  void doSomething() => useCaseContainer.useCase<SomeUseCase>().doSomething();
}

class UseCase1 {
  void doSomething() => useCaseContainer.useCase<UseCase2>().doSomething();
}

// if you call some useCase often in class body, just define a getter.

class UseCase1 {

  UseCase2 get useCase2 => useCaseContainer.useCase<UseCase2>();

  void doSomething1() => useCase2.doSomething1();
  void doSomething2() => useCase2.doSomething2();
}

To provide useCaseContainer for all presenters and useCases use the UseCaseProvider. UseCaseProvider need's your appState, uiState if exist, and useCaseContainer.

Example:

void main() {
  final repoFactory = RepoFactory();

  final countRepository = repoFactory.counterRepo();

  final appState = AppState();

  final useCase = CounterUseCase(countRepository, appState);

  final useCaseContainer = UseCaseContainer([useCase]);

  runApp(UseCaseProvider(
    appState,
    useCaseContainer,
    null,
    child: MyApp(),
  ));
}

Presenters:

Presenters needs to provide entities and other business data for Ui layer and call useCases to do something.

Example:

class MyPresenter extends Presenter<UiState> {
  CounterUseCase get counterUseCase => useCaseContainer.useCase<CounterUseCase>();
  
  Future<void> inc() async => await counterUseCase.increment();
  
  ValueStream<int> get numberValue => counterUseCase.numberValue;
}

The UiState is data structure witch can contains something like scroll positions, navigation stack, and other things you need to persist for ui. The UiState is accessible in presenter all time. Also: presenters have init and dispose methods. Init called first time when presenter appeared in Widget tree. and dispose called when no ones references to this presenter. Presenters could be provided by PresenterProvider and MultiPresenterProviders. If you familiar with Provider package you should know what to do.

10
likes
0
pub points
0%
popularity

Publisher

verified publisherrenesanse.net

State management library inspired SOLID prenciples.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

equatable, hive, path_provider

More

Packages that depend on osam