mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-28 21:49:10 +01:00
Compare commits
4 Commits
34783a5022
...
51e3945507
| Author | SHA1 | Date | |
|---|---|---|---|
| 51e3945507 | |||
| 43c502c940 | |||
| 76771e2495 | |||
| a68313f236 |
@ -1,6 +1,14 @@
|
|||||||
include: package:very_good_analysis/analysis_options.yaml
|
include: package:very_good_analysis/analysis_options.yaml
|
||||||
|
|
||||||
analyzer:
|
analyzer:
|
||||||
|
exclude:
|
||||||
|
- build/**
|
||||||
|
- android/**
|
||||||
|
- ios/**
|
||||||
|
- web/**
|
||||||
|
- windows/**
|
||||||
|
- macos/**
|
||||||
|
- linux/**
|
||||||
language:
|
language:
|
||||||
strict-casts: true
|
strict-casts: true
|
||||||
strict-inference: true
|
strict-inference: true
|
||||||
|
|||||||
200
billsplit/lib/domain/assignment.dart
Normal file
200
billsplit/lib/domain/assignment.dart
Normal file
@ -0,0 +1,200 @@
|
|||||||
|
/// Assignment and the split calculation.
|
||||||
|
///
|
||||||
|
/// Split out of models.dart to keep it under the 250-line cap. A `part`
|
||||||
|
/// because Dart privates are library-scoped.
|
||||||
|
|
||||||
|
part of 'models.dart';
|
||||||
|
|
||||||
|
/// The assignment of one item: a symbolic mode plus mode-specific data.
|
||||||
|
class Assignment {
|
||||||
|
/// Creates an assignment; defaults to [AssignMode.everyone].
|
||||||
|
Assignment({
|
||||||
|
this.mode = AssignMode.everyone,
|
||||||
|
this.groupId,
|
||||||
|
Set<String>? customIds,
|
||||||
|
}) : customIds = customIds ?? <String>{};
|
||||||
|
|
||||||
|
/// Restores an assignment from its [toJson] map.
|
||||||
|
factory Assignment.fromJson(Map<String, dynamic> j) => Assignment(
|
||||||
|
mode: AssignMode.values.asNameMap()[j['mode']] ?? AssignMode.everyone,
|
||||||
|
groupId: j['groupId'] as String?,
|
||||||
|
customIds: (j['customIds'] as List? ?? const <dynamic>[])
|
||||||
|
.cast<String>()
|
||||||
|
.toSet(),
|
||||||
|
);
|
||||||
|
|
||||||
|
/// Current mode.
|
||||||
|
AssignMode mode;
|
||||||
|
|
||||||
|
/// Group id when [mode] is [AssignMode.group].
|
||||||
|
String? groupId;
|
||||||
|
|
||||||
|
/// Hand-picked person ids when [mode] is [AssignMode.custom].
|
||||||
|
final Set<String> customIds;
|
||||||
|
|
||||||
|
/// JSON representation for persistence.
|
||||||
|
Map<String, dynamic> toJson() => {
|
||||||
|
'mode': mode.name,
|
||||||
|
if (groupId != null) 'groupId': groupId,
|
||||||
|
'customIds': customIds.toList(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Standard item categories. [Item.category] is free text so users can add
|
||||||
|
/// their own; these constants are the ones the importer produces.
|
||||||
|
abstract final class Categories {
|
||||||
|
/// Alcoholic drinks — default to drinkers only.
|
||||||
|
static const alcohol = 'Alcohol';
|
||||||
|
|
||||||
|
/// Mixers (tonic, limes, …) — tagged for visibility, split among everyone.
|
||||||
|
static const mixer = 'Mixer';
|
||||||
|
|
||||||
|
/// Returnable bottle deposits (kaucja).
|
||||||
|
static const deposit = 'Deposit';
|
||||||
|
|
||||||
|
/// Anything else.
|
||||||
|
static const general = 'General';
|
||||||
|
}
|
||||||
|
|
||||||
|
/// One receipt line.
|
||||||
|
class Item {
|
||||||
|
/// Creates an item; [totalGr] is the post-discount line total.
|
||||||
|
Item({
|
||||||
|
required this.id,
|
||||||
|
required this.name,
|
||||||
|
required this.totalGr,
|
||||||
|
this.qty = 1,
|
||||||
|
this.unitPriceGr = 0,
|
||||||
|
this.discountGr = 0,
|
||||||
|
this.category = Categories.general,
|
||||||
|
Assignment? assignment,
|
||||||
|
}) : assignment = assignment ?? Assignment();
|
||||||
|
|
||||||
|
/// Restores an item from its [toJson] map.
|
||||||
|
factory Item.fromJson(Map<String, dynamic> j) => Item(
|
||||||
|
id: j['id'] as String,
|
||||||
|
name: j['name'] as String,
|
||||||
|
totalGr: j['totalGr'] as int,
|
||||||
|
qty: (j['qty'] as num? ?? 1).toDouble(),
|
||||||
|
unitPriceGr: j['unitPriceGr'] as int? ?? 0,
|
||||||
|
discountGr: j['discountGr'] as int? ?? 0,
|
||||||
|
category: j['category'] as String? ?? Categories.general,
|
||||||
|
assignment: Assignment.fromJson(
|
||||||
|
(j['assignment'] as Map?)?.cast<String, dynamic>() ?? const {},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
/// Stable unique id.
|
||||||
|
final String id;
|
||||||
|
|
||||||
|
/// Display name.
|
||||||
|
String name;
|
||||||
|
|
||||||
|
/// Final line total after discount — the ground truth used for splitting.
|
||||||
|
int totalGr;
|
||||||
|
|
||||||
|
/// Quantity (may be fractional for weighed goods).
|
||||||
|
double qty;
|
||||||
|
|
||||||
|
/// Unit price in grosze.
|
||||||
|
int unitPriceGr;
|
||||||
|
|
||||||
|
/// Discount already included in [totalGr]; kept for display (<= 0).
|
||||||
|
int discountGr;
|
||||||
|
|
||||||
|
/// Free-text category; see [Categories] for the standard values.
|
||||||
|
String category;
|
||||||
|
|
||||||
|
/// Who shares this item.
|
||||||
|
Assignment assignment;
|
||||||
|
|
||||||
|
/// JSON representation for persistence.
|
||||||
|
Map<String, dynamic> toJson() => {
|
||||||
|
'id': id,
|
||||||
|
'name': name,
|
||||||
|
'totalGr': totalGr,
|
||||||
|
'qty': qty,
|
||||||
|
'unitPriceGr': unitPriceGr,
|
||||||
|
'discountGr': discountGr,
|
||||||
|
'category': category,
|
||||||
|
'assignment': assignment.toJson(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// One shopping receipt with its items and participants.
|
||||||
|
class Receipt {
|
||||||
|
/// Creates a receipt; [participantIds] defaults to empty.
|
||||||
|
Receipt({
|
||||||
|
required this.id,
|
||||||
|
required this.title,
|
||||||
|
this.store,
|
||||||
|
DateTime? date,
|
||||||
|
List<Item>? items,
|
||||||
|
Set<String>? participantIds,
|
||||||
|
this.paidTotalGr,
|
||||||
|
}) : date = date ?? DateTime.now(),
|
||||||
|
items = items ?? <Item>[],
|
||||||
|
participantIds = participantIds ?? <String>{};
|
||||||
|
|
||||||
|
/// Restores a receipt from its [toJson] map.
|
||||||
|
factory Receipt.fromJson(Map<String, dynamic> j) => Receipt(
|
||||||
|
id: j['id'] as String,
|
||||||
|
title: j['title'] as String,
|
||||||
|
store: j['store'] as String?,
|
||||||
|
date: DateTime.tryParse(j['date'] as String? ?? '') ?? DateTime.now(),
|
||||||
|
items: (j['items'] as List? ?? const <dynamic>[])
|
||||||
|
.map((e) => Item.fromJson((e as Map).cast<String, dynamic>()))
|
||||||
|
.toList(),
|
||||||
|
participantIds: (j['participantIds'] as List? ?? const <dynamic>[])
|
||||||
|
.cast<String>()
|
||||||
|
.toSet(),
|
||||||
|
paidTotalGr: j['paidTotalGr'] as int?,
|
||||||
|
);
|
||||||
|
|
||||||
|
/// Stable unique id.
|
||||||
|
final String id;
|
||||||
|
|
||||||
|
/// Display title.
|
||||||
|
String title;
|
||||||
|
|
||||||
|
/// Store name, if known.
|
||||||
|
String? store;
|
||||||
|
|
||||||
|
/// Sale date.
|
||||||
|
DateTime date;
|
||||||
|
|
||||||
|
/// Line items.
|
||||||
|
final List<Item> items;
|
||||||
|
|
||||||
|
/// Subset of the roster taking part in this receipt.
|
||||||
|
final Set<String> participantIds;
|
||||||
|
|
||||||
|
/// What was actually paid (incl. deposits) — used for the reconciliation
|
||||||
|
/// check. Null for manual receipts where the user never entered it.
|
||||||
|
int? paidTotalGr;
|
||||||
|
|
||||||
|
/// Sum of all item totals in grosze.
|
||||||
|
int get itemsTotalGr => items.fold(0, (s, i) => s + i.totalGr);
|
||||||
|
|
||||||
|
/// JSON representation for persistence.
|
||||||
|
Map<String, dynamic> toJson() => {
|
||||||
|
'id': id,
|
||||||
|
'title': title,
|
||||||
|
'store': store,
|
||||||
|
'date': date.toIso8601String(),
|
||||||
|
'items': items.map((i) => i.toJson()).toList(),
|
||||||
|
'participantIds': participantIds.toList(),
|
||||||
|
'paidTotalGr': paidTotalGr,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Simple unique id source: time + counter. Good enough for a local,
|
||||||
|
/// single-user app.
|
||||||
|
class Ids {
|
||||||
|
static int _c = 0;
|
||||||
|
|
||||||
|
/// Returns a new unique id.
|
||||||
|
static String next() =>
|
||||||
|
'${DateTime.now().microsecondsSinceEpoch.toRadixString(36)}'
|
||||||
|
'-${(_c++).toRadixString(36)}';
|
||||||
|
}
|
||||||
124
billsplit/lib/domain/eparagon_fields.dart
Normal file
124
billsplit/lib/domain/eparagon_fields.dart
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
/// Field extraction helpers for the e-paragon parser.
|
||||||
|
///
|
||||||
|
/// Split out of eparagon_parser.dart to keep it under the 250-line cap.
|
||||||
|
/// A `part` because Dart privates are library-scoped.
|
||||||
|
|
||||||
|
part of 'eparagon_parser.dart';
|
||||||
|
|
||||||
|
DateTime _saleDate(
|
||||||
|
Map<dynamic, dynamic> paragon,
|
||||||
|
Map<dynamic, dynamic> dokument,
|
||||||
|
) {
|
||||||
|
final raw =
|
||||||
|
paragon['zakSprzed'] ?? (dokument['naglowek'] as Map?)?['dataJPK'];
|
||||||
|
if (raw is String) {
|
||||||
|
final d = DateTime.tryParse(raw);
|
||||||
|
if (d != null) return d.toLocal();
|
||||||
|
}
|
||||||
|
return DateTime.now();
|
||||||
|
}
|
||||||
|
|
||||||
|
String _iso(DateTime d) {
|
||||||
|
final m = d.month.toString().padLeft(2, '0');
|
||||||
|
final day = d.day.toString().padLeft(2, '0');
|
||||||
|
return '${d.year}-$m-$day';
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, dynamic> _extractPayload(String source) {
|
||||||
|
dynamic root;
|
||||||
|
try {
|
||||||
|
root = jsonDecode(source.trim());
|
||||||
|
} on FormatException {
|
||||||
|
// Maybe the user pasted just the JWS token.
|
||||||
|
final p = _tryJws(source.trim());
|
||||||
|
if (p != null) return p;
|
||||||
|
throw EParagonParseException(
|
||||||
|
'Input is neither valid JSON nor a JWS.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (root is Map) {
|
||||||
|
if (root['dokument'] is Map) return root.cast<String, dynamic>();
|
||||||
|
final data = root['data'];
|
||||||
|
if (data is String) {
|
||||||
|
final p = _tryJws(data);
|
||||||
|
if (p != null) return p;
|
||||||
|
throw EParagonParseException('"data" field is not a decodable JWS.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw EParagonParseException(
|
||||||
|
'Unrecognized structure: expected e-paragon wrapper or JPK payload.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, dynamic>? _tryJws(String token) {
|
||||||
|
final parts = token.split('.');
|
||||||
|
if (parts.length != 3) return null;
|
||||||
|
try {
|
||||||
|
final normalized = base64Url.normalize(parts[1]);
|
||||||
|
final decoded = utf8.decode(base64Url.decode(normalized));
|
||||||
|
final obj = jsonDecode(decoded);
|
||||||
|
return obj is Map ? obj.cast<String, dynamic>() : null;
|
||||||
|
} on FormatException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Item? _itemFromTowar(Map<String, dynamic> t, List<String> warnings) {
|
||||||
|
var name = (t['nazwa'] as String? ?? '').trim();
|
||||||
|
// Names end with the VAT rate letter, e.g. "Tonic A".
|
||||||
|
if (name.length > 2 &&
|
||||||
|
name[name.length - 2] == ' ' &&
|
||||||
|
RegExp(r'[A-G]$').hasMatch(name)) {
|
||||||
|
name = name.substring(0, name.length - 2).trim();
|
||||||
|
}
|
||||||
|
name = name.replaceAll(RegExp(r'\s+'), ' ');
|
||||||
|
if (name.isEmpty) return null;
|
||||||
|
|
||||||
|
final total = _asIntOrNull(t['brutto']);
|
||||||
|
if (total == null) {
|
||||||
|
warnings.add('Skipped "$name": missing brutto amount.');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
final unit = _asIntOrNull(t['cena']) ?? 0;
|
||||||
|
final qty = _parseQty(t['ilosc']);
|
||||||
|
final rabat = t['rabat'];
|
||||||
|
final discount = rabat is Map ? (_asIntOrNull(rabat['wart']) ?? 0) : 0;
|
||||||
|
|
||||||
|
final lower = name.toLowerCase();
|
||||||
|
var category = Categories.general;
|
||||||
|
var assignment = Assignment();
|
||||||
|
if (_alcoholKeywords.any(lower.contains)) {
|
||||||
|
category = Categories.alcohol;
|
||||||
|
assignment = Assignment(mode: AssignMode.drinkers);
|
||||||
|
} else if (_mixerKeywords.any(lower.contains)) {
|
||||||
|
category = Categories.mixer; // still split among everyone by default
|
||||||
|
}
|
||||||
|
|
||||||
|
return Item(
|
||||||
|
id: Ids.next(),
|
||||||
|
name: name,
|
||||||
|
totalGr: total,
|
||||||
|
qty: qty,
|
||||||
|
unitPriceGr: unit,
|
||||||
|
discountGr: discount,
|
||||||
|
category: category,
|
||||||
|
assignment: assignment,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
double _parseQty(dynamic v) {
|
||||||
|
if (v is num) return v.toDouble();
|
||||||
|
if (v is String) {
|
||||||
|
return double.tryParse(v.replaceAll(',', '.').trim()) ?? 1;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int _asInt(dynamic v) => _asIntOrNull(v) ?? 0;
|
||||||
|
|
||||||
|
int? _asIntOrNull(dynamic v) {
|
||||||
|
if (v is int) return v;
|
||||||
|
if (v is num) return v.round();
|
||||||
|
if (v is String) return int.tryParse(v);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
@ -14,6 +14,8 @@ import 'dart:convert';
|
|||||||
|
|
||||||
import 'package:billsplit/domain/models.dart';
|
import 'package:billsplit/domain/models.dart';
|
||||||
|
|
||||||
|
part 'eparagon_fields.dart';
|
||||||
|
|
||||||
/// A parsed receipt plus non-fatal [warnings] collected on the way.
|
/// A parsed receipt plus non-fatal [warnings] collected on the way.
|
||||||
class EParagonResult {
|
class EParagonResult {
|
||||||
/// Bundles a parsed [receipt] with its [warnings].
|
/// Bundles a parsed [receipt] with its [warnings].
|
||||||
@ -173,121 +175,3 @@ String? _storeName(Map<dynamic, dynamic> dokument) {
|
|||||||
if (name.toUpperCase().contains('JERONIMO MARTINS')) return 'Biedronka';
|
if (name.toUpperCase().contains('JERONIMO MARTINS')) return 'Biedronka';
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
DateTime _saleDate(
|
|
||||||
Map<dynamic, dynamic> paragon,
|
|
||||||
Map<dynamic, dynamic> dokument,
|
|
||||||
) {
|
|
||||||
final raw =
|
|
||||||
paragon['zakSprzed'] ?? (dokument['naglowek'] as Map?)?['dataJPK'];
|
|
||||||
if (raw is String) {
|
|
||||||
final d = DateTime.tryParse(raw);
|
|
||||||
if (d != null) return d.toLocal();
|
|
||||||
}
|
|
||||||
return DateTime.now();
|
|
||||||
}
|
|
||||||
|
|
||||||
String _iso(DateTime d) {
|
|
||||||
final m = d.month.toString().padLeft(2, '0');
|
|
||||||
final day = d.day.toString().padLeft(2, '0');
|
|
||||||
return '${d.year}-$m-$day';
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<String, dynamic> _extractPayload(String source) {
|
|
||||||
dynamic root;
|
|
||||||
try {
|
|
||||||
root = jsonDecode(source.trim());
|
|
||||||
} on FormatException {
|
|
||||||
// Maybe the user pasted just the JWS token.
|
|
||||||
final p = _tryJws(source.trim());
|
|
||||||
if (p != null) return p;
|
|
||||||
throw EParagonParseException(
|
|
||||||
'Input is neither valid JSON nor a JWS.',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (root is Map) {
|
|
||||||
if (root['dokument'] is Map) return root.cast<String, dynamic>();
|
|
||||||
final data = root['data'];
|
|
||||||
if (data is String) {
|
|
||||||
final p = _tryJws(data);
|
|
||||||
if (p != null) return p;
|
|
||||||
throw EParagonParseException('"data" field is not a decodable JWS.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw EParagonParseException(
|
|
||||||
'Unrecognized structure: expected e-paragon wrapper or JPK payload.',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<String, dynamic>? _tryJws(String token) {
|
|
||||||
final parts = token.split('.');
|
|
||||||
if (parts.length != 3) return null;
|
|
||||||
try {
|
|
||||||
final normalized = base64Url.normalize(parts[1]);
|
|
||||||
final decoded = utf8.decode(base64Url.decode(normalized));
|
|
||||||
final obj = jsonDecode(decoded);
|
|
||||||
return obj is Map ? obj.cast<String, dynamic>() : null;
|
|
||||||
} on FormatException {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Item? _itemFromTowar(Map<String, dynamic> t, List<String> warnings) {
|
|
||||||
var name = (t['nazwa'] as String? ?? '').trim();
|
|
||||||
// Names end with the VAT rate letter, e.g. "Tonic A".
|
|
||||||
if (name.length > 2 &&
|
|
||||||
name[name.length - 2] == ' ' &&
|
|
||||||
RegExp(r'[A-G]$').hasMatch(name)) {
|
|
||||||
name = name.substring(0, name.length - 2).trim();
|
|
||||||
}
|
|
||||||
name = name.replaceAll(RegExp(r'\s+'), ' ');
|
|
||||||
if (name.isEmpty) return null;
|
|
||||||
|
|
||||||
final total = _asIntOrNull(t['brutto']);
|
|
||||||
if (total == null) {
|
|
||||||
warnings.add('Skipped "$name": missing brutto amount.');
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
final unit = _asIntOrNull(t['cena']) ?? 0;
|
|
||||||
final qty = _parseQty(t['ilosc']);
|
|
||||||
final rabat = t['rabat'];
|
|
||||||
final discount = rabat is Map ? (_asIntOrNull(rabat['wart']) ?? 0) : 0;
|
|
||||||
|
|
||||||
final lower = name.toLowerCase();
|
|
||||||
var category = Categories.general;
|
|
||||||
var assignment = Assignment();
|
|
||||||
if (_alcoholKeywords.any(lower.contains)) {
|
|
||||||
category = Categories.alcohol;
|
|
||||||
assignment = Assignment(mode: AssignMode.drinkers);
|
|
||||||
} else if (_mixerKeywords.any(lower.contains)) {
|
|
||||||
category = Categories.mixer; // still split among everyone by default
|
|
||||||
}
|
|
||||||
|
|
||||||
return Item(
|
|
||||||
id: Ids.next(),
|
|
||||||
name: name,
|
|
||||||
totalGr: total,
|
|
||||||
qty: qty,
|
|
||||||
unitPriceGr: unit,
|
|
||||||
discountGr: discount,
|
|
||||||
category: category,
|
|
||||||
assignment: assignment,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
double _parseQty(dynamic v) {
|
|
||||||
if (v is num) return v.toDouble();
|
|
||||||
if (v is String) {
|
|
||||||
return double.tryParse(v.replaceAll(',', '.').trim()) ?? 1;
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int _asInt(dynamic v) => _asIntOrNull(v) ?? 0;
|
|
||||||
|
|
||||||
int? _asIntOrNull(dynamic v) {
|
|
||||||
if (v is int) return v;
|
|
||||||
if (v is num) return v.round();
|
|
||||||
if (v is String) return int.tryParse(v);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|||||||
@ -2,6 +2,8 @@
|
|||||||
/// floating point drift; formatting happens only at the UI/export edge.
|
/// floating point drift; formatting happens only at the UI/export edge.
|
||||||
library;
|
library;
|
||||||
|
|
||||||
|
part 'assignment.dart';
|
||||||
|
|
||||||
/// Formats [grosze] as a Polish złoty string, e.g. `1234` → `12,34 zł`.
|
/// Formats [grosze] as a Polish złoty string, e.g. `1234` → `12,34 zł`.
|
||||||
String formatPln(int grosze) {
|
String formatPln(int grosze) {
|
||||||
final neg = grosze < 0;
|
final neg = grosze < 0;
|
||||||
@ -82,197 +84,3 @@ enum AssignMode {
|
|||||||
/// An explicit hand-picked set of people shares the item.
|
/// An explicit hand-picked set of people shares the item.
|
||||||
custom,
|
custom,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The assignment of one item: a symbolic mode plus mode-specific data.
|
|
||||||
class Assignment {
|
|
||||||
/// Creates an assignment; defaults to [AssignMode.everyone].
|
|
||||||
Assignment({
|
|
||||||
this.mode = AssignMode.everyone,
|
|
||||||
this.groupId,
|
|
||||||
Set<String>? customIds,
|
|
||||||
}) : customIds = customIds ?? <String>{};
|
|
||||||
|
|
||||||
/// Restores an assignment from its [toJson] map.
|
|
||||||
factory Assignment.fromJson(Map<String, dynamic> j) => Assignment(
|
|
||||||
mode: AssignMode.values.asNameMap()[j['mode']] ?? AssignMode.everyone,
|
|
||||||
groupId: j['groupId'] as String?,
|
|
||||||
customIds: (j['customIds'] as List? ?? const <dynamic>[])
|
|
||||||
.cast<String>()
|
|
||||||
.toSet(),
|
|
||||||
);
|
|
||||||
|
|
||||||
/// Current mode.
|
|
||||||
AssignMode mode;
|
|
||||||
|
|
||||||
/// Group id when [mode] is [AssignMode.group].
|
|
||||||
String? groupId;
|
|
||||||
|
|
||||||
/// Hand-picked person ids when [mode] is [AssignMode.custom].
|
|
||||||
final Set<String> customIds;
|
|
||||||
|
|
||||||
/// JSON representation for persistence.
|
|
||||||
Map<String, dynamic> toJson() => {
|
|
||||||
'mode': mode.name,
|
|
||||||
if (groupId != null) 'groupId': groupId,
|
|
||||||
'customIds': customIds.toList(),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Standard item categories. [Item.category] is free text so users can add
|
|
||||||
/// their own; these constants are the ones the importer produces.
|
|
||||||
abstract final class Categories {
|
|
||||||
/// Alcoholic drinks — default to drinkers only.
|
|
||||||
static const alcohol = 'Alcohol';
|
|
||||||
|
|
||||||
/// Mixers (tonic, limes, …) — tagged for visibility, split among everyone.
|
|
||||||
static const mixer = 'Mixer';
|
|
||||||
|
|
||||||
/// Returnable bottle deposits (kaucja).
|
|
||||||
static const deposit = 'Deposit';
|
|
||||||
|
|
||||||
/// Anything else.
|
|
||||||
static const general = 'General';
|
|
||||||
}
|
|
||||||
|
|
||||||
/// One receipt line.
|
|
||||||
class Item {
|
|
||||||
/// Creates an item; [totalGr] is the post-discount line total.
|
|
||||||
Item({
|
|
||||||
required this.id,
|
|
||||||
required this.name,
|
|
||||||
required this.totalGr,
|
|
||||||
this.qty = 1,
|
|
||||||
this.unitPriceGr = 0,
|
|
||||||
this.discountGr = 0,
|
|
||||||
this.category = Categories.general,
|
|
||||||
Assignment? assignment,
|
|
||||||
}) : assignment = assignment ?? Assignment();
|
|
||||||
|
|
||||||
/// Restores an item from its [toJson] map.
|
|
||||||
factory Item.fromJson(Map<String, dynamic> j) => Item(
|
|
||||||
id: j['id'] as String,
|
|
||||||
name: j['name'] as String,
|
|
||||||
totalGr: j['totalGr'] as int,
|
|
||||||
qty: (j['qty'] as num? ?? 1).toDouble(),
|
|
||||||
unitPriceGr: j['unitPriceGr'] as int? ?? 0,
|
|
||||||
discountGr: j['discountGr'] as int? ?? 0,
|
|
||||||
category: j['category'] as String? ?? Categories.general,
|
|
||||||
assignment: Assignment.fromJson(
|
|
||||||
(j['assignment'] as Map?)?.cast<String, dynamic>() ?? const {},
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
/// Stable unique id.
|
|
||||||
final String id;
|
|
||||||
|
|
||||||
/// Display name.
|
|
||||||
String name;
|
|
||||||
|
|
||||||
/// Final line total after discount — the ground truth used for splitting.
|
|
||||||
int totalGr;
|
|
||||||
|
|
||||||
/// Quantity (may be fractional for weighed goods).
|
|
||||||
double qty;
|
|
||||||
|
|
||||||
/// Unit price in grosze.
|
|
||||||
int unitPriceGr;
|
|
||||||
|
|
||||||
/// Discount already included in [totalGr]; kept for display (<= 0).
|
|
||||||
int discountGr;
|
|
||||||
|
|
||||||
/// Free-text category; see [Categories] for the standard values.
|
|
||||||
String category;
|
|
||||||
|
|
||||||
/// Who shares this item.
|
|
||||||
Assignment assignment;
|
|
||||||
|
|
||||||
/// JSON representation for persistence.
|
|
||||||
Map<String, dynamic> toJson() => {
|
|
||||||
'id': id,
|
|
||||||
'name': name,
|
|
||||||
'totalGr': totalGr,
|
|
||||||
'qty': qty,
|
|
||||||
'unitPriceGr': unitPriceGr,
|
|
||||||
'discountGr': discountGr,
|
|
||||||
'category': category,
|
|
||||||
'assignment': assignment.toJson(),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/// One shopping receipt with its items and participants.
|
|
||||||
class Receipt {
|
|
||||||
/// Creates a receipt; [participantIds] defaults to empty.
|
|
||||||
Receipt({
|
|
||||||
required this.id,
|
|
||||||
required this.title,
|
|
||||||
this.store,
|
|
||||||
DateTime? date,
|
|
||||||
List<Item>? items,
|
|
||||||
Set<String>? participantIds,
|
|
||||||
this.paidTotalGr,
|
|
||||||
}) : date = date ?? DateTime.now(),
|
|
||||||
items = items ?? <Item>[],
|
|
||||||
participantIds = participantIds ?? <String>{};
|
|
||||||
|
|
||||||
/// Restores a receipt from its [toJson] map.
|
|
||||||
factory Receipt.fromJson(Map<String, dynamic> j) => Receipt(
|
|
||||||
id: j['id'] as String,
|
|
||||||
title: j['title'] as String,
|
|
||||||
store: j['store'] as String?,
|
|
||||||
date: DateTime.tryParse(j['date'] as String? ?? '') ?? DateTime.now(),
|
|
||||||
items: (j['items'] as List? ?? const <dynamic>[])
|
|
||||||
.map((e) => Item.fromJson((e as Map).cast<String, dynamic>()))
|
|
||||||
.toList(),
|
|
||||||
participantIds: (j['participantIds'] as List? ?? const <dynamic>[])
|
|
||||||
.cast<String>()
|
|
||||||
.toSet(),
|
|
||||||
paidTotalGr: j['paidTotalGr'] as int?,
|
|
||||||
);
|
|
||||||
|
|
||||||
/// Stable unique id.
|
|
||||||
final String id;
|
|
||||||
|
|
||||||
/// Display title.
|
|
||||||
String title;
|
|
||||||
|
|
||||||
/// Store name, if known.
|
|
||||||
String? store;
|
|
||||||
|
|
||||||
/// Sale date.
|
|
||||||
DateTime date;
|
|
||||||
|
|
||||||
/// Line items.
|
|
||||||
final List<Item> items;
|
|
||||||
|
|
||||||
/// Subset of the roster taking part in this receipt.
|
|
||||||
final Set<String> participantIds;
|
|
||||||
|
|
||||||
/// What was actually paid (incl. deposits) — used for the reconciliation
|
|
||||||
/// check. Null for manual receipts where the user never entered it.
|
|
||||||
int? paidTotalGr;
|
|
||||||
|
|
||||||
/// Sum of all item totals in grosze.
|
|
||||||
int get itemsTotalGr => items.fold(0, (s, i) => s + i.totalGr);
|
|
||||||
|
|
||||||
/// JSON representation for persistence.
|
|
||||||
Map<String, dynamic> toJson() => {
|
|
||||||
'id': id,
|
|
||||||
'title': title,
|
|
||||||
'store': store,
|
|
||||||
'date': date.toIso8601String(),
|
|
||||||
'items': items.map((i) => i.toJson()).toList(),
|
|
||||||
'participantIds': participantIds.toList(),
|
|
||||||
'paidTotalGr': paidTotalGr,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Simple unique id source: time + counter. Good enough for a local,
|
|
||||||
/// single-user app.
|
|
||||||
class Ids {
|
|
||||||
static int _c = 0;
|
|
||||||
|
|
||||||
/// Returns a new unique id.
|
|
||||||
static String next() =>
|
|
||||||
'${DateTime.now().microsecondsSinceEpoch.toRadixString(36)}'
|
|
||||||
'-${(_c++).toRadixString(36)}';
|
|
||||||
}
|
|
||||||
|
|||||||
@ -4,6 +4,8 @@ import 'package:design_system/design_system.dart';
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
|
part 'people_tiles.dart';
|
||||||
|
|
||||||
/// Roster management: people with a drinks toggle, plus custom groups.
|
/// Roster management: people with a drinks toggle, plus custom groups.
|
||||||
class PeopleScreen extends StatelessWidget {
|
class PeopleScreen extends StatelessWidget {
|
||||||
/// Creates the people screen.
|
/// Creates the people screen.
|
||||||
@ -131,136 +133,3 @@ class PeopleScreen extends StatelessWidget {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class _PersonTile extends StatelessWidget {
|
|
||||||
const _PersonTile({required this.person});
|
|
||||||
final Person person;
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
final state = context.read<AppState>();
|
|
||||||
return ListTile(
|
|
||||||
leading: CircleAvatar(
|
|
||||||
child: Text(
|
|
||||||
person.name.isEmpty ? '?' : person.name.substring(0, 1).toUpperCase(),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
title: Text(person.name),
|
|
||||||
subtitle: Wrap(
|
|
||||||
spacing: 6,
|
|
||||||
children: [
|
|
||||||
for (final g in state.groups.where(
|
|
||||||
(g) => g.memberIds.contains(person.id),
|
|
||||||
))
|
|
||||||
Chip(
|
|
||||||
label: Text(g.name, style: const TextStyle(fontSize: 11)),
|
|
||||||
visualDensity: VisualDensity.compact,
|
|
||||||
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
trailing: Row(
|
|
||||||
mainAxisSize: MainAxisSize.min,
|
|
||||||
children: [
|
|
||||||
Tooltip(
|
|
||||||
message: 'Drinks alcohol',
|
|
||||||
child: Switch(
|
|
||||||
value: person.drinks,
|
|
||||||
onChanged: (v) => state.mutate(() => person.drinks = v),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
IconButton(
|
|
||||||
icon: const Icon(Icons.delete_outline),
|
|
||||||
onPressed: () => _confirmRemove(context, state),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
onTap: () => _editMembership(context, state),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _confirmRemove(BuildContext context, AppState state) async {
|
|
||||||
final ok = await showDialog<bool>(
|
|
||||||
context: context,
|
|
||||||
builder: (c) => AlertDialog(
|
|
||||||
title: Text('Remove ${person.name}?'),
|
|
||||||
content: const Text(
|
|
||||||
'They will be removed from all receipts and item assignments.',
|
|
||||||
),
|
|
||||||
actions: [
|
|
||||||
TextButton(
|
|
||||||
onPressed: () => Navigator.pop(c, false),
|
|
||||||
child: const Text('Cancel'),
|
|
||||||
),
|
|
||||||
FilledButton(
|
|
||||||
onPressed: () => Navigator.pop(c, true),
|
|
||||||
child: const Text('Remove'),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
if (ok == true) state.removePerson(person);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _editMembership(BuildContext context, AppState state) async {
|
|
||||||
if (state.groups.isEmpty) return;
|
|
||||||
await showModalBottomSheet<void>(
|
|
||||||
context: context,
|
|
||||||
builder: (c) => StatefulBuilder(
|
|
||||||
builder: (c, setState) => SafeArea(
|
|
||||||
child: ListView(
|
|
||||||
shrinkWrap: true,
|
|
||||||
children: [
|
|
||||||
Padding(
|
|
||||||
padding: const EdgeInsets.all(16),
|
|
||||||
child: Text(
|
|
||||||
'${person.name} — groups',
|
|
||||||
style: Theme.of(c).textTheme.titleMedium,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
for (final g in state.groups)
|
|
||||||
CheckboxListTile(
|
|
||||||
title: Text(g.name),
|
|
||||||
value: g.memberIds.contains(person.id),
|
|
||||||
onChanged: (v) {
|
|
||||||
state.mutate(() {
|
|
||||||
if (v == true) {
|
|
||||||
g.memberIds.add(person.id);
|
|
||||||
} else {
|
|
||||||
g.memberIds.remove(person.id);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
setState(() {});
|
|
||||||
},
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class _GroupTile extends StatelessWidget {
|
|
||||||
const _GroupTile({required this.group});
|
|
||||||
final Group group;
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
final state = context.read<AppState>();
|
|
||||||
final names = state.people
|
|
||||||
.where((p) => group.memberIds.contains(p.id))
|
|
||||||
.map((p) => p.name)
|
|
||||||
.join(', ');
|
|
||||||
return ListTile(
|
|
||||||
leading: const Icon(Icons.label_outline),
|
|
||||||
title: Text(group.name),
|
|
||||||
subtitle:
|
|
||||||
Text(names.isEmpty ? 'No members — tap a person to add' : names),
|
|
||||||
trailing: IconButton(
|
|
||||||
icon: const Icon(Icons.delete_outline),
|
|
||||||
onPressed: () => state.removeGroup(group),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
139
billsplit/lib/ui/people_tiles.dart
Normal file
139
billsplit/lib/ui/people_tiles.dart
Normal file
@ -0,0 +1,139 @@
|
|||||||
|
/// Person and group tile widgets.
|
||||||
|
///
|
||||||
|
/// Split out of people_screen.dart to keep it under the 250-line cap. A `part`
|
||||||
|
/// because Dart privates are library-scoped.
|
||||||
|
|
||||||
|
part of 'people_screen.dart';
|
||||||
|
|
||||||
|
class _PersonTile extends StatelessWidget {
|
||||||
|
const _PersonTile({required this.person});
|
||||||
|
final Person person;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final state = context.read<AppState>();
|
||||||
|
return ListTile(
|
||||||
|
leading: CircleAvatar(
|
||||||
|
child: Text(
|
||||||
|
person.name.isEmpty ? '?' : person.name.substring(0, 1).toUpperCase(),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
title: Text(person.name),
|
||||||
|
subtitle: Wrap(
|
||||||
|
spacing: 6,
|
||||||
|
children: [
|
||||||
|
for (final g in state.groups.where(
|
||||||
|
(g) => g.memberIds.contains(person.id),
|
||||||
|
))
|
||||||
|
Chip(
|
||||||
|
label: Text(g.name, style: const TextStyle(fontSize: 11)),
|
||||||
|
visualDensity: VisualDensity.compact,
|
||||||
|
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
trailing: Row(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
Tooltip(
|
||||||
|
message: 'Drinks alcohol',
|
||||||
|
child: Switch(
|
||||||
|
value: person.drinks,
|
||||||
|
onChanged: (v) => state.mutate(() => person.drinks = v),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
IconButton(
|
||||||
|
icon: const Icon(Icons.delete_outline),
|
||||||
|
onPressed: () => _confirmRemove(context, state),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
onTap: () => _editMembership(context, state),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _confirmRemove(BuildContext context, AppState state) async {
|
||||||
|
final ok = await showDialog<bool>(
|
||||||
|
context: context,
|
||||||
|
builder: (c) => AlertDialog(
|
||||||
|
title: Text('Remove ${person.name}?'),
|
||||||
|
content: const Text(
|
||||||
|
'They will be removed from all receipts and item assignments.',
|
||||||
|
),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.pop(c, false),
|
||||||
|
child: const Text('Cancel'),
|
||||||
|
),
|
||||||
|
FilledButton(
|
||||||
|
onPressed: () => Navigator.pop(c, true),
|
||||||
|
child: const Text('Remove'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
if (ok == true) state.removePerson(person);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _editMembership(BuildContext context, AppState state) async {
|
||||||
|
if (state.groups.isEmpty) return;
|
||||||
|
await showModalBottomSheet<void>(
|
||||||
|
context: context,
|
||||||
|
builder: (c) => StatefulBuilder(
|
||||||
|
builder: (c, setState) => SafeArea(
|
||||||
|
child: ListView(
|
||||||
|
shrinkWrap: true,
|
||||||
|
children: [
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.all(16),
|
||||||
|
child: Text(
|
||||||
|
'${person.name} — groups',
|
||||||
|
style: Theme.of(c).textTheme.titleMedium,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
for (final g in state.groups)
|
||||||
|
CheckboxListTile(
|
||||||
|
title: Text(g.name),
|
||||||
|
value: g.memberIds.contains(person.id),
|
||||||
|
onChanged: (v) {
|
||||||
|
state.mutate(() {
|
||||||
|
if (v == true) {
|
||||||
|
g.memberIds.add(person.id);
|
||||||
|
} else {
|
||||||
|
g.memberIds.remove(person.id);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
setState(() {});
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _GroupTile extends StatelessWidget {
|
||||||
|
const _GroupTile({required this.group});
|
||||||
|
final Group group;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final state = context.read<AppState>();
|
||||||
|
final names = state.people
|
||||||
|
.where((p) => group.memberIds.contains(p.id))
|
||||||
|
.map((p) => p.name)
|
||||||
|
.join(', ');
|
||||||
|
return ListTile(
|
||||||
|
leading: const Icon(Icons.label_outline),
|
||||||
|
title: Text(group.name),
|
||||||
|
subtitle:
|
||||||
|
Text(names.isEmpty ? 'No members — tap a person to add' : names),
|
||||||
|
trailing: IconButton(
|
||||||
|
icon: const Icon(Icons.delete_outline),
|
||||||
|
onPressed: () => state.removeGroup(group),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
198
billsplit/lib/ui/receipt_items.dart
Normal file
198
billsplit/lib/ui/receipt_items.dart
Normal file
@ -0,0 +1,198 @@
|
|||||||
|
/// The receipt item list and its row widgets.
|
||||||
|
///
|
||||||
|
/// Split out of receipt_screen.dart to keep it under the 250-line cap. A `part`
|
||||||
|
/// because Dart privates are library-scoped.
|
||||||
|
|
||||||
|
part of 'receipt_screen.dart';
|
||||||
|
|
||||||
|
class _ItemsList extends StatelessWidget {
|
||||||
|
const _ItemsList({required this.receipt, required this.split});
|
||||||
|
final Receipt receipt;
|
||||||
|
final SplitResult split;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final state = context.read<AppState>();
|
||||||
|
if (receipt.items.isEmpty) {
|
||||||
|
return const Center(child: Text('No items — add with +.'));
|
||||||
|
}
|
||||||
|
return ListView.builder(
|
||||||
|
padding: const EdgeInsets.only(bottom: 96),
|
||||||
|
itemCount: receipt.items.length,
|
||||||
|
itemBuilder: (context, i) {
|
||||||
|
final item = receipt.items[i];
|
||||||
|
final people =
|
||||||
|
resolveParticipants(item, receipt, state.people, state.groups);
|
||||||
|
final unassigned = people.isEmpty;
|
||||||
|
return ListTile(
|
||||||
|
dense: true,
|
||||||
|
leading: _CategoryDot(category: item.category),
|
||||||
|
title: Text(item.name),
|
||||||
|
subtitle: Text(
|
||||||
|
_assignmentLabel(item, people.length, state),
|
||||||
|
style: TextStyle(
|
||||||
|
color: unassigned ? Theme.of(context).colorScheme.error : null,
|
||||||
|
fontWeight: unassigned ? FontWeight.bold : null,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
trailing: Text(formatPln(item.totalGr)),
|
||||||
|
onTap: () => showAssignmentSheet(context, state, receipt, item),
|
||||||
|
onLongPress: () => showItemEditDialog(context, state, receipt, item),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
String _assignmentLabel(Item item, int n, AppState state) {
|
||||||
|
switch (item.assignment.mode) {
|
||||||
|
case AssignMode.everyone:
|
||||||
|
return 'Everyone ($n)';
|
||||||
|
case AssignMode.drinkers:
|
||||||
|
return 'Drinkers only ($n)';
|
||||||
|
case AssignMode.nonDrinkers:
|
||||||
|
return 'Non-drinkers ($n)';
|
||||||
|
case AssignMode.group:
|
||||||
|
final g = state.groups
|
||||||
|
.where((g) => g.id == item.assignment.groupId)
|
||||||
|
.firstOrNull;
|
||||||
|
return 'Group: ${g?.name ?? '?'} ($n)';
|
||||||
|
case AssignMode.custom:
|
||||||
|
return n == 0 ? 'UNASSIGNED — tap to fix' : 'Selected people ($n)';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _CategoryDot extends StatelessWidget {
|
||||||
|
const _CategoryDot({required this.category});
|
||||||
|
final String category;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final color = switch (category) {
|
||||||
|
// Categorical encoding, deliberately NOT mapped onto ColorScheme slots.
|
||||||
|
// These four say "which category", not "which theme role", and a
|
||||||
|
// single-accent design system has no four distinct hues to lend. Folding
|
||||||
|
// them into primary/secondary/tertiary would make the categories stop
|
||||||
|
// being distinguishable, which is the only job they have. Tracked in
|
||||||
|
// unified-design-system/nielsen-audit.md as needing a categorical ramp.
|
||||||
|
Categories.alcohol => Colors.deepOrange,
|
||||||
|
Categories.mixer => Colors.amber,
|
||||||
|
Categories.deposit => Colors.blueGrey,
|
||||||
|
_ => Colors.teal,
|
||||||
|
};
|
||||||
|
return CircleAvatar(radius: 6, backgroundColor: color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _TotalsView extends StatelessWidget {
|
||||||
|
const _TotalsView({required this.receipt, required this.split});
|
||||||
|
final Receipt receipt;
|
||||||
|
final SplitResult split;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final state = context.read<AppState>();
|
||||||
|
final people = receiptParticipants(receipt, state.people)
|
||||||
|
..sort((a, b) => a.name.toLowerCase().compareTo(b.name.toLowerCase()));
|
||||||
|
final itemsTotal = receipt.itemsTotalGr;
|
||||||
|
final assigned = split.assignedTotalGr;
|
||||||
|
final paid = receipt.paidTotalGr;
|
||||||
|
|
||||||
|
return ListView(
|
||||||
|
padding: const EdgeInsets.all(16),
|
||||||
|
children: [
|
||||||
|
if (split.unassigned.isNotEmpty)
|
||||||
|
Card(
|
||||||
|
color: Theme.of(context).colorScheme.errorContainer,
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.all(12),
|
||||||
|
child: Text(
|
||||||
|
'${split.unassigned.length} item(s) have nobody assigned and '
|
||||||
|
'are excluded from totals:\n'
|
||||||
|
'${split.unassigned.map((i) => '· ${i.name}').join('\n')}',
|
||||||
|
style: TextStyle(
|
||||||
|
color: Theme.of(context).colorScheme.onErrorContainer,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
for (final p in people)
|
||||||
|
_PersonRow(person: p, total: split.perPerson[p.id]),
|
||||||
|
const Divider(height: 32),
|
||||||
|
_kv(context, 'Items total', formatPln(itemsTotal), bold: true),
|
||||||
|
_kv(
|
||||||
|
context,
|
||||||
|
'Assigned to people',
|
||||||
|
formatPln(assigned),
|
||||||
|
warn: assigned != itemsTotal,
|
||||||
|
),
|
||||||
|
if (paid != null)
|
||||||
|
_kv(
|
||||||
|
context,
|
||||||
|
'Paid (receipt)',
|
||||||
|
formatPln(paid),
|
||||||
|
warn: paid != itemsTotal,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 8),
|
||||||
|
Text(
|
||||||
|
'Splits are grosz-exact: shares always add up to the item total, '
|
||||||
|
'so no fractional grosze are lost.',
|
||||||
|
style: Theme.of(context).textTheme.bodySmall,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget _kv(
|
||||||
|
BuildContext context,
|
||||||
|
String k,
|
||||||
|
String v, {
|
||||||
|
bool bold = false,
|
||||||
|
bool warn = false,
|
||||||
|
}) {
|
||||||
|
final style = TextStyle(
|
||||||
|
fontWeight: bold || warn ? FontWeight.bold : null,
|
||||||
|
color: warn ? Theme.of(context).colorScheme.error : null,
|
||||||
|
);
|
||||||
|
return Padding(
|
||||||
|
padding: const EdgeInsets.symmetric(vertical: 2),
|
||||||
|
child: Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||||
|
children: [Text(k, style: style), Text(v, style: style)],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _PersonRow extends StatelessWidget {
|
||||||
|
const _PersonRow({required this.person, required this.total});
|
||||||
|
final Person person;
|
||||||
|
final PersonTotal? total;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final t = total;
|
||||||
|
return ListTile(
|
||||||
|
contentPadding: EdgeInsets.zero,
|
||||||
|
leading: CircleAvatar(
|
||||||
|
child: Text(
|
||||||
|
person.name.isEmpty ? '?' : person.name.substring(0, 1).toUpperCase(),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
title: Text(person.name),
|
||||||
|
subtitle: t == null || t.alcoholGr == 0
|
||||||
|
? null
|
||||||
|
: Text(
|
||||||
|
'alcohol ${formatPln(t.alcoholGr)} · rest ${formatPln(t.restGr)}',
|
||||||
|
),
|
||||||
|
trailing: Text(
|
||||||
|
formatPln(t?.totalGr ?? 0),
|
||||||
|
style: Theme.of(context).textTheme.titleMedium,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension<T> on Iterable<T> {
|
||||||
|
T? get firstOrNull => isEmpty ? null : first;
|
||||||
|
}
|
||||||
@ -9,6 +9,8 @@ import 'package:flutter/foundation.dart';
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
|
part 'receipt_items.dart';
|
||||||
|
|
||||||
/// Writes exported [bytes] to [path]; the seam the export flow writes through.
|
/// Writes exported [bytes] to [path]; the seam the export flow writes through.
|
||||||
///
|
///
|
||||||
/// Kept injectable because `file_selector` only returns a destination — the
|
/// Kept injectable because `file_selector` only returns a destination — the
|
||||||
@ -207,195 +209,3 @@ class _ReceiptScreenState extends State<ReceiptScreen> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class _ItemsList extends StatelessWidget {
|
|
||||||
const _ItemsList({required this.receipt, required this.split});
|
|
||||||
final Receipt receipt;
|
|
||||||
final SplitResult split;
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
final state = context.read<AppState>();
|
|
||||||
if (receipt.items.isEmpty) {
|
|
||||||
return const Center(child: Text('No items — add with +.'));
|
|
||||||
}
|
|
||||||
return ListView.builder(
|
|
||||||
padding: const EdgeInsets.only(bottom: 96),
|
|
||||||
itemCount: receipt.items.length,
|
|
||||||
itemBuilder: (context, i) {
|
|
||||||
final item = receipt.items[i];
|
|
||||||
final people =
|
|
||||||
resolveParticipants(item, receipt, state.people, state.groups);
|
|
||||||
final unassigned = people.isEmpty;
|
|
||||||
return ListTile(
|
|
||||||
dense: true,
|
|
||||||
leading: _CategoryDot(category: item.category),
|
|
||||||
title: Text(item.name),
|
|
||||||
subtitle: Text(
|
|
||||||
_assignmentLabel(item, people.length, state),
|
|
||||||
style: TextStyle(
|
|
||||||
color: unassigned ? Theme.of(context).colorScheme.error : null,
|
|
||||||
fontWeight: unassigned ? FontWeight.bold : null,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
trailing: Text(formatPln(item.totalGr)),
|
|
||||||
onTap: () => showAssignmentSheet(context, state, receipt, item),
|
|
||||||
onLongPress: () => showItemEditDialog(context, state, receipt, item),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
String _assignmentLabel(Item item, int n, AppState state) {
|
|
||||||
switch (item.assignment.mode) {
|
|
||||||
case AssignMode.everyone:
|
|
||||||
return 'Everyone ($n)';
|
|
||||||
case AssignMode.drinkers:
|
|
||||||
return 'Drinkers only ($n)';
|
|
||||||
case AssignMode.nonDrinkers:
|
|
||||||
return 'Non-drinkers ($n)';
|
|
||||||
case AssignMode.group:
|
|
||||||
final g = state.groups
|
|
||||||
.where((g) => g.id == item.assignment.groupId)
|
|
||||||
.firstOrNull;
|
|
||||||
return 'Group: ${g?.name ?? '?'} ($n)';
|
|
||||||
case AssignMode.custom:
|
|
||||||
return n == 0 ? 'UNASSIGNED — tap to fix' : 'Selected people ($n)';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class _CategoryDot extends StatelessWidget {
|
|
||||||
const _CategoryDot({required this.category});
|
|
||||||
final String category;
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
final color = switch (category) {
|
|
||||||
// Categorical encoding, deliberately NOT mapped onto ColorScheme slots.
|
|
||||||
// These four say "which category", not "which theme role", and a
|
|
||||||
// single-accent design system has no four distinct hues to lend. Folding
|
|
||||||
// them into primary/secondary/tertiary would make the categories stop
|
|
||||||
// being distinguishable, which is the only job they have. Tracked in
|
|
||||||
// unified-design-system/nielsen-audit.md as needing a categorical ramp.
|
|
||||||
Categories.alcohol => Colors.deepOrange,
|
|
||||||
Categories.mixer => Colors.amber,
|
|
||||||
Categories.deposit => Colors.blueGrey,
|
|
||||||
_ => Colors.teal,
|
|
||||||
};
|
|
||||||
return CircleAvatar(radius: 6, backgroundColor: color);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class _TotalsView extends StatelessWidget {
|
|
||||||
const _TotalsView({required this.receipt, required this.split});
|
|
||||||
final Receipt receipt;
|
|
||||||
final SplitResult split;
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
final state = context.read<AppState>();
|
|
||||||
final people = receiptParticipants(receipt, state.people)
|
|
||||||
..sort((a, b) => a.name.toLowerCase().compareTo(b.name.toLowerCase()));
|
|
||||||
final itemsTotal = receipt.itemsTotalGr;
|
|
||||||
final assigned = split.assignedTotalGr;
|
|
||||||
final paid = receipt.paidTotalGr;
|
|
||||||
|
|
||||||
return ListView(
|
|
||||||
padding: const EdgeInsets.all(16),
|
|
||||||
children: [
|
|
||||||
if (split.unassigned.isNotEmpty)
|
|
||||||
Card(
|
|
||||||
color: Theme.of(context).colorScheme.errorContainer,
|
|
||||||
child: Padding(
|
|
||||||
padding: const EdgeInsets.all(12),
|
|
||||||
child: Text(
|
|
||||||
'${split.unassigned.length} item(s) have nobody assigned and '
|
|
||||||
'are excluded from totals:\n'
|
|
||||||
'${split.unassigned.map((i) => '· ${i.name}').join('\n')}',
|
|
||||||
style: TextStyle(
|
|
||||||
color: Theme.of(context).colorScheme.onErrorContainer,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
for (final p in people)
|
|
||||||
_PersonRow(person: p, total: split.perPerson[p.id]),
|
|
||||||
const Divider(height: 32),
|
|
||||||
_kv(context, 'Items total', formatPln(itemsTotal), bold: true),
|
|
||||||
_kv(
|
|
||||||
context,
|
|
||||||
'Assigned to people',
|
|
||||||
formatPln(assigned),
|
|
||||||
warn: assigned != itemsTotal,
|
|
||||||
),
|
|
||||||
if (paid != null)
|
|
||||||
_kv(
|
|
||||||
context,
|
|
||||||
'Paid (receipt)',
|
|
||||||
formatPln(paid),
|
|
||||||
warn: paid != itemsTotal,
|
|
||||||
),
|
|
||||||
const SizedBox(height: 8),
|
|
||||||
Text(
|
|
||||||
'Splits are grosz-exact: shares always add up to the item total, '
|
|
||||||
'so no fractional grosze are lost.',
|
|
||||||
style: Theme.of(context).textTheme.bodySmall,
|
|
||||||
),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
Widget _kv(
|
|
||||||
BuildContext context,
|
|
||||||
String k,
|
|
||||||
String v, {
|
|
||||||
bool bold = false,
|
|
||||||
bool warn = false,
|
|
||||||
}) {
|
|
||||||
final style = TextStyle(
|
|
||||||
fontWeight: bold || warn ? FontWeight.bold : null,
|
|
||||||
color: warn ? Theme.of(context).colorScheme.error : null,
|
|
||||||
);
|
|
||||||
return Padding(
|
|
||||||
padding: const EdgeInsets.symmetric(vertical: 2),
|
|
||||||
child: Row(
|
|
||||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
||||||
children: [Text(k, style: style), Text(v, style: style)],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class _PersonRow extends StatelessWidget {
|
|
||||||
const _PersonRow({required this.person, required this.total});
|
|
||||||
final Person person;
|
|
||||||
final PersonTotal? total;
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
final t = total;
|
|
||||||
return ListTile(
|
|
||||||
contentPadding: EdgeInsets.zero,
|
|
||||||
leading: CircleAvatar(
|
|
||||||
child: Text(
|
|
||||||
person.name.isEmpty ? '?' : person.name.substring(0, 1).toUpperCase(),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
title: Text(person.name),
|
|
||||||
subtitle: t == null || t.alcoholGr == 0
|
|
||||||
? null
|
|
||||||
: Text(
|
|
||||||
'alcohol ${formatPln(t.alcoholGr)} · rest ${formatPln(t.restGr)}',
|
|
||||||
),
|
|
||||||
trailing: Text(
|
|
||||||
formatPln(t?.totalGr ?? 0),
|
|
||||||
style: Theme.of(context).textTheme.titleMedium,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension<T> on Iterable<T> {
|
|
||||||
T? get firstOrNull => isEmpty ? null : first;
|
|
||||||
}
|
|
||||||
|
|||||||
155
billsplit/test/people_flows_test.dart
Normal file
155
billsplit/test/people_flows_test.dart
Normal file
@ -0,0 +1,155 @@
|
|||||||
|
// People screen flows.
|
||||||
|
|
||||||
|
|
||||||
|
import 'package:billsplit/domain/models.dart';
|
||||||
|
import 'package:billsplit/state/app_state.dart';
|
||||||
|
import 'package:billsplit/ui/people_screen.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
|
||||||
|
import 'helpers.dart';
|
||||||
|
|
||||||
|
Receipt demoReceipt(AppState s, {bool withUnassigned = false}) {
|
||||||
|
final r = Receipt(
|
||||||
|
id: 'r1',
|
||||||
|
title: 'Demo',
|
||||||
|
store: 'Biedronka',
|
||||||
|
paidTotalGr: 1000,
|
||||||
|
items: [
|
||||||
|
Item(id: 'i1', name: 'Bread', totalGr: 500),
|
||||||
|
Item(
|
||||||
|
id: 'i2',
|
||||||
|
name: 'Beer',
|
||||||
|
totalGr: 300,
|
||||||
|
category: Categories.alcohol,
|
||||||
|
assignment: Assignment(mode: AssignMode.drinkers),
|
||||||
|
),
|
||||||
|
Item(
|
||||||
|
id: 'i3',
|
||||||
|
name: 'Tonic',
|
||||||
|
totalGr: 100,
|
||||||
|
category: Categories.mixer,
|
||||||
|
),
|
||||||
|
Item(
|
||||||
|
id: 'i4',
|
||||||
|
name: 'Kaucja',
|
||||||
|
totalGr: 100,
|
||||||
|
category: Categories.deposit,
|
||||||
|
),
|
||||||
|
if (withUnassigned)
|
||||||
|
Item(
|
||||||
|
id: 'i5',
|
||||||
|
name: 'Ghost',
|
||||||
|
totalGr: 50,
|
||||||
|
assignment: Assignment(mode: AssignMode.custom),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
s.addReceipt(r);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
|
||||||
|
group('people', () {
|
||||||
|
testWidgets('add person dialog: toggle, cancel, empty, valid',
|
||||||
|
(tester) async {
|
||||||
|
final s = tempState();
|
||||||
|
await pumpApp(tester, s, const PeopleScreen());
|
||||||
|
expect(find.textContaining('Nobody yet'), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.tap(find.text('Add person'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.byType(Switch));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.text('Cancel'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(s.people, isEmpty);
|
||||||
|
|
||||||
|
await tester.tap(find.text('Add person'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.text('Add'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(s.people, isEmpty);
|
||||||
|
|
||||||
|
await tester.tap(find.text('Add person'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.enterText(find.byType(TextField), 'Kasia');
|
||||||
|
await tester.tap(find.byType(Switch));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.text('Add'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(s.people.single.name, 'Kasia');
|
||||||
|
expect(s.people.single.drinks, isFalse);
|
||||||
|
await flushSaves(tester);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('drinks switch, membership early-return, delete flows',
|
||||||
|
(tester) async {
|
||||||
|
final s = tempState();
|
||||||
|
final p = s.addPerson('Ala');
|
||||||
|
await pumpApp(tester, s, const PeopleScreen());
|
||||||
|
await flushSaves(tester);
|
||||||
|
|
||||||
|
await tester.tap(find.byType(Switch).first);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(p.drinks, isFalse);
|
||||||
|
|
||||||
|
// No groups yet → tapping the person is a no-op branch.
|
||||||
|
await tester.tap(find.text('Ala'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text('Group membership'), findsNothing);
|
||||||
|
|
||||||
|
await tester.tap(find.byIcon(Icons.delete_outline).first);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.text('Cancel'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(s.people, hasLength(1));
|
||||||
|
|
||||||
|
await tester.tap(find.byIcon(Icons.delete_outline).first);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.text('Remove'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(s.people, isEmpty);
|
||||||
|
await flushSaves(tester);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('groups: add, membership sheet, delete', (tester) async {
|
||||||
|
final s = tempState()..addPerson('Ala');
|
||||||
|
await pumpApp(tester, s, const PeopleScreen());
|
||||||
|
await flushSaves(tester);
|
||||||
|
|
||||||
|
await tester.tap(find.text('Add group'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.text('Cancel'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(s.groups, isEmpty);
|
||||||
|
|
||||||
|
await tester.tap(find.text('Add group'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.enterText(find.byType(TextField), 'Meat');
|
||||||
|
await tester.tap(find.text('Add'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(s.groups.single.name, 'Meat');
|
||||||
|
expect(find.textContaining('No members'), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.tap(find.text('Ala'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.byType(CheckboxListTile), findsOneWidget);
|
||||||
|
await tester.tap(find.byType(CheckboxListTile));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(s.groups.single.memberIds, hasLength(1));
|
||||||
|
await tester.tap(find.byType(CheckboxListTile));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(s.groups.single.memberIds, isEmpty);
|
||||||
|
await tester.tapAt(const Offset(10, 10)); // close sheet
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
await tester.tap(find.byIcon(Icons.delete_outline).last);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(s.groups, isEmpty);
|
||||||
|
await flushSaves(tester);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
141
billsplit/test/receipt_flows_test.dart
Normal file
141
billsplit/test/receipt_flows_test.dart
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
// Receipt screen flows.
|
||||||
|
|
||||||
|
|
||||||
|
import 'package:billsplit/domain/models.dart';
|
||||||
|
import 'package:billsplit/state/app_state.dart';
|
||||||
|
import 'package:billsplit/ui/receipt_screen.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
|
||||||
|
import 'helpers.dart';
|
||||||
|
|
||||||
|
Receipt demoReceipt(AppState s, {bool withUnassigned = false}) {
|
||||||
|
final r = Receipt(
|
||||||
|
id: 'r1',
|
||||||
|
title: 'Demo',
|
||||||
|
store: 'Biedronka',
|
||||||
|
paidTotalGr: 1000,
|
||||||
|
items: [
|
||||||
|
Item(id: 'i1', name: 'Bread', totalGr: 500),
|
||||||
|
Item(
|
||||||
|
id: 'i2',
|
||||||
|
name: 'Beer',
|
||||||
|
totalGr: 300,
|
||||||
|
category: Categories.alcohol,
|
||||||
|
assignment: Assignment(mode: AssignMode.drinkers),
|
||||||
|
),
|
||||||
|
Item(
|
||||||
|
id: 'i3',
|
||||||
|
name: 'Tonic',
|
||||||
|
totalGr: 100,
|
||||||
|
category: Categories.mixer,
|
||||||
|
),
|
||||||
|
Item(
|
||||||
|
id: 'i4',
|
||||||
|
name: 'Kaucja',
|
||||||
|
totalGr: 100,
|
||||||
|
category: Categories.deposit,
|
||||||
|
),
|
||||||
|
if (withUnassigned)
|
||||||
|
Item(
|
||||||
|
id: 'i5',
|
||||||
|
name: 'Ghost',
|
||||||
|
totalGr: 50,
|
||||||
|
assignment: Assignment(mode: AssignMode.custom),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
s.addReceipt(r);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
|
||||||
|
group('receipt screen', () {
|
||||||
|
testWidgets('unknown id shows deleted note', (tester) async {
|
||||||
|
final s = tempState();
|
||||||
|
await pumpApp(tester, s, const ReceiptScreen(receiptId: 'nope'));
|
||||||
|
expect(find.text('Receipt deleted.'), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('tabs on narrow, panes on wide', (tester) async {
|
||||||
|
final s = tempState()..addPerson('Ala');
|
||||||
|
demoReceipt(s);
|
||||||
|
await pumpApp(tester, s, const ReceiptScreen(receiptId: 'r1'));
|
||||||
|
await flushSaves(tester);
|
||||||
|
expect(find.byType(TabBar), findsOneWidget);
|
||||||
|
await tester.tap(find.text('Totals'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.textContaining('grosz-exact'), findsOneWidget);
|
||||||
|
|
||||||
|
tester.view.physicalSize = const Size(1600, 900);
|
||||||
|
tester.view.devicePixelRatio = 1;
|
||||||
|
addTearDown(tester.view.reset);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.byType(TabBar), findsNothing);
|
||||||
|
expect(find.byType(VerticalDivider), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('participants: empty roster snackbar, then editing',
|
||||||
|
(tester) async {
|
||||||
|
final s = tempState();
|
||||||
|
demoReceipt(s);
|
||||||
|
await pumpApp(tester, s, const ReceiptScreen(receiptId: 'r1'));
|
||||||
|
await flushSaves(tester);
|
||||||
|
await tester.tap(find.byIcon(Icons.group));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.textContaining('Add people first'), findsOneWidget);
|
||||||
|
|
||||||
|
final p = s.addPerson('Ala');
|
||||||
|
s.addPerson('Bob');
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.byIcon(Icons.group));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text('Who is on this receipt?'), findsOneWidget);
|
||||||
|
await tester.tap(find.byType(CheckboxListTile).first);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(s.receipts.single.participantIds.contains(p.id), isTrue);
|
||||||
|
await tester.tap(find.byType(CheckboxListTile).first);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tapAt(const Offset(10, 10));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await flushSaves(tester);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('export: no participants, success, cancel, failure',
|
||||||
|
(tester) async {
|
||||||
|
final s = tempState();
|
||||||
|
demoReceipt(s);
|
||||||
|
await pumpApp(tester, s, const ReceiptScreen(receiptId: 'r1'));
|
||||||
|
await flushSaves(tester);
|
||||||
|
|
||||||
|
await tester.tap(find.byIcon(Icons.table_view));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.textContaining('Add participants'), findsOneWidget);
|
||||||
|
|
||||||
|
s.addPerson('Ala');
|
||||||
|
s.receipts.single.participantIds.addAll(s.people.map((p) => p.id));
|
||||||
|
mockFilePicker(saveResult: '/tmp/out.xlsx');
|
||||||
|
await tester.pump(const Duration(seconds: 5)); // expire prior snackbar
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.byIcon(Icons.table_view));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.textContaining('Saved: out.xlsx'), findsOneWidget);
|
||||||
|
|
||||||
|
mockFilePicker();
|
||||||
|
await tester.pump(const Duration(seconds: 5));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.byIcon(Icons.table_view));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
mockFilePicker(saveThrows: true);
|
||||||
|
await tester.pump(const Duration(seconds: 5));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.byIcon(Icons.table_view));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.textContaining('Export failed'), findsOneWidget);
|
||||||
|
await flushSaves(tester);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
196
billsplit/test/receipt_items_test.dart
Normal file
196
billsplit/test/receipt_items_test.dart
Normal file
@ -0,0 +1,196 @@
|
|||||||
|
// Receipt item list and assignment sheet flows.
|
||||||
|
|
||||||
|
// Receipt screen flows.
|
||||||
|
|
||||||
|
|
||||||
|
import 'package:billsplit/domain/models.dart';
|
||||||
|
import 'package:billsplit/state/app_state.dart';
|
||||||
|
import 'package:billsplit/ui/receipt_screen.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
|
||||||
|
import 'helpers.dart';
|
||||||
|
|
||||||
|
Receipt demoReceipt(AppState s, {bool withUnassigned = false}) {
|
||||||
|
final r = Receipt(
|
||||||
|
id: 'r1',
|
||||||
|
title: 'Demo',
|
||||||
|
store: 'Biedronka',
|
||||||
|
paidTotalGr: 1000,
|
||||||
|
items: [
|
||||||
|
Item(id: 'i1', name: 'Bread', totalGr: 500),
|
||||||
|
Item(
|
||||||
|
id: 'i2',
|
||||||
|
name: 'Beer',
|
||||||
|
totalGr: 300,
|
||||||
|
category: Categories.alcohol,
|
||||||
|
assignment: Assignment(mode: AssignMode.drinkers),
|
||||||
|
),
|
||||||
|
Item(
|
||||||
|
id: 'i3',
|
||||||
|
name: 'Tonic',
|
||||||
|
totalGr: 100,
|
||||||
|
category: Categories.mixer,
|
||||||
|
),
|
||||||
|
Item(
|
||||||
|
id: 'i4',
|
||||||
|
name: 'Kaucja',
|
||||||
|
totalGr: 100,
|
||||||
|
category: Categories.deposit,
|
||||||
|
),
|
||||||
|
if (withUnassigned)
|
||||||
|
Item(
|
||||||
|
id: 'i5',
|
||||||
|
name: 'Ghost',
|
||||||
|
totalGr: 50,
|
||||||
|
assignment: Assignment(mode: AssignMode.custom),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
s.addReceipt(r);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
|
||||||
|
group('receipt items', () {
|
||||||
|
testWidgets('items list: labels, unassigned, edit dialog paths',
|
||||||
|
(tester) async {
|
||||||
|
final s = tempState();
|
||||||
|
final p = s.addPerson('Ala');
|
||||||
|
s.addPerson('Bob', drinks: false);
|
||||||
|
final g = s.addGroup('Meat')..memberIds.add(p.id);
|
||||||
|
final r = demoReceipt(s, withUnassigned: true);
|
||||||
|
r.items[0].assignment = Assignment(mode: AssignMode.group, groupId: g.id);
|
||||||
|
r.items[2].assignment = Assignment(mode: AssignMode.nonDrinkers);
|
||||||
|
r.items[3].assignment = Assignment(
|
||||||
|
mode: AssignMode.group,
|
||||||
|
groupId: 'gone',
|
||||||
|
);
|
||||||
|
await pumpApp(tester, s, const ReceiptScreen(receiptId: 'r1'));
|
||||||
|
await flushSaves(tester);
|
||||||
|
|
||||||
|
expect(find.textContaining('Group: Meat'), findsOneWidget);
|
||||||
|
expect(find.textContaining('Group: ?'), findsOneWidget);
|
||||||
|
expect(find.textContaining('Drinkers'), findsOneWidget);
|
||||||
|
expect(find.textContaining('Non-drinkers'), findsOneWidget);
|
||||||
|
expect(find.textContaining('UNASSIGNED'), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.tap(find.text('Totals'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.textContaining('excluded from totals'), findsOneWidget);
|
||||||
|
await tester.tap(find.text('Items'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
// Edit via long-press: invalid total keeps dialog open, then fix+save.
|
||||||
|
await tester.longPress(find.text('Bread'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.enterText(
|
||||||
|
find.widgetWithText(TextField, 'Total (zł)'),
|
||||||
|
'abc',
|
||||||
|
);
|
||||||
|
await tester.tap(find.text('Save'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.text('Save'), findsOneWidget); // still open
|
||||||
|
await tester.enterText(
|
||||||
|
find.widgetWithText(TextField, 'Total (zł)'),
|
||||||
|
'6,00',
|
||||||
|
);
|
||||||
|
await tester.enterText(find.widgetWithText(TextField, 'Name'), 'Rolls');
|
||||||
|
await tester.tap(find.text('Save'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(r.items[0].totalGr, 600);
|
||||||
|
expect(r.items[0].name, 'Rolls');
|
||||||
|
|
||||||
|
// Delete an item from the edit dialog.
|
||||||
|
await tester.longPress(find.text('Ghost'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.text('Delete'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(r.items.any((i) => i.name == 'Ghost'), isFalse);
|
||||||
|
|
||||||
|
// Add a new alcohol item via the FAB: cancel first, then create.
|
||||||
|
await tester.tap(find.byType(FloatingActionButton));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.text('Cancel'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
await tester.tap(find.byType(FloatingActionButton));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.enterText(find.widgetWithText(TextField, 'Name'), 'Wine');
|
||||||
|
await tester.enterText(
|
||||||
|
find.widgetWithText(TextField, 'Total (zł)'),
|
||||||
|
'30',
|
||||||
|
);
|
||||||
|
await tester.tap(find.byType(DropdownButtonFormField<String>));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.text('Alcohol').last);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(find.text('Save'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
final wine = r.items.singleWhere((i) => i.name == 'Wine');
|
||||||
|
expect(wine.totalGr, 3000);
|
||||||
|
expect(wine.assignment.mode, AssignMode.drinkers);
|
||||||
|
|
||||||
|
await tester.tap(find.byType(FloatingActionButton));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.enterText(find.widgetWithText(TextField, 'Name'), 'Chips');
|
||||||
|
await tester.enterText(
|
||||||
|
find.widgetWithText(TextField, 'Total (zł)'),
|
||||||
|
'5',
|
||||||
|
);
|
||||||
|
await tester.tap(find.text('Save'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(
|
||||||
|
r.items.singleWhere((i) => i.name == 'Chips').assignment.mode,
|
||||||
|
AssignMode.everyone,
|
||||||
|
);
|
||||||
|
await flushSaves(tester);
|
||||||
|
});
|
||||||
|
|
||||||
|
testWidgets('assignment sheet: modes, materialization, nobody',
|
||||||
|
(tester) async {
|
||||||
|
final s = tempState();
|
||||||
|
final ala = s.addPerson('Ala');
|
||||||
|
s.addPerson('Bob');
|
||||||
|
final g = s.addGroup('Meat')..memberIds.add(ala.id);
|
||||||
|
final r = demoReceipt(s);
|
||||||
|
await pumpApp(tester, s, const ReceiptScreen(receiptId: 'r1'));
|
||||||
|
await flushSaves(tester);
|
||||||
|
|
||||||
|
await tester.tap(find.text('Bread'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
final item = r.items[0];
|
||||||
|
|
||||||
|
await tester.tap(find.widgetWithText(ChoiceChip, 'Drinkers'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(item.assignment.mode, AssignMode.drinkers);
|
||||||
|
await tester.tap(find.widgetWithText(ChoiceChip, 'Non-drinkers'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(item.assignment.mode, AssignMode.nonDrinkers);
|
||||||
|
expect(find.textContaining('Nobody selected'), findsOneWidget);
|
||||||
|
await tester.tap(find.widgetWithText(ChoiceChip, 'Meat'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(item.assignment.mode, AssignMode.group);
|
||||||
|
expect(item.assignment.groupId, g.id);
|
||||||
|
await tester.tap(find.widgetWithText(ChoiceChip, 'Everyone'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(item.assignment.mode, AssignMode.everyone);
|
||||||
|
|
||||||
|
// Toggling a person on a symbolic mode materializes to custom.
|
||||||
|
await tester.tap(find.byType(CheckboxListTile).first);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(item.assignment.mode, AssignMode.custom);
|
||||||
|
expect(item.assignment.customIds, hasLength(1));
|
||||||
|
await tester.tap(find.byType(CheckboxListTile).last);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(item.assignment.customIds, isEmpty);
|
||||||
|
expect(find.textContaining('Nobody selected'), findsOneWidget);
|
||||||
|
await tester.tap(find.byType(CheckboxListTile).first);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(item.assignment.customIds, hasLength(1));
|
||||||
|
await flushSaves(tester);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
@ -207,330 +207,4 @@ void main() {
|
|||||||
expect(find.byType(PeopleScreen), findsOneWidget);
|
expect(find.byType(PeopleScreen), findsOneWidget);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
group('people', () {
|
|
||||||
testWidgets('add person dialog: toggle, cancel, empty, valid',
|
|
||||||
(tester) async {
|
|
||||||
final s = tempState();
|
|
||||||
await pumpApp(tester, s, const PeopleScreen());
|
|
||||||
expect(find.textContaining('Nobody yet'), findsOneWidget);
|
|
||||||
|
|
||||||
await tester.tap(find.text('Add person'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
await tester.tap(find.byType(Switch));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
await tester.tap(find.text('Cancel'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(s.people, isEmpty);
|
|
||||||
|
|
||||||
await tester.tap(find.text('Add person'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
await tester.tap(find.text('Add'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(s.people, isEmpty);
|
|
||||||
|
|
||||||
await tester.tap(find.text('Add person'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
await tester.enterText(find.byType(TextField), 'Kasia');
|
|
||||||
await tester.tap(find.byType(Switch));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
await tester.tap(find.text('Add'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(s.people.single.name, 'Kasia');
|
|
||||||
expect(s.people.single.drinks, isFalse);
|
|
||||||
await flushSaves(tester);
|
|
||||||
});
|
|
||||||
|
|
||||||
testWidgets('drinks switch, membership early-return, delete flows',
|
|
||||||
(tester) async {
|
|
||||||
final s = tempState();
|
|
||||||
final p = s.addPerson('Ala');
|
|
||||||
await pumpApp(tester, s, const PeopleScreen());
|
|
||||||
await flushSaves(tester);
|
|
||||||
|
|
||||||
await tester.tap(find.byType(Switch).first);
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(p.drinks, isFalse);
|
|
||||||
|
|
||||||
// No groups yet → tapping the person is a no-op branch.
|
|
||||||
await tester.tap(find.text('Ala'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(find.text('Group membership'), findsNothing);
|
|
||||||
|
|
||||||
await tester.tap(find.byIcon(Icons.delete_outline).first);
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
await tester.tap(find.text('Cancel'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(s.people, hasLength(1));
|
|
||||||
|
|
||||||
await tester.tap(find.byIcon(Icons.delete_outline).first);
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
await tester.tap(find.text('Remove'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(s.people, isEmpty);
|
|
||||||
await flushSaves(tester);
|
|
||||||
});
|
|
||||||
|
|
||||||
testWidgets('groups: add, membership sheet, delete', (tester) async {
|
|
||||||
final s = tempState()..addPerson('Ala');
|
|
||||||
await pumpApp(tester, s, const PeopleScreen());
|
|
||||||
await flushSaves(tester);
|
|
||||||
|
|
||||||
await tester.tap(find.text('Add group'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
await tester.tap(find.text('Cancel'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(s.groups, isEmpty);
|
|
||||||
|
|
||||||
await tester.tap(find.text('Add group'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
await tester.enterText(find.byType(TextField), 'Meat');
|
|
||||||
await tester.tap(find.text('Add'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(s.groups.single.name, 'Meat');
|
|
||||||
expect(find.textContaining('No members'), findsOneWidget);
|
|
||||||
|
|
||||||
await tester.tap(find.text('Ala'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(find.byType(CheckboxListTile), findsOneWidget);
|
|
||||||
await tester.tap(find.byType(CheckboxListTile));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(s.groups.single.memberIds, hasLength(1));
|
|
||||||
await tester.tap(find.byType(CheckboxListTile));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(s.groups.single.memberIds, isEmpty);
|
|
||||||
await tester.tapAt(const Offset(10, 10)); // close sheet
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
|
|
||||||
await tester.tap(find.byIcon(Icons.delete_outline).last);
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(s.groups, isEmpty);
|
|
||||||
await flushSaves(tester);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
group('receipt screen', () {
|
|
||||||
testWidgets('unknown id shows deleted note', (tester) async {
|
|
||||||
final s = tempState();
|
|
||||||
await pumpApp(tester, s, const ReceiptScreen(receiptId: 'nope'));
|
|
||||||
expect(find.text('Receipt deleted.'), findsOneWidget);
|
|
||||||
});
|
|
||||||
|
|
||||||
testWidgets('tabs on narrow, panes on wide', (tester) async {
|
|
||||||
final s = tempState()..addPerson('Ala');
|
|
||||||
demoReceipt(s);
|
|
||||||
await pumpApp(tester, s, const ReceiptScreen(receiptId: 'r1'));
|
|
||||||
await flushSaves(tester);
|
|
||||||
expect(find.byType(TabBar), findsOneWidget);
|
|
||||||
await tester.tap(find.text('Totals'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(find.textContaining('grosz-exact'), findsOneWidget);
|
|
||||||
|
|
||||||
tester.view.physicalSize = const Size(1600, 900);
|
|
||||||
tester.view.devicePixelRatio = 1;
|
|
||||||
addTearDown(tester.view.reset);
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(find.byType(TabBar), findsNothing);
|
|
||||||
expect(find.byType(VerticalDivider), findsOneWidget);
|
|
||||||
});
|
|
||||||
|
|
||||||
testWidgets('participants: empty roster snackbar, then editing',
|
|
||||||
(tester) async {
|
|
||||||
final s = tempState();
|
|
||||||
demoReceipt(s);
|
|
||||||
await pumpApp(tester, s, const ReceiptScreen(receiptId: 'r1'));
|
|
||||||
await flushSaves(tester);
|
|
||||||
await tester.tap(find.byIcon(Icons.group));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(find.textContaining('Add people first'), findsOneWidget);
|
|
||||||
|
|
||||||
final p = s.addPerson('Ala');
|
|
||||||
s.addPerson('Bob');
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
await tester.tap(find.byIcon(Icons.group));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(find.text('Who is on this receipt?'), findsOneWidget);
|
|
||||||
await tester.tap(find.byType(CheckboxListTile).first);
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(s.receipts.single.participantIds.contains(p.id), isTrue);
|
|
||||||
await tester.tap(find.byType(CheckboxListTile).first);
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
await tester.tapAt(const Offset(10, 10));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
await flushSaves(tester);
|
|
||||||
});
|
|
||||||
|
|
||||||
testWidgets('export: no participants, success, cancel, failure',
|
|
||||||
(tester) async {
|
|
||||||
final s = tempState();
|
|
||||||
demoReceipt(s);
|
|
||||||
await pumpApp(tester, s, const ReceiptScreen(receiptId: 'r1'));
|
|
||||||
await flushSaves(tester);
|
|
||||||
|
|
||||||
await tester.tap(find.byIcon(Icons.table_view));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(find.textContaining('Add participants'), findsOneWidget);
|
|
||||||
|
|
||||||
s.addPerson('Ala');
|
|
||||||
s.receipts.single.participantIds.addAll(s.people.map((p) => p.id));
|
|
||||||
mockFilePicker(saveResult: '/tmp/out.xlsx');
|
|
||||||
await tester.pump(const Duration(seconds: 5)); // expire prior snackbar
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
await tester.tap(find.byIcon(Icons.table_view));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(find.textContaining('Saved: out.xlsx'), findsOneWidget);
|
|
||||||
|
|
||||||
mockFilePicker();
|
|
||||||
await tester.pump(const Duration(seconds: 5));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
await tester.tap(find.byIcon(Icons.table_view));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
|
|
||||||
mockFilePicker(saveThrows: true);
|
|
||||||
await tester.pump(const Duration(seconds: 5));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
await tester.tap(find.byIcon(Icons.table_view));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(find.textContaining('Export failed'), findsOneWidget);
|
|
||||||
await flushSaves(tester);
|
|
||||||
});
|
|
||||||
|
|
||||||
testWidgets('items list: labels, unassigned, edit dialog paths',
|
|
||||||
(tester) async {
|
|
||||||
final s = tempState();
|
|
||||||
final p = s.addPerson('Ala');
|
|
||||||
s.addPerson('Bob', drinks: false);
|
|
||||||
final g = s.addGroup('Meat')..memberIds.add(p.id);
|
|
||||||
final r = demoReceipt(s, withUnassigned: true);
|
|
||||||
r.items[0].assignment = Assignment(mode: AssignMode.group, groupId: g.id);
|
|
||||||
r.items[2].assignment = Assignment(mode: AssignMode.nonDrinkers);
|
|
||||||
r.items[3].assignment = Assignment(
|
|
||||||
mode: AssignMode.group,
|
|
||||||
groupId: 'gone',
|
|
||||||
);
|
|
||||||
await pumpApp(tester, s, const ReceiptScreen(receiptId: 'r1'));
|
|
||||||
await flushSaves(tester);
|
|
||||||
|
|
||||||
expect(find.textContaining('Group: Meat'), findsOneWidget);
|
|
||||||
expect(find.textContaining('Group: ?'), findsOneWidget);
|
|
||||||
expect(find.textContaining('Drinkers'), findsOneWidget);
|
|
||||||
expect(find.textContaining('Non-drinkers'), findsOneWidget);
|
|
||||||
expect(find.textContaining('UNASSIGNED'), findsOneWidget);
|
|
||||||
|
|
||||||
await tester.tap(find.text('Totals'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(find.textContaining('excluded from totals'), findsOneWidget);
|
|
||||||
await tester.tap(find.text('Items'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
|
|
||||||
// Edit via long-press: invalid total keeps dialog open, then fix+save.
|
|
||||||
await tester.longPress(find.text('Bread'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
await tester.enterText(
|
|
||||||
find.widgetWithText(TextField, 'Total (zł)'),
|
|
||||||
'abc',
|
|
||||||
);
|
|
||||||
await tester.tap(find.text('Save'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(find.text('Save'), findsOneWidget); // still open
|
|
||||||
await tester.enterText(
|
|
||||||
find.widgetWithText(TextField, 'Total (zł)'),
|
|
||||||
'6,00',
|
|
||||||
);
|
|
||||||
await tester.enterText(find.widgetWithText(TextField, 'Name'), 'Rolls');
|
|
||||||
await tester.tap(find.text('Save'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(r.items[0].totalGr, 600);
|
|
||||||
expect(r.items[0].name, 'Rolls');
|
|
||||||
|
|
||||||
// Delete an item from the edit dialog.
|
|
||||||
await tester.longPress(find.text('Ghost'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
await tester.tap(find.text('Delete'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(r.items.any((i) => i.name == 'Ghost'), isFalse);
|
|
||||||
|
|
||||||
// Add a new alcohol item via the FAB: cancel first, then create.
|
|
||||||
await tester.tap(find.byType(FloatingActionButton));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
await tester.tap(find.text('Cancel'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
|
|
||||||
await tester.tap(find.byType(FloatingActionButton));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
await tester.enterText(find.widgetWithText(TextField, 'Name'), 'Wine');
|
|
||||||
await tester.enterText(
|
|
||||||
find.widgetWithText(TextField, 'Total (zł)'),
|
|
||||||
'30',
|
|
||||||
);
|
|
||||||
await tester.tap(find.byType(DropdownButtonFormField<String>));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
await tester.tap(find.text('Alcohol').last);
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
await tester.tap(find.text('Save'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
final wine = r.items.singleWhere((i) => i.name == 'Wine');
|
|
||||||
expect(wine.totalGr, 3000);
|
|
||||||
expect(wine.assignment.mode, AssignMode.drinkers);
|
|
||||||
|
|
||||||
await tester.tap(find.byType(FloatingActionButton));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
await tester.enterText(find.widgetWithText(TextField, 'Name'), 'Chips');
|
|
||||||
await tester.enterText(
|
|
||||||
find.widgetWithText(TextField, 'Total (zł)'),
|
|
||||||
'5',
|
|
||||||
);
|
|
||||||
await tester.tap(find.text('Save'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(
|
|
||||||
r.items.singleWhere((i) => i.name == 'Chips').assignment.mode,
|
|
||||||
AssignMode.everyone,
|
|
||||||
);
|
|
||||||
await flushSaves(tester);
|
|
||||||
});
|
|
||||||
|
|
||||||
testWidgets('assignment sheet: modes, materialization, nobody',
|
|
||||||
(tester) async {
|
|
||||||
final s = tempState();
|
|
||||||
final ala = s.addPerson('Ala');
|
|
||||||
s.addPerson('Bob');
|
|
||||||
final g = s.addGroup('Meat')..memberIds.add(ala.id);
|
|
||||||
final r = demoReceipt(s);
|
|
||||||
await pumpApp(tester, s, const ReceiptScreen(receiptId: 'r1'));
|
|
||||||
await flushSaves(tester);
|
|
||||||
|
|
||||||
await tester.tap(find.text('Bread'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
final item = r.items[0];
|
|
||||||
|
|
||||||
await tester.tap(find.widgetWithText(ChoiceChip, 'Drinkers'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(item.assignment.mode, AssignMode.drinkers);
|
|
||||||
await tester.tap(find.widgetWithText(ChoiceChip, 'Non-drinkers'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(item.assignment.mode, AssignMode.nonDrinkers);
|
|
||||||
expect(find.textContaining('Nobody selected'), findsOneWidget);
|
|
||||||
await tester.tap(find.widgetWithText(ChoiceChip, 'Meat'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(item.assignment.mode, AssignMode.group);
|
|
||||||
expect(item.assignment.groupId, g.id);
|
|
||||||
await tester.tap(find.widgetWithText(ChoiceChip, 'Everyone'));
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(item.assignment.mode, AssignMode.everyone);
|
|
||||||
|
|
||||||
// Toggling a person on a symbolic mode materializes to custom.
|
|
||||||
await tester.tap(find.byType(CheckboxListTile).first);
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(item.assignment.mode, AssignMode.custom);
|
|
||||||
expect(item.assignment.customIds, hasLength(1));
|
|
||||||
await tester.tap(find.byType(CheckboxListTile).last);
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(item.assignment.customIds, isEmpty);
|
|
||||||
expect(find.textContaining('Nobody selected'), findsOneWidget);
|
|
||||||
await tester.tap(find.byType(CheckboxListTile).first);
|
|
||||||
await tester.pumpAndSettle();
|
|
||||||
expect(item.assignment.customIds, hasLength(1));
|
|
||||||
await flushSaves(tester);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -164,6 +164,18 @@ file, and the entry itself still linted clean with `-x`.
|
|||||||
still heredoc-unaware, so check for `<<` in the region you are moving before
|
still heredoc-unaware, so check for `<<` in the region you are moving before
|
||||||
choosing a boundary.
|
choosing a boundary.
|
||||||
|
|
||||||
|
### Extracting an embedded program pulls it into the Python gate
|
||||||
|
|
||||||
|
`install_plagiarism_tools.sh` (534) is mostly two heredocs emitting a 224-line
|
||||||
|
and a 90-line Python program. Extracting them to real files under
|
||||||
|
`plagiarism/` gets all three under the cap in one move and is what the shell
|
||||||
|
rules ask for — but those files then face `ruff select = ["ALL"]`, `pylint`
|
||||||
|
and `mypy`, and they arrived with **22 violations**. Fixing those means
|
||||||
|
rewriting a plagiarism checker, and it changes code that gets _installed_.
|
||||||
|
|
||||||
|
So: extracting an embedded program is the right shape, but budget it as a
|
||||||
|
**Python cleanup task, not a line-count split**. Reverted for now.
|
||||||
|
|
||||||
### When to give up on a seam
|
### When to give up on a seam
|
||||||
|
|
||||||
`clean_audio.sh` was reverted. Two probe-result globals were read by functions
|
`clean_audio.sh` was reverted. Two probe-result globals were read by functions
|
||||||
|
|||||||
18
docs/superpowers/contracts/billsplit-split-2026-08-16.json
Normal file
18
docs/superpowers/contracts/billsplit-split-2026-08-16.json
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"title": "Split the billsplit Dart files under the 250-line cap",
|
||||||
|
"objective": "Bring all four over-cap billsplit Dart sources and its 536-line UI flow test under the cap, leaving only Flutter's generated Windows runner.",
|
||||||
|
"acceptance_criteria": [
|
||||||
|
"Every billsplit Dart file is at most 250 lines.",
|
||||||
|
"flutter test reports exactly 60 passing tests, the same as before the split.",
|
||||||
|
"flutter analyze reports no MORE issues than the pre-split baseline. That baseline is 4 pre-existing unnecessary_unawaited infos, measured from a detached worktree at HEAD rather than assumed.",
|
||||||
|
"Library-private declarations that cross a seam use part/part of. A `library;` directive must come before any `part`.",
|
||||||
|
"Split widget-test halves each call TestWidgetsFlutterBinding.ensureInitialized().",
|
||||||
|
"No test is deleted, skipped or weakened."
|
||||||
|
],
|
||||||
|
"out_of_scope": [
|
||||||
|
"windows/runner/win32_window.cpp, which is Flutter-generated scaffolding that regeneration would overwrite.",
|
||||||
|
"Changing what any test asserts.",
|
||||||
|
"The 4 pre-existing unnecessary_unawaited infos, which are not this task's to fix."
|
||||||
|
],
|
||||||
|
"verifier": "flutter test (expects 60 passed), flutter analyze compared against the same command run in a detached worktree at HEAD, and check_file_length.sh over billsplit."
|
||||||
|
}
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"title": "Split scripts/lib/common.sh under the 250-line cap",
|
||||||
|
"objective": "Bring the shared library that 49 scripts source under the cap, without changing any name it exports or requiring a single consumer to be edited.",
|
||||||
|
"acceptance_criteria": [
|
||||||
|
"common.sh and all three new parts are at most 250 lines.",
|
||||||
|
"common.sh remains the single path every consumer sources; it sources its own parts, so no caller changes.",
|
||||||
|
"Sourcing common.sh yields exactly the same set of function names as before the split, proven by diffing `declare -F` against a detached worktree at HEAD. A passing lint is not sufficient evidence for a library with this many consumers.",
|
||||||
|
"Any array or constant a moved function reads moves with it, rather than being left behind and suppressed.",
|
||||||
|
"The pre-commit shellcheck hook passes on every file, with no new suppression.",
|
||||||
|
"The parts are sourced in the original definition order, so a consumer relying on ordering is unaffected."
|
||||||
|
],
|
||||||
|
"out_of_scope": [
|
||||||
|
"Executing any of the 49 consumer scripts.",
|
||||||
|
"Changing what any helper does.",
|
||||||
|
"The other over-cap linux_configuration scripts."
|
||||||
|
],
|
||||||
|
"verifier": "A subshell that sources common.sh and prints `declare -F`, diffed against the same run from a detached worktree at HEAD; plus array-length checks, shellcheck -x, meta/scripts/verify_shell_split.sh on a real consumer, and check_file_length.sh."
|
||||||
|
}
|
||||||
42
docs/superpowers/evidence/billsplit-split-2026-08-16.json
Normal file
42
docs/superpowers/evidence/billsplit-split-2026-08-16.json
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
{
|
||||||
|
"intent": "Clear every over-cap billsplit Dart file, leaving only Flutter's generated Windows runner.",
|
||||||
|
"scope": [
|
||||||
|
"lib/ui/receipt_screen.dart (401 -> 216) + receipt_items.dart",
|
||||||
|
"lib/domain/eparagon_parser.dart (293 -> 182) + eparagon_fields.dart",
|
||||||
|
"lib/domain/models.dart (278 -> 194) + assignment.dart",
|
||||||
|
"lib/ui/people_screen.dart (266 -> 139) + people_tiles.dart",
|
||||||
|
"test/ui_flows_test.dart (536 -> 214) + people_flows_test.dart, receipt_flows_test.dart, receipt_items_test.dart",
|
||||||
|
"Not touched: windows/runner/win32_window.cpp (288)",
|
||||||
|
"Non-goal: no assertion changed"
|
||||||
|
],
|
||||||
|
"changes": [
|
||||||
|
"All four lib splits use part/part of, applying the rule learned in focus_owner rather than rediscovering it - none of them needed a second attempt.",
|
||||||
|
"models.dart has a `library;` directive, and the splitter inserted `part` above it, which is an error: the library directive must come first. Fixed by hand, and it also revealed that the insertion had split the file's doc comment in two.",
|
||||||
|
"The test file split three ways, each half declaring the test binding explicitly and keeping only the imports it uses. One aliased import (`as app`) survived the first pruning pass because the filter matched exact text - worth remembering that unused-import cleanup must handle aliases."
|
||||||
|
],
|
||||||
|
"verification": [
|
||||||
|
{
|
||||||
|
"command": "flutter test",
|
||||||
|
"result": "pass",
|
||||||
|
"evidence": "60 passed, matching the baseline measured before any edit."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"command": "flutter analyze, compared against the same command in a detached worktree at HEAD",
|
||||||
|
"result": "pass",
|
||||||
|
"evidence": "4 issues after, 4 issues before - all pre-existing unnecessary_unawaited infos. Comparing against a worktree rather than assuming a clean baseline is what showed the intermediate run's 7 issues were 3 of my own making, including one real error."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"command": "bash ~/utils/scripts/check_file_length.sh --all | grep billsplit",
|
||||||
|
"result": "pass",
|
||||||
|
"evidence": "Only windows/runner/win32_window.cpp remains, and it is Flutter-generated scaffolding: editing it would be undone the next time the Windows target is regenerated."
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"risks": [
|
||||||
|
"part/part of puts the split files in one library namespace, so two parts cannot declare the same private name. Not an issue today, but invisible until it collides.",
|
||||||
|
"win32_window.cpp stays over the cap. It is vendored Flutter scaffolding, so the honest options are an exemption or leaving it; splitting it would be undone by `flutter create`."
|
||||||
|
],
|
||||||
|
"rollback": [
|
||||||
|
"git revert the commit; the five originals return and the seven new files disappear.",
|
||||||
|
"After rollback, re-run flutter test and expect 60 passed."
|
||||||
|
]
|
||||||
|
}
|
||||||
@ -0,0 +1,43 @@
|
|||||||
|
{
|
||||||
|
"intent": "Split scripts/lib/common.sh, the library 49 scripts source, without changing a single name it exports.",
|
||||||
|
"scope": [
|
||||||
|
"linux_configuration/scripts/lib/common.sh (620 -> 163)",
|
||||||
|
"lib/common_packages.sh, common_services.sh, common_datetime.sh (new)",
|
||||||
|
"Non-goal: no behaviour change, no change to the public surface"
|
||||||
|
],
|
||||||
|
"changes": [
|
||||||
|
"common.sh stays the single entry point and now sources its own three parts, exactly the shape the handoff prescribes for phone_focus_mode/config.sh. All 49 consumers keep sourcing one path and need no edit.",
|
||||||
|
"Split by concern: user/root/setup helpers stay, focus-app detection and package installation go to common_packages.sh, service control and the log_* family to common_services.sh, and the date/time/uptime helpers to common_datetime.sh.",
|
||||||
|
"The FOCUS_APPS_WINDOWS and FOCUS_APPS_PROCESSES arrays moved with is_focus_app_running, the only function that reads them - leaving them behind produced two SC2034 warnings."
|
||||||
|
],
|
||||||
|
"verification": [
|
||||||
|
{
|
||||||
|
"command": "source common.sh in a subshell and diff `declare -F` against the same from a detached worktree at HEAD",
|
||||||
|
"result": "pass",
|
||||||
|
"evidence": "Identical 48-function set before and after. For a library with 49 consumers this is the check that matters: it proves the public surface is unchanged rather than inferring it from a passing lint."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"command": "source common.sh and print the array lengths",
|
||||||
|
"result": "pass",
|
||||||
|
"evidence": "WINDOWS=13 PROCESSES=2 - the arrays are populated, so moving them did not break is_focus_app_running."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"command": "shellcheck -x common.sh and shellcheck on each part",
|
||||||
|
"result": "fail then pass",
|
||||||
|
"evidence": "Two SC2034 warnings for the arrays left behind; zero after moving them. No suppression added."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"command": "meta/scripts/verify_shell_split.sh on convert_video.sh, a real consumer",
|
||||||
|
"result": "pass",
|
||||||
|
"evidence": "'libs sourced, functions defined' - a script that sources common.sh from two directories up still resolves everything through the new chain."
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"risks": [
|
||||||
|
"common.sh is sourced by 49 scripts and by systemd-invoked ones; the function-set diff covers the names, but nothing here executes those 49 scripts. A consumer that relied on a definition ORDER rather than a name would not be caught - the parts are sourced in the original order to keep that safe.",
|
||||||
|
"_COMMON_DIR uses ${BASH_SOURCE[0]}, which is required here: common.sh is always sourced, never executed, so $0 would point at the calling script."
|
||||||
|
],
|
||||||
|
"rollback": [
|
||||||
|
"git revert the commit; common.sh returns to 620 lines and the three parts disappear.",
|
||||||
|
"After rollback, re-run the declare -F diff and expect the same 48 functions."
|
||||||
|
]
|
||||||
|
}
|
||||||
@ -154,467 +154,10 @@ init_setup_script() {
|
|||||||
print_setup_header "$title"
|
print_setup_header "$title"
|
||||||
}
|
}
|
||||||
|
|
||||||
# =============================================================================
|
_COMMON_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
|
||||||
# FOCUS APP DETECTION (for digital wellbeing scripts)
|
# shellcheck source=common_services.sh
|
||||||
# =============================================================================
|
source "$_COMMON_DIR/common_packages.sh"
|
||||||
|
# shellcheck source=common_services.sh
|
||||||
# Default focus apps - can be overridden before calling is_focus_app_running
|
source "$_COMMON_DIR/common_services.sh"
|
||||||
FOCUS_APPS_WINDOWS=(
|
# shellcheck source=common_datetime.sh
|
||||||
"Visual Studio Code"
|
source "$_COMMON_DIR/common_datetime.sh"
|
||||||
"VSCodium"
|
|
||||||
"Cursor"
|
|
||||||
"IntelliJ IDEA"
|
|
||||||
"PyCharm"
|
|
||||||
"WebStorm"
|
|
||||||
"CLion"
|
|
||||||
"Rider"
|
|
||||||
"Sublime Text"
|
|
||||||
"Blender"
|
|
||||||
"Godot"
|
|
||||||
"Unity"
|
|
||||||
"Unreal Editor"
|
|
||||||
)
|
|
||||||
|
|
||||||
FOCUS_APPS_PROCESSES=(
|
|
||||||
"steam_app_"
|
|
||||||
"gamescope"
|
|
||||||
)
|
|
||||||
|
|
||||||
# Check if any focus app is running (window-based detection)
|
|
||||||
# Returns 0 if focus app found, 1 otherwise
|
|
||||||
# Echoes the name of the found app
|
|
||||||
is_focus_app_running() {
|
|
||||||
# One xdotool call with a combined regex instead of N separate calls
|
|
||||||
if command -v xdotool &>/dev/null && [[ ${#FOCUS_APPS_WINDOWS[@]} -gt 0 ]]; then
|
|
||||||
local regex wid
|
|
||||||
printf -v regex '%s|' "${FOCUS_APPS_WINDOWS[@]}"
|
|
||||||
regex="${regex%|}" # strip trailing |
|
|
||||||
while IFS= read -r wid; do
|
|
||||||
[[ -n $wid ]] || continue
|
|
||||||
echo "focus app"
|
|
||||||
return 0
|
|
||||||
done < <(xdotool search --name "$regex" 2>/dev/null)
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Check specific processes via /proc (no fork)
|
|
||||||
local app comm
|
|
||||||
for app in "${FOCUS_APPS_PROCESSES[@]}"; do
|
|
||||||
for comm in /proc/[0-9]*/comm; do
|
|
||||||
[[ -r $comm ]] || continue
|
|
||||||
read -r _proc_comm <"$comm" 2>/dev/null || continue
|
|
||||||
if [[ $_proc_comm == *"$app"* ]]; then
|
|
||||||
echo "$_proc_comm"
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
done
|
|
||||||
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
# =============================================================================
|
|
||||||
# COMMAND AVAILABILITY
|
|
||||||
# =============================================================================
|
|
||||||
|
|
||||||
# Check if a command exists
|
|
||||||
# Usage: if require_command ffmpeg; then ...
|
|
||||||
require_command() {
|
|
||||||
local cmd="$1"
|
|
||||||
local pkg="${2:-$1}"
|
|
||||||
if ! command -v "$cmd" >/dev/null 2>&1; then
|
|
||||||
echo "Error: '$cmd' is not installed or not in PATH." >&2
|
|
||||||
echo "Install with: sudo pacman -S $pkg" >&2
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
# Check for ImageMagick and display helpful installation message
|
|
||||||
# Usage: require_imagemagick [optional: "magick" or "convert"]
|
|
||||||
# Returns: Sets MAGICK_CMD variable to available command
|
|
||||||
require_imagemagick() {
|
|
||||||
local preferred="${1:-}"
|
|
||||||
|
|
||||||
if [[ $preferred == "magick" ]] || [[ -z $preferred ]]; then
|
|
||||||
if command -v magick &>/dev/null; then
|
|
||||||
MAGICK_CMD="magick"
|
|
||||||
export MAGICK_CMD
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ $preferred == "convert" ]] || [[ -z $preferred ]]; then
|
|
||||||
if command -v convert &>/dev/null; then
|
|
||||||
MAGICK_CMD="convert"
|
|
||||||
export MAGICK_CMD
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Error: ImageMagick is not installed." >&2
|
|
||||||
echo "Install it with:" >&2
|
|
||||||
echo " Arch Linux: sudo pacman -S imagemagick" >&2
|
|
||||||
echo " Ubuntu/Debian: sudo apt install imagemagick" >&2
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
# Install missing pacman packages
|
|
||||||
# Usage: install_missing_pacman_packages pkg1 pkg2 pkg3 ...
|
|
||||||
# Returns 0 if all packages installed successfully, 1 otherwise
|
|
||||||
install_missing_pacman_packages() {
|
|
||||||
local packages=("$@")
|
|
||||||
local missing=()
|
|
||||||
|
|
||||||
for pkg in "${packages[@]}"; do
|
|
||||||
if ! pacman -Qi "$pkg" >/dev/null 2>&1; then
|
|
||||||
missing+=("$pkg")
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
if [[ ${#missing[@]} -eq 0 ]]; then
|
|
||||||
echo "[INFO] All required packages are already installed."
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "[INFO] Installing missing packages: ${missing[*]}"
|
|
||||||
if ! sudo pacman -S --needed --noconfirm "${missing[@]}"; then
|
|
||||||
echo "[ERROR] Failed to install packages" >&2
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
# =============================================================================
|
|
||||||
# NOTIFICATION
|
|
||||||
# =============================================================================
|
|
||||||
|
|
||||||
# Send desktop notification (fails silently if notify-send not available)
|
|
||||||
# Usage: notify "Title" "Message" [urgency: low/normal/critical] [timeout_ms]
|
|
||||||
notify() {
|
|
||||||
local title="$1"
|
|
||||||
local message="$2"
|
|
||||||
local urgency="${3:-normal}"
|
|
||||||
local timeout="${4:-5000}"
|
|
||||||
|
|
||||||
if command -v notify-send &>/dev/null; then
|
|
||||||
notify-send -u "$urgency" -t "$timeout" "$title" "$message" 2>/dev/null || true
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# =============================================================================
|
|
||||||
# FILE/PATH UTILITIES
|
|
||||||
# =============================================================================
|
|
||||||
|
|
||||||
# Get the directory containing the calling script
|
|
||||||
# Usage: SCRIPT_DIR=$(get_script_dir)
|
|
||||||
get_script_dir() {
|
|
||||||
dirname "$(readlink -f "${BASH_SOURCE[1]:-$0}")"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Ensure a directory exists
|
|
||||||
# Usage: ensure_dir "/path/to/dir"
|
|
||||||
ensure_dir() {
|
|
||||||
local dir="$1"
|
|
||||||
if [[ ! -d $dir ]]; then
|
|
||||||
mkdir -p "$dir"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# =============================================================================
|
|
||||||
# SYSTEMD HELPERS
|
|
||||||
# =============================================================================
|
|
||||||
|
|
||||||
# Internal helper for running systemctl with optional --user flag
|
|
||||||
_systemctl_cmd() {
|
|
||||||
local user_flag="$1"
|
|
||||||
shift
|
|
||||||
if [[ $user_flag == "--user" ]]; then
|
|
||||||
systemctl --user "$@"
|
|
||||||
else
|
|
||||||
systemctl "$@"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# Enable and start a systemd service (user or system)
|
|
||||||
# Usage: enable_service "service-name" [--user]
|
|
||||||
enable_service() {
|
|
||||||
local service="$1"
|
|
||||||
local user_flag="${2:-}"
|
|
||||||
_systemctl_cmd "$user_flag" daemon-reload
|
|
||||||
_systemctl_cmd "$user_flag" enable --now "$service"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Check if a systemd service is active
|
|
||||||
# Usage: if is_service_active "service-name" [--user]; then ...
|
|
||||||
is_service_active() {
|
|
||||||
_systemctl_cmd "${2:-}" is-active --quiet "$1"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Check if a systemd service is enabled
|
|
||||||
# Usage: if is_service_enabled "service-name" [--user]; then ...
|
|
||||||
is_service_enabled() {
|
|
||||||
_systemctl_cmd "${2:-}" is-enabled --quiet "$1" 2>/dev/null
|
|
||||||
}
|
|
||||||
|
|
||||||
# =============================================================================
|
|
||||||
# COLORED LOGGING (for scripts that need colored output)
|
|
||||||
# =============================================================================
|
|
||||||
|
|
||||||
# ANSI color codes
|
|
||||||
declare -g COLOR_RED='\033[1;31m'
|
|
||||||
declare -g COLOR_GREEN='\033[1;32m'
|
|
||||||
declare -g COLOR_YELLOW='\033[1;33m'
|
|
||||||
declare -g COLOR_BLUE='\033[1;34m'
|
|
||||||
declare -g COLOR_NC='\033[0m'
|
|
||||||
|
|
||||||
log_info() {
|
|
||||||
printf "${COLOR_BLUE}[INFO]${COLOR_NC} %s\n" "$*"
|
|
||||||
}
|
|
||||||
|
|
||||||
log_ok() {
|
|
||||||
printf "${COLOR_GREEN}[ OK ]${COLOR_NC} %s\n" "$*"
|
|
||||||
}
|
|
||||||
|
|
||||||
log_warn() {
|
|
||||||
printf "${COLOR_YELLOW}[WARN]${COLOR_NC} %s\n" "$*" >&2
|
|
||||||
}
|
|
||||||
|
|
||||||
log_error() {
|
|
||||||
printf "${COLOR_RED}[ERROR]${COLOR_NC} %s\n" "$*" >&2
|
|
||||||
}
|
|
||||||
|
|
||||||
# Alias for compatibility
|
|
||||||
warn() { log_warn "$@"; }
|
|
||||||
err() { log_error "$@"; }
|
|
||||||
|
|
||||||
# =============================================================================
|
|
||||||
# EFFICIENT TIME FUNCTIONS (zero-fork bash builtins)
|
|
||||||
# =============================================================================
|
|
||||||
# These functions use printf '%(...)'T' bash builtin (NO external commands)
|
|
||||||
# to avoid fork-storm anti-patterns in polling scripts.
|
|
||||||
# See: .github/skills/efficient-polling-scripts/SKILL.md
|
|
||||||
|
|
||||||
# Get current Unix timestamp (seconds since epoch)
|
|
||||||
# Usage: ts=$(get_timestamp)
|
|
||||||
# FORK-FREE: uses bash builtin printf %s (sec_since_epoch)
|
|
||||||
get_timestamp() {
|
|
||||||
printf '%(%s)T' -1
|
|
||||||
}
|
|
||||||
|
|
||||||
# Get current date in YYYY-MM-DD format
|
|
||||||
# Usage: date=$(get_date)
|
|
||||||
get_date() {
|
|
||||||
printf '%(%Y-%m-%d)T' -1
|
|
||||||
}
|
|
||||||
|
|
||||||
# Get current time in HH:MM:SS format
|
|
||||||
# Usage: time=$(get_time)
|
|
||||||
get_time() {
|
|
||||||
printf '%(%H:%M:%S)T' -1
|
|
||||||
}
|
|
||||||
|
|
||||||
# Get current date-time in YYYY-MM-DD HH:MM:SS format
|
|
||||||
# Usage: dt=$(get_datetime)
|
|
||||||
get_datetime() {
|
|
||||||
printf '%(%Y-%m-%d %H:%M:%S)T' -1
|
|
||||||
}
|
|
||||||
|
|
||||||
# Get day of week (1=Monday, 7=Sunday)
|
|
||||||
# Usage: dow=$(get_day_of_week)
|
|
||||||
get_day_of_week() {
|
|
||||||
printf '%(%u)T' -1
|
|
||||||
}
|
|
||||||
|
|
||||||
# Get day name (Monday, Tuesday, ...)
|
|
||||||
# Usage: day=$(get_day_name)
|
|
||||||
get_day_name() {
|
|
||||||
printf '%(%A)T' -1
|
|
||||||
}
|
|
||||||
|
|
||||||
# Get current hour (00-23)
|
|
||||||
# Usage: hour=$(get_hour)
|
|
||||||
get_hour() {
|
|
||||||
printf '%(%H)T' -1
|
|
||||||
}
|
|
||||||
|
|
||||||
# Get current minute (00-59)
|
|
||||||
# Usage: minute=$(get_minute)
|
|
||||||
get_minute() {
|
|
||||||
printf '%(%M)T' -1
|
|
||||||
}
|
|
||||||
|
|
||||||
# Get current second (00-59)
|
|
||||||
# Usage: second=$(get_second)
|
|
||||||
get_second() {
|
|
||||||
printf '%(%S)T' -1
|
|
||||||
}
|
|
||||||
|
|
||||||
# Get Unix timestamp from boot (uptime in seconds)
|
|
||||||
# Usage: boot_seconds=$(get_uptime_seconds)
|
|
||||||
get_uptime_seconds() {
|
|
||||||
read -r uptime_with_fraction _ </proc/uptime
|
|
||||||
printf '%.*f\n' 0 "$uptime_with_fraction"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Get boot time in YYYY-MM-DD HH:MM:SS format
|
|
||||||
# Usage: boot_time=$(get_boot_datetime)
|
|
||||||
# Calculates: current_time - uptime_seconds
|
|
||||||
get_boot_datetime() {
|
|
||||||
local uptime_seconds
|
|
||||||
uptime_seconds=$(get_uptime_seconds)
|
|
||||||
local boot_ts=$(($(get_timestamp) - uptime_seconds))
|
|
||||||
printf '%(%Y-%m-%d %H:%M:%S)T' "$boot_ts"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Get boot time date only (YYYY-MM-DD)
|
|
||||||
# Usage: boot_date=$(get_boot_date)
|
|
||||||
get_boot_date() {
|
|
||||||
local uptime_seconds
|
|
||||||
uptime_seconds=$(get_uptime_seconds)
|
|
||||||
local boot_ts=$(($(get_timestamp) - uptime_seconds))
|
|
||||||
printf '%(%Y-%m-%d)T' "$boot_ts"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Get boot time hour only (00-23)
|
|
||||||
# Usage: boot_hour=$(get_boot_hour)
|
|
||||||
get_boot_hour() {
|
|
||||||
local uptime_seconds
|
|
||||||
uptime_seconds=$(get_uptime_seconds)
|
|
||||||
local boot_ts=$(($(get_timestamp) - uptime_seconds))
|
|
||||||
printf '%(%H)T' "$boot_ts"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Check if current time is within a given hour range
|
|
||||||
# Usage: if is_hour_in_range 5 8; then ... # 5AM-8AM
|
|
||||||
is_hour_in_range() {
|
|
||||||
local start_hour=$1
|
|
||||||
local end_hour=$2
|
|
||||||
local current_hour
|
|
||||||
current_hour=$(get_hour)
|
|
||||||
local current_hour_num=$((10#$current_hour))
|
|
||||||
[[ $current_hour_num -ge $start_hour ]] && [[ $current_hour_num -lt $end_hour ]]
|
|
||||||
}
|
|
||||||
|
|
||||||
# Check if current day is a specific day of week
|
|
||||||
# Usage: if is_day_of_week 1 5 6 7; then ... # Monday, Friday, Saturday, Sunday
|
|
||||||
is_day_of_week() {
|
|
||||||
local target_day
|
|
||||||
target_day=$(get_day_of_week)
|
|
||||||
for day in "$@"; do
|
|
||||||
[[ $target_day -eq $day ]] && return 0
|
|
||||||
done
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
# =============================================================================
|
|
||||||
# INTERACTIVE PROMPTS
|
|
||||||
# =============================================================================
|
|
||||||
|
|
||||||
# Ask yes/no question, returns 0 for yes, 1 for no
|
|
||||||
# Usage: if ask_yes_no "Continue?"; then ...
|
|
||||||
ask_yes_no() {
|
|
||||||
local prompt="$1"
|
|
||||||
local ans
|
|
||||||
read -r -p "$prompt [y/N]: " ans || true
|
|
||||||
case "${ans:-}" in
|
|
||||||
y | Y | yes | YES) return 0 ;;
|
|
||||||
*) return 1 ;;
|
|
||||||
esac
|
|
||||||
}
|
|
||||||
|
|
||||||
# Check if a command is available
|
|
||||||
# Usage: if has_cmd git; then ...
|
|
||||||
has_cmd() {
|
|
||||||
command -v "$1" >/dev/null 2>&1
|
|
||||||
}
|
|
||||||
|
|
||||||
# =============================================================================
|
|
||||||
# STANDARD SETUP HEADER
|
|
||||||
# =============================================================================
|
|
||||||
|
|
||||||
# Print a standard setup header for scripts
|
|
||||||
# Usage: print_setup_header "Script Name"
|
|
||||||
print_setup_header() {
|
|
||||||
local title="$1"
|
|
||||||
local current_datetime
|
|
||||||
current_datetime=$(get_datetime)
|
|
||||||
echo "$title"
|
|
||||||
printf '=%.0s' $(seq 1 ${#title})
|
|
||||||
echo ""
|
|
||||||
echo "Current Date: $current_datetime"
|
|
||||||
echo "User: $USER"
|
|
||||||
echo "Original user: $(get_actual_user)"
|
|
||||||
if [[ $INTERACTIVE_MODE == "true" ]]; then
|
|
||||||
echo "Mode: Interactive (prompts enabled)"
|
|
||||||
else
|
|
||||||
echo "Mode: Automatic (auto-yes, use --interactive for prompts)"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# =============================================================================
|
|
||||||
# MOUNT/UNMOUNT HELPERS (for hosts guard and similar)
|
|
||||||
# =============================================================================
|
|
||||||
|
|
||||||
# Count mount layers for a path
|
|
||||||
# Usage: count=$(mount_layers_count "/etc/hosts")
|
|
||||||
mount_layers_count() {
|
|
||||||
local target="$1"
|
|
||||||
awk -v t="$target" '$5==t{c++} END{print c+0}' /proc/self/mountinfo 2>/dev/null || echo 0
|
|
||||||
}
|
|
||||||
|
|
||||||
# Collapse all bind mount layers for a path
|
|
||||||
# Usage: collapse_mounts "/etc/hosts" [max_iterations]
|
|
||||||
collapse_mounts() {
|
|
||||||
local target="$1"
|
|
||||||
local max_iter="${2:-20}"
|
|
||||||
local i=0
|
|
||||||
|
|
||||||
if has_cmd mountpoint; then
|
|
||||||
while mountpoint -q "$target"; do
|
|
||||||
umount -l "$target" >/dev/null 2>&1 || break
|
|
||||||
i=$((i + 1))
|
|
||||||
((i >= max_iter)) && break
|
|
||||||
done
|
|
||||||
else
|
|
||||||
local cnt
|
|
||||||
cnt=$(mount_layers_count "$target")
|
|
||||||
while ((cnt > 1)); do
|
|
||||||
umount -l "$target" >/dev/null 2>&1 || break
|
|
||||||
i=$((i + 1))
|
|
||||||
((i >= max_iter)) && break
|
|
||||||
cnt=$(mount_layers_count "$target")
|
|
||||||
done
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# =============================================================================
|
|
||||||
# RESOLUTION/FORMAT VALIDATION
|
|
||||||
# =============================================================================
|
|
||||||
|
|
||||||
# Validate resolution format (WIDTHxHEIGHT)
|
|
||||||
# Usage: if validate_resolution "1920x1080"; then ...
|
|
||||||
validate_resolution() {
|
|
||||||
local res="$1"
|
|
||||||
[[ $res =~ ^[0-9]+x[0-9]+$ ]]
|
|
||||||
}
|
|
||||||
|
|
||||||
# Generate output filename with suffix
|
|
||||||
# Usage: output=$(generate_output_filename "input.jpg" "_resized")
|
|
||||||
generate_output_filename() {
|
|
||||||
local input="$1"
|
|
||||||
local suffix="$2"
|
|
||||||
local ext="${3:-}"
|
|
||||||
|
|
||||||
local basename dirname filename extension
|
|
||||||
basename=$(basename "$input")
|
|
||||||
dirname=$(dirname "$input")
|
|
||||||
filename="${basename%.*}"
|
|
||||||
extension="${basename##*.}"
|
|
||||||
|
|
||||||
# Handle files without extension
|
|
||||||
if [[ $filename == "$extension" ]]; then
|
|
||||||
extension="${ext:-jpg}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "${dirname}/${filename}${suffix}.${extension}"
|
|
||||||
}
|
|
||||||
|
|||||||
237
linux_configuration/scripts/lib/common_datetime.sh
Executable file
237
linux_configuration/scripts/lib/common_datetime.sh
Executable file
@ -0,0 +1,237 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# Date, time, uptime and boot-time helpers.
|
||||||
|
#
|
||||||
|
# Sourced by common.sh, which stays the single entry point every script
|
||||||
|
# sources -- 49 of them, so the public surface must not change.
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# EFFICIENT TIME FUNCTIONS (zero-fork bash builtins)
|
||||||
|
# =============================================================================
|
||||||
|
# These functions use printf '%(...)'T' bash builtin (NO external commands)
|
||||||
|
# to avoid fork-storm anti-patterns in polling scripts.
|
||||||
|
# See: .github/skills/efficient-polling-scripts/SKILL.md
|
||||||
|
|
||||||
|
# Get current Unix timestamp (seconds since epoch)
|
||||||
|
# Usage: ts=$(get_timestamp)
|
||||||
|
# FORK-FREE: uses bash builtin printf %s (sec_since_epoch)
|
||||||
|
get_timestamp() {
|
||||||
|
printf '%(%s)T' -1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get current date in YYYY-MM-DD format
|
||||||
|
# Usage: date=$(get_date)
|
||||||
|
get_date() {
|
||||||
|
printf '%(%Y-%m-%d)T' -1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get current time in HH:MM:SS format
|
||||||
|
# Usage: time=$(get_time)
|
||||||
|
get_time() {
|
||||||
|
printf '%(%H:%M:%S)T' -1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get current date-time in YYYY-MM-DD HH:MM:SS format
|
||||||
|
# Usage: dt=$(get_datetime)
|
||||||
|
get_datetime() {
|
||||||
|
printf '%(%Y-%m-%d %H:%M:%S)T' -1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get day of week (1=Monday, 7=Sunday)
|
||||||
|
# Usage: dow=$(get_day_of_week)
|
||||||
|
get_day_of_week() {
|
||||||
|
printf '%(%u)T' -1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get day name (Monday, Tuesday, ...)
|
||||||
|
# Usage: day=$(get_day_name)
|
||||||
|
get_day_name() {
|
||||||
|
printf '%(%A)T' -1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get current hour (00-23)
|
||||||
|
# Usage: hour=$(get_hour)
|
||||||
|
get_hour() {
|
||||||
|
printf '%(%H)T' -1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get current minute (00-59)
|
||||||
|
# Usage: minute=$(get_minute)
|
||||||
|
get_minute() {
|
||||||
|
printf '%(%M)T' -1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get current second (00-59)
|
||||||
|
# Usage: second=$(get_second)
|
||||||
|
get_second() {
|
||||||
|
printf '%(%S)T' -1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get Unix timestamp from boot (uptime in seconds)
|
||||||
|
# Usage: boot_seconds=$(get_uptime_seconds)
|
||||||
|
get_uptime_seconds() {
|
||||||
|
read -r uptime_with_fraction _ </proc/uptime
|
||||||
|
printf '%.*f\n' 0 "$uptime_with_fraction"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get boot time in YYYY-MM-DD HH:MM:SS format
|
||||||
|
# Usage: boot_time=$(get_boot_datetime)
|
||||||
|
# Calculates: current_time - uptime_seconds
|
||||||
|
get_boot_datetime() {
|
||||||
|
local uptime_seconds
|
||||||
|
uptime_seconds=$(get_uptime_seconds)
|
||||||
|
local boot_ts=$(($(get_timestamp) - uptime_seconds))
|
||||||
|
printf '%(%Y-%m-%d %H:%M:%S)T' "$boot_ts"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get boot time date only (YYYY-MM-DD)
|
||||||
|
# Usage: boot_date=$(get_boot_date)
|
||||||
|
get_boot_date() {
|
||||||
|
local uptime_seconds
|
||||||
|
uptime_seconds=$(get_uptime_seconds)
|
||||||
|
local boot_ts=$(($(get_timestamp) - uptime_seconds))
|
||||||
|
printf '%(%Y-%m-%d)T' "$boot_ts"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get boot time hour only (00-23)
|
||||||
|
# Usage: boot_hour=$(get_boot_hour)
|
||||||
|
get_boot_hour() {
|
||||||
|
local uptime_seconds
|
||||||
|
uptime_seconds=$(get_uptime_seconds)
|
||||||
|
local boot_ts=$(($(get_timestamp) - uptime_seconds))
|
||||||
|
printf '%(%H)T' "$boot_ts"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check if current time is within a given hour range
|
||||||
|
# Usage: if is_hour_in_range 5 8; then ... # 5AM-8AM
|
||||||
|
is_hour_in_range() {
|
||||||
|
local start_hour=$1
|
||||||
|
local end_hour=$2
|
||||||
|
local current_hour
|
||||||
|
current_hour=$(get_hour)
|
||||||
|
local current_hour_num=$((10#$current_hour))
|
||||||
|
[[ $current_hour_num -ge $start_hour ]] && [[ $current_hour_num -lt $end_hour ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check if current day is a specific day of week
|
||||||
|
# Usage: if is_day_of_week 1 5 6 7; then ... # Monday, Friday, Saturday, Sunday
|
||||||
|
is_day_of_week() {
|
||||||
|
local target_day
|
||||||
|
target_day=$(get_day_of_week)
|
||||||
|
for day in "$@"; do
|
||||||
|
[[ $target_day -eq $day ]] && return 0
|
||||||
|
done
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# INTERACTIVE PROMPTS
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
# Ask yes/no question, returns 0 for yes, 1 for no
|
||||||
|
# Usage: if ask_yes_no "Continue?"; then ...
|
||||||
|
ask_yes_no() {
|
||||||
|
local prompt="$1"
|
||||||
|
local ans
|
||||||
|
read -r -p "$prompt [y/N]: " ans || true
|
||||||
|
case "${ans:-}" in
|
||||||
|
y | Y | yes | YES) return 0 ;;
|
||||||
|
*) return 1 ;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check if a command is available
|
||||||
|
# Usage: if has_cmd git; then ...
|
||||||
|
has_cmd() {
|
||||||
|
command -v "$1" >/dev/null 2>&1
|
||||||
|
}
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# STANDARD SETUP HEADER
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
# Print a standard setup header for scripts
|
||||||
|
# Usage: print_setup_header "Script Name"
|
||||||
|
print_setup_header() {
|
||||||
|
local title="$1"
|
||||||
|
local current_datetime
|
||||||
|
current_datetime=$(get_datetime)
|
||||||
|
echo "$title"
|
||||||
|
printf '=%.0s' $(seq 1 ${#title})
|
||||||
|
echo ""
|
||||||
|
echo "Current Date: $current_datetime"
|
||||||
|
echo "User: $USER"
|
||||||
|
echo "Original user: $(get_actual_user)"
|
||||||
|
if [[ $INTERACTIVE_MODE == "true" ]]; then
|
||||||
|
echo "Mode: Interactive (prompts enabled)"
|
||||||
|
else
|
||||||
|
echo "Mode: Automatic (auto-yes, use --interactive for prompts)"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# MOUNT/UNMOUNT HELPERS (for hosts guard and similar)
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
# Count mount layers for a path
|
||||||
|
# Usage: count=$(mount_layers_count "/etc/hosts")
|
||||||
|
mount_layers_count() {
|
||||||
|
local target="$1"
|
||||||
|
awk -v t="$target" '$5==t{c++} END{print c+0}' /proc/self/mountinfo 2>/dev/null || echo 0
|
||||||
|
}
|
||||||
|
|
||||||
|
# Collapse all bind mount layers for a path
|
||||||
|
# Usage: collapse_mounts "/etc/hosts" [max_iterations]
|
||||||
|
collapse_mounts() {
|
||||||
|
local target="$1"
|
||||||
|
local max_iter="${2:-20}"
|
||||||
|
local i=0
|
||||||
|
|
||||||
|
if has_cmd mountpoint; then
|
||||||
|
while mountpoint -q "$target"; do
|
||||||
|
umount -l "$target" >/dev/null 2>&1 || break
|
||||||
|
i=$((i + 1))
|
||||||
|
((i >= max_iter)) && break
|
||||||
|
done
|
||||||
|
else
|
||||||
|
local cnt
|
||||||
|
cnt=$(mount_layers_count "$target")
|
||||||
|
while ((cnt > 1)); do
|
||||||
|
umount -l "$target" >/dev/null 2>&1 || break
|
||||||
|
i=$((i + 1))
|
||||||
|
((i >= max_iter)) && break
|
||||||
|
cnt=$(mount_layers_count "$target")
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# RESOLUTION/FORMAT VALIDATION
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
# Validate resolution format (WIDTHxHEIGHT)
|
||||||
|
# Usage: if validate_resolution "1920x1080"; then ...
|
||||||
|
validate_resolution() {
|
||||||
|
local res="$1"
|
||||||
|
[[ $res =~ ^[0-9]+x[0-9]+$ ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
# Generate output filename with suffix
|
||||||
|
# Usage: output=$(generate_output_filename "input.jpg" "_resized")
|
||||||
|
generate_output_filename() {
|
||||||
|
local input="$1"
|
||||||
|
local suffix="$2"
|
||||||
|
local ext="${3:-}"
|
||||||
|
|
||||||
|
local basename dirname filename extension
|
||||||
|
basename=$(basename "$input")
|
||||||
|
dirname=$(dirname "$input")
|
||||||
|
filename="${basename%.*}"
|
||||||
|
extension="${basename##*.}"
|
||||||
|
|
||||||
|
# Handle files without extension
|
||||||
|
if [[ $filename == "$extension" ]]; then
|
||||||
|
extension="${ext:-jpg}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "${dirname}/${filename}${suffix}.${extension}"
|
||||||
|
}
|
||||||
153
linux_configuration/scripts/lib/common_packages.sh
Executable file
153
linux_configuration/scripts/lib/common_packages.sh
Executable file
@ -0,0 +1,153 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# Focus-app detection, command checks and package installation.
|
||||||
|
#
|
||||||
|
# Sourced by common.sh, which stays the single entry point every script
|
||||||
|
# sources -- 49 of them, so the public surface must not change.
|
||||||
|
|
||||||
|
# Check if any focus app is running (window-based detection)
|
||||||
|
# Returns 0 if focus app found, 1 otherwise
|
||||||
|
# Echoes the name of the found app
|
||||||
|
# =============================================================================
|
||||||
|
# FOCUS APP DETECTION (for digital wellbeing scripts)
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
# Default focus apps - can be overridden before calling is_focus_app_running
|
||||||
|
FOCUS_APPS_WINDOWS=(
|
||||||
|
"Visual Studio Code"
|
||||||
|
"VSCodium"
|
||||||
|
"Cursor"
|
||||||
|
"IntelliJ IDEA"
|
||||||
|
"PyCharm"
|
||||||
|
"WebStorm"
|
||||||
|
"CLion"
|
||||||
|
"Rider"
|
||||||
|
"Sublime Text"
|
||||||
|
"Blender"
|
||||||
|
"Godot"
|
||||||
|
"Unity"
|
||||||
|
"Unreal Editor"
|
||||||
|
)
|
||||||
|
|
||||||
|
FOCUS_APPS_PROCESSES=(
|
||||||
|
"steam_app_"
|
||||||
|
"gamescope"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
is_focus_app_running() {
|
||||||
|
# One xdotool call with a combined regex instead of N separate calls
|
||||||
|
if command -v xdotool &>/dev/null && [[ ${#FOCUS_APPS_WINDOWS[@]} -gt 0 ]]; then
|
||||||
|
local regex wid
|
||||||
|
printf -v regex '%s|' "${FOCUS_APPS_WINDOWS[@]}"
|
||||||
|
regex="${regex%|}" # strip trailing |
|
||||||
|
while IFS= read -r wid; do
|
||||||
|
[[ -n $wid ]] || continue
|
||||||
|
echo "focus app"
|
||||||
|
return 0
|
||||||
|
done < <(xdotool search --name "$regex" 2>/dev/null)
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check specific processes via /proc (no fork)
|
||||||
|
local app comm
|
||||||
|
for app in "${FOCUS_APPS_PROCESSES[@]}"; do
|
||||||
|
for comm in /proc/[0-9]*/comm; do
|
||||||
|
[[ -r $comm ]] || continue
|
||||||
|
read -r _proc_comm <"$comm" 2>/dev/null || continue
|
||||||
|
if [[ $_proc_comm == *"$app"* ]]; then
|
||||||
|
echo "$_proc_comm"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
done
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# COMMAND AVAILABILITY
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
# Check if a command exists
|
||||||
|
# Usage: if require_command ffmpeg; then ...
|
||||||
|
require_command() {
|
||||||
|
local cmd="$1"
|
||||||
|
local pkg="${2:-$1}"
|
||||||
|
if ! command -v "$cmd" >/dev/null 2>&1; then
|
||||||
|
echo "Error: '$cmd' is not installed or not in PATH." >&2
|
||||||
|
echo "Install with: sudo pacman -S $pkg" >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check for ImageMagick and display helpful installation message
|
||||||
|
# Usage: require_imagemagick [optional: "magick" or "convert"]
|
||||||
|
# Returns: Sets MAGICK_CMD variable to available command
|
||||||
|
require_imagemagick() {
|
||||||
|
local preferred="${1:-}"
|
||||||
|
|
||||||
|
if [[ $preferred == "magick" ]] || [[ -z $preferred ]]; then
|
||||||
|
if command -v magick &>/dev/null; then
|
||||||
|
MAGICK_CMD="magick"
|
||||||
|
export MAGICK_CMD
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ $preferred == "convert" ]] || [[ -z $preferred ]]; then
|
||||||
|
if command -v convert &>/dev/null; then
|
||||||
|
MAGICK_CMD="convert"
|
||||||
|
export MAGICK_CMD
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Error: ImageMagick is not installed." >&2
|
||||||
|
echo "Install it with:" >&2
|
||||||
|
echo " Arch Linux: sudo pacman -S imagemagick" >&2
|
||||||
|
echo " Ubuntu/Debian: sudo apt install imagemagick" >&2
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Install missing pacman packages
|
||||||
|
# Usage: install_missing_pacman_packages pkg1 pkg2 pkg3 ...
|
||||||
|
# Returns 0 if all packages installed successfully, 1 otherwise
|
||||||
|
install_missing_pacman_packages() {
|
||||||
|
local packages=("$@")
|
||||||
|
local missing=()
|
||||||
|
|
||||||
|
for pkg in "${packages[@]}"; do
|
||||||
|
if ! pacman -Qi "$pkg" >/dev/null 2>&1; then
|
||||||
|
missing+=("$pkg")
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
if [[ ${#missing[@]} -eq 0 ]]; then
|
||||||
|
echo "[INFO] All required packages are already installed."
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "[INFO] Installing missing packages: ${missing[*]}"
|
||||||
|
if ! sudo pacman -S --needed --noconfirm "${missing[@]}"; then
|
||||||
|
echo "[ERROR] Failed to install packages" >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# NOTIFICATION
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
# Send desktop notification (fails silently if notify-send not available)
|
||||||
|
# Usage: notify "Title" "Message" [urgency: low/normal/critical] [timeout_ms]
|
||||||
|
notify() {
|
||||||
|
local title="$1"
|
||||||
|
local message="$2"
|
||||||
|
local urgency="${3:-normal}"
|
||||||
|
local timeout="${4:-5000}"
|
||||||
|
|
||||||
|
if command -v notify-send &>/dev/null; then
|
||||||
|
notify-send -u "$urgency" -t "$timeout" "$title" "$message" 2>/dev/null || true
|
||||||
|
fi
|
||||||
|
}
|
||||||
91
linux_configuration/scripts/lib/common_services.sh
Executable file
91
linux_configuration/scripts/lib/common_services.sh
Executable file
@ -0,0 +1,91 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# Service control, directory helpers and the log_* family.
|
||||||
|
#
|
||||||
|
# Sourced by common.sh, which stays the single entry point every script
|
||||||
|
# sources -- 49 of them, so the public surface must not change.
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# FILE/PATH UTILITIES
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
# Get the directory containing the calling script
|
||||||
|
# Usage: SCRIPT_DIR=$(get_script_dir)
|
||||||
|
get_script_dir() {
|
||||||
|
dirname "$(readlink -f "${BASH_SOURCE[1]:-$0}")"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Ensure a directory exists
|
||||||
|
# Usage: ensure_dir "/path/to/dir"
|
||||||
|
ensure_dir() {
|
||||||
|
local dir="$1"
|
||||||
|
if [[ ! -d $dir ]]; then
|
||||||
|
mkdir -p "$dir"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# SYSTEMD HELPERS
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
# Internal helper for running systemctl with optional --user flag
|
||||||
|
_systemctl_cmd() {
|
||||||
|
local user_flag="$1"
|
||||||
|
shift
|
||||||
|
if [[ $user_flag == "--user" ]]; then
|
||||||
|
systemctl --user "$@"
|
||||||
|
else
|
||||||
|
systemctl "$@"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Enable and start a systemd service (user or system)
|
||||||
|
# Usage: enable_service "service-name" [--user]
|
||||||
|
enable_service() {
|
||||||
|
local service="$1"
|
||||||
|
local user_flag="${2:-}"
|
||||||
|
_systemctl_cmd "$user_flag" daemon-reload
|
||||||
|
_systemctl_cmd "$user_flag" enable --now "$service"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check if a systemd service is active
|
||||||
|
# Usage: if is_service_active "service-name" [--user]; then ...
|
||||||
|
is_service_active() {
|
||||||
|
_systemctl_cmd "${2:-}" is-active --quiet "$1"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check if a systemd service is enabled
|
||||||
|
# Usage: if is_service_enabled "service-name" [--user]; then ...
|
||||||
|
is_service_enabled() {
|
||||||
|
_systemctl_cmd "${2:-}" is-enabled --quiet "$1" 2>/dev/null
|
||||||
|
}
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# COLORED LOGGING (for scripts that need colored output)
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
# ANSI color codes
|
||||||
|
declare -g COLOR_RED='\033[1;31m'
|
||||||
|
declare -g COLOR_GREEN='\033[1;32m'
|
||||||
|
declare -g COLOR_YELLOW='\033[1;33m'
|
||||||
|
declare -g COLOR_BLUE='\033[1;34m'
|
||||||
|
declare -g COLOR_NC='\033[0m'
|
||||||
|
|
||||||
|
log_info() {
|
||||||
|
printf "${COLOR_BLUE}[INFO]${COLOR_NC} %s\n" "$*"
|
||||||
|
}
|
||||||
|
|
||||||
|
log_ok() {
|
||||||
|
printf "${COLOR_GREEN}[ OK ]${COLOR_NC} %s\n" "$*"
|
||||||
|
}
|
||||||
|
|
||||||
|
log_warn() {
|
||||||
|
printf "${COLOR_YELLOW}[WARN]${COLOR_NC} %s\n" "$*" >&2
|
||||||
|
}
|
||||||
|
|
||||||
|
log_error() {
|
||||||
|
printf "${COLOR_RED}[ERROR]${COLOR_NC} %s\n" "$*" >&2
|
||||||
|
}
|
||||||
|
|
||||||
|
# Alias for compatibility
|
||||||
|
warn() { log_warn "$@"; }
|
||||||
|
err() { log_error "$@"; }
|
||||||
@ -6,8 +6,8 @@
|
|||||||
|
|
||||||
## Where things stand
|
## Where things stand
|
||||||
|
|
||||||
**78 files** over 250 lines (was 183). Everything is committed and pushed to
|
**73 files** over 250 lines (was 183). Everything is committed and pushed to
|
||||||
`main` at `2ce86a5`. `focus_owner/analysis_options.yaml` has an uncommitted
|
`main` at `a68313f`. `focus_owner/analysis_options.yaml` has an uncommitted
|
||||||
change from an earlier session — **leave it unstaged**, it is not ours.
|
change from an earlier session — **leave it unstaged**, it is not ours.
|
||||||
|
|
||||||
**`python_pkg/` is DONE — 50 violations → 0.** Do not reopen it. Cleared one
|
**`python_pkg/` is DONE — 50 violations → 0.** Do not reopen it. Cleared one
|
||||||
@ -46,15 +46,16 @@ Earlier sessions had already done: the `~/utils` exemptions, the
|
|||||||
one reason: **state crosses the seam.** Pass it explicitly first, then split;
|
one reason: **state crosses the seam.** Pass it explicitly first, then split;
|
||||||
never suppress. See "When to give up on a seam".
|
never suppress. See "When to give up on a seam".
|
||||||
|
|
||||||
| File | Why |
|
| File | Why |
|
||||||
| ------------------------------------- | -------------------------------------------- |
|
| --------------------------------------- | -------------------------------------------- |
|
||||||
| `check_and_enable_services.sh` (1337) | every `check_*` writes one `SERVICE_STATUS` |
|
| `check_and_enable_services.sh` (1337) | every `check_*` writes one `SERVICE_STATUS` |
|
||||||
| `steam_compatibility.sh` (663) | `CACHE_MAP` written in a lib, read by `main` |
|
| `steam_compatibility.sh` (663) | `CACHE_MAP` written in a lib, read by `main` |
|
||||||
| `diagnose_pacman_hook_stall.sh` (493) | `LAST_ELAPSED` / `PACMAN_BIN` cross the seam |
|
| `diagnose_pacman_hook_stall.sh` (493) | `LAST_ELAPSED` / `PACMAN_BIN` cross the seam |
|
||||||
| `libre_translate.sh` (488) | ~19 globals cross any seam |
|
| `libre_translate.sh` (488) | ~19 globals cross any seam |
|
||||||
| `enforce_vbox_hosts.sh` (443) | every seam falls inside a heredoc |
|
| `enforce_vbox_hosts.sh` (443) | every seam falls inside a heredoc |
|
||||||
| `clean_audio.sh` (419) | probe-result globals through two libs |
|
| `clean_audio.sh` (419) | probe-result globals through two libs |
|
||||||
| `setup_passwordless_system.sh` (374) | no `main()`; top-level block kept by hand |
|
| `setup_passwordless_system.sh` (374) | no `main()`; top-level block kept by hand |
|
||||||
|
| `meta/scripts/optimize_vscode.py` (498) | `_apply_variant` calls back into the caller |
|
||||||
|
|
||||||
Same in TypeScript: `kcd2`'s `search.ts` and `badgeValue.ts`, and
|
Same in TypeScript: `kcd2`'s `search.ts` and `badgeValue.ts`, and
|
||||||
`reverse_survivors`' `sim.ts` (`step` ⇄ `survivorStep`) and `types.ts`.
|
`reverse_survivors`' `sim.ts` (`step` ⇄ `survivorStep`) and `types.ts`.
|
||||||
@ -63,8 +64,10 @@ Same in TypeScript: `kcd2`'s `search.ts` and `badgeValue.ts`, and
|
|||||||
caller's globals — stricter than `shellcheck -x` on the entry, and the check
|
caller's globals — stricter than `shellcheck -x` on the entry, and the check
|
||||||
that decides whether a seam is real. Run it before believing a split.
|
that decides whether a seam is real. Run it before believing a split.
|
||||||
|
|
||||||
Next: `billsplit` (Dart, untouched), then the remaining `main()`-shaped shell
|
`billsplit`'s last file is Flutter's generated `win32_window.cpp`; editing it
|
||||||
scripts. `focus_owner`'s six leftovers are four Kotlin sources that read a
|
would be undone by `flutter create`, so it needs an exemption or a shrug.
|
||||||
|
|
||||||
|
Next: the remaining `main()`-shaped shell scripts, then `bucket_catch` (TS). `focus_owner`'s six leftovers are four Kotlin sources that read a
|
||||||
private `context`, one State class, and one test one line over.
|
private `context`, one State class, and one test one line over.
|
||||||
|
|
||||||
Baselines and the per-language traps (Dart `part`, Kotlin fixture objects, the
|
Baselines and the per-language traps (Dart `part`, Kotlin fixture objects, the
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user