- Datenmodell: users/{uid}, households/{id} mit memberUids, plants/locations als Subcollections
- StreamProvider liefern Live-Daten, Repositories kapseln Schreibzugriffe
- Login-Screen mit Registrierung (legt Haushalt automatisch an), Logout in Einstellungen
- Router-Redirect bei Login/Logout, firestore.rules mit Haushalts-Prinzip
- Tests auf firebase_auth_mocks + fake_cloud_firestore umgestellt
- Verifiziert: analyze/test grün, Android-Debug-Build erfolgreich
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
63 lines
1.9 KiB
Dart
63 lines
1.9 KiB
Dart
import 'package:cloud_firestore/cloud_firestore.dart';
|
||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||
|
||
import '../../../core/firebase/firebase_providers.dart';
|
||
import '../../household/data/household_providers.dart';
|
||
import '../domain/plant_location.dart';
|
||
|
||
/// Firestore-Anbindung der Stellplätze:
|
||
/// households/{householdId}/locations/{locationId}
|
||
|
||
/// Alle Stellplätze des Haushalts, live aus Firestore.
|
||
final locationsProvider = StreamProvider<List<PlantLocation>>((ref) {
|
||
final householdId = ref.watch(householdIdProvider).value;
|
||
if (householdId == null) return Stream.value(const []);
|
||
|
||
return ref
|
||
.watch(firestoreProvider)
|
||
.collection('households')
|
||
.doc(householdId)
|
||
.collection('locations')
|
||
.orderBy('name')
|
||
.snapshots()
|
||
.map((snapshot) => [
|
||
for (final doc in snapshot.docs)
|
||
PlantLocation(id: doc.id, name: doc.data()['name'] as String? ?? ''),
|
||
]);
|
||
});
|
||
|
||
final locationByIdProvider =
|
||
Provider.family<PlantLocation?, String?>((ref, id) {
|
||
if (id == null) return null;
|
||
final locations = ref.watch(locationsProvider).value ?? const [];
|
||
for (final location in locations) {
|
||
if (location.id == id) return location;
|
||
}
|
||
return null;
|
||
});
|
||
|
||
class LocationRepository {
|
||
LocationRepository(this._firestore, this._householdIdGetter);
|
||
|
||
final FirebaseFirestore _firestore;
|
||
final String? Function() _householdIdGetter;
|
||
|
||
Future<void> addLocation(String name) {
|
||
final householdId = _householdIdGetter();
|
||
if (householdId == null) {
|
||
throw StateError('Kein Haushalt geladen – Aktion nicht möglich.');
|
||
}
|
||
return _firestore
|
||
.collection('households')
|
||
.doc(householdId)
|
||
.collection('locations')
|
||
.add({'name': name});
|
||
}
|
||
}
|
||
|
||
final locationRepositoryProvider = Provider<LocationRepository>((ref) {
|
||
return LocationRepository(
|
||
ref.watch(firestoreProvider),
|
||
() => ref.read(householdIdProvider).value,
|
||
);
|
||
});
|