import 'package:equatable/equatable.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:ticket_booth/model/branch_model.dart'; import 'package:ticket_booth/provider/dio_provider.dart'; import 'package:ticket_booth/repository/base_repository.dart'; import 'package:ticket_booth/repository/booth_repository.dart'; abstract class BranchListState extends Equatable { final DateTime date; const BranchListState(this.date); @override List get props => [date]; } class BranchListStateInit extends BranchListState { BranchListStateInit() : super(DateTime.now()); } class BranchListStateLoading extends BranchListState { BranchListStateLoading() : super(DateTime.now()); } class BranchListStateError extends BranchListState { final String message; BranchListStateError({ required this.message, }) : super(DateTime.now()); } class BranchListStateDone extends BranchListState { final List model; BranchListStateDone({ required this.model, }) : super(DateTime.now()); } //notifier class BranchListNotifier extends StateNotifier { final Ref ref; BranchListNotifier({ required this.ref, }) : super(BranchListStateInit()); void list({required hostIp}) async { try { state = BranchListStateLoading(); final dio = ref.read(dioProvider); final resp = await BoothRepository(dio: dio).getBranch(hostIP: hostIp); state = BranchListStateDone(model: resp); } catch (e) { if (e is BaseRepositoryException) { state = BranchListStateError(message: e.message); } else { state = BranchListStateError(message: e.toString()); } } } } //provider final BranchListProvider = StateNotifierProvider( (ref) => BranchListNotifier(ref: ref));