Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ app.*.map.json
/android/app/release

# FVM Version Cache
.fvm/
.fvm/
# Generated localization
lib/gen/
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dev_dependencies:
flutter_lints: ^5.0.0

flutter:

generate: true
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
Expand Down
Empty file added l10n/placeholder
Empty file.
141 changes: 84 additions & 57 deletions lib/core/auto_fill_engine.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import 'package:sheets/core/cell_properties.dart';
import 'package:sheets/core/sheet_data.dart';
import 'package:sheets/core/sheet_index.dart';
import 'package:sheets/core/values/patterns/linear_date_pattern.dart';
import 'package:sheets/core/values/patterns/linear_duration_pattern.dart';
import 'package:sheets/core/values/patterns/linear_numeric_pattern.dart';
import 'package:sheets/core/values/patterns/linear_string_pattern.dart';
import 'package:sheets/core/values/patterns/repeat_value_pattern.dart';
import 'package:sheets/core/pattern_detector.dart';
import 'package:sheets/core/values/patterns/value_pattern.dart';
import 'package:sheets/utils/direction.dart';
import 'package:sheets/utils/extensions/cell_properties_extensions.dart';
Expand Down Expand Up @@ -34,10 +30,13 @@ class AutoFillEngine {
);

if (fillDirection.isVertical) {
Map<ColumnIndex, List<IndexedCellProperties>> groupedPatternCells = _patternCells.groupByColumns();
_fillCells(groupedPatternCells, _cellsToFill.whereColumn, cellMergePattern);
Map<ColumnIndex, List<IndexedCellProperties>> groupedPatternCells =
_patternCells.groupByColumns();
_fillCells(
groupedPatternCells, _cellsToFill.whereColumn, cellMergePattern);
} else {
Map<RowIndex, List<IndexedCellProperties>> groupedPatternCells = _patternCells.groupByRows();
Map<RowIndex, List<IndexedCellProperties>> groupedPatternCells =
_patternCells.groupByRows();
_fillCells(groupedPatternCells, _cellsToFill.whereRow, cellMergePattern);
}
}
Expand All @@ -49,9 +48,12 @@ class AutoFillEngine {
) {
bool reversed = fillDirection.isReversed;

for (MapEntry<T, List<IndexedCellProperties>> entry in groupedPatternCells.entries) {
List<IndexedCellProperties> patternCells = entry.value.maybeReverse(reversed);
List<IndexedCellProperties> fillCells = getFillCellsForKey(entry.key).maybeReverse(reversed);
for (MapEntry<T, List<IndexedCellProperties>> entry
in groupedPatternCells.entries) {
List<IndexedCellProperties> patternCells =
entry.value.maybeReverse(reversed);
List<IndexedCellProperties> fillCells =
getFillCellsForKey(entry.key).maybeReverse(reversed);

patternApplier.apply(patternCells, fillCells);
}
Expand All @@ -75,15 +77,20 @@ class _PatternApplier {

final Set<CellIndex> completedCells = <CellIndex>{};

void apply(List<IndexedCellProperties> patternCells, List<IndexedCellProperties> cellsToFill) {
void apply(List<IndexedCellProperties> patternCells,
List<IndexedCellProperties> cellsToFill) {
int templateIndex = 0;
List<IndexedCellProperties> unprocessedFillCells = List<IndexedCellProperties>.from(cellsToFill);
List<IndexedCellProperties> unprocessedFillCells =
List<IndexedCellProperties>.from(cellsToFill);

Map<String, Set<IndexedCellProperties>> templateRanges = <String, Set<IndexedCellProperties>>{};
Map<String, List<IndexedCellProperties>> fillRanges = <String, List<IndexedCellProperties>>{};
Map<String, Set<IndexedCellProperties>> templateRanges =
<String, Set<IndexedCellProperties>>{};
Map<String, List<IndexedCellProperties>> fillRanges =
<String, List<IndexedCellProperties>>{};

while (unprocessedFillCells.isNotEmpty) {
IndexedCellProperties baseCell = patternCells[templateIndex % patternCells.length];
IndexedCellProperties baseCell =
patternCells[templateIndex % patternCells.length];
IndexedCellProperties targetCell = unprocessedFillCells.removeAt(0);
CellMergeStatus baseMergeStatus = baseCell.properties.mergeStatus;

Expand All @@ -92,7 +99,8 @@ class _PatternApplier {
}

if (baseMergeStatus is! MergedCell) {
_handleUnmergedTemplateCell(baseCell, targetCell, templateRanges, fillRanges);
_handleUnmergedTemplateCell(
baseCell, targetCell, templateRanges, fillRanges);
templateIndex++;
continue;
}
Expand Down Expand Up @@ -120,8 +128,12 @@ class _PatternApplier {
Map<String, List<IndexedCellProperties>> fillRanges,
) {
const String unmergedKey = '1x1';
templateRanges.putIfAbsent(unmergedKey, () => <IndexedCellProperties>{}).add(baseCell);
fillRanges.putIfAbsent(unmergedKey, () => <IndexedCellProperties>[]).add(targetCell);
templateRanges
.putIfAbsent(unmergedKey, () => <IndexedCellProperties>{})
.add(baseCell);
fillRanges
.putIfAbsent(unmergedKey, () => <IndexedCellProperties>[])
.add(targetCell);
}

bool _handleMergedTemplateCell(
Expand All @@ -131,73 +143,88 @@ class _PatternApplier {
List<IndexedCellProperties> unprocessedFillCells,
Map<String, Set<IndexedCellProperties>> templateRanges,
Map<String, List<IndexedCellProperties>> fillRanges,
) {
MergedCell movedStatus =
_calculateMovedStatus(baseMergeStatus, baseCell, targetCell);
if (_isOverlappingRange(movedStatus)) {
return false;
}

_markCellsProcessed(unprocessedFillCells, movedStatus.mergedCells);
data.cells.merge(movedStatus.mergedCells);
_addMergeRange(movedStatus, baseCell, templateRanges, fillRanges);

return true;
}

MergedCell _calculateMovedStatus(
MergedCell baseMergeStatus,
IndexedCellProperties baseCell,
IndexedCellProperties targetCell,
) {
int dxDiff = targetCell.index.column.value - baseCell.index.column.value;
int dyDiff = targetCell.index.row.value - baseCell.index.row.value;

MergedCell movedMergeStatus = fillDirection.isHorizontal
return fillDirection.isHorizontal
? baseMergeStatus.moveHorizontal(dx: dxDiff, reverse: reversed)
: baseMergeStatus.moveVertical(dy: dyDiff, reverse: reversed);
}

if (movedMergeStatus.contains(rangeStart) || movedMergeStatus.contains(rangeEnd)) {
return false;
}
bool _isOverlappingRange(MergedCell movedStatus) {
return movedStatus.contains(rangeStart) || movedStatus.contains(rangeEnd);
}

for (CellIndex index in movedMergeStatus.mergedCells) {
unprocessedFillCells.removeWhere((IndexedCellProperties cell) => cell.index == index);
void _markCellsProcessed(
List<IndexedCellProperties> unprocessed,
Iterable<CellIndex> mergedCells,
) {
for (CellIndex index in mergedCells) {
unprocessed
.removeWhere((IndexedCellProperties cell) => cell.index == index);
completedCells.add(index);
}
}

data.cells.merge(movedMergeStatus.mergedCells);

String key = movedMergeStatus.id;
templateRanges.putIfAbsent(key, () => <IndexedCellProperties>{}).add(baseCell);
void _addMergeRange(
MergedCell movedStatus,
IndexedCellProperties baseCell,
Map<String, Set<IndexedCellProperties>> templateRanges,
Map<String, List<IndexedCellProperties>> fillRanges,
) {
String key = movedStatus.id;
templateRanges
.putIfAbsent(key, () => <IndexedCellProperties>{})
.add(baseCell);
fillRanges.putIfAbsent(key, () => <IndexedCellProperties>[]).add(
IndexedCellProperties(
index: movedMergeStatus.start,
properties: data.cells.get(movedMergeStatus.start),
index: movedStatus.start,
properties: data.cells.get(movedStatus.start),
),
);

return true;
}

void _applyPatterns(
Map<String, Set<IndexedCellProperties>> templateRanges,
Map<String, List<IndexedCellProperties>> fillRanges,
) {
for (MapEntry<String, List<IndexedCellProperties>> fillRangeEntry in fillRanges.entries) {
for (MapEntry<String, List<IndexedCellProperties>> fillRangeEntry
in fillRanges.entries) {
String key = fillRangeEntry.key;
List<IndexedCellProperties> patternCells = templateRanges[key]!.toList();
ValuePattern<dynamic, dynamic> pattern = _detectPattern(patternCells);
List<IndexedCellProperties> filledCells = pattern.apply( patternCells, fillRangeEntry.value);
List<IndexedCellProperties> filledCells =
pattern.apply(patternCells, fillRangeEntry.value);
data.cells.setAll(filledCells);
}
}

ValuePattern<dynamic, dynamic> _detectPattern(List<IndexedCellProperties> cells) {
ValuePattern<dynamic, dynamic> _detectPattern(
List<IndexedCellProperties> patternCells,
) {
PatternDetector detector = PatternDetector();
List<IndexedCellProperties> propertiesToFill = reversed ? cells.reversed.toList() : cells;
List<IndexedCellProperties> processedCells =
reversed ? patternCells.reversed.toList() : patternCells;

return detector.detectPattern(propertiesToFill);
}
}

class PatternDetector {
final List<ValuePatternMatcher> matchers = <ValuePatternMatcher>[
LinearNumericPatternMatcher(),
LinearDatePatternMatcher(),
LinearDurationPatternMatcher(),
LinearStringPatternMatcher(),
];

ValuePattern<dynamic, dynamic> detectPattern(List<IndexedCellProperties> patternCells) {
for (ValuePatternMatcher matcher in matchers) {
ValuePattern<dynamic, dynamic>? pattern = matcher.detect(patternCells);
if (pattern != null) {
return pattern;
}
}
return RepeatValuePattern();
return detector.detectPattern(processedCells);
}
}
4 changes: 4 additions & 0 deletions lib/core/config/sheet_constants.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import 'package:flutter/material.dart';

double borderWidth = 1;
// Thickness for borders separating pinned rows and columns from
// the scrollable area. Used for both the draggable pin area and
// drawing pinned dividers.
double pinnedBorderWidth = 5;

double scrollbarWidth = 14;

Expand Down
75 changes: 75 additions & 0 deletions lib/core/events/sheet_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,80 @@ class SetViewportSizeAction extends SheetAction<SetViewportSizeEvent> {
@override
void execute() {
worksheet.viewport.setViewportRect(event.rect);
worksheet.scroll.setViewportSize(
event.rect.size,
pinnedColumnsWidth: worksheet.data.pinnedColumnsFullWidth,
pinnedRowsHeight: worksheet.data.pinnedRowsFullHeight,
);
}
}

// Set Pinned Columns Count
class SetPinnedColumnsEvent extends SheetEvent {
SetPinnedColumnsEvent(this.count);

final int count;

@override
SheetAction<SheetEvent> createAction(Worksheet worksheet) =>
SetPinnedColumnsAction(this, worksheet);

@override
SheetRebuildConfig get rebuildConfig {
return SheetRebuildConfig.all();
}

@override
List<Object?> get props => <Object?>[count];
}

class SetPinnedColumnsAction extends SheetAction<SetPinnedColumnsEvent> {
SetPinnedColumnsAction(super.event, super.worksheet);

@override
void execute() {
worksheet.data.pinnedColumnCount = event.count;
worksheet.scroll.setContentSize(worksheet.data.scrollableContentSize);
worksheet.scroll.setViewportSize(
worksheet.viewport.rect.size,
pinnedColumnsWidth: worksheet.data.pinnedColumnsFullWidth,
pinnedRowsHeight: worksheet.data.pinnedRowsFullHeight,
);
worksheet.viewport.rebuild(worksheet.scroll.offset);
}
}

// Set Pinned Rows Count
class SetPinnedRowsEvent extends SheetEvent {
SetPinnedRowsEvent(this.count);

final int count;

@override
SheetAction<SheetEvent> createAction(Worksheet worksheet) =>
SetPinnedRowsAction(this, worksheet);

@override
SheetRebuildConfig get rebuildConfig {
return SheetRebuildConfig.all();
}

@override
List<Object?> get props => <Object?>[count];
}

class SetPinnedRowsAction extends SheetAction<SetPinnedRowsEvent> {
SetPinnedRowsAction(super.event, super.worksheet);

@override
void execute() {
worksheet.data.pinnedRowCount = event.count;
worksheet.scroll.setContentSize(worksheet.data.scrollableContentSize);
worksheet.scroll.setViewportSize(
worksheet.viewport.rect.size,
pinnedColumnsWidth: worksheet.data.pinnedColumnsFullWidth,
pinnedRowsHeight: worksheet.data.pinnedRowsFullHeight,
);
worksheet.viewport.rebuild(worksheet.scroll.offset);
}
}
28 changes: 28 additions & 0 deletions lib/core/pattern_detector.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:sheets/core/cell_properties.dart';
import 'package:sheets/core/values/patterns/linear_date_pattern.dart';
import 'package:sheets/core/values/patterns/linear_duration_pattern.dart';
import 'package:sheets/core/values/patterns/linear_numeric_pattern.dart';
import 'package:sheets/core/values/patterns/linear_string_pattern.dart';
import 'package:sheets/core/values/patterns/repeat_value_pattern.dart';
import 'package:sheets/core/values/patterns/value_pattern.dart';

class PatternDetector {
final List<ValuePatternMatcher> matchers = <ValuePatternMatcher>[
LinearNumericPatternMatcher(),
LinearDatePatternMatcher(),
LinearDurationPatternMatcher(),
LinearStringPatternMatcher(),
];

ValuePattern<dynamic, dynamic> detectPattern(
List<IndexedCellProperties> patternCells,
) {
for (ValuePatternMatcher matcher in matchers) {
ValuePattern<dynamic, dynamic>? pattern = matcher.detect(patternCells);
if (pattern != null) {
return pattern;
}
}
return RepeatValuePattern();
}
}
10 changes: 7 additions & 3 deletions lib/core/scroll/sheet_scroll_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ class SheetScrollController {
late DirectionalValues<SheetScrollPosition> position;
late DirectionalValues<SheetScrollMetrics> metrics;

void setViewportSize(Size size) {
void setViewportSize(Size size, {double pinnedColumnsWidth = 0, double pinnedRowsHeight = 0}) {
metrics = DirectionalValues<SheetScrollMetrics>(
horizontal: metrics.horizontal.copyWith(viewportDimension: size.width - rowHeadersWidth),
vertical: metrics.vertical.copyWith(viewportDimension: size.height - columnHeadersHeight),
horizontal: metrics.horizontal.copyWith(
viewportDimension: size.width - rowHeadersWidth - pinnedColumnsWidth,
),
vertical: metrics.vertical.copyWith(
viewportDimension: size.height - columnHeadersHeight - pinnedRowsHeight,
),
);
}

Expand Down
Loading