Skip to content

Commit

Permalink
Simplify changeset comment (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbendebiene authored Aug 16, 2023
1 parent 334a78b commit ef392fb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
45 changes: 23 additions & 22 deletions lib/models/changeset_comment_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,26 @@ class ChangesetCommentBuilder {
const moreStringAsIterable = ['mehr'];

final mapFeatureNames = _getElementNames(modifiedElements);
final stopNames = _getStopNames(stopArea.stops);
final stopName = _getMostCommonStopName(stopArea.stops);

var countElementNames = mapFeatureNames.length;
var countStopNames = stopNames.length;
var finalString = _buildString(mapFeatureNames, stopNames);
var finalString = _buildString(mapFeatureNames, stopName);

// prevent changeset comment from getting larger than 255 characters
// try to reduce the string length step by step
while (finalString.length > 255) {
// try to reduce the string length by removing terms step by step
if (countElementNames > 1 || countStopNames > 1) {
if (countElementNames > 1) {
var elementStrings = mapFeatureNames;
var stopStrings = stopNames;

if (countElementNames > 1) {
elementStrings = elementStrings.take(--countElementNames);
}
else {
stopStrings = stopStrings.take(--countStopNames);
}
// append "more" string if any term was removed
if (countElementNames < mapFeatureNames.length) {
elementStrings = elementStrings.followedBy(moreStringAsIterable);
}
if (countStopNames < stopNames.length) {
stopStrings = stopStrings.followedBy(moreStringAsIterable);
}
finalString = _buildString(elementStrings, stopStrings);
finalString = _buildString(elementStrings, stopName);
}
// hard truncate string
else {
Expand All @@ -67,19 +59,19 @@ class ChangesetCommentBuilder {

/// Build changeset comment string based on given map feature names and stop names.
String _buildString(Iterable<String> mapFeatureNames, Iterable<String> stopNames) {
String _buildString(Iterable<String> mapFeatureNames, String? stopName) {
const mainString = 'Details zu %s im Haltestellenbereich %s hinzugefügt.';

String mapFeaturesString;
mapFeaturesString = _concat(mapFeatureNames, conjunctionString: ' und ');
// fallback name string
if (mapFeaturesString.isEmpty) mapFeaturesString = 'Element';

final stopsString = _concat(stopNames, conjunctionString: ' und ');
final stopString = stopName ?? '';

return mainString
.replaceFirst('%s', mapFeaturesString)
.replaceFirst('%s', stopsString)
.replaceFirst('%s', stopString)
// replace all double spaces that might occur when the value of any %s is empty
.replaceAll(RegExp('\\s+'), ' ');
}
Expand Down Expand Up @@ -117,12 +109,21 @@ class ChangesetCommentBuilder {

/// Extracts the names of multiple stops while filtering duplicates.
Iterable<String> _getStopNames(Iterable<Stop> stops) {
// use set to automatically remove duplicates
return Set<String>.from(
stops
.where((stop) => stop.name.isNotEmpty)
.map((stop) => stop.name)
);
String? _getMostCommonStopName(Set<Stop> stops) {
final stopNameCount = <String, int>{};

for (final stop in stops) {
stopNameCount.update(
stop.name,
(value) => value + 1,
ifAbsent: () => 0,
);
}

if (stopNameCount.isEmpty) return null;

return stopNameCount.entries.reduce(
(acc, cur) => cur.value > acc.value ? cur : acc
).key;
}
}
12 changes: 6 additions & 6 deletions test/osm_upload_api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void main() async {
name: 'Stop1'
),
Stop(
location: LatLng(10.00001, 20.00001),
location: LatLng(10.00002, 20.00002),
name: 'Stop2'
)
], LatLng(10.00001, 20.00001), 200);
Expand All @@ -48,12 +48,12 @@ void main() async {
name: 'Stop1'
),
Stop(
location: LatLng(10.00001, 20.00001),
location: LatLng(10.00002, 20.00002),
name: 'Stop2'
),
Stop(
location: LatLng(10.00001, 20.00001),
name: 'Stop3'
location: LatLng(10.00003, 20.00003),
name: 'Stop2'
)
], LatLng(10.00001, 20.00001), 200);

Expand Down Expand Up @@ -268,7 +268,7 @@ void main() async {
'created_by': uploadApi02.changesetCreatedBy,
'locale': uploadApi02.changesetLocale,
'source': uploadApi02.changesetSource,
'comment': 'Details zu MapFeature3, MapFeature1 und MapFeature2 im Haltestellenbereich Stop1 und Stop2 hinzugefügt.',
'comment': 'Details zu MapFeature3, MapFeature1 und MapFeature2 im Haltestellenbereich Stop1 hinzugefügt.',
})));

// update way for triple stop area
Expand All @@ -294,7 +294,7 @@ void main() async {
'created_by': uploadApi03.changesetCreatedBy,
'locale': uploadApi03.changesetLocale,
'source': uploadApi03.changesetSource,
'comment': 'Details zu MapFeature3, MapFeature1 und MapFeature2 im Haltestellenbereich Stop1, Stop2 und Stop3 hinzugefügt.',
'comment': 'Details zu MapFeature3, MapFeature1 und MapFeature2 im Haltestellenbereich Stop2 hinzugefügt.',
})));

// check if no additional changeset was created by comparing the amount of changesets each query returned
Expand Down

0 comments on commit ef392fb

Please sign in to comment.