refactor dashboard note list
This commit is contained in:
@@ -18,6 +18,7 @@ import 'package:flux/features/customers/models/customer_model.dart';
|
|||||||
import 'package:flux/features/customers/ui/customer_detail_screen.dart';
|
import 'package:flux/features/customers/ui/customer_detail_screen.dart';
|
||||||
import 'package:flux/features/customers/ui/customer_form_screen.dart';
|
import 'package:flux/features/customers/ui/customer_form_screen.dart';
|
||||||
import 'package:flux/features/customers/ui/customers_list_screen.dart';
|
import 'package:flux/features/customers/ui/customers_list_screen.dart';
|
||||||
|
import 'package:flux/features/home/dashboard_note_list/blocs/dashboard_note_list_cubit.dart';
|
||||||
import 'package:flux/features/home/dashboard_store_operation_list/bloc/dashboard_store_operation_list_cubit.dart';
|
import 'package:flux/features/home/dashboard_store_operation_list/bloc/dashboard_store_operation_list_cubit.dart';
|
||||||
import 'package:flux/features/home/dashboard_store_ticket_list/blocs/dashboard_store_ticket_list_cubit.dart';
|
import 'package:flux/features/home/dashboard_store_ticket_list/blocs/dashboard_store_ticket_list_cubit.dart';
|
||||||
import 'package:flux/features/home/dashboard_task_list/blocs/dashboard_task_list_cubit.dart';
|
import 'package:flux/features/home/dashboard_task_list/blocs/dashboard_task_list_cubit.dart';
|
||||||
@@ -165,6 +166,12 @@ class AppRouter {
|
|||||||
storeId: sessionCubit.state.currentStore?.id,
|
storeId: sessionCubit.state.currentStore?.id,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
BlocProvider<DashboardNoteListCubit>(
|
||||||
|
create: (context) => DashboardNoteListCubit(
|
||||||
|
companyId: sessionCubit.state.company?.id,
|
||||||
|
staffId: sessionCubit.state.currentStaffMember?.id,
|
||||||
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
child: const HomeScreen(),
|
child: const HomeScreen(),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -0,0 +1,73 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:equatable/equatable.dart';
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:flux/features/notes/data/notes_repository.dart';
|
||||||
|
import 'package:flux/features/notes/models/note_model.dart';
|
||||||
|
import 'package:get_it/get_it.dart';
|
||||||
|
|
||||||
|
part 'dashboard_note_list_state.dart';
|
||||||
|
|
||||||
|
class DashboardNoteListCubit extends Cubit<DashboardNoteListState> {
|
||||||
|
final NotesRepository _repository = GetIt.I.get<NotesRepository>();
|
||||||
|
final String? companyId;
|
||||||
|
final String? staffId;
|
||||||
|
StreamSubscription<void>? _subscription;
|
||||||
|
|
||||||
|
DashboardNoteListCubit({required this.companyId, required this.staffId})
|
||||||
|
: super(DashboardNoteListState(status: DashboardNoteListStatus.initial));
|
||||||
|
|
||||||
|
void stopListening() {
|
||||||
|
_subscription?.cancel();
|
||||||
|
_subscription = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
void startListening() {
|
||||||
|
stopListening();
|
||||||
|
|
||||||
|
emit(state.copyWith(status: DashboardNoteListStatus.loading));
|
||||||
|
|
||||||
|
// Primo caricamento
|
||||||
|
_loadNotesSilently();
|
||||||
|
|
||||||
|
// Inizio ascolto campanello
|
||||||
|
try {
|
||||||
|
_subscription = _repository
|
||||||
|
.notesStream(companyId: companyId!, currentStaffId: staffId!)
|
||||||
|
.listen((_) {
|
||||||
|
// Quando il campanello suona (qualcosa è cambiato a DB), ricarichiamo!
|
||||||
|
_loadNotesSilently();
|
||||||
|
});
|
||||||
|
} on Exception catch (e) {
|
||||||
|
debugPrint(e.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _loadNotesSilently() async {
|
||||||
|
try {
|
||||||
|
final notes = await _repository.getNotes();
|
||||||
|
|
||||||
|
emit(
|
||||||
|
state.copyWith(
|
||||||
|
status: DashboardNoteListStatus.success,
|
||||||
|
notes: notes,
|
||||||
|
errorMessage: null,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
emit(
|
||||||
|
state.copyWith(
|
||||||
|
status: DashboardNoteListStatus.failure,
|
||||||
|
errorMessage: e.toString(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> close() {
|
||||||
|
stopListening();
|
||||||
|
return super.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
part of 'dashboard_note_list_cubit.dart';
|
||||||
|
|
||||||
|
enum DashboardNoteListStatus { initial, loading, success, failure }
|
||||||
|
|
||||||
|
class DashboardNoteListState extends Equatable {
|
||||||
|
final DashboardNoteListStatus status;
|
||||||
|
final List<NoteModel> notes;
|
||||||
|
final String? errorMessage;
|
||||||
|
|
||||||
|
const DashboardNoteListState({
|
||||||
|
this.status = DashboardNoteListStatus.initial,
|
||||||
|
this.notes = const [],
|
||||||
|
this.errorMessage,
|
||||||
|
});
|
||||||
|
|
||||||
|
DashboardNoteListState copyWith({
|
||||||
|
DashboardNoteListStatus? status,
|
||||||
|
List<NoteModel>? notes,
|
||||||
|
String? errorMessage,
|
||||||
|
}) {
|
||||||
|
return DashboardNoteListState(
|
||||||
|
status: status ?? this.status,
|
||||||
|
notes: notes ?? this.notes,
|
||||||
|
errorMessage: errorMessage ?? this.errorMessage,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [status, notes, errorMessage];
|
||||||
|
}
|
||||||
@@ -2,12 +2,12 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:flux/core/routes/routes.dart';
|
import 'package:flux/core/routes/routes.dart';
|
||||||
import 'package:flux/core/theme/theme.dart';
|
import 'package:flux/core/theme/theme.dart';
|
||||||
import 'package:flux/features/notes/blocs/notes_bloc.dart';
|
import 'package:flux/features/home/dashboard_note_list/blocs/dashboard_note_list_cubit.dart';
|
||||||
import 'package:flux/features/notes/models/note_model.dart';
|
import 'package:flux/features/notes/models/note_model.dart';
|
||||||
import 'package:go_router/go_router.dart'; // Supponendo tu usi GoRouter per la navigazione
|
import 'package:go_router/go_router.dart'; // Supponendo tu usi GoRouter per la navigazione
|
||||||
|
|
||||||
class DashboardNotesWidget extends StatelessWidget {
|
class DashboardNoteListCard extends StatelessWidget {
|
||||||
const DashboardNotesWidget({super.key});
|
const DashboardNoteListCard({super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@@ -59,14 +59,14 @@ class DashboardNotesWidget extends StatelessWidget {
|
|||||||
const SizedBox(height: 12),
|
const SizedBox(height: 12),
|
||||||
|
|
||||||
// Il corpo del widget collegato al Bloc
|
// Il corpo del widget collegato al Bloc
|
||||||
BlocBuilder<NotesBloc, NotesState>(
|
BlocBuilder<DashboardNoteListCubit, DashboardNoteListState>(
|
||||||
builder: (context, state) {
|
builder: (context, state) {
|
||||||
if (state.status == NotesStatus.loading &&
|
if (state.status == DashboardNoteListStatus.loading &&
|
||||||
state.notes.isEmpty) {
|
state.notes.isEmpty) {
|
||||||
return const Center(child: CircularProgressIndicator());
|
return const Center(child: CircularProgressIndicator());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (state.status == NotesStatus.failure) {
|
if (state.status == DashboardNoteListStatus.failure) {
|
||||||
return const Center(
|
return const Center(
|
||||||
child: Text(
|
child: Text(
|
||||||
'Errore nel caricamento delle note.',
|
'Errore nel caricamento delle note.',
|
||||||
@@ -6,8 +6,8 @@ import 'package:flux/features/home/dashboard_task_list/blocs/dashboard_task_list
|
|||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
import 'package:flux/features/tasks/models/task_status.dart';
|
import 'package:flux/features/tasks/models/task_status.dart';
|
||||||
|
|
||||||
class DashboardTasksCard extends StatelessWidget {
|
class DashboardTaskListCard extends StatelessWidget {
|
||||||
const DashboardTasksCard({super.key});
|
const DashboardTaskListCard({super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@@ -5,18 +5,19 @@ import 'package:flux/core/routes/routes.dart';
|
|||||||
import 'package:flux/core/theme/theme.dart';
|
import 'package:flux/core/theme/theme.dart';
|
||||||
import 'package:flux/core/utils/extensions.dart';
|
import 'package:flux/core/utils/extensions.dart';
|
||||||
import 'package:flux/core/widgets/staff_selector_modal.dart';
|
import 'package:flux/core/widgets/staff_selector_modal.dart';
|
||||||
|
import 'package:flux/features/home/dashboard_note_list/blocs/dashboard_note_list_cubit.dart';
|
||||||
import 'package:flux/features/home/dashboard_store_operation_list/bloc/dashboard_store_operation_list_cubit.dart';
|
import 'package:flux/features/home/dashboard_store_operation_list/bloc/dashboard_store_operation_list_cubit.dart';
|
||||||
import 'package:flux/features/home/dashboard_store_ticket_list/blocs/dashboard_store_ticket_list_cubit.dart';
|
import 'package:flux/features/home/dashboard_store_ticket_list/blocs/dashboard_store_ticket_list_cubit.dart';
|
||||||
import 'package:flux/features/home/dashboard_store_ticket_list/ui/dashboard_store_ticket_list_card.dart';
|
import 'package:flux/features/home/dashboard_store_ticket_list/ui/dashboard_store_ticket_list_card.dart';
|
||||||
import 'package:flux/features/home/dashboard_task_list/blocs/dashboard_task_list_cubit.dart';
|
import 'package:flux/features/home/dashboard_task_list/blocs/dashboard_task_list_cubit.dart';
|
||||||
import 'package:flux/features/home/dashboard_task_list/ui/dashboard_tasks_card.dart';
|
import 'package:flux/features/home/dashboard_task_list/ui/dashboard_task_list_card.dart';
|
||||||
import 'package:flux/features/home/dashboard_store_operation_list/ui/latest_store_operations_card.dart';
|
import 'package:flux/features/home/dashboard_store_operation_list/ui/latest_store_operations_card.dart';
|
||||||
import 'package:flux/features/home/ui/quick_actions_widget.dart';
|
import 'package:flux/features/home/ui/quick_actions_widget.dart';
|
||||||
import 'package:flux/features/master_data/staff/blocs/staff_cubit.dart';
|
import 'package:flux/features/master_data/staff/blocs/staff_cubit.dart';
|
||||||
import 'package:flux/features/master_data/staff/models/staff_member_model.dart';
|
import 'package:flux/features/master_data/staff/models/staff_member_model.dart';
|
||||||
import 'package:flux/features/notes/data/notes_repository.dart';
|
import 'package:flux/features/notes/data/notes_repository.dart';
|
||||||
import 'package:flux/features/notes/models/note_model.dart';
|
import 'package:flux/features/notes/models/note_model.dart';
|
||||||
import 'package:flux/features/notes/ui/dashboard_notes_widget.dart';
|
import 'package:flux/features/home/dashboard_note_list/ui/dashboard_note_list_card.dart';
|
||||||
import 'package:get_it/get_it.dart';
|
import 'package:get_it/get_it.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
|
|
||||||
@@ -58,12 +59,14 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
context.read<DashboardStoreOperationListCubit>().stopListening();
|
context.read<DashboardStoreOperationListCubit>().stopListening();
|
||||||
context.read<DashboardTaskListCubit>().stopListening();
|
context.read<DashboardTaskListCubit>().stopListening();
|
||||||
context.read<DashboardStoreTicketListCubit>().stopListening();
|
context.read<DashboardStoreTicketListCubit>().stopListening();
|
||||||
|
context.read<DashboardNoteListCubit>().stopListening();
|
||||||
}
|
}
|
||||||
|
|
||||||
void _startListeners() {
|
void _startListeners() {
|
||||||
context.read<DashboardStoreOperationListCubit>().startListening();
|
context.read<DashboardStoreOperationListCubit>().startListening();
|
||||||
context.read<DashboardTaskListCubit>().startListening();
|
context.read<DashboardTaskListCubit>().startListening();
|
||||||
context.read<DashboardStoreTicketListCubit>().startListening();
|
context.read<DashboardStoreTicketListCubit>().startListening();
|
||||||
|
context.read<DashboardNoteListCubit>().startListening();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -128,8 +131,8 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
color: Colors.orange,
|
color: Colors.orange,
|
||||||
context: context,
|
context: context,
|
||||||
),
|
),
|
||||||
DashboardNotesWidget(),
|
DashboardNoteListCard(),
|
||||||
DashboardTasksCard(),
|
DashboardTaskListCard(),
|
||||||
]),
|
]),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|||||||
Reference in New Issue
Block a user