barcode_scanner/lib/pages/operation/reception/reception_page_model.dart
mandreshope 1d15fd4d1c refactor: Streamlines stock picking data model
Refactors the `getAllStockPiking` API call to directly return a list of `StockPickingRecordModel`.

This change simplifies the data structure by removing the `StockPickingResponseModel` wrapper, making the `receptions` state in `ReceptionPageState` a direct, non-nullable list. It improves type safety and reduces verbose property access in the UI.
2025-07-30 14:32:28 +03:00

69 lines
2.2 KiB
Dart

import 'package:e_scan/backend/api/api_calls.dart';
import 'package:e_scan/backend/schema/stock_picking/stock_picking_record_model.dart';
import 'package:e_scan/backend/schema/user/user_struct.dart';
import 'package:e_scan/services/secure_storage.dart';
import 'package:e_scan/services/token_provider.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'reception_page_model.freezed.dart';
final receptionPageModelProvider =
StateNotifierProvider<ReceptionPageModel, ReceptionPageState>((ref) {
return ReceptionPageModel(
secureStorage: ref.read(sharedPrefsProvider),
tokenProvider: ref.read(tokenProvider),
userConnectedProvider: ref.read(userConnectedProvider),
);
});
class ReceptionPageModel extends StateNotifier<ReceptionPageState> {
ReceptionPageModel({
required this.secureStorage,
required this.tokenProvider,
required this.userConnectedProvider,
}) : super(const ReceptionPageState()) {
getAllReceptions();
}
late FlutterSecureStorage secureStorage;
late TokenProvider tokenProvider;
final UserConnectedProvider userConnectedProvider;
Future getUserConnected() async {
state = state.copyWith(loadingUser: true);
final user = await userConnectedProvider.get();
state = state.copyWith(user: user, loadingUser: false);
}
Future getAllReceptions() async {
try {
state = state.copyWith(loadingReceptions: true);
final res = await ApiCalls.getAllStockPiking();
res.when(
(data) {
state = state.copyWith(receptions: data, loadingReceptions: false);
},
(error) {
state = state.copyWith(loadingReceptions: false);
},
);
} catch (e) {
state = state.copyWith(loadingReceptions: false);
}
}
}
@freezed
abstract class ReceptionPageState with _$ReceptionPageState {
const factory ReceptionPageState({
UserStruct? user,
@Default(<StockPickingRecordModel>[])
List<StockPickingRecordModel> receptions,
@Default(false) bool loadingReceptions,
@Default(false) bool loadingUser,
}) = _ReceptionPageState;
}