NourEldinShobier/pmvvm

No option to maintain page state

Closed this issue · 5 comments

as far as I can figure out, there is no option like the 'AutomaticKeepClientAlive' mixin to keep the widget from being disposed of when clicking away. The desired behaviour I'm looking for is a page with tabs. each tab has its own MVVM page with a view and a ViewModel. Cycling through each tab should pause the last opened tab rather than disposing of it and maintain the state resuming rather than rebuilding the viewModel every time you click on the tab after it has been initialised. As far as I can tell, this is not possible with PMVVM currently and is stopping me from using this very appealing package.

Hi @Kiwi-KiwiCorp

Honestly, i wasn't aware that such mixen exists in Flutter, so thanks a lot for the tip ✌️, i will handle this within 1:2 days

That being said, the disposeVM parameter of the MVVM widget was meant to handle such a case. When it's set to false, this enables us to pass an existing instance of the VM so that when you cycle through the tabs it doesn't get disposed

In the upcoming version you should be able to do the same through the disposeVM but without creating an instance manually and passing it to the MVVM widget

Hi @Kiwi-KiwiCorp

Honestly, i wasn't aware that such mixen exists in Flutter, so thanks a lot for the tip ✌️, i will handle this within 1:2 days

That being said, the disposeVM parameter of the MVVM widget was meant to handle such a case. When it's set to false, this enables us to pass an existing instance of the VM so that when you cycle through the tabs it doesn't get disposed

In the upcoming version you should be able to do the same through the disposeVM but without creating an instance manually and passing it to the MVVM widget

If that is the case then I'm pretty sure there is a bug with the disposeVM param as I tried this and the only difference it made was the fact that there was no error when switching between the tabs. If disposeVM is set to true and you click away from a tab and then click back on the tab it errors out. Also, it appears initOnce makes no difference as well due to the fact that even with disposeVM set to false the VM still gets disposed when clicking on another tab.

I'm not sure about what the other use cases are, however, what I would love to see is 3 simple parameters:

  • view
  • viewModel
  • keepAlive (true/false)

keep alive would simply switch the functionality from disposing the widget and VM when the widget is not in view to keeping the widget alive and simply calling the onPause and onResume methods when the widget is visible and not visible. The 'AutomaticKeepClientAlive' mixin has a param that you can set to keep alive true/false so you could simply hook the param up to this (i think).

Hi @Kiwi-KiwiCorp

I'm going to answer all your points in this comment, so let's get started =D:

  • After deep-diving into how AutomaticKeepAliveClientMixin works, i believe this is out of the scope PMVVM package (state management) since it changes the behavior of the build, dispose, and intState methods. The view should be responsible for this, so i would suggest making a wrapper class and calling it KeepAliveWrapper and making the MVVM its child.
class KeepAliveWrapper extends StatefulWidget {
  const KeepAliveWrapper({Key? key, required this.child}) : super(key: key);

  final Widget child;

  @override
  _KeepAliveWrapperState createState() => _KeepAliveWrapperState();
}

class _KeepAliveWrapperState extends State<KeepAliveWrapper> with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return widget.child;
  }

  @override
  bool get wantKeepAlive => true;
}
  • The onResume, onPause, onInactive, and onDetach are application life cycle methods... There are many packages that provide widgets that can help you detect whether the widget is visible or not, again, this issue has nothing to do with state management 😉. However, in v4.0.0 the view model has two callbacks onMount and onUnmount, they are triggered when the view of the MVVM widget is mounted/unmounted
  • Keep in mind that the behavior of onMount and onUnmount methods will change if they are wrapped with AutomaticKeepAliveClientMixin, for example the onMount will be triggered only once when the tab is opened for the first time and onUnmount will never be invoked even if the tab was changed, this is how the AutomaticKeepAliveClientMixin works
    • i would recommend listening to the tab controller, its responibility is to provide such data. Relying on any other solution would be just a workaround, i guess so!
  • There is no longer initOnce parameter in v4.0.0, the init method will only be called once after MVVM widget initState is called, it seems that this parameter is confusing people of PMVVM works.
  • In v4.0.0, a new method called onDependenciesChange that gets triggered when the didChangeDependencies of the MVVM widget is called.
  • Finally, regarding the bug you were facing with disposeVM and the error that is being raised, can u provide a code sample as i can't reproduce this issue from my side.

Hi @Kiwi-KiwiCorp

I'm assuming the issue is fixed so i will close it for now, and feel free to reopen it if u have any other questions =D