Application/medcify/lib/pages/main/notification/notification_bloc.dart
2022-09-26 12:03:52 +05:30

76 lines
2.3 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:medcify/models/notification_item.dart';
import 'package:medcify/models/order_item.dart';
import 'package:medcify/network/api_provider.dart';
import '../../../components/alert.dart';
import '../../../constants.dart';
import '../../../helpers.dart';
import '../../../models/banner_item.dart';
import '../../../models/medicine_item.dart';
import '../../../navigation/navigation.dart';
abstract class NotificationState{}
class NotificationInitial extends NotificationState{}
class NotificationLoading extends NotificationState{}
class NotificationSuccess extends NotificationState{}
class NotificationFailure extends NotificationState{
String error;
NotificationFailure(this.error);
}
class NotificationCubit extends Cubit<NotificationState> {
NotificationCubit() : super(NotificationInitial());
List<NotificationItem> notifications = [];
Future<void> fetchNotifications() async{
emit(NotificationLoading());
final response = await ApiProvider.instance.fetchNotifications();
if(response.status ?? false){
notifications = response.notifications ?? [];
emit(NotificationSuccess());
}else{
emit(NotificationFailure(response.message ?? "Something went wrong"));
}
}
Future<void> removeNotification(int id) async{
Navigation.instance.navigate("/loading");
final response = await ApiProvider.instance.removeNotification("$id");
Navigation.instance.goBack();
if(response.status ?? false){
showToast("Notification removed successfully");
fetchNotifications();
}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();
}
);
}
}