Produk kini punya cara jual `unit` (cacah) atau `weight` (timbangan), mengikuti kontrak backend di docs/integration-weight-based-products.md. - tipe SellBy, domain/infrastructure unit baru, dan endpoint /api/v1/units - dialog input berat saat produk timbangan ditambahkan ke keranjang - tiap penimbangan jadi baris keranjang sendiri; total baris dihitung berat x harga satuan, sedangkan produk cacah tetap digabung - request ke backend mengirim quantity 1 dan membawa berat - migrasi DB v3: kolom sell_by, unit_id, unit_name, unit_abbreviation di tabel products supaya satuan tetap tampil saat offline - tampilan keranjang, struk, split bill, void, dan refund memakai berat - test/weight_based_products_test.dart menutup perilaku di atas (9 test) Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
39 lines
1.2 KiB
Dart
39 lines
1.2 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:injectable/injectable.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import '../../../common/constant/local_storage_key.dart';
|
|
import '../unit_dtos.dart';
|
|
|
|
/// Satuan berjumlah sedikit dan jarang berubah, jadi cukup disimpan sebagai
|
|
/// satu blob JSON di SharedPreferences — tidak perlu tabel sqflite sendiri.
|
|
@injectable
|
|
class UnitLocalDataProvider {
|
|
final SharedPreferences _sharedPreferences;
|
|
|
|
UnitLocalDataProvider(this._sharedPreferences);
|
|
|
|
Future<void> saveUnits(List<UnitDto> units) async {
|
|
final jsonString = jsonEncode(units.map((e) => e.toJson()).toList());
|
|
await _sharedPreferences.setString(LocalStorageKey.units, jsonString);
|
|
}
|
|
|
|
List<UnitDto> getUnits() {
|
|
final jsonString = _sharedPreferences.getString(LocalStorageKey.units);
|
|
if (jsonString == null || jsonString.isEmpty) return const [];
|
|
|
|
final decoded = jsonDecode(jsonString);
|
|
if (decoded is! List) return const [];
|
|
|
|
return decoded
|
|
.whereType<Map<String, dynamic>>()
|
|
.map(UnitDto.fromJson)
|
|
.toList();
|
|
}
|
|
|
|
Future<void> clearUnits() async {
|
|
await _sharedPreferences.remove(LocalStorageKey.units);
|
|
}
|
|
}
|