moveView method

int moveView(
  1. int scrollTop,
  2. int diffPx,
  3. int maxPaddingTop,
  4. int maxPaddingBottom,
)

Moves the 'view' by adjusting the heights of the top and bottom padding divs. Returns the value that scrollTop should be set to.

Implementation

int moveView(
    int scrollTop, int diffPx, int maxPaddingTop, int maxPaddingBottom) {
  var newPaddingTop = paddingTop + diffPx;
  var newPaddingBottom = paddingBottom - diffPx;

  int minPaddingTop = min(MIN_BUFFER_SIZE_PX, maxPaddingTop);
  int minPaddingBottom = min(MIN_BUFFER_SIZE_PX, maxPaddingBottom);

  // If we're approaching the top of the div, we need to add more padding
  // there, and adjust scrollTop / startTop to compensate.
  if (newPaddingTop < minPaddingTop) {
    startTop -= (minPaddingTop - newPaddingTop);
    scrollTop += (minPaddingTop - newPaddingTop);
    newPaddingTop = minPaddingTop;

    var excessHeightPx =
        _totalHeight(newPaddingTop, newPaddingBottom) - MAX_CALENDAR_SIZE_PX;
    if (excessHeightPx > 0) {
      newPaddingBottom -= excessHeightPx;
    }
  }
  // If we're approaching the bottom of the div, we need to add more padding
  // there.
  if (newPaddingBottom < minPaddingBottom) {
    newPaddingBottom = minPaddingBottom;

    var excessHeightPx =
        _totalHeight(newPaddingTop, newPaddingBottom) - MAX_CALENDAR_SIZE_PX;
    if (excessHeightPx > 0) {
      startTop += excessHeightPx;
      scrollTop -= excessHeightPx;
      newPaddingTop -= excessHeightPx;
    }
  }

  // Ensure there's no whitespace before the first month or after the last
  // month, by enforcing maxPaddingTop/maxPaddingBottom.
  if (newPaddingTop > maxPaddingTop) {
    // Changing the top padding means we have to adjust the scroll position
    int delta = newPaddingTop - maxPaddingTop;
    newPaddingTop = maxPaddingTop;
    startTop += delta;
    scrollTop -= delta;
  }
  if (newPaddingBottom > maxPaddingBottom) {
    // Changing the bottom padding doesn't affect the scroll position
    newPaddingBottom = maxPaddingBottom;
  }

  paddingTop = newPaddingTop;
  paddingBottom = newPaddingBottom;
  changeDetector.markForCheck();
  return scrollTop;
}