fix: cetak semua station tanpa saling membatalkan
printStruckOrder berhenti di kegagalan pertama, jadi satu station gagal membuat semua station sesudahnya tidak pernah dicoba. Karena checker (Bluetooth) berada sebelum kitchen dan bar (Network) dalam rantai, satu kegagalan koneksi Bluetooth menghilangkan tiga tiket sekaligus, dua di antaranya di jaringan yang sehat. Jalur printer Network juga mengembalikan right(false) saat gagal, yang lolos sebagai sukses ke pemanggil, sehingga kasir melihat 'berhasil dicetak' padahal tidak ada kertas keluar. Perubahan: repository kini mengembalikan Stream<List<PrintStationResult>> dan tetap mencoba semua station; kegagalan Network jadi left; station Network berjalan paralel dengan rantai Bluetooth; station Bluetooth dikelompokkan per alamat agar perpindahan koneksi seminimal mungkin; CapabilityProfile di-cache. Bluetooth: retry connect dengan backoff dan disconnect di tiap percobaan ulang (socket RFCOMM setengah terbuka membuat connect berulang gagal identik), jeda settle dikembalikan setelah dipangkas ke 300ms, satu lock untuk seluruh radio (bukan per MAC), izin runtime dipastikan di jalur cetak, dan repository jadi LazySingleton agar state koneksi benar-benar dibagi. UI: dialog progres per-station menggantikan toast tunggal, lengkap dengan durasi tiap station dan tombol cetak ulang khusus yang gagal. Enam listener duplikat di halaman diganti satu listener terpusat di AppWidget. Menu Print di detail transaksi dapat item Print All. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:dartz/dartz.dart' hide Order;
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:injectable/injectable.dart' hide Order;
|
||||
|
||||
@@ -21,136 +20,63 @@ class PrintStruckBloc extends Bloc<PrintStruckEvent, PrintStruckState> {
|
||||
Future<void> _onPrintStruckEvent(
|
||||
PrintStruckEvent event,
|
||||
Emitter<PrintStruckState> emit,
|
||||
) {
|
||||
return event.map(
|
||||
order: (e) async {
|
||||
Either<PrinterFailure, Unit> failureOrSuccess;
|
||||
) => event.map(
|
||||
order: (e) => _print(emit, e.order, _stationsForOrder(e.order)),
|
||||
payment: (e) => _print(emit, e.order, const [PrintStation.receipt]),
|
||||
cashier: (e) => _print(emit, e.order, const [PrintStation.cashier]),
|
||||
checker: (e) => _print(emit, e.order, const [PrintStation.checker]),
|
||||
kitchen: (e) => _print(emit, e.order, const [PrintStation.kitchen]),
|
||||
bar: (e) => _print(emit, e.order, const [PrintStation.bar]),
|
||||
voided: (e) => _print(emit, e.order, const [PrintStation.voided]),
|
||||
splitBill: (e) => _print(emit, e.order, const [PrintStation.splitBill]),
|
||||
retryFailed: (_) {
|
||||
final order = state.order;
|
||||
final failed = [
|
||||
for (final station in state.failedStations) station.station,
|
||||
];
|
||||
|
||||
emit(state.copyWith(isPrinting: true, failureOrPrintStruck: none()));
|
||||
if (order == null || failed.isEmpty) return Future.value();
|
||||
|
||||
failureOrSuccess = await _printerRepository.printStruckOrder(
|
||||
order: e.order,
|
||||
);
|
||||
return _print(emit, order, failed);
|
||||
},
|
||||
);
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
isPrinting: false,
|
||||
failureOrPrintStruck: optionOf(failureOrSuccess),
|
||||
),
|
||||
);
|
||||
},
|
||||
cashier: (e) async {
|
||||
Either<PrinterFailure, Unit> failureOrSuccess;
|
||||
/// Struk pelanggan hanya ikut dicetak kalau ordernya sudah dibayar.
|
||||
List<PrintStation> _stationsForOrder(Order order) => [
|
||||
if (order.totalPaid > 0) PrintStation.receipt,
|
||||
PrintStation.cashier,
|
||||
PrintStation.checker,
|
||||
PrintStation.kitchen,
|
||||
PrintStation.bar,
|
||||
];
|
||||
|
||||
emit(state.copyWith(isPrinting: true, failureOrPrintStruck: none()));
|
||||
Future<void> _print(
|
||||
Emitter<PrintStruckState> emit,
|
||||
Order order,
|
||||
List<PrintStation> stations,
|
||||
) async {
|
||||
// Tombol yang tertekan dua kali tidak boleh memulai job kedua — repository
|
||||
// akan mengantrikannya dan progres yang tampil jadi membingungkan.
|
||||
if (state.isPrinting) return;
|
||||
|
||||
failureOrSuccess = await _printerRepository.printStruckCashier(
|
||||
order: e.order,
|
||||
);
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
isPrinting: false,
|
||||
failureOrPrintStruck: optionOf(failureOrSuccess),
|
||||
),
|
||||
);
|
||||
},
|
||||
payment: (e) async {
|
||||
Either<PrinterFailure, Unit> failureOrSuccess;
|
||||
|
||||
emit(state.copyWith(isPrinting: true, failureOrPrintStruck: none()));
|
||||
|
||||
failureOrSuccess = await _printerRepository.printStruckPayment(
|
||||
order: e.order,
|
||||
);
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
isPrinting: false,
|
||||
failureOrPrintStruck: optionOf(failureOrSuccess),
|
||||
),
|
||||
);
|
||||
},
|
||||
voided: (e) async {
|
||||
Either<PrinterFailure, Unit> failureOrSuccess;
|
||||
|
||||
emit(state.copyWith(isPrinting: true, failureOrPrintStruck: none()));
|
||||
|
||||
failureOrSuccess = await _printerRepository.printStruckVoid(
|
||||
order: e.order,
|
||||
);
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
isPrinting: false,
|
||||
failureOrPrintStruck: optionOf(failureOrSuccess),
|
||||
),
|
||||
);
|
||||
},
|
||||
splitBill: (e) async {
|
||||
Either<PrinterFailure, Unit> failureOrSuccess;
|
||||
|
||||
emit(state.copyWith(isPrinting: true, failureOrPrintStruck: none()));
|
||||
|
||||
failureOrSuccess = await _printerRepository.printStruckSplitBill(
|
||||
order: e.order,
|
||||
);
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
isPrinting: false,
|
||||
failureOrPrintStruck: optionOf(failureOrSuccess),
|
||||
),
|
||||
);
|
||||
},
|
||||
kitchen: (e) async {
|
||||
Either<PrinterFailure, Unit> failureOrSuccess;
|
||||
|
||||
emit(state.copyWith(isPrinting: true, failureOrPrintStruck: none()));
|
||||
|
||||
failureOrSuccess = await _printerRepository.printStruckKitchen(
|
||||
order: e.order,
|
||||
);
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
isPrinting: false,
|
||||
failureOrPrintStruck: optionOf(failureOrSuccess),
|
||||
),
|
||||
);
|
||||
},
|
||||
bar: (e) async {
|
||||
Either<PrinterFailure, Unit> failureOrSuccess;
|
||||
|
||||
emit(state.copyWith(isPrinting: true, failureOrPrintStruck: none()));
|
||||
|
||||
failureOrSuccess = await _printerRepository.printStruckBar(
|
||||
order: e.order,
|
||||
);
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
isPrinting: false,
|
||||
failureOrPrintStruck: optionOf(failureOrSuccess),
|
||||
),
|
||||
);
|
||||
},
|
||||
checker: (e) async {
|
||||
Either<PrinterFailure, Unit> failureOrSuccess;
|
||||
|
||||
emit(state.copyWith(isPrinting: true, failureOrPrintStruck: none()));
|
||||
|
||||
failureOrSuccess = await _printerRepository.printStruckChecker(
|
||||
order: e.order,
|
||||
);
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
isPrinting: false,
|
||||
failureOrPrintStruck: optionOf(failureOrSuccess),
|
||||
),
|
||||
);
|
||||
},
|
||||
emit(
|
||||
state.copyWith(
|
||||
isPrinting: true,
|
||||
order: order,
|
||||
jobId: state.jobId + 1,
|
||||
stations: [
|
||||
for (final station in stations) PrintStationResult.pending(station),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
await emit.forEach<List<PrintStationResult>>(
|
||||
_printerRepository.printStations(order: order, stations: stations),
|
||||
onData: (results) => state.copyWith(stations: results),
|
||||
);
|
||||
|
||||
// Cetak bisa selesai setelah bloc ditutup (kasir keluar dari aplikasi).
|
||||
if (isClosed) return;
|
||||
emit(state.copyWith(isPrinting: false));
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -2,7 +2,9 @@ part of 'print_struck_bloc.dart';
|
||||
|
||||
@freezed
|
||||
class PrintStruckEvent with _$PrintStruckEvent {
|
||||
/// Cetak seluruh station untuk satu order.
|
||||
const factory PrintStruckEvent.order(Order order) = _Order;
|
||||
|
||||
const factory PrintStruckEvent.cashier(Order order) = _Cashier;
|
||||
const factory PrintStruckEvent.payment(Order order) = _Payment;
|
||||
const factory PrintStruckEvent.voided(Order order) = _Voided;
|
||||
@@ -10,4 +12,8 @@ class PrintStruckEvent with _$PrintStruckEvent {
|
||||
const factory PrintStruckEvent.kitchen(Order order) = _Kitchen;
|
||||
const factory PrintStruckEvent.bar(Order order) = _Bar;
|
||||
const factory PrintStruckEvent.splitBill(Order order) = _SplitBill;
|
||||
|
||||
/// Ulangi hanya station yang gagal di job terakhir, tanpa mencetak ulang
|
||||
/// station yang sudah keluar.
|
||||
const factory PrintStruckEvent.retryFailed() = _RetryFailed;
|
||||
}
|
||||
|
||||
@@ -2,11 +2,33 @@ part of 'print_struck_bloc.dart';
|
||||
|
||||
@freezed
|
||||
class PrintStruckState with _$PrintStruckState {
|
||||
factory PrintStruckState({
|
||||
required Option<Either<PrinterFailure, Unit>> failureOrPrintStruck,
|
||||
const PrintStruckState._();
|
||||
|
||||
const factory PrintStruckState({
|
||||
/// Status seluruh station di job cetak terakhir, urut sesuai urutan cetak.
|
||||
@Default(<PrintStationResult>[]) List<PrintStationResult> stations,
|
||||
@Default(false) bool isPrinting,
|
||||
|
||||
/// Order yang sedang dicetak, disimpan supaya station yang gagal bisa
|
||||
/// diulang tanpa perlu dikirim ulang dari halaman pemanggil.
|
||||
Order? order,
|
||||
|
||||
/// Naik tiap job baru dimulai. UI memakainya untuk tahu kapan harus
|
||||
/// membuka dialog progres.
|
||||
@Default(0) int jobId,
|
||||
}) = _PrintStruckState;
|
||||
|
||||
factory PrintStruckState.initial() =>
|
||||
PrintStruckState(failureOrPrintStruck: none());
|
||||
factory PrintStruckState.initial() => const PrintStruckState();
|
||||
|
||||
List<PrintStationResult> get failedStations =>
|
||||
stations.where((station) => station.isFailed).toList();
|
||||
|
||||
bool get hasFailure => failedStations.isNotEmpty;
|
||||
|
||||
/// Station yang benar-benar dicetak — yang dilewati tidak ikut dihitung.
|
||||
int get plannedCount =>
|
||||
stations.where((station) => !station.isSkipped).length;
|
||||
|
||||
int get successCount =>
|
||||
stations.where((station) => station.isSuccess).length;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user