
Adds an `image` field to the user data structure to support profile pictures. Introduces a new profile page and sets up navigation from the home screen. Changes the product list page provider to auto dispose for improved resource management.
55 lines
1.4 KiB
Dart
55 lines
1.4 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:barcode_scanner/provider_container.dart';
|
|
import 'package:barcode_scanner/services/secure_storage.dart';
|
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
|
|
|
part 'user_struct.freezed.dart';
|
|
part 'user_struct.g.dart';
|
|
|
|
@Freezed(toJson: true)
|
|
abstract class UserStruct with _$UserStruct {
|
|
factory UserStruct({
|
|
String? id,
|
|
String? image,
|
|
String? firstName,
|
|
String? lastName,
|
|
String? email,
|
|
String? phone,
|
|
}) = _UserStruct;
|
|
|
|
factory UserStruct.fromJson(Map<String, dynamic> json) =>
|
|
_$UserStructFromJson(json);
|
|
}
|
|
|
|
extension UserStructExt on UserStruct {
|
|
String get key => 'user';
|
|
|
|
String get fullName {
|
|
final sb = StringBuffer();
|
|
sb.write(firstName);
|
|
if (firstName != null) {
|
|
sb.write(' ');
|
|
}
|
|
sb.write(lastName);
|
|
return sb.toString();
|
|
}
|
|
|
|
Future setToLocalStorage() {
|
|
final storage = providerContainer.read(sharedPrefsProvider);
|
|
return storage.write(key: id ?? key, value: jsonEncode(toJson()));
|
|
}
|
|
|
|
Future<UserStruct?> getFromLocalStorage() async {
|
|
final storage = providerContainer.read(sharedPrefsProvider);
|
|
final jsonString = await storage.read(key: id ?? key);
|
|
if (jsonString == null) return null;
|
|
return UserStruct.fromJson(jsonDecode(jsonString));
|
|
}
|
|
|
|
Future<void> deleteLocalStorage() {
|
|
final storage = providerContainer.read(sharedPrefsProvider);
|
|
return storage.delete(key: id ?? key);
|
|
}
|
|
}
|