94 lines
2.4 KiB
Dart
94 lines
2.4 KiB
Dart
/// A user-defined supermarket with a display color.
|
|
library;
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
class Supermarket {
|
|
const Supermarket({
|
|
this.id,
|
|
required this.name,
|
|
required this.colorValue,
|
|
this.sortOrder = 0,
|
|
this.bonusImagePath,
|
|
});
|
|
|
|
final int? id;
|
|
final String name;
|
|
final int colorValue;
|
|
final int sortOrder;
|
|
final String? bonusImagePath;
|
|
|
|
Color get color => Color(colorValue);
|
|
|
|
factory Supermarket.fromMap(Map<String, Object?> map) {
|
|
return Supermarket(
|
|
id: map['id'] as int?,
|
|
name: map['name']! as String,
|
|
colorValue: map['color']! as int,
|
|
sortOrder: map['sort_order'] as int? ?? 0,
|
|
bonusImagePath: map['bonus_image_path'] as String?,
|
|
);
|
|
}
|
|
|
|
Supermarket copyWith({
|
|
int? id,
|
|
String? name,
|
|
int? colorValue,
|
|
int? sortOrder,
|
|
String? bonusImagePath,
|
|
bool clearBonusImage = false,
|
|
}) {
|
|
return Supermarket(
|
|
id: id ?? this.id,
|
|
name: name ?? this.name,
|
|
colorValue: colorValue ?? this.colorValue,
|
|
sortOrder: sortOrder ?? this.sortOrder,
|
|
bonusImagePath: clearBonusImage
|
|
? null
|
|
: (bonusImagePath ?? this.bonusImagePath),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Puts stores with the most recent trip first, then earlier trips, then unused.
|
|
List<Supermarket> sortStoresByLastTrip(
|
|
List<Supermarket> stores,
|
|
Iterable<String> lastTripStores,
|
|
) {
|
|
final rank = <String, int>{};
|
|
var next = 0;
|
|
for (final name in lastTripStores) {
|
|
final key = name.trim().toUpperCase();
|
|
if (key.isEmpty || rank.containsKey(key)) continue;
|
|
rank[key] = next++;
|
|
}
|
|
|
|
final sorted = [...stores];
|
|
sorted.sort((a, b) {
|
|
final aRank = rank[a.name.trim().toUpperCase()] ?? 1 << 20;
|
|
final bRank = rank[b.name.trim().toUpperCase()] ?? 1 << 20;
|
|
final byTrip = aRank.compareTo(bRank);
|
|
if (byTrip != 0) return byTrip;
|
|
final byOrder = a.sortOrder.compareTo(b.sortOrder);
|
|
if (byOrder != 0) return byOrder;
|
|
return a.name.toLowerCase().compareTo(b.name.toLowerCase());
|
|
});
|
|
return sorted;
|
|
}
|
|
|
|
/// Palette offered when the user picks a store color.
|
|
const List<int> kStoreColorPalette = [
|
|
0xFFEEC21B, // Jumbo yellow
|
|
0xFF0050AA, // Lidl blue
|
|
0xFF00A0E2, // AH blue
|
|
0xFF00205B, // Aldi navy
|
|
0xFF6EC31E, // Plus green
|
|
0xFFE30613, // Dirk / Coop red
|
|
0xFF009640, // SPAR green
|
|
0xFFE87722, // orange
|
|
0xFFE31C5F, // Picnic pink
|
|
0xFF7B1FA2, // purple
|
|
0xFF00897B, // teal
|
|
0xFF78909C, // grey
|
|
];
|