-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
150 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,87 @@ | ||
abstract class LayoutResolver<T> { | ||
const LayoutResolver(); | ||
import 'dart:collection'; | ||
|
||
class Breakpoint<T> implements Comparable<Breakpoint<T>> { | ||
Breakpoint(this.size, {required this.value}): assert(size == null || size > 0); | ||
|
||
final int? size; | ||
final T value; | ||
|
||
operator > (Breakpoint<T> other) => (size ?? 0) > (other.size ?? 0); | ||
operator >= (Breakpoint<T> other) => (size ?? 0) >= (other.size ?? 0); | ||
operator <= (Breakpoint<T> other) => (size ?? 0) <= (other.size ?? 0); | ||
operator < (Breakpoint<T> other) => (size ?? 0) < (other.size ?? 0); | ||
|
||
@override | ||
int compareTo(Breakpoint<T> other) { | ||
if (other > this) { return -1; } | ||
if (other < this) { return 1; } | ||
|
||
return 0; | ||
} | ||
} | ||
|
||
extension BreakpointListExtension<T> on List<Breakpoint<T>> { | ||
|
||
List<Breakpoint<T>> get descendingSorted { | ||
final descendingSorted = List<Breakpoint<T>>.from(this); | ||
|
||
descendingSorted..sort((a, b) { | ||
final comparison = b.compareTo(a); | ||
if (comparison == 0) { | ||
throw 'breakpoint of size `${b.size}` (value `${b.value}`) had the same size of other breakpoint (value `${a.value})'; | ||
} | ||
|
||
return comparison; | ||
}); | ||
|
||
return descendingSorted; | ||
} | ||
|
||
T resolveBreakpoint(double size); | ||
|
||
Breakpoint<T> firstCascadingBreakpoint(double size) { | ||
for (final breakpoint in this) { | ||
if (size >= (breakpoint.size ?? 0)) { | ||
return breakpoint; | ||
} | ||
} | ||
|
||
return last; | ||
} | ||
} | ||
|
||
|
||
|
||
abstract class LayoutResolver<T> { | ||
LayoutResolver({required double size, required List<Breakpoint<T>> breakpoints}): assert(size > 0), assert(breakpoints.length > 1), _descendingBreakpoints = breakpoints.descendingSorted, breakpoint = breakpoints.descendingSorted.firstCascadingBreakpoint(size); | ||
|
||
final List<Breakpoint<T>> _descendingBreakpoints; | ||
|
||
|
||
List<Breakpoint<T>> get breakpoints => _descendingBreakpoints; | ||
final Breakpoint<T> breakpoint; | ||
T get breakpointValue => breakpoint.value; | ||
|
||
bool matchesValueOrSmaller(T match) { | ||
final valueBreakpoint = breakpoints.firstWhere((breakpoint) => breakpoint.value == match, orElse: () { | ||
throw 'TODO: throw or return false?'; | ||
}); | ||
|
||
return breakpoint <= valueBreakpoint; | ||
} | ||
|
||
bool matchesValue(T match) { | ||
final valueBreakpoint = breakpoints.firstWhere((breakpoint) => breakpoint.value == match, orElse: () { | ||
throw 'TODO'; | ||
}); | ||
|
||
return breakpoint.size == valueBreakpoint.size; | ||
} | ||
|
||
bool matchesValueOrLarger(T match) { | ||
final valueBreakpoint = breakpoints.firstWhere((breakpoint) => breakpoint.value == match, orElse: () { | ||
throw 'TODO'; | ||
}); | ||
|
||
return breakpoint >= valueBreakpoint; | ||
} | ||
} |