Рефакторинг

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 'package:flutter/material.dart';
import 'weekManager.dart';
@ -41,29 +40,20 @@ class _MyHomePageState extends State<MyHomePage> {
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<void> _updateDeadlines() async {
Future<void> _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.updateCurrentDay();
});
});
}
}
void _incrementCounter() {
setState(() {
for (var weekManager in _weekManagers) {
weekManager.incrementCurrentDate();
weekManager.updateCurrentDay();
}
});
});
}
}
@override
@ -83,9 +73,9 @@ class _MyHomePageState extends State<MyHomePage> {
..._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),
],
)),

View File

@ -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);
}
}