2026-05-12 12:36:50 +02:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2026-05-13 12:41:07 +02:00
|
|
|
import 'package:flux/core/blocs/session/session_cubit.dart';
|
2026-05-12 12:36:50 +02:00
|
|
|
import 'package:flux/features/tracking/data/tracking_repository.dart';
|
|
|
|
|
import 'package:flux/features/tracking/models/tracking_model.dart';
|
2026-05-13 12:41:07 +02:00
|
|
|
import 'package:get_it/get_it.dart';
|
2026-05-12 12:36:50 +02:00
|
|
|
|
|
|
|
|
// Stati base: initial, loading, loaded, error
|
|
|
|
|
class TrackingState {
|
|
|
|
|
final bool isLoading;
|
|
|
|
|
final List<TrackingModel> logs;
|
|
|
|
|
|
|
|
|
|
TrackingState({this.isLoading = false, this.logs = const []});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class TrackingCubit extends Cubit<TrackingState> {
|
2026-05-13 12:41:07 +02:00
|
|
|
final TrackingRepository _repo = GetIt.I.get<TrackingRepository>();
|
|
|
|
|
final String companyId = GetIt.I.get<SessionCubit>().state.company!.id!;
|
2026-05-12 12:36:50 +02:00
|
|
|
|
2026-05-14 15:59:46 +02:00
|
|
|
TrackingCubit() : super(TrackingState());
|
2026-05-12 12:36:50 +02:00
|
|
|
|
2026-05-14 15:59:46 +02:00
|
|
|
Future<void> loadTrackings(
|
|
|
|
|
String parentId,
|
|
|
|
|
TrackingParentType parentType,
|
|
|
|
|
) async {
|
|
|
|
|
emit(TrackingState(isLoading: true, logs: []));
|
2026-05-12 12:36:50 +02:00
|
|
|
final trackings = await _repo.getTrackingsByParent(
|
|
|
|
|
parentId: parentId,
|
|
|
|
|
parentType: parentType,
|
|
|
|
|
);
|
|
|
|
|
emit(TrackingState(isLoading: false, logs: trackings));
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-14 15:59:46 +02:00
|
|
|
Future<void> addTimelineEvent({
|
|
|
|
|
required String parentId,
|
|
|
|
|
required TrackingParentType parentType,
|
|
|
|
|
required String message,
|
|
|
|
|
required bool isInternal,
|
2026-05-12 12:36:50 +02:00
|
|
|
String? staffId,
|
|
|
|
|
}) async {
|
|
|
|
|
// Aggiungiamo un feedback visivo immediato (Optimistic UI) se vogliamo,
|
|
|
|
|
// oppure semplicemente mostriamo il loading
|
|
|
|
|
await _repo.logQuickEvent(
|
|
|
|
|
companyId: companyId,
|
|
|
|
|
message: message,
|
|
|
|
|
type: TrackingType.manualNote,
|
2026-05-13 19:24:25 +02:00
|
|
|
parentId: parentId,
|
2026-05-12 12:36:50 +02:00
|
|
|
parentType: parentType,
|
|
|
|
|
staffId: staffId,
|
|
|
|
|
isInternal: isInternal,
|
|
|
|
|
);
|
|
|
|
|
// Ricarichiamo la lista fresca dal server
|
2026-05-14 15:59:46 +02:00
|
|
|
await loadTrackings(parentId, parentType);
|
2026-05-12 12:36:50 +02:00
|
|
|
}
|
|
|
|
|
}
|