Agrego frontend app
This commit is contained in:
+67
@@ -0,0 +1,67 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:gimnasio_soma/features/actividades/data/repositories/actividades_repository_impl.dart';
|
||||
import 'package:gimnasio_soma/features/actividades/domain/entities/actividad.dart';
|
||||
import 'package:gimnasio_soma/features/actividades/domain/repositories/actividades_repository.dart';
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
|
||||
String _errorMessage(Object e) {
|
||||
if (e is PostgrestException) return e.message;
|
||||
return e.toString().replaceFirst('Exception: ', '');
|
||||
}
|
||||
|
||||
final actividadesRepositoryProvider = Provider<ActividadesRepository>((ref) {
|
||||
return ActividadesRepositoryImpl();
|
||||
});
|
||||
|
||||
final actividadesProvider =
|
||||
StateNotifierProvider<ActividadesNotifier, AsyncValue<List<Actividad>>>((ref) {
|
||||
return ActividadesNotifier(ref.read(actividadesRepositoryProvider));
|
||||
});
|
||||
|
||||
class ActividadesNotifier extends StateNotifier<AsyncValue<List<Actividad>>> {
|
||||
final ActividadesRepository _repository;
|
||||
|
||||
ActividadesNotifier(this._repository) : super(const AsyncValue.loading()) {
|
||||
loadActividades();
|
||||
}
|
||||
|
||||
Future<void> loadActividades() async {
|
||||
state = const AsyncValue.loading();
|
||||
try {
|
||||
final actividades = await _repository.getActividades();
|
||||
state = AsyncValue.data(actividades);
|
||||
} catch (e, st) {
|
||||
state = AsyncValue.error(e, st);
|
||||
}
|
||||
}
|
||||
|
||||
Future<String?> insertActividad(Map<String, dynamic> datos) async {
|
||||
try {
|
||||
await _repository.insertActividad(datos);
|
||||
await loadActividades();
|
||||
return null;
|
||||
} catch (e) {
|
||||
return _errorMessage(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<String?> updateActividad(int id, Map<String, dynamic> datos) async {
|
||||
try {
|
||||
await _repository.updateActividad(id, datos);
|
||||
await loadActividades();
|
||||
return null;
|
||||
} catch (e) {
|
||||
return _errorMessage(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<String?> deleteActividad(int id) async {
|
||||
try {
|
||||
await _repository.deleteActividad(id);
|
||||
await loadActividades();
|
||||
return null;
|
||||
} catch (e) {
|
||||
return _errorMessage(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user