mobxjs / mobx.dart

MobX for the Dart language. Hassle-free, reactive state-management for your Dart and Flutter apps.
https://mobx.netlify.app
MIT License
2.39k stars 311 forks source link

Add support for dart-json-mapper #318

Closed felipeleon73 closed 4 years ago

felipeleon73 commented 4 years ago

I accidentally discovered dart-json-mapper which looks very pretty. But I can't get him to live with mobx_codegen ... Any help? Thanks!

pavanpodila commented 4 years ago

Can you share the code of how you are annotating the MobX Store class?

felipeleon73 commented 4 years ago

Sorry, I made some trivial mistakes. In reality the two libraries seem to live together perfectly! Indeed ... it seems that the union of the two allows to avoid having to implement mobx's _ObservableListJsonConverter and dart_json_mapper's iterableCarDecorator. Below is my example code:

library json_mapper.example;

import 'package:mobx/mobx.dart';
import 'package:dart_json_mapper/annotations.dart';
import 'package:dart_json_mapper/json_mapper.dart';

import 'main.reflectable.dart';
part 'main.g.dart';

@jsonSerializable
class TestVerbale extends _TestVerbale with _$TestVerbale {}
@jsonSerializable
abstract class _TestVerbale with Store{
  @observable
  int counter = 0;
}

@jsonSerializable
class TestChain extends TestChainBase with _$TestChain {
  TestChain(String id, String matrice) : super(id,matrice);
}

@jsonSerializable
abstract class TestChainBase with Store {
  final String id;
  final String matrice;

  @JsonProperty(ignore: true)
  ReactionDisposer _disposable;

  @JsonProperty(ignore: true)
  @observable
  bool modified = true;

  @observable
  int counter = 0;

  @observable
  ObservableList<String> mailingList = ObservableList<String>();

  @observable
  ObservableList<TestVerbale> verbali = ObservableList<TestVerbale>();

  TestChainBase(this.id, this.matrice){
    reaction((_)=>verbali.map((v)=>v.counter).toList(), (x)=> print(x) );
    reaction((_)=>mailingList.toList(), (x)=> print(x) );
  }

  @action
  setSaved() {
    modified = false;
    _disposable = reaction((_)=>JsonMapper.serialize(this), (_) { setDirty(); print("called");});
  }

  @action
  setDirty() {
    if (_disposable != null) _disposable.call();
    modified = true;
  }

}

main()
{
  initializeReflectable();
  var test = TestChain("ID1","Acque");
  final dispose = autorun((_)=>print(test.modified));
  test.setSaved();

  test.counter +=10;

  var verbale = TestVerbale()..counter =1 ;
  test.verbali.add(verbale);
  verbale.counter += 100;
  verbale.counter = 200;

  test.setSaved();

  test.mailingList.add("test12345@test.com");
  test.mailingList.add("test2222@test.com");
  test.mailingList.add("test33333@test.com");
  test.mailingList.removeLast();

  print(JsonMapper.serialize(test));
  dispose.call();

}
pavanpodila commented 4 years ago

Wow that's awesome @felipeleon73. Thanks so much for sharing this. Would you mind sending a PR with this change for the Todos examples 🚀 ? I think this is a great package to include.

felipeleon73 commented 4 years ago

Ok. I will try in the next few days!

felipeleon73 commented 4 years ago

PR Sent.. #321

deadsoul44 commented 4 years ago

This can be closed since another example is added.