diff --git a/lib/main.dart b/lib/main.dart index eb05b1d..659463d 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,4 @@ import 'dart:math'; - import 'package:flutter/material.dart'; import 'weekManager.dart'; @@ -41,31 +40,22 @@ class _MyHomePageState extends State { final random = Random(); _weekManagers = List.generate(5, (index) { DateTime deadline = DateTime.now().add(Duration(days: random.nextInt(365))); - return WeekManager(deadline); + DateTime currentDate = DateTime.now().add(Duration(days: random.nextInt(365))); + return WeekManager(deadline, currentDate); }); - _updateDeadlines(); } - Future _updateDeadlines() async { + Future _incrementCounter() async { for (var weekManager in _weekManagers) { - await Future.delayed(const Duration(seconds: 1), () { + await Future.delayed(const Duration(milliseconds: 500), () { setState(() { - weekManager.updateCurrentDate(); + weekManager.incrementCurrentDate(); weekManager.updateCurrentDay(); }); }); } } - void _incrementCounter() { - setState(() { - for (var weekManager in _weekManagers) { - weekManager.incrementCurrentDate(); - weekManager.updateCurrentDay(); - } - }); - } - @override Widget build(BuildContext context) { return Scaffold( @@ -83,9 +73,9 @@ class _MyHomePageState extends State { ..._weekManagers.map((weekManager) => Column( children: [ Text('Текущий день недели: ${weekManager.currentDay.name}'), - Text('Текущая дата: ${weekManager.formattedCurrentDate}'), + Text('Текущая дата: ${weekManager.formatDate(weekManager.currentDate)}'), Text('Дней до дедлайна: ${weekManager.daysUntilDeadline}'), - Text('Дата дедлайна: ${weekManager.formattedDeadline}'), + Text('Дата дедлайна: ${weekManager.formatDate(weekManager.deadline)}'), const SizedBox(height: 20), ], )), diff --git a/lib/weekManager.dart b/lib/weekManager.dart index f9bc6be..55e6b32 100644 --- a/lib/weekManager.dart +++ b/lib/weekManager.dart @@ -1,18 +1,16 @@ -import 'dart:math'; - import 'package:intl/intl.dart'; - import 'main.dart'; class WeekManager { - DateTime deadline; - DateTime currentDate; + final DateTime _deadline; + DateTime _currentDate; Weekday _currentDay; - WeekManager(this.deadline) - : currentDate = DateTime.now(), - _currentDay = _getCurrentDayOfWeek(DateTime.now()); + WeekManager(this._deadline, this._currentDate) + : _currentDay = _getCurrentDayOfWeek(_currentDate); + DateTime get deadline => _deadline; + DateTime get currentDate => _currentDate; Weekday get currentDay => _currentDay; static Weekday _getCurrentDayOfWeek(DateTime date) { @@ -37,29 +35,19 @@ class WeekManager { } int get daysUntilDeadline { - return deadline.difference(currentDate).inDays; + return _deadline.difference(_currentDate).inDays; } - String get formattedDeadline { + String formatDate(DateTime date) { DateFormat formatter = DateFormat('dd-MM-yyyy'); - return formatter.format(deadline); - } - - String get formattedCurrentDate { - DateFormat formatter = DateFormat('dd-MM-yyyy'); - return formatter.format(currentDate); - } - - void updateCurrentDay() { - _currentDay = _getCurrentDayOfWeek(currentDate); - } - - void updateCurrentDate() { - final random = Random(); - currentDate = DateTime.now().add(Duration(days: random.nextInt(365))); + return formatter.format(date); } void incrementCurrentDate() { - currentDate = currentDate.add(const Duration(days: 1)); + _currentDate = _currentDate.add(const Duration(days: 1)); + } + + void updateCurrentDay() { + _currentDay = _getCurrentDayOfWeek(_currentDate); } }