Files
nearle_pos/lib/presentation/modules/providers/printer_settings.dart
2026-07-31 17:06:52 +05:30

119 lines
3.5 KiB
Dart

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:printing/printing.dart';
import '../../../app/providers.dart';
import '../../../data/local/app_database.dart';
/// Hardware preferences for this terminal.
///
/// Persisted in `app_meta` rather than held in widget state, so the choice
/// survives a restart — a cashier should not have to reselect the printer
/// every morning.
class PrinterSettings {
const PrinterSettings({
this.printerUrl,
this.printerName,
this.autoPrint = false,
this.openDrawer = true,
});
/// Target passed to `directPrintPdf`. Null means "use the system default".
final String? printerUrl;
/// Shown in Settings. Kept alongside the url because the url is opaque.
final String? printerName;
/// Print silently the moment a sale completes.
///
/// Defaults to **off**: with no printer attached, auto-printing fails
/// invisibly and looks like a bug.
final bool autoPrint;
final bool openDrawer;
bool get hasPrinter => printerUrl != null;
PrinterSettings copyWith({
String? printerUrl,
String? printerName,
bool clearPrinter = false,
bool? autoPrint,
bool? openDrawer,
}) {
return PrinterSettings(
printerUrl: clearPrinter ? null : (printerUrl ?? this.printerUrl),
printerName: clearPrinter ? null : (printerName ?? this.printerName),
autoPrint: autoPrint ?? this.autoPrint,
openDrawer: openDrawer ?? this.openDrawer,
);
}
}
class PrinterSettingsController extends StateNotifier<PrinterSettings> {
PrinterSettingsController(this._ref) : super(const PrinterSettings()) {
_load();
}
final Ref _ref;
Future<void> _load() async {
final store = _ref.read(localStoreProvider);
if (!store.isReady) return;
final dao = store.catalogue;
state = PrinterSettings(
printerUrl: await dao.meta(MetaKeys.printerUrl),
printerName: await dao.meta(MetaKeys.printerName),
autoPrint: (await dao.meta(MetaKeys.autoPrint)) == '1',
openDrawer: (await dao.meta(MetaKeys.openDrawer)) != '0',
);
}
Future<void> selectPrinter(Printer? printer) async {
final dao = _ref.read(localStoreProvider).catalogue;
if (printer == null) {
await dao.setMeta(MetaKeys.printerUrl, '');
await dao.setMeta(MetaKeys.printerName, '');
state = state.copyWith(clearPrinter: true, autoPrint: false);
return;
}
await dao.setMeta(MetaKeys.printerUrl, printer.url);
await dao.setMeta(MetaKeys.printerName, printer.name);
state = state.copyWith(
printerUrl: printer.url,
printerName: printer.name,
);
}
Future<void> setAutoPrint(bool value) async {
// Auto-print with nothing selected would fail silently on every sale.
if (value && !state.hasPrinter) return;
await _ref
.read(localStoreProvider)
.catalogue
.setMeta(MetaKeys.autoPrint, value ? '1' : '0');
state = state.copyWith(autoPrint: value);
}
Future<void> setOpenDrawer(bool value) async {
await _ref
.read(localStoreProvider)
.catalogue
.setMeta(MetaKeys.openDrawer, value ? '1' : '0');
state = state.copyWith(openDrawer: value);
}
}
final printerSettingsProvider =
StateNotifierProvider<PrinterSettingsController, PrinterSettings>(
(ref) => PrinterSettingsController(ref),
);
/// Printers the OS can currently see. Re-read whenever Settings is opened.
final availablePrintersProvider = FutureProvider<List<Printer>>(
(ref) => ref.watch(receiptServiceProvider).availablePrinters(),
);