Skip to content

Commit

Permalink
#836: Add auto-set checkbox to SaveTripPage
Browse files Browse the repository at this point in the history
  • Loading branch information
cohenadair committed Jun 27, 2023
1 parent 001663c commit e81717b
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 257 deletions.
15 changes: 11 additions & 4 deletions mobile/lib/i18n/english_strings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ Map<String, Map<String, String>> get englishStrings => {
"Are you sure you want to delete trip %s? This cannot be undone.",
"saveTripPage_editTitle": "Edit Trip",
"saveTripPage_newTitle": "New Trip",
"saveTripPage_autoSetTitle": "Auto-set Fields",
"saveTripPage_autoSetDescription":
"Automatically set applicable fields when catches are selected.",
"saveTripPage_startDate": "Start Date",
"saveTripPage_startTime": "Start Time",
"saveTripPage_startDateTime": "Start Date and Time",
Expand Down Expand Up @@ -1128,10 +1131,14 @@ Map<String, Map<String, String>> get englishStrings => {
"changeLog_2.4.0_4": "Added \"Sunny\" as a sky condition",
"changeLog_2.4.0_5": "Note fields can now include blank lines",
"changeLog_2.4.0_6": "Note fields are no longer truncated to 4 lines",
"changeLog_2.4.1_1": "Fixed a crash while fetching atmosphere and weather data",
"changeLog_2.4.1_1":
"Fixed a crash while fetching atmosphere and weather data",
"changeLog_2.4.1_2": "Fixed a rare crash while adding a catch",
"changeLog_2.4.1_3": "Fixed an issue where the fishing spot was reset while adding a catch",
"changeLog_2.4.1_4": "A trip's catches per species, bait, and fishing spot will now only update after selecting catches if they weren't already set",
"changeLog_2.4.1_5": "Several general stability improvements and crash fixes",
"changeLog_2.4.1_3":
"Fixed an issue where the fishing spot was reset while adding a catch",
"changeLog_2.4.1_4":
"When adding trips, you are now given the option to override existing input fields with data from selected catches",
"changeLog_2.4.1_5":
"Several general stability improvements and crash fixes",
},
};
5 changes: 5 additions & 0 deletions mobile/lib/i18n/strings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ class Strings {

String get saveTripPageNewTitle => _string("saveTripPage_newTitle");

String get saveTripPageAutoSetTitle => _string("saveTripPage_autoSetTitle");

String get saveTripPageAutoSetDescription =>
_string("saveTripPage_autoSetDescription");

String get saveTripPageStartDate => _string("saveTripPage_startDate");

String get saveTripPageStartTime => _string("saveTripPage_startTime");
Expand Down
80 changes: 36 additions & 44 deletions mobile/lib/pages/save_trip_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,6 @@ class SaveTripPageState extends State<SaveTripPage> {
final Set<String> _catchImages = {};

List<CustomEntityValue> _customEntityValues = [];
bool _userDidChangeStartTime = false;
bool _userDidChangeEndTime = false;
bool _userDidChangeCatchesPerFishingSpot = false;
bool _userDidChangeCatchesPerAngler = false;
bool _userDidChangeCatchesPerBait = false;
bool _userDidChangeCatchesPerSpecies = false;

Trip? get _oldTrip => widget.oldTrip;

Expand Down Expand Up @@ -293,6 +287,7 @@ class SaveTripPageState extends State<SaveTripPage> {
title: Text(_isEditing
? Strings.of(context).saveTripPageEditTitle
: Strings.of(context).saveTripPageNewTitle),
header: _buildAutoPopulateFieldsHeader(),
padding: insetsZero,
runSpacing: 0,
fields: _fields,
Expand All @@ -306,6 +301,15 @@ class SaveTripPageState extends State<SaveTripPage> {
);
}

Widget _buildAutoPopulateFieldsHeader() {
return CheckboxInput(
label: Strings.of(context).saveTripPageAutoSetTitle,
description: Strings.of(context).saveTripPageAutoSetDescription,
value: _userPreferenceManager.autoSetTripFields,
onChanged: (value) => _userPreferenceManager.setAutoSetTripFields(value),
);
}

Widget _buildField(Id id) {
if (id == _idStartTimestamp) {
return _buildStartTime();
Expand Down Expand Up @@ -347,8 +351,8 @@ class SaveTripPageState extends State<SaveTripPage> {
child: _DateTimeAllDayPicker(
controller: _startTimestampController,
dateLabel: Strings.of(context).saveTripPageStartDate,
timeLabel: Strings.of(context).saveTripPageStartDate,
onChange: () => setState(() => _userDidChangeStartTime = true),
timeLabel: Strings.of(context).saveTripPageStartTime,
onChange: () => setState(() {}),
),
);
}
Expand All @@ -359,8 +363,8 @@ class SaveTripPageState extends State<SaveTripPage> {
child: _DateTimeAllDayPicker(
controller: _endTimestampController,
dateLabel: Strings.of(context).saveTripPageEndDate,
timeLabel: Strings.of(context).saveTripPageEndDate,
onChange: () => setState(() => _userDidChangeEndTime = true),
timeLabel: Strings.of(context).saveTripPageEndTime,
onChange: () => setState(() {}),
),
);
}
Expand Down Expand Up @@ -402,7 +406,6 @@ class SaveTripPageState extends State<SaveTripPage> {
delegate: FishingSpotQuantityPickerInputDelegate(
manager: _fishingSpotManager,
controller: _fishingSpotCatchesController,
didUpdateValue: () => _userDidChangeCatchesPerFishingSpot = true,
),
);
}
Expand All @@ -414,7 +417,6 @@ class SaveTripPageState extends State<SaveTripPage> {
manager: _anglerManager,
controller: _anglerCatchesController,
listPageBuilder: (settings) => AnglerListPage(pickerSettings: settings),
didUpdateValue: () => _userDidChangeCatchesPerAngler = true,
),
);
}
Expand All @@ -425,7 +427,6 @@ class SaveTripPageState extends State<SaveTripPage> {
delegate: BaitQuantityPickerInputDelegate(
baitManager: _baitManager,
controller: _baitCatchesController,
didUpdateValue: () => _userDidChangeCatchesPerBait = true,
),
);
}
Expand All @@ -438,7 +439,6 @@ class SaveTripPageState extends State<SaveTripPage> {
controller: _speciesCatchesController,
listPageBuilder: (settings) =>
SpeciesListPage(pickerSettings: settings),
didUpdateValue: () => _userDidChangeCatchesPerSpecies = true,
),
);
}
Expand Down Expand Up @@ -544,29 +544,33 @@ class SaveTripPageState extends State<SaveTripPage> {
/// the time if "All day" checkboxes are checked. This will _not_ overwrite any
/// changes the user made to the time.
void _updateTimestampControllersIfNeeded(List<Catch> catches) {
if (!_userDidChangeStartTime) {
var startDateTime = catches.last.dateTime(context);
if (_startTimestampController.isMidnight) {
_startTimestampController.date = startDateTime;
} else {
_startTimestampController.value = startDateTime;
}
if (!_userPreferenceManager.autoSetTripFields) {
return;
}

if (!_userDidChangeEndTime) {
var endDateTime = catches.first.dateTime(context);
if (_endTimestampController.isMidnight) {
_endTimestampController.date = endDateTime;
} else {
_endTimestampController.value = endDateTime;
}
var startDateTime = catches.last.dateTime(context);
if (_startTimestampController.isMidnight) {
_startTimestampController.date = startDateTime;
} else {
_startTimestampController.value = startDateTime;
}

var endDateTime = catches.first.dateTime(context);
if (_endTimestampController.isMidnight) {
_endTimestampController.date = endDateTime;
} else {
_endTimestampController.value = endDateTime;
}
}

/// Updates "Catches Per Entity" values based on the given catches.
/// This will _not_ overwrite any changes the user made to the catches per
/// entity values.
void _updateCatchesPerEntityControllersIfNeeded(List<Catch> catches) {
if (!_userPreferenceManager.autoSetTripFields) {
return;
}

var catchesPerAngler = <Trip_CatchesPerEntity>[];
var catchesPerBait = <Trip_CatchesPerBait>[];
var catchesPerFishingSpot = <Trip_CatchesPerEntity>[];
Expand All @@ -591,22 +595,10 @@ class SaveTripPageState extends State<SaveTripPage> {
}
}

// Only update fields if the user hasn't already changed them.
if (!_userDidChangeCatchesPerAngler) {
_anglerCatchesController.value = catchesPerAngler.toSet();
}

if (!_userDidChangeCatchesPerBait) {
_baitCatchesController.value = catchesPerBait.toSet();
}

if (!_userDidChangeCatchesPerFishingSpot) {
_fishingSpotCatchesController.value = catchesPerFishingSpot.toSet();
}

if (!_userDidChangeCatchesPerSpecies) {
_speciesCatchesController.value = catchesPerSpecies.toSet();
}
_anglerCatchesController.value = catchesPerAngler.toSet();
_baitCatchesController.value = catchesPerBait.toSet();
_fishingSpotCatchesController.value = catchesPerFishingSpot.toSet();
_speciesCatchesController.value = catchesPerSpecies.toSet();
}

/// Adds body of water values based on the given catches. This will add to
Expand Down
7 changes: 7 additions & 0 deletions mobile/lib/user_preference_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class UserPreferenceManager extends PreferenceManager {
static const _keyBaitVariantFieldIds = "bait_variant_field_ids";
static const _keyCatchFieldIds = "catch_field_ids";
static const _keyTripFieldIds = "trip_field_ids";
static const _keyTripAutoSetFields = "trip_auto_set_fields";
static const _keyCatchLengthSystem = "catch_length_system";
static const _keyCatchWeightSystem = "catch_weight_system";
static const _keyWaterDepthSystem = "water_depth_system";
Expand Down Expand Up @@ -74,6 +75,12 @@ class UserPreferenceManager extends PreferenceManager {

List<Id> get tripFieldIds => idList(_keyTripFieldIds);

// ignore: avoid_positional_boolean_parameters
Future<void> setAutoSetTripFields(bool autoFetch) =>
put(_keyTripAutoSetFields, autoFetch);

bool get autoSetTripFields => preferences[_keyTripAutoSetFields] ?? true;

Future<void> setCatchLengthSystem(MeasurementSystem? system) =>
put(_keyCatchLengthSystem, system?.value);

Expand Down
14 changes: 14 additions & 0 deletions mobile/test/mocks/mocks.mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9961,6 +9961,11 @@ class MockUserPreferenceManager extends _i1.Mock
returnValue: <_i5.Id>[],
) as List<_i5.Id>);
@override
bool get autoSetTripFields => (super.noSuchMethod(
Invocation.getter(#autoSetTripFields),
returnValue: false,
) as bool);
@override
_i5.MeasurementSystem get catchLengthSystem => (super.noSuchMethod(
Invocation.getter(#catchLengthSystem),
returnValue: _FakeMeasurementSystem_90(
Expand Down Expand Up @@ -10200,6 +10205,15 @@ class MockUserPreferenceManager extends _i1.Mock
returnValueForMissingStub: _i2.Future<void>.value(),
) as _i2.Future<void>);
@override
_i2.Future<void> setAutoSetTripFields(bool? autoFetch) => (super.noSuchMethod(
Invocation.method(
#setAutoSetTripFields,
[autoFetch],
),
returnValue: _i2.Future<void>.value(),
returnValueForMissingStub: _i2.Future<void>.value(),
) as _i2.Future<void>);
@override
_i2.Future<void> setCatchLengthSystem(_i5.MeasurementSystem? system) =>
(super.noSuchMethod(
Invocation.method(
Expand Down
Loading

0 comments on commit e81717b

Please sign in to comment.