Skip to content

Commit

Permalink
start reworking goals
Browse files Browse the repository at this point in the history
  • Loading branch information
NeXTormer committed Jan 13, 2024
1 parent fa696d1 commit c4873ef
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 26 deletions.
14 changes: 13 additions & 1 deletion lib/backend/goals/frederic_goal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:frederic/backend/database/frederic_data_object.dart';
///
class FredericGoal implements FredericDataObject {
///
/// Construct FredericGoal usin a DocumentSnapshot from the database
/// Construct FredericGoal using a DocumentSnapshot from the database
///
FredericGoal(DocumentSnapshot<Object?> snapshot) : id = snapshot.id {
var data = (snapshot as DocumentSnapshot<Map<String, dynamic>?>).data();
Expand Down Expand Up @@ -73,19 +73,31 @@ class FredericGoal implements FredericDataObject {
bool? _isDeleted;

String get title => _title ?? 'Goal';

String get image =>
_image ??
'https://firebasestorage.googleapis.com/v0/b/hawkford-frederic.appspot.com/o/icons%2Fgoal.png?alt=media&token=9f580776-3dec-47f6-b4e2-ea8788fa02a1';

String get activityID => _activityID ?? '';

String get unit => _unit ?? '';

num get startState => _startState ?? 0;

num get endState => _endState ?? 0;

num get currentState => _currentState ?? -1;

DateTime get startDate => _startDate ?? DateTime.now();

DateTime get endDate => _endDate ?? DateTime.now();

bool get isCompleted => _isCompleted ?? false;

bool get isNotCompleted => !isCompleted;

bool get isDeleted => _isDeleted ?? false;

bool get isLoss => startState > endState;

@override
Expand Down
6 changes: 4 additions & 2 deletions lib/backend/goals/frederic_goal_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ class FredericGoalManager
FutureOr<void> _onEvent(
FredericGoalEvent event, Emitter<FredericGoalListData> emit) async {
if (event is FredericGoalUpdateEvent) {
// new goals on update get into the list twice
_goals[event.updatedGoal.id] = event.updatedGoal;
await _dataInterface.update(event.updatedGoal);
emit(FredericGoalListData(event.changed, _goals));
} else if (event is FredericGoalCreateEvent) {
_goals[event.newGoal.id] = event.newGoal;
await _dataInterface.create(event.newGoal);
final newGoalFromDatabase = await _dataInterface.create(event.newGoal);
_goals[event.newGoal.id] = newGoalFromDatabase;
emit(FredericGoalListData(event.changed, _goals));
} else if (event is FredericGoalDeleteEvent) {
_goals.remove(event.goal.id);
Expand All @@ -67,6 +68,7 @@ class FredericGoalManager

class FredericGoalEvent {
FredericGoalEvent(this.changed);

List<String> changed;
}

Expand Down
37 changes: 17 additions & 20 deletions lib/screens/edit_goal_data_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,14 @@ enum FormError {
}

class EditGoalDataScreen extends StatefulWidget {
EditGoalDataScreen(this.goal, {this.sets, this.activity}) {
isNewGoal = goal.id == 'new';
}
EditGoalDataScreen(this.goal, {this.sets, this.activity});

final FredericGoal goal;

final FredericSetListData? sets;
final FredericActivity? activity;

late final bool isNewGoal;
bool get isNewGoal => goal.id == 'new';

@override
_EditGoalDataScreenState createState() => _EditGoalDataScreenState();
Expand All @@ -60,8 +58,6 @@ class _EditGoalDataScreenState extends State<EditGoalDataScreen> {

final UnitSliderController unitSliderController = UnitSliderController();

PageController? endPageController;

String startDateText = '';
String endDateText = '';

Expand Down Expand Up @@ -173,20 +169,20 @@ class _EditGoalDataScreenState extends State<EditGoalDataScreen> {
);
}

void saveData() {
FormError _checkForm() {
if (startStateController.value == endStateController.value) {
return FormError.StartStateEqualsEndState;
} else if (currentStateController.value == endStateController.value) {
return FormError.CurrentStateEqualsEndStart;
} else if (startDateText == endDateText) {
return FormError.StartDateEqualsEndDate;
} else if (selectedStartDate!.compareTo(selectedEndDate!) > 0) {
return FormError.EndDateSmallerThenStartDate;
}
return FormError.Success;
FormError _checkFormInputs() {
if (startStateController.value == endStateController.value) {
return FormError.StartStateEqualsEndState;
} else if (currentStateController.value == endStateController.value) {
return FormError.CurrentStateEqualsEndStart;
} else if (startDateText == endDateText) {
return FormError.StartDateEqualsEndDate;
} else if (selectedStartDate!.compareTo(selectedEndDate!) > 0) {
return FormError.EndDateSmallerThenStartDate;
}
return FormError.Success;
}

void saveData() {
Future<void> _showErrorMessage(String text) async {
await showDialog<bool>(
context: context,
Expand All @@ -206,9 +202,10 @@ class _EditGoalDataScreenState extends State<EditGoalDataScreen> {
}

if (widget.isNewGoal) {
switch (_checkForm()) {
final formStatus = _checkFormInputs();
switch (formStatus) {
case FormError.CurrentStateEqualsEndStart:
_showErrorMessage('The curret state must not match the end state!');
_showErrorMessage('The current state must not match the end state!');
return;
case FormError.StartStateEqualsEndState:
_showErrorMessage('The start state must not match the end state!');
Expand Down
4 changes: 2 additions & 2 deletions lib/widgets/home_screen/goal_segment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class GoalSegment extends StatelessWidget {
left: 16, right: 16, top: 6, bottom: 8),
child: FredericHeading(
tr('home.my_goals'),
onPressed: () => handleClick(context, setData),
onPressed: () => handleNewGoal(context, setData),
icon: Icons.add,
),
),
Expand Down Expand Up @@ -96,7 +96,7 @@ class GoalSegment extends StatelessWidget {
});
}

void handleClick(BuildContext context, FredericSetListData setData) {
void handleNewGoal(BuildContext context, FredericSetListData setData) {
showFredericBottomSheet(
context: context,
builder: (c) => Scaffold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class _NormalGoalCard extends State<NormalGoalCard> {
fit: widget.interactable ? StackFit.loose : StackFit.expand,
children: [
FredericCard(
shimmer: isCompleted ? true : false,
//shimmer: isCompleted ? true : false, // doesn't make sense here
onLongPress: () {
if (widget.interactable) handleLongClick(context);
},
Expand Down

0 comments on commit c4873ef

Please sign in to comment.