Рефакторинг

This commit is contained in:
Вячеслав Иванов 2024-09-11 15:25:59 +04:00
parent ffcabca487
commit 4260c7e1a5
2 changed files with 21 additions and 43 deletions

View File

@ -1,5 +1,4 @@
import 'dart:math'; import 'dart:math';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'weekManager.dart'; import 'weekManager.dart';
@ -41,31 +40,22 @@ class _MyHomePageState extends State<MyHomePage> {
final random = Random(); final random = Random();
_weekManagers = List.generate(5, (index) { _weekManagers = List.generate(5, (index) {
DateTime deadline = DateTime.now().add(Duration(days: random.nextInt(365))); 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<void> _updateDeadlines() async { Future<void> _incrementCounter() async {
for (var weekManager in _weekManagers) { for (var weekManager in _weekManagers) {
await Future.delayed(const Duration(seconds: 1), () { await Future.delayed(const Duration(milliseconds: 500), () {
setState(() { setState(() {
weekManager.updateCurrentDate(); weekManager.incrementCurrentDate();
weekManager.updateCurrentDay(); weekManager.updateCurrentDay();
}); });
}); });
} }
} }
void _incrementCounter() {
setState(() {
for (var weekManager in _weekManagers) {
weekManager.incrementCurrentDate();
weekManager.updateCurrentDay();
}
});
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
@ -83,9 +73,9 @@ class _MyHomePageState extends State<MyHomePage> {
..._weekManagers.map((weekManager) => Column( ..._weekManagers.map((weekManager) => Column(
children: [ children: [
Text('Текущий день недели: ${weekManager.currentDay.name}'), Text('Текущий день недели: ${weekManager.currentDay.name}'),
Text('Текущая дата: ${weekManager.formattedCurrentDate}'), Text('Текущая дата: ${weekManager.formatDate(weekManager.currentDate)}'),
Text('Дней до дедлайна: ${weekManager.daysUntilDeadline}'), Text('Дней до дедлайна: ${weekManager.daysUntilDeadline}'),
Text('Дата дедлайна: ${weekManager.formattedDeadline}'), Text('Дата дедлайна: ${weekManager.formatDate(weekManager.deadline)}'),
const SizedBox(height: 20), const SizedBox(height: 20),
], ],
)), )),

View File

@ -1,18 +1,16 @@
import 'dart:math';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'main.dart'; import 'main.dart';
class WeekManager { class WeekManager {
DateTime deadline; final DateTime _deadline;
DateTime currentDate; DateTime _currentDate;
Weekday _currentDay; Weekday _currentDay;
WeekManager(this.deadline) WeekManager(this._deadline, this._currentDate)
: currentDate = DateTime.now(), : _currentDay = _getCurrentDayOfWeek(_currentDate);
_currentDay = _getCurrentDayOfWeek(DateTime.now());
DateTime get deadline => _deadline;
DateTime get currentDate => _currentDate;
Weekday get currentDay => _currentDay; Weekday get currentDay => _currentDay;
static Weekday _getCurrentDayOfWeek(DateTime date) { static Weekday _getCurrentDayOfWeek(DateTime date) {
@ -37,29 +35,19 @@ class WeekManager {
} }
int get daysUntilDeadline { 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'); DateFormat formatter = DateFormat('dd-MM-yyyy');
return formatter.format(deadline); return formatter.format(date);
}
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)));
} }
void incrementCurrentDate() { void incrementCurrentDate() {
currentDate = currentDate.add(const Duration(days: 1)); _currentDate = _currentDate.add(const Duration(days: 1));
}
void updateCurrentDay() {
_currentDay = _getCurrentDayOfWeek(_currentDate);
} }
} }