NourEldinShobier / pmvvm

A clean & simple MVVM solution for state management using Provider package.
MIT License
53 stars 6 forks source link

No option to maintain page state #11

Closed Kiwi-KiwiCorp closed 2 years ago

Kiwi-KiwiCorp commented 2 years ago

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.

NourEldinShobier commented 2 years ago

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

Kiwi-KiwiCorp commented 2 years ago

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.

Kiwi-KiwiCorp commented 2 years ago

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

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).

NourEldinShobier commented 2 years ago

Hi @Kiwi-KiwiCorp

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

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;
}
NourEldinShobier commented 2 years ago

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