import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'package:intl/intl.dart'; import 'package:medcify/models/timing_item.dart'; import '../../../components/alert.dart'; import '../../../navigation/navigation.dart'; import '../../../network/api_provider.dart'; abstract class ChangeTimingState{} class ChangeTimingInitial extends ChangeTimingState{} class ChangeTimingLoading extends ChangeTimingState{} class ChangeTimingSuccess extends ChangeTimingState{} class ChangeTimingFailure extends ChangeTimingState{ String error; ChangeTimingFailure(this.error); } class ChangeTimingCubit extends Cubit { ChangeTimingCubit() : super(ChangeTimingInitial()); List timings = []; Future fetchTimings() async{ emit(ChangeTimingLoading()); final response = await ApiProvider.instance.fetchTimings(); if(response.status ?? false){ timings = response.timings ?? []; emit(ChangeTimingSuccess()); }else{ emit(ChangeTimingFailure(response.message ?? "Something went wrong")); } } void onOpeningTimeChanged(DateTime? val,int pos){ if(val != null){ timings[pos].openingTime = DateFormat().add_jms().format(val); emit(ChangeTimingSuccess()); } } void onClosingTimeChanged(DateTime? val,int pos){ if(val != null){ timings[pos].closingTime = DateFormat().add_jms().format(val); emit(ChangeTimingSuccess()); } } Future updateTiming() async{ Navigation.instance.navigate("/loading"); final response = await ApiProvider.instance.updateTimings(timings); Navigation.instance.goBack(); if(response.status ?? false){ showToast("Timings updated successfully"); fetchTimings(); }else{ showAlert(response.message ?? "Something went wrong"); } } void showToast(String msg) { Fluttertoast.showToast( msg: msg, toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM, timeInSecForIosWeb: 1, backgroundColor: Colors.grey.shade200, textColor: Colors.black, fontSize: 14.0 ); } void showAlert(String err){ AlertX.instance.showAlert( title: "Error", msg: err, positiveButtonText: "Done", positiveButtonPressed: (){ Navigation.instance.goBack(); } ); } }