feat: Implement note positioning logic and tests for position conversion
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
const int notePositionScale = 10;
|
||||
const int notePositionStep = 1000 * notePositionScale;
|
||||
const int notePositionRebalanceThreshold = 1;
|
||||
|
||||
int toStoredNotePosition(double position) {
|
||||
return (position * notePositionScale).round();
|
||||
}
|
||||
|
||||
double fromStoredNotePosition(int storedPosition) {
|
||||
return storedPosition / notePositionScale;
|
||||
}
|
||||
|
||||
int nextTopNotePosition(Iterable<int> storedPositions) {
|
||||
int? highestPosition;
|
||||
|
||||
for (final int position in storedPositions) {
|
||||
if (highestPosition == null || position > highestPosition) {
|
||||
highestPosition = position;
|
||||
}
|
||||
}
|
||||
|
||||
if (highestPosition == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return highestPosition + notePositionStep;
|
||||
}
|
||||
|
||||
int? midpointNotePosition({
|
||||
required int higherPosition,
|
||||
required int lowerPosition,
|
||||
}) {
|
||||
final int gap = higherPosition - lowerPosition;
|
||||
if (gap <= notePositionRebalanceThreshold) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return lowerPosition + (gap ~/ 2);
|
||||
}
|
||||
|
||||
List<int> rebalanceNotePositions(int itemCount) {
|
||||
return List<int>.generate(
|
||||
itemCount,
|
||||
(int index) => (itemCount - 1 - index) * notePositionStep,
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user