Renamed all Timer named things to TimeProgress
Signed-off-by: Andreas Fahrecker <AndreasFahrecker@gmail.com>
This commit is contained in:
parent
7f6eec43de
commit
976fbec455
30
lib/actions/actions.dart
Normal file
30
lib/actions/actions.dart
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import 'package:time_progress_calculator/models/time_progress.dart';
|
||||||
|
|
||||||
|
class LoadTimeProgressListAction {}
|
||||||
|
|
||||||
|
class TimeProgressListLoadedAction {
|
||||||
|
final List<TimeProgress> timeProgressList;
|
||||||
|
|
||||||
|
TimeProgressListLoadedAction(this.timeProgressList);
|
||||||
|
}
|
||||||
|
|
||||||
|
class TimeProgressListNotLoadedAction {}
|
||||||
|
|
||||||
|
class AddTimeProgressAction {
|
||||||
|
final TimeProgress timeProgress;
|
||||||
|
|
||||||
|
AddTimeProgressAction(this.timeProgress);
|
||||||
|
}
|
||||||
|
|
||||||
|
class UpdateTimeProgressAction {
|
||||||
|
final String id;
|
||||||
|
final TimeProgress updatedTimeProgress;
|
||||||
|
|
||||||
|
UpdateTimeProgressAction(this.id, this.updatedTimeProgress);
|
||||||
|
}
|
||||||
|
|
||||||
|
class DeleteTimeProgressAction {
|
||||||
|
final String id;
|
||||||
|
|
||||||
|
DeleteTimeProgressAction(this.id);
|
||||||
|
}
|
@ -1,30 +0,0 @@
|
|||||||
import 'package:time_progress_calculator/models/timer.dart';
|
|
||||||
|
|
||||||
class LoadTimersAction {}
|
|
||||||
|
|
||||||
class TimersLoadedAction {
|
|
||||||
final List<Timer> timers;
|
|
||||||
|
|
||||||
TimersLoadedAction(this.timers);
|
|
||||||
}
|
|
||||||
|
|
||||||
class TimersNotLoadedAction {}
|
|
||||||
|
|
||||||
class AddTimerAction {
|
|
||||||
final Timer timer;
|
|
||||||
|
|
||||||
AddTimerAction(this.timer);
|
|
||||||
}
|
|
||||||
|
|
||||||
class UpdateTimerAction {
|
|
||||||
final String id;
|
|
||||||
final Timer updatedTimer;
|
|
||||||
|
|
||||||
UpdateTimerAction(this.id, this.updatedTimer);
|
|
||||||
}
|
|
||||||
|
|
||||||
class DeleteTimerAction {
|
|
||||||
final String id;
|
|
||||||
|
|
||||||
DeleteTimerAction(this.id);
|
|
||||||
}
|
|
@ -2,9 +2,9 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:redux/redux.dart';
|
import 'package:redux/redux.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
import 'package:time_progress_calculator/app.dart';
|
import 'package:time_progress_calculator/app.dart';
|
||||||
import 'package:time_progress_calculator/middleware/store_timers_middleware.dart';
|
import 'package:time_progress_calculator/middleware/store_time_progress_middleware.dart';
|
||||||
import 'package:time_progress_calculator/models/app_state.dart';
|
import 'package:time_progress_calculator/models/app_state.dart';
|
||||||
import 'package:time_progress_calculator/persistence/timers_repository.dart';
|
import 'package:time_progress_calculator/persistence/time_progress_repository.dart';
|
||||||
import 'package:time_progress_calculator/reducers/app_state_reducer.dart';
|
import 'package:time_progress_calculator/reducers/app_state_reducer.dart';
|
||||||
|
|
||||||
Future<void> main() async {
|
Future<void> main() async {
|
||||||
@ -14,8 +14,8 @@ Future<void> main() async {
|
|||||||
store: Store<AppState>(
|
store: Store<AppState>(
|
||||||
appStateReducer,
|
appStateReducer,
|
||||||
initialState: AppState.initial(),
|
initialState: AppState.initial(),
|
||||||
middleware: createStoreTimersMiddleware(
|
middleware: createStoreTimeProgressListMiddleware(
|
||||||
TimersRepository(await SharedPreferences.getInstance()),
|
TimeProgressRepository(await SharedPreferences.getInstance()),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
));
|
));
|
||||||
|
42
lib/middleware/store_time_progress_middleware.dart
Normal file
42
lib/middleware/store_time_progress_middleware.dart
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
import 'package:redux/redux.dart';
|
||||||
|
import 'package:time_progress_calculator/actions/actions.dart';
|
||||||
|
import 'package:time_progress_calculator/models/app_state.dart';
|
||||||
|
import 'package:time_progress_calculator/models/time_progress.dart';
|
||||||
|
import 'package:time_progress_calculator/persistence/time_progress_entity.dart';
|
||||||
|
import 'package:time_progress_calculator/persistence/time_progress_repository.dart';
|
||||||
|
import 'package:time_progress_calculator/selectors/time_progress_selectors.dart';
|
||||||
|
|
||||||
|
List<Middleware<AppState>> createStoreTimeProgressListMiddleware(
|
||||||
|
TimeProgressRepository repository) {
|
||||||
|
final saveTimeProgressList = _createSaveTimeProgressList(repository);
|
||||||
|
final loadTimeProgressList = _createLoadTimeProgressList(repository);
|
||||||
|
|
||||||
|
return [
|
||||||
|
TypedMiddleware<AppState, LoadTimeProgressListAction>(loadTimeProgressList),
|
||||||
|
TypedMiddleware<AppState, AddTimeProgressAction>(saveTimeProgressList),
|
||||||
|
TypedMiddleware<AppState, UpdateTimeProgressAction>(saveTimeProgressList),
|
||||||
|
TypedMiddleware<AppState, DeleteTimeProgressAction>(saveTimeProgressList),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
Middleware<AppState> _createSaveTimeProgressList(TimeProgressRepository repository) {
|
||||||
|
return (Store<AppState> store, dynamic action, NextDispatcher next) {
|
||||||
|
next(action);
|
||||||
|
|
||||||
|
repository.saveTimeProgressList(
|
||||||
|
timeProgressListSelector(store.state)
|
||||||
|
.map<TimeProgressEntity>((timeProgress) => timeProgress.toEntity())
|
||||||
|
.toList(growable: false),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
Middleware<AppState> _createLoadTimeProgressList(TimeProgressRepository repository) {
|
||||||
|
return (Store<AppState> store, dynamic action, NextDispatcher next) {
|
||||||
|
repository.loadTimeProgressList().then((timeProgresses) {
|
||||||
|
store.dispatch(
|
||||||
|
TimeProgressListLoadedAction(timeProgresses.map<TimeProgress>(TimeProgress.fromEntity).toList()),
|
||||||
|
);
|
||||||
|
}).catchError((_) => store.dispatch(TimeProgressListNotLoadedAction()));
|
||||||
|
};
|
||||||
|
}
|
@ -1,42 +0,0 @@
|
|||||||
import 'package:redux/redux.dart';
|
|
||||||
import 'package:time_progress_calculator/actions/timer_actions.dart';
|
|
||||||
import 'package:time_progress_calculator/models/app_state.dart';
|
|
||||||
import 'package:time_progress_calculator/models/timer.dart';
|
|
||||||
import 'package:time_progress_calculator/persistence/timer_entity.dart';
|
|
||||||
import 'package:time_progress_calculator/persistence/timers_repository.dart';
|
|
||||||
import 'package:time_progress_calculator/selectors/timer_selectors.dart';
|
|
||||||
|
|
||||||
List<Middleware<AppState>> createStoreTimersMiddleware(
|
|
||||||
TimersRepository repository) {
|
|
||||||
final saveTimers = _createSaveTimers(repository);
|
|
||||||
final loadTimers = _createLoadTimers(repository);
|
|
||||||
|
|
||||||
return [
|
|
||||||
TypedMiddleware<AppState, LoadTimersAction>(loadTimers),
|
|
||||||
TypedMiddleware<AppState, AddTimerAction>(saveTimers),
|
|
||||||
TypedMiddleware<AppState, UpdateTimerAction>(saveTimers),
|
|
||||||
TypedMiddleware<AppState, DeleteTimerAction>(saveTimers),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
Middleware<AppState> _createSaveTimers(TimersRepository repository) {
|
|
||||||
return (Store<AppState> store, dynamic action, NextDispatcher next) {
|
|
||||||
next(action);
|
|
||||||
|
|
||||||
repository.saveTimers(
|
|
||||||
timersSelector(store.state)
|
|
||||||
.map<TimerEntity>((timer) => timer.toEntity())
|
|
||||||
.toList(growable: false),
|
|
||||||
);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
Middleware<AppState> _createLoadTimers(TimersRepository repository) {
|
|
||||||
return (Store<AppState> store, dynamic action, NextDispatcher next) {
|
|
||||||
repository.loadTimers().then((timers) {
|
|
||||||
store.dispatch(
|
|
||||||
TimersLoadedAction(timers.map<Timer>(Timer.fromEntity).toList()),
|
|
||||||
);
|
|
||||||
}).catchError((_) => store.dispatch(TimersNotLoadedAction()));
|
|
||||||
};
|
|
||||||
}
|
|
@ -1,35 +1,35 @@
|
|||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
import 'package:time_progress_calculator/models/timer.dart';
|
import 'package:time_progress_calculator/models/time_progress.dart';
|
||||||
|
|
||||||
@immutable
|
@immutable
|
||||||
class AppState {
|
class AppState {
|
||||||
final bool isLoading;
|
final bool isLoading;
|
||||||
final List<Timer> timers;
|
final List<TimeProgress> timeProgressList;
|
||||||
|
|
||||||
AppState({
|
AppState({
|
||||||
this.isLoading = false,
|
this.isLoading = false,
|
||||||
this.timers = const [],
|
this.timeProgressList = const [],
|
||||||
});
|
});
|
||||||
|
|
||||||
factory AppState.initial() => AppState(isLoading: true);
|
factory AppState.initial() => AppState(isLoading: true);
|
||||||
|
|
||||||
AppState copyWith({
|
AppState copyWith({
|
||||||
bool isLoading,
|
bool isLoading,
|
||||||
List<Timer> timers,
|
List<TimeProgress> timeProgressList,
|
||||||
}) {
|
}) {
|
||||||
return AppState(
|
return AppState(
|
||||||
isLoading: isLoading ?? this.isLoading,
|
isLoading: isLoading ?? this.isLoading,
|
||||||
timers: timers ?? this.timers,
|
timeProgressList: timeProgressList ?? this.timeProgressList,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
int get hashCode => timers.hashCode;
|
int get hashCode => timeProgressList.hashCode;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
bool operator ==(Object other) =>
|
bool operator ==(Object other) =>
|
||||||
identical(this, other) ||
|
identical(this, other) ||
|
||||||
other is AppState &&
|
other is AppState &&
|
||||||
runtimeType == other.runtimeType &&
|
runtimeType == other.runtimeType &&
|
||||||
timers == other.timers;
|
timeProgressList == other.timeProgressList;
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,18 @@
|
|||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
import 'package:time_progress_calculator/persistence/timer_entity.dart';
|
import 'package:time_progress_calculator/persistence/time_progress_entity.dart';
|
||||||
import 'package:time_progress_calculator/uuid.dart';
|
import 'package:time_progress_calculator/uuid.dart';
|
||||||
|
|
||||||
@immutable
|
@immutable
|
||||||
class Timer {
|
class TimeProgress {
|
||||||
final String id;
|
final String id;
|
||||||
final DateTime startTime;
|
final DateTime startTime;
|
||||||
final DateTime endTime;
|
final DateTime endTime;
|
||||||
|
|
||||||
Timer(this.startTime, this.endTime, {String id})
|
TimeProgress(this.startTime, this.endTime, {String id})
|
||||||
: id = id ?? Uuid().generateV4();
|
: id = id ?? Uuid().generateV4();
|
||||||
|
|
||||||
Timer copyWith({String id, DateTime startTime, DateTime endTime}) {
|
TimeProgress copyWith({String id, DateTime startTime, DateTime endTime}) {
|
||||||
return Timer(
|
return TimeProgress(
|
||||||
startTime ?? this.startTime,
|
startTime ?? this.startTime,
|
||||||
endTime ?? this.endTime,
|
endTime ?? this.endTime,
|
||||||
id: id ?? this.id,
|
id: id ?? this.id,
|
||||||
@ -25,7 +25,7 @@ class Timer {
|
|||||||
@override
|
@override
|
||||||
bool operator ==(Object other) =>
|
bool operator ==(Object other) =>
|
||||||
identical(this, other) ||
|
identical(this, other) ||
|
||||||
other is Timer &&
|
other is TimeProgress &&
|
||||||
runtimeType == other.runtimeType &&
|
runtimeType == other.runtimeType &&
|
||||||
id == other.id &&
|
id == other.id &&
|
||||||
startTime == other.startTime &&
|
startTime == other.startTime &&
|
||||||
@ -36,12 +36,12 @@ class Timer {
|
|||||||
return "Timer{id: $id, startTimer: $startTime, endTimer: $endTime}";
|
return "Timer{id: $id, startTimer: $startTime, endTimer: $endTime}";
|
||||||
}
|
}
|
||||||
|
|
||||||
TimerEntity toEntity() {
|
TimeProgressEntity toEntity() {
|
||||||
return TimerEntity(id, startTime, endTime);
|
return TimeProgressEntity(id, startTime, endTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Timer fromEntity(TimerEntity entity) {
|
static TimeProgress fromEntity(TimeProgressEntity entity) {
|
||||||
return Timer(
|
return TimeProgress(
|
||||||
entity.startTime,
|
entity.startTime,
|
||||||
entity.endTime,
|
entity.endTime,
|
||||||
id: entity.id ?? Uuid().generateV4(),
|
id: entity.id ?? Uuid().generateV4(),
|
@ -1,9 +1,9 @@
|
|||||||
class TimerEntity {
|
class TimeProgressEntity {
|
||||||
final String id;
|
final String id;
|
||||||
final DateTime startTime;
|
final DateTime startTime;
|
||||||
final DateTime endTime;
|
final DateTime endTime;
|
||||||
|
|
||||||
TimerEntity(this.id, this.startTime, this.endTime);
|
TimeProgressEntity(this.id, this.startTime, this.endTime);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
int get hashCode => id.hashCode ^ startTime.hashCode ^ endTime.hashCode;
|
int get hashCode => id.hashCode ^ startTime.hashCode ^ endTime.hashCode;
|
||||||
@ -11,7 +11,7 @@ class TimerEntity {
|
|||||||
@override
|
@override
|
||||||
bool operator ==(Object other) =>
|
bool operator ==(Object other) =>
|
||||||
identical(this, other) ||
|
identical(this, other) ||
|
||||||
other is TimerEntity &&
|
other is TimeProgressEntity &&
|
||||||
runtimeType == other.runtimeType &&
|
runtimeType == other.runtimeType &&
|
||||||
id == other.id &&
|
id == other.id &&
|
||||||
startTime == other.startTime &&
|
startTime == other.startTime &&
|
||||||
@ -25,12 +25,12 @@ class TimerEntity {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
static TimerEntity fromJson(Map<String, Object> json) {
|
static TimeProgressEntity fromJson(Map<String, Object> json) {
|
||||||
final String id = json["id"] as String;
|
final String id = json["id"] as String;
|
||||||
final DateTime startTime =
|
final DateTime startTime =
|
||||||
DateTime.fromMillisecondsSinceEpoch(json["startTime"] as int);
|
DateTime.fromMillisecondsSinceEpoch(json["startTime"] as int);
|
||||||
final DateTime endTime =
|
final DateTime endTime =
|
||||||
DateTime.fromMillisecondsSinceEpoch(json["endTime"] as int);
|
DateTime.fromMillisecondsSinceEpoch(json["endTime"] as int);
|
||||||
return TimerEntity(id, startTime, endTime);
|
return TimeProgressEntity(id, startTime, endTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
27
lib/persistence/time_progress_repository.dart
Normal file
27
lib/persistence/time_progress_repository.dart
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
import 'package:time_progress_calculator/persistence/time_progress_entity.dart';
|
||||||
|
|
||||||
|
class TimeProgressRepository {
|
||||||
|
static const String _key = "time_progress_repo";
|
||||||
|
final SharedPreferences prefs;
|
||||||
|
final JsonCodec codec;
|
||||||
|
|
||||||
|
TimeProgressRepository(this.prefs, {this.codec = json});
|
||||||
|
|
||||||
|
Future<List<TimeProgressEntity>> loadTimeProgressList() {
|
||||||
|
final String jsonString = this.prefs.getString(_key);
|
||||||
|
return codec
|
||||||
|
.decode(jsonString)["timers"]
|
||||||
|
.cast<Map<String, Object>>()
|
||||||
|
.map<TimeProgressEntity>(TimeProgressEntity.fromJson)
|
||||||
|
.toList(growable: false);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<bool> saveTimeProgressList(List<TimeProgressEntity> timeProgressList) {
|
||||||
|
final String jsonString = codec
|
||||||
|
.encode({"timers": timeProgressList.map((timer) => timer.toJson()).toList()});
|
||||||
|
return this.prefs.setString(_key, jsonString);
|
||||||
|
}
|
||||||
|
}
|
@ -1,27 +0,0 @@
|
|||||||
import 'dart:convert';
|
|
||||||
|
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
|
||||||
import 'package:time_progress_calculator/persistence/timer_entity.dart';
|
|
||||||
|
|
||||||
class TimersRepository {
|
|
||||||
static const String _key = "timers_repo";
|
|
||||||
final SharedPreferences prefs;
|
|
||||||
final JsonCodec codec;
|
|
||||||
|
|
||||||
TimersRepository(this.prefs, {this.codec = json});
|
|
||||||
|
|
||||||
Future<List<TimerEntity>> loadTimers() {
|
|
||||||
final String jsonString = this.prefs.getString(_key);
|
|
||||||
return codec
|
|
||||||
.decode(jsonString)["timers"]
|
|
||||||
.cast<Map<String, Object>>()
|
|
||||||
.map<TimerEntity>(TimerEntity.fromJson)
|
|
||||||
.toList(growable: false);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<bool> saveTimers(List<TimerEntity> timers) {
|
|
||||||
final String jsonString = codec
|
|
||||||
.encode({"timers": timers.map((timer) => timer.toJson()).toList()});
|
|
||||||
return this.prefs.setString(_key, jsonString);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,6 +1,6 @@
|
|||||||
import 'package:time_progress_calculator/models/app_state.dart';
|
import 'package:time_progress_calculator/models/app_state.dart';
|
||||||
import 'package:time_progress_calculator/reducers/timer_reducer.dart';
|
import 'package:time_progress_calculator/reducers/time_progress_list_reducer.dart';
|
||||||
|
|
||||||
AppState appStateReducer(AppState state, dynamic action) {
|
AppState appStateReducer(AppState state, dynamic action) {
|
||||||
return AppState(timers: timersReducer(state.timers, action));
|
return AppState(timeProgressList: timeProgressListReducer(state.timeProgressList, action));
|
||||||
}
|
}
|
||||||
|
45
lib/reducers/time_progress_list_reducer.dart
Normal file
45
lib/reducers/time_progress_list_reducer.dart
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
import 'package:time_progress_calculator/actions/actions.dart';
|
||||||
|
import 'package:time_progress_calculator/models/time_progress.dart';
|
||||||
|
import 'package:redux/redux.dart';
|
||||||
|
|
||||||
|
final timeProgressListReducer = combineReducers<List<TimeProgress>>([
|
||||||
|
TypedReducer<List<TimeProgress>, TimeProgressListLoadedAction>(
|
||||||
|
_setLoadedTimeProgressList),
|
||||||
|
TypedReducer<List<TimeProgress>, TimeProgressListNotLoadedAction>(
|
||||||
|
_setEmptyTimeProgressList),
|
||||||
|
TypedReducer<List<TimeProgress>, AddTimeProgressAction>(_addTimeProgress),
|
||||||
|
TypedReducer<List<TimeProgress>, UpdateTimeProgressAction>(
|
||||||
|
_updateTimeProgress),
|
||||||
|
TypedReducer<List<TimeProgress>, DeleteTimeProgressAction>(_deleteTimeProgress),
|
||||||
|
]);
|
||||||
|
|
||||||
|
List<TimeProgress> _setLoadedTimeProgressList(
|
||||||
|
List<TimeProgress> timeProgressList, TimeProgressListLoadedAction action) {
|
||||||
|
return action.timeProgressList;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<TimeProgress> _setEmptyTimeProgressList(
|
||||||
|
List<TimeProgress> timeProgressList, TimeProgressListNotLoadedAction action) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
List<TimeProgress> _addTimeProgress(
|
||||||
|
List<TimeProgress> timeProgressList, AddTimeProgressAction action) {
|
||||||
|
return List.from(timeProgressList)
|
||||||
|
..add(action.timeProgress)
|
||||||
|
..toList(growable: false);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<TimeProgress> _updateTimeProgress(
|
||||||
|
List<TimeProgress> timeProgressList, UpdateTimeProgressAction action) {
|
||||||
|
return timeProgressList
|
||||||
|
.map((timeProgress) => timeProgress.id == action.id
|
||||||
|
? action.updatedTimeProgress
|
||||||
|
: timeProgress)
|
||||||
|
.toList(growable: false);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<TimeProgress> _deleteTimeProgress(
|
||||||
|
List<TimeProgress> timeProgressList, DeleteTimeProgressAction action) {
|
||||||
|
return timeProgressList.where((timeProgress) => timeProgress.id != action.id).toList(growable: false);
|
||||||
|
}
|
@ -1,35 +0,0 @@
|
|||||||
import 'package:time_progress_calculator/actions/timer_actions.dart';
|
|
||||||
import 'package:time_progress_calculator/models/timer.dart';
|
|
||||||
import 'package:redux/redux.dart';
|
|
||||||
|
|
||||||
final timersReducer = combineReducers<List<Timer>>([
|
|
||||||
TypedReducer<List<Timer>, TimersLoadedAction>(_setLoadedTimers),
|
|
||||||
TypedReducer<List<Timer>, TimersNotLoadedAction>(_setEmptyTimers),
|
|
||||||
TypedReducer<List<Timer>, AddTimerAction>(_addTimer),
|
|
||||||
TypedReducer<List<Timer>, UpdateTimerAction>(_updateTimer),
|
|
||||||
TypedReducer<List<Timer>, DeleteTimerAction>(_deleteTimer),
|
|
||||||
]);
|
|
||||||
|
|
||||||
List<Timer> _setLoadedTimers(List<Timer> timers, TimersLoadedAction action) {
|
|
||||||
return action.timers;
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Timer> _setEmptyTimers(List<Timer> timers, TimersNotLoadedAction action) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Timer> _addTimer(List<Timer> timers, AddTimerAction action) {
|
|
||||||
return List.from(timers)
|
|
||||||
..add(action.timer)
|
|
||||||
..toList(growable: false);
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Timer> _updateTimer(List<Timer> timers, UpdateTimerAction action) {
|
|
||||||
return timers
|
|
||||||
.map((timer) => timer.id == action.id ? action.updatedTimer : timer)
|
|
||||||
.toList(growable: false);
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Timer> _deleteTimer(List<Timer> timers, DeleteTimerAction action) {
|
|
||||||
return timers.where((timer) => timer.id != action.id).toList(growable: false);
|
|
||||||
}
|
|
@ -3,9 +3,9 @@ import 'package:flutter_redux/flutter_redux.dart';
|
|||||||
import 'package:percent_indicator/circular_percent_indicator.dart';
|
import 'package:percent_indicator/circular_percent_indicator.dart';
|
||||||
import 'package:percent_indicator/linear_percent_indicator.dart';
|
import 'package:percent_indicator/linear_percent_indicator.dart';
|
||||||
import 'package:redux/redux.dart';
|
import 'package:redux/redux.dart';
|
||||||
import 'package:time_progress_calculator/actions/timer_actions.dart';
|
import 'package:time_progress_calculator/actions/actions.dart';
|
||||||
import 'package:time_progress_calculator/models/app_state.dart';
|
import 'package:time_progress_calculator/models/app_state.dart';
|
||||||
import 'package:time_progress_calculator/models/timer.dart';
|
import 'package:time_progress_calculator/models/time_progress.dart';
|
||||||
|
|
||||||
class ProgressScreen extends StatefulWidget {
|
class ProgressScreen extends StatefulWidget {
|
||||||
const ProgressScreen({Key key, @required this.context, this.name})
|
const ProgressScreen({Key key, @required this.context, this.name})
|
||||||
@ -25,13 +25,13 @@ class _ProgressScreenState extends State<ProgressScreen> {
|
|||||||
Store<AppState> store = StoreProvider.of<AppState>(context);
|
Store<AppState> store = StoreProvider.of<AppState>(context);
|
||||||
final DateTime picked = await showDatePicker(
|
final DateTime picked = await showDatePicker(
|
||||||
context: context,
|
context: context,
|
||||||
initialDate: store.state.timers[0].startTime,
|
initialDate: store.state.timeProgressList[0].startTime,
|
||||||
firstDate: DateTime(2000),
|
firstDate: DateTime(2000),
|
||||||
lastDate: DateTime(2100));
|
lastDate: DateTime(2100));
|
||||||
if (picked != null && picked != store.state.timers[0].startTime) {
|
if (picked != null && picked != store.state.timeProgressList[0].startTime) {
|
||||||
store.dispatch(UpdateTimerAction(
|
store.dispatch(UpdateTimeProgressAction(
|
||||||
store.state.timers[0].id,
|
store.state.timeProgressList[0].id,
|
||||||
store.state.timers[0].copyWith(startTime: picked),
|
store.state.timeProgressList[0].copyWith(startTime: picked),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -40,13 +40,13 @@ class _ProgressScreenState extends State<ProgressScreen> {
|
|||||||
Store<AppState> store = StoreProvider.of<AppState>(context);
|
Store<AppState> store = StoreProvider.of<AppState>(context);
|
||||||
final DateTime picked = await showDatePicker(
|
final DateTime picked = await showDatePicker(
|
||||||
context: context,
|
context: context,
|
||||||
initialDate: store.state.timers[0].endTime,
|
initialDate: store.state.timeProgressList[0].endTime,
|
||||||
firstDate: DateTime(2000),
|
firstDate: DateTime(2000),
|
||||||
lastDate: DateTime(2100));
|
lastDate: DateTime(2100));
|
||||||
if (picked != null && picked != store.state.timers[0].endTime) {
|
if (picked != null && picked != store.state.timeProgressList[0].endTime) {
|
||||||
store.dispatch(UpdateTimerAction(
|
store.dispatch(UpdateTimeProgressAction(
|
||||||
store.state.timers[0].id,
|
store.state.timeProgressList[0].id,
|
||||||
store.state.timers[0].copyWith(endTime: picked),
|
store.state.timeProgressList[0].copyWith(endTime: picked),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -54,8 +54,8 @@ class _ProgressScreenState extends State<ProgressScreen> {
|
|||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
if (StoreProvider.of<AppState>(widget.context).state.timers.length < 1) {
|
if (StoreProvider.of<AppState>(widget.context).state.timeProgressList.length < 1) {
|
||||||
StoreProvider.of<AppState>(widget.context).dispatch(AddTimerAction(Timer(
|
StoreProvider.of<AppState>(widget.context).dispatch(AddTimeProgressAction(TimeProgress(
|
||||||
DateTime(2000),
|
DateTime(2000),
|
||||||
DateTime(2100),
|
DateTime(2100),
|
||||||
)));
|
)));
|
||||||
@ -72,11 +72,11 @@ class _ProgressScreenState extends State<ProgressScreen> {
|
|||||||
converter: _ViewModel.fromStore,
|
converter: _ViewModel.fromStore,
|
||||||
builder: (context, _ViewModel vm) {
|
builder: (context, _ViewModel vm) {
|
||||||
final int daysDone =
|
final int daysDone =
|
||||||
DateTime.now().difference(vm.timer.startTime).inDays;
|
DateTime.now().difference(vm.timeProgress.startTime).inDays;
|
||||||
final int daysLeft =
|
final int daysLeft =
|
||||||
vm.timer.endTime.difference(DateTime.now()).inDays;
|
vm.timeProgress.endTime.difference(DateTime.now()).inDays;
|
||||||
final int allDays =
|
final int allDays =
|
||||||
vm.timer.endTime.difference(vm.timer.startTime).inDays;
|
vm.timeProgress.endTime.difference(vm.timeProgress.startTime).inDays;
|
||||||
final double percent = daysDone / (allDays / 100) / 100;
|
final double percent = daysDone / (allDays / 100) / 100;
|
||||||
|
|
||||||
return Container(
|
return Container(
|
||||||
@ -97,7 +97,7 @@ class _ProgressScreenState extends State<ProgressScreen> {
|
|||||||
Expanded(
|
Expanded(
|
||||||
flex: 2,
|
flex: 2,
|
||||||
child: Text(
|
child: Text(
|
||||||
"${vm.timer.startTime.toLocal()}".split(" ")[0]),
|
"${vm.timeProgress.startTime.toLocal()}".split(" ")[0]),
|
||||||
),
|
),
|
||||||
Expanded(
|
Expanded(
|
||||||
flex: 2,
|
flex: 2,
|
||||||
@ -125,7 +125,7 @@ class _ProgressScreenState extends State<ProgressScreen> {
|
|||||||
Expanded(
|
Expanded(
|
||||||
flex: 2,
|
flex: 2,
|
||||||
child: Text(
|
child: Text(
|
||||||
"${vm.timer.endTime.toLocal()}".split(" ")[0]),
|
"${vm.timeProgress.endTime.toLocal()}".split(" ")[0]),
|
||||||
),
|
),
|
||||||
Expanded(
|
Expanded(
|
||||||
flex: 2,
|
flex: 2,
|
||||||
@ -176,15 +176,15 @@ class _ProgressScreenState extends State<ProgressScreen> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _ViewModel {
|
class _ViewModel {
|
||||||
final Timer timer;
|
final TimeProgress timeProgress;
|
||||||
|
|
||||||
_ViewModel({
|
_ViewModel({
|
||||||
@required this.timer,
|
@required this.timeProgress,
|
||||||
});
|
});
|
||||||
|
|
||||||
static _ViewModel fromStore(Store<AppState> store) {
|
static _ViewModel fromStore(Store<AppState> store) {
|
||||||
return _ViewModel(
|
return _ViewModel(
|
||||||
timer: store.state.timers[0],
|
timeProgress: store.state.timeProgressList[0],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
4
lib/selectors/time_progress_selectors.dart
Normal file
4
lib/selectors/time_progress_selectors.dart
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
import 'package:time_progress_calculator/models/app_state.dart';
|
||||||
|
import 'package:time_progress_calculator/models/time_progress.dart';
|
||||||
|
|
||||||
|
List<TimeProgress> timeProgressListSelector(AppState state) => state.timeProgressList;
|
@ -1,4 +0,0 @@
|
|||||||
import 'package:time_progress_calculator/models/app_state.dart';
|
|
||||||
import 'package:time_progress_calculator/models/timer.dart';
|
|
||||||
|
|
||||||
List<Timer> timersSelector(AppState state) => state.timers;
|
|
Loading…
x
Reference in New Issue
Block a user