Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: Being able to access current state of timer #18

Closed
leo-paz opened this issue Aug 7, 2022 · 7 comments
Closed

Request: Being able to access current state of timer #18

leo-paz opened this issue Aug 7, 2022 · 7 comments
Labels
enhancement New feature or request

Comments

@leo-paz
Copy link

leo-paz commented Aug 7, 2022

It would be nice to be have the controller be able to keep track of the timer regardless of whether or not the timer is in view or not.

I envision the controller being the source of truth for the timer widget and the widget simply latches on to the current state of the controller it was initialized with whenever it comes into view. This way we can get and set the timer from just the controller in other parts of an app.

The initial discussions for this issue can be found here.

@federicodesia federicodesia added the enhancement New feature or request label Aug 10, 2022
@federicodesia
Copy link
Owner

Would be great if you put down your thoughts on why Timer didn't work and what you're thinking of doing

First of all, if we go back to using Timer we should stop supporting milliseconds.

Running something like this 1000 times actually takes more than 1 second:

const Duration interval = Duration(milliseconds: 1);
Timer.periodic(interval, (_) => time += interval);

It even happens with a smaller interval like 10 milliseconds. But if we ignore that and make the controller the source of truth and still use ChangeNotifier, we'd have to call notifyListeners to update the view 10, 100, or 1000 times per second and would get a similar result.

Suppose that from now on we forget about milliseconds and only work with seconds, we would still get some problems:

  • In not very modern devices, the periodic registration of the task every 1 second could take longer. Maybe with a 5 minute timer we won't see anything strange, but surely we would have a time difference after an hour.
  • If you have a timer and you fiddle with the controls before the seconds value changes for five minutes, after those five minutes the timer will still be zero.
  • There may be more points, although the current solution is not perfect either.

Being able to access current state of timer

Returning to the main topic of the problem. I agree that it would be a nice feature, as would being able to change the current value. We could achieve it in some dirty way, but I prefer to avoid it and it would also still depend on the view.

If you do not consider the points mentioned important and we take the current value to the controller (including begin and end) the library would be reduced to a single controller class. It would not be necessary to define a CustomTimer widget, since just using the AnimatedBuilder and listening to the controller would suffice.

So, do you really need this package or maybe it's a better idea to write your own class tailored to your project? Maybe you will find more points against the current approach or more benefits of making this change that would be great to know.

@leo-paz
Copy link
Author

leo-paz commented Aug 10, 2022

Yes I agree that it isn't feasible to run it for milliseconds at this moment, perhaps theres a solution where the animation and Timer are running in their own isolate.

Those are good points but for my current use cases those points don't make much of a difference as it doesn't need to be that time critical.

I have actually already done it using Timer in my fork here :) I needed to get the ball rolling on this but I'm happy to put up a PR or iterate on my changes before doing so.

I admit clean code was not top of my mind as I just wanted to get something working but it does well for having the controller be the source of truth and controlling the timer from within other parts of my app.

@federicodesia
Copy link
Owner

I found this article related to this topic. Mention some important points of using Timer or Ticker.

I recently updated this package, with the goal of making the controller the source of truth for the timer.
In this way it is possible to access or listen to the status and remaining time of the timer.
The controller now receives a TickerProvider that allows this to be achieved. Although this solution is still not interface agnostic.
I suppose one way to control the timer from anywhere in the application would be through dependency injection, but I don't think it should be included within this interface package.

I'll close this for now, though I'd like to hear your thoughts on these changes.
Thank you for helping to improve this package! 🚀

@leo-paz
Copy link
Author

leo-paz commented Jan 14, 2023

@federicodesia It looks great man! Big quality of life improvement and I think a lot of people looking for a flutter timer package will turn to this now. One small function people might find useful is to have a function to add/remove time from the timer.

I personally just stored the controller in my global store but I would leave that up to the consumer of the package.

Something to think about would be maybe changing the package name to just flutter_timer or along those lines so it comes up more when people are searching for a timer in flutter. I'm not sure how that would affect pub dev ratings though.

Also a PR would be nice next time a major change happens to provide all context in one spot and describe any changes as this project gains visibility. I know this is your baby but something to keep in mind for the future.

Happy to help and thanks for making the package better!

@federicodesia
Copy link
Owner

Thank you for your feedback!

One small function people might find useful is to have a function to add/remove time from the timer.

This is something similar to what I'm currently working on. So far it is not possible to modify begin or end once the controller has been initialized. I'd like to allow something like this in future versions, even with the timer running:

_controller.begin = Duration(hours: 12);

So after that, allowing something like add(Duration(minutes: 30)) would be easy. It's a very good idea!

Yes, the package name is definitely a bit tricky, although names like flutter_timer are taken. If you find an easier one please let me know! I will keep what you say about PR in mind for major changes!

@federicodesia
Copy link
Owner

@leo-paz The add and subtract functions, to increase or decrease the remaining time, were added in the latest version!
Thank you very much for helping to improve this package! 🚀

@leo-paz
Copy link
Author

leo-paz commented Jan 16, 2023

@federicodesia Thats awesome dude, I think you should highlight the new methods in the README features along with jumpTO.

For package names, maybe something like flutter_timekeeper or even something like flutter_ticktock? I feel like people resonate with humour haha but ultimately up to you!

Haha of course it was great helping out :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants