barcode_scanner/lib/pages/login_page/login_page_model.dart
mandreshope 44289e29dc feat: Adds product image field to data models
Includes an `image` field in the `ProductStruct` and `ProductEntity` data models.

Updates the ObjectBox schema and regenerates the necessary code to persist the new field.

Refactors the `ProductScannedComponent` to accept a `ProductStruct` object, simplifying parameter passing.

Updates the `ScannerPage` to build and pass the `ProductStruct` to the component, enabling the display of the product image and using the structured data for other details like quantity. The 'Marque' display is replaced with 'Quantité' in the component.
2025-07-03 17:20:44 +03:00

123 lines
3.8 KiB
Dart

import 'package:barcode_scanner/backend/schema/user/user_struct.dart';
import 'package:barcode_scanner/services/secure_storage.dart';
import 'package:barcode_scanner/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 'login_page_model.freezed.dart';
/// The provider for the AuthViewModel, using Riverpod's StateNotifierProvider
/// with autoDispose to manage the lifecycle of the view model.
final loginPageModelProvider =
StateNotifierProvider<LoginPageModel, LoginPageState>((ref) {
return LoginPageModel(
secureStorage: ref.read(sharedPrefsProvider),
tokenProvider: ref.read(tokenProvider),
);
});
class LoginPageModel extends StateNotifier<LoginPageState> {
/// Constructor initializes the TaskRepository using the provider reference.
LoginPageModel({required this.secureStorage, required this.tokenProvider})
: super(const LoginPageState());
late FlutterSecureStorage secureStorage;
late TokenProvider tokenProvider;
Future<void> checkHasUserConnected() async {
try {
state = state.copyWith(
loading: true,
status: LoginPageStateStatus.logOut,
);
final token = await secureStorage.read(key: TokenProvider.tokenKey);
state = state.copyWith(
loading: false,
status: token != null
? LoginPageStateStatus.logged
: LoginPageStateStatus.logOut,
);
} catch (e) {
debugPrint("Error reading secure storage, clearing storage...");
await secureStorage
.deleteAll(); // Suppression des anciennes données en cas d'erreur
}
}
Future<void> signIn({
required String email,
required String password,
VoidCallback? onSuccess,
VoidCallback? onError,
}) async {
try {
state = state.copyWith(loading: true);
await Future.delayed(Duration(seconds: 5));
if (email == "user@yopmail.com" && password == "password") {
setTokenInLocal(
'token',
UserStruct(
id: '1',
firstName: 'User',
lastName: 'Anonymous',
email: 'user@yopmail.com',
),
);
state = state.copyWith(
loading: false,
status: LoginPageStateStatus.logOut,
);
onSuccess?.call();
} else {
state = state.copyWith(
loading: false,
status: LoginPageStateStatus.logOut,
);
}
} catch (e) {
state = state.copyWith(
loading: false,
status: LoginPageStateStatus.logOut,
);
debugPrint("$e");
// Toast.showError(e.toString());
}
}
Future<void> setTokenInLocal(String token, UserStruct user) async {
await Future.wait([
tokenProvider.setToken(token),
tokenProvider.setRefreshToken(token),
user.setToLocalStorage(),
]);
state = state.copyWith(loading: false, status: LoginPageStateStatus.logged);
debugPrint(token);
}
Future<void> logOut() async {
await secureStorage.delete(key: TokenProvider.tokenKey);
await checkHasUserConnected();
}
}
@freezed
abstract class LoginPageState with _$LoginPageState {
const factory LoginPageState({
@Default(LoginPageStateStatus.logOut) LoginPageStateStatus status,
@Default(false) bool hasLangSelected,
@Default(false) bool loading,
@Default(false) bool googleSigninloading,
}) = _LoginPageState;
}
enum LoginPageStateStatus { otpSent, logged, logOut }
extension LoginPageStateStatusExt on LoginPageStateStatus {
bool get isLogged => this == LoginPageStateStatus.logged;
bool get isLogOut => this == LoginPageStateStatus.logOut;
}