186 lines
5.6 KiB
Dart
186 lines
5.6 KiB
Dart
|
|
||
|
import 'dart:io';
|
||
|
|
||
|
import 'package:flutter/material.dart';
|
||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||
|
import 'package:fluttertoast/fluttertoast.dart';
|
||
|
import 'package:image_picker/image_picker.dart';
|
||
|
import 'package:medcify/models/store_item.dart';
|
||
|
import 'package:medcify/navigation/navigation.dart';
|
||
|
|
||
|
import '../../../components/alert.dart';
|
||
|
import '../../../network/api_provider.dart';
|
||
|
|
||
|
abstract class SettingsState{}
|
||
|
|
||
|
class SettingsInitial extends SettingsState{}
|
||
|
class SettingsLoading extends SettingsState{}
|
||
|
class SettingsSuccess extends SettingsState{}
|
||
|
class SettingsFailure extends SettingsState{
|
||
|
String error;
|
||
|
SettingsFailure(this.error);
|
||
|
}
|
||
|
|
||
|
class SettingsCubit extends Cubit<SettingsState> {
|
||
|
SettingsCubit() : super(SettingsInitial());
|
||
|
bool pharmacyStatus = true;
|
||
|
bool pickup = true;
|
||
|
bool codEnabled = true;
|
||
|
bool prescriptionEnabled = true;
|
||
|
bool discountEnabled = true;
|
||
|
String returnPolicy = "";
|
||
|
String wideDiscount = "0";
|
||
|
StoreItem? store;
|
||
|
String? image;
|
||
|
String? document;
|
||
|
ImagePicker picker = ImagePicker();
|
||
|
|
||
|
Future<void> fetchMyStore() async{
|
||
|
emit(SettingsLoading());
|
||
|
final response = await ApiProvider.instance.fetchMyStore();
|
||
|
if(response.status ?? false){
|
||
|
store = response.store;
|
||
|
pharmacyStatus = store?.storeStatus ?? false;
|
||
|
pickup = store?.pickupEnabled ?? false;
|
||
|
codEnabled = store?.codEnabled ?? false;
|
||
|
discountEnabled = store?.discountEnabled ?? false;
|
||
|
prescriptionEnabled = store?.prescriptionEnabled ?? false;
|
||
|
returnPolicy = store?.returnPolicy ?? "";
|
||
|
wideDiscount = store?.wideDiscount ?? "0";
|
||
|
image = store?.storeImage ?? "";
|
||
|
document = store?.storeDoc ?? "";
|
||
|
emit(SettingsSuccess());
|
||
|
}else{
|
||
|
emit(SettingsFailure(response.message ?? "Something went wrong"));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Future<void> getStoreImage(int pos) async{
|
||
|
final XFile? value = await picker.pickImage(source: (pos == 0) ? ImageSource.camera : ImageSource.gallery);
|
||
|
if(value != null){
|
||
|
final bytes = (await value.readAsBytes()).lengthInBytes;
|
||
|
final kb = bytes / 1024;
|
||
|
final mb = kb / 1024;
|
||
|
if(mb <= 5){
|
||
|
updateStoreImage(value.path);
|
||
|
}else{
|
||
|
showToast("Select image below 5mb");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Future<void> updateStoreImage(String val) async{
|
||
|
Navigation.instance.navigate("/loading");
|
||
|
final response = await ApiProvider.instance.updateStoreImage(val);
|
||
|
Navigation.instance.goBack();
|
||
|
if(response.status ?? false){
|
||
|
fetchMyStore();
|
||
|
}else{
|
||
|
showAlert(response.message ?? "Something went wrong");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Future<void> getDocumentImage(int pos) async{
|
||
|
final XFile? value = await picker.pickImage(source: (pos == 0) ? ImageSource.camera : ImageSource.gallery);
|
||
|
if(value != null){
|
||
|
final bytes = (await value.readAsBytes()).lengthInBytes;
|
||
|
final kb = bytes / 1024;
|
||
|
final mb = kb / 1024;
|
||
|
if(mb <= 5){
|
||
|
updateDocumentImage(value.path);
|
||
|
}else{
|
||
|
showToast("Select image below 5mb");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Future<void> updateDocumentImage(String val) async{
|
||
|
Navigation.instance.navigate("/loading");
|
||
|
final response = await ApiProvider.instance.updateDocumentImage(val);
|
||
|
Navigation.instance.goBack();
|
||
|
if(response.status ?? false){
|
||
|
fetchMyStore();
|
||
|
}else{
|
||
|
showAlert(response.message ?? "Something went wrong");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Future<void> changePharmacyStatus(bool val) async{
|
||
|
Navigation.instance.navigate("/loading");
|
||
|
final response = await ApiProvider.instance.updateStoreStatus((val) ? "1" : "2");
|
||
|
Navigation.instance.goBack();
|
||
|
if(response.status ?? false){
|
||
|
fetchMyStore();
|
||
|
}else{
|
||
|
showAlert(response.message ?? "Something went wrong");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Future<void> changePickup(bool val) async{
|
||
|
Navigation.instance.navigate("/loading");
|
||
|
final response = await ApiProvider.instance.updatePickup((val) ? "1" : "2");
|
||
|
Navigation.instance.goBack();
|
||
|
if(response.status ?? false){
|
||
|
fetchMyStore();
|
||
|
}else{
|
||
|
showAlert(response.message ?? "Something went wrong");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Future<void> changeCodStatus(bool val) async{
|
||
|
Navigation.instance.navigate("/loading");
|
||
|
final response = await ApiProvider.instance.updateCod((val) ? "1" : "2");
|
||
|
Navigation.instance.goBack();
|
||
|
if(response.status ?? false){
|
||
|
fetchMyStore();
|
||
|
}else{
|
||
|
showAlert(response.message ?? "Something went wrong");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Future<void> changeDiscountStatus(bool val) async{
|
||
|
Navigation.instance.navigate("/loading");
|
||
|
final response = await ApiProvider.instance.updateDiscount((val) ? "1" : "2");
|
||
|
Navigation.instance.goBack();
|
||
|
if(response.status ?? false){
|
||
|
fetchMyStore();
|
||
|
}else{
|
||
|
showAlert(response.message ?? "Something went wrong");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Future<void> changePrescriptionStatus(bool val) async{
|
||
|
Navigation.instance.navigate("/loading");
|
||
|
final response = await ApiProvider.instance.updatePrescription((val) ? "1" : "2");
|
||
|
Navigation.instance.goBack();
|
||
|
if(response.status ?? false){
|
||
|
fetchMyStore();
|
||
|
}else{
|
||
|
showAlert(response.message ?? "Something went wrong");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void showAlert(String err){
|
||
|
AlertX.instance.showAlert(
|
||
|
title: "Error",
|
||
|
msg: err,
|
||
|
positiveButtonText: "Done",
|
||
|
positiveButtonPressed: (){
|
||
|
Navigation.instance.goBack();
|
||
|
}
|
||
|
);
|
||
|
}
|
||
|
|
||
|
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
|
||
|
);
|
||
|
}
|
||
|
|
||
|
}
|