Initialer Commit: Projekt Start
This commit is contained in:
118
lib/providers/cart_provider.dart
Normal file
118
lib/providers/cart_provider.dart
Normal file
@@ -0,0 +1,118 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'dart:convert';
|
||||
import '../models/product.dart';
|
||||
|
||||
class CartItem {
|
||||
final Product product;
|
||||
int quantity;
|
||||
|
||||
CartItem({
|
||||
required this.product,
|
||||
this.quantity = 1,
|
||||
});
|
||||
|
||||
double get totalPrice => product.priceValue * quantity;
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'product_id': product.id,
|
||||
'quantity': quantity,
|
||||
'product': product.toJson(),
|
||||
};
|
||||
}
|
||||
|
||||
factory CartItem.fromJson(Map<String, dynamic> json) {
|
||||
return CartItem(
|
||||
product: Product.fromJson(json['product']),
|
||||
quantity: json['quantity'] ?? 1,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class CartProvider extends ChangeNotifier {
|
||||
final List<CartItem> _items = [];
|
||||
static const String _cartKey = 'cart_items';
|
||||
|
||||
List<CartItem> get items => _items;
|
||||
|
||||
int get itemCount => _items.fold(0, (sum, item) => sum + item.quantity);
|
||||
|
||||
double get totalPrice {
|
||||
return _items.fold(0.0, (sum, item) => sum + item.totalPrice);
|
||||
}
|
||||
|
||||
CartProvider() {
|
||||
_loadCart();
|
||||
}
|
||||
|
||||
/// Lädt gespeicherten Warenkorb
|
||||
Future<void> _loadCart() async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final cartJson = prefs.getString(_cartKey);
|
||||
|
||||
if (cartJson != null) {
|
||||
final List<dynamic> cartData = json.decode(cartJson);
|
||||
_items.clear();
|
||||
_items.addAll(
|
||||
cartData.map((item) => CartItem.fromJson(item)).toList(),
|
||||
);
|
||||
notifyListeners();
|
||||
}
|
||||
} catch (e) {
|
||||
print('Fehler beim Laden des Warenkorbs: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Speichert Warenkorb
|
||||
Future<void> _saveCart() async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final cartJson = json.encode(
|
||||
_items.map((item) => item.toJson()).toList(),
|
||||
);
|
||||
await prefs.setString(_cartKey, cartJson);
|
||||
} catch (e) {
|
||||
print('Fehler beim Speichern des Warenkorbs: $e');
|
||||
}
|
||||
}
|
||||
|
||||
void addItem(Product product) {
|
||||
final existingIndex = _items.indexWhere((item) => item.product.id == product.id);
|
||||
|
||||
if (existingIndex >= 0) {
|
||||
_items[existingIndex].quantity++;
|
||||
} else {
|
||||
_items.add(CartItem(product: product));
|
||||
}
|
||||
notifyListeners();
|
||||
_saveCart();
|
||||
}
|
||||
|
||||
void removeItem(Product product) {
|
||||
final existingIndex = _items.indexWhere((item) => item.product.id == product.id);
|
||||
|
||||
if (existingIndex >= 0) {
|
||||
if (_items[existingIndex].quantity > 1) {
|
||||
_items[existingIndex].quantity--;
|
||||
} else {
|
||||
_items.removeAt(existingIndex);
|
||||
}
|
||||
notifyListeners();
|
||||
_saveCart();
|
||||
}
|
||||
}
|
||||
|
||||
void removeItemCompletely(Product product) {
|
||||
_items.removeWhere((item) => item.product.id == product.id);
|
||||
notifyListeners();
|
||||
_saveCart();
|
||||
}
|
||||
|
||||
void clearCart() {
|
||||
_items.clear();
|
||||
notifyListeners();
|
||||
_saveCart();
|
||||
}
|
||||
}
|
||||
63
lib/providers/user_provider.dart
Normal file
63
lib/providers/user_provider.dart
Normal file
@@ -0,0 +1,63 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import '../models/user.dart';
|
||||
import '../services/auth_service.dart';
|
||||
|
||||
class UserProvider extends ChangeNotifier {
|
||||
final AuthService _authService = AuthService();
|
||||
User? _user;
|
||||
bool _isLoading = false;
|
||||
|
||||
User? get user => _user;
|
||||
bool get isLoggedIn => _user != null;
|
||||
bool get isLoading => _isLoading;
|
||||
|
||||
UserProvider() {
|
||||
_loadSavedUser();
|
||||
}
|
||||
|
||||
Future<void> _loadSavedUser() async {
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
_user = await _authService.getSavedUser();
|
||||
} catch (e) {
|
||||
print('Fehler beim Laden des Benutzers: $e');
|
||||
} finally {
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> login(String username, String password) async {
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
final user = await _authService.login(username, password);
|
||||
if (user != null) {
|
||||
_user = user;
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
return true;
|
||||
}
|
||||
} catch (e) {
|
||||
print('Login-Fehler: $e');
|
||||
}
|
||||
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
return false;
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
await _authService.logout();
|
||||
_user = null;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> refreshUser() async {
|
||||
await _loadSavedUser();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user