change Medicine Provider to use BaseAppClient

merge-requests/140/head
Mohammad ALjammal 6 years ago
parent 42a639f8b8
commit 7cc1c36ab4

@ -1,3 +1,4 @@
import 'package:doctor_app_flutter/providers/medicine_provider.dart';
import 'package:doctor_app_flutter/providers/project_provider.dart'; import 'package:doctor_app_flutter/providers/project_provider.dart';
import 'package:doctor_app_flutter/providers/schedule_provider.dart'; import 'package:doctor_app_flutter/providers/schedule_provider.dart';
import 'package:doctor_app_flutter/util/translations_delegate_base.dart'; import 'package:doctor_app_flutter/util/translations_delegate_base.dart';
@ -30,7 +31,8 @@ class MyApp extends StatelessWidget {
ChangeNotifierProvider<AuthProvider>(create: (context) => AuthProvider()), ChangeNotifierProvider<AuthProvider>(create: (context) => AuthProvider()),
ChangeNotifierProvider<HospitalProvider>(create: (context) => HospitalProvider()), ChangeNotifierProvider<HospitalProvider>(create: (context) => HospitalProvider()),
ChangeNotifierProvider<ProjectProvider>(create: (context) => ProjectProvider(),), ChangeNotifierProvider<ProjectProvider>(create: (context) => ProjectProvider(),),
ChangeNotifierProvider<ScheduleProvider>(create: (context) => ScheduleProvider(),) ChangeNotifierProvider<ScheduleProvider>(create: (context) => ScheduleProvider(),),
ChangeNotifierProvider<MedicineProvider>(create: (context) => MedicineProvider(),),
], ],
child: Consumer<ProjectProvider>( child: Consumer<ProjectProvider>(
builder: (context,projectProvider,child) => MaterialApp( builder: (context,projectProvider,child) => MaterialApp(

@ -1,103 +1,72 @@
import 'dart:convert'; import 'package:doctor_app_flutter/client/base_app_client.dart';
import 'package:doctor_app_flutter/client/app_client.dart';
import 'package:doctor_app_flutter/config/config.dart'; import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/config/shared_pref_kay.dart';
import 'package:doctor_app_flutter/models/pharmacies/pharmacies_List_request_model.dart'; import 'package:doctor_app_flutter/models/pharmacies/pharmacies_List_request_model.dart';
import 'package:doctor_app_flutter/models/pharmacies/pharmacies_items_request_model.dart'; import 'package:doctor_app_flutter/models/pharmacies/pharmacies_items_request_model.dart';
import 'package:doctor_app_flutter/util/dr_app_shared_pref.dart'; import 'package:doctor_app_flutter/util/dr_app_shared_pref.dart';
import 'package:doctor_app_flutter/util/helpers.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
class MedicineProvider with ChangeNotifier { class MedicineProvider with ChangeNotifier {
DrAppSharedPreferances sharedPref = new DrAppSharedPreferances(); DrAppSharedPreferances sharedPref = new DrAppSharedPreferances();
var pharmacyItemsList = []; var pharmacyItemsList = [];
var pharmaciesList = []; var pharmaciesList = [];
bool isFinished = true; bool isFinished = true;
bool hasError = false; bool hasError = false;
String errorMsg = ''; String errorMsg = '';
PharmaciesItemsRequestModel _itemsRequestModel =PharmaciesItemsRequestModel(); PharmaciesItemsRequestModel _itemsRequestModel =
PharmaciesItemsRequestModel();
PharmaciesListRequestModel _listRequestModel = PharmaciesListRequestModel(); PharmaciesListRequestModel _listRequestModel = PharmaciesListRequestModel();
Future<String> getMedicineItem(String itemName) async {
clearPharmacyItemsList(){
pharmacyItemsList.clear();
notifyListeners();
}
getMedicineItem(String itemName) async {
_itemsRequestModel.pHRItemName = itemName;
resetDefaultValues(); resetDefaultValues();
pharmacyItemsList.clear();
notifyListeners();
try { try {
if (await Helpers.checkConnection()) { await BaseAppClient.post(PHARMACY_ITEMS_URL,
_itemsRequestModel.pHRItemName = itemName; onSuccess: (dynamic response, int statusCode) {
pharmacyItemsList = response['ListPharmcy_Region'];
final response = await AppClient.post(PHARMACY_ITEMS_URL,
body: json.encode(_itemsRequestModel.toJson()));
final int statusCode = response.statusCode;
isFinished = true;
if (statusCode < 200 || statusCode >= 400 || json == null) {
isFinished = true;
hasError = true;
errorMsg = 'Error While Fetching data';
} else {
var parsed = json.decode(response.body.toString());
if (parsed['MessageStatus'] == 1) {
pharmacyItemsList = parsed['ListPharmcy_Region'];
hasError = false; hasError = false;
isFinished = true; isFinished = true;
errorMsg = "Done"; errorMsg = "Done";
} else { }, onFailure: (String error, int statusCode) {
isFinished = true;
hasError = true; hasError = true;
errorMsg = parsed['ErrorMessage'] ?? parsed['ErrorEndUserMessage']; errorMsg = error;
} }, body: _itemsRequestModel.toJson());
}
} else {
isFinished = true;
hasError = true;
errorMsg = 'Please Check The Internet Connection';
}
notifyListeners(); notifyListeners();
} catch (error) { } catch (error) {
throw error; throw error;
} }
return errorMsg;
} }
Future<String> getPharmaciesList(int itemId) async { getPharmaciesList(int itemId) async {
String token = await sharedPref.getString(TOKEN);
resetDefaultValues(); resetDefaultValues();
try { try {
if (await Helpers.checkConnection()) { _listRequestModel.itemID = itemId;
_listRequestModel.itemID = itemId; isFinished = true;
_listRequestModel.tokenID = token; await BaseAppClient.post(PHARMACY_LIST_URL,
final response = await AppClient.post(PHARMACY_LIST_URL, onSuccess: (dynamic response, int statusCode) {
body: json.encode(_listRequestModel.toJson())); pharmaciesList = response['PharmList'];
final int statusCode = response.statusCode;
isFinished = true;
if (statusCode < 200 || statusCode >= 400 || json == null) {
isFinished = true;
hasError = true;
errorMsg = 'Error While Fetching data';
} else {
var parsed = json.decode(response.body.toString());
if (parsed['MessageStatus'] == 1) {
pharmaciesList = parsed['PharmList'];
hasError = false; hasError = false;
isFinished = true; isFinished = true;
errorMsg = "Done"; errorMsg = "Done";
} else { }, onFailure: (String error, int statusCode) {
isFinished = true;
hasError = true; hasError = true;
errorMsg = parsed['ErrorMessage'] ?? parsed['ErrorEndUserMessage']; errorMsg = error;
} }, body: _listRequestModel.toJson());
}
} else {
isFinished = true;
hasError = true;
errorMsg = 'Please Check The Internet Connection';
}
notifyListeners(); notifyListeners();
} catch (error) { } catch (error) {
throw error; throw error;
} }
return errorMsg;
} }
resetDefaultValues() { resetDefaultValues() {

@ -92,7 +92,7 @@ class _MedicineSearchState extends State<MedicineSearchScreen> {
children: <Widget>[ children: <Widget>[
AppText( AppText(
TranslationBase.of(context).youCanFind + TranslationBase.of(context).youCanFind +
(data == null ? "0" : data.length.toString()) + (_medicineProvider.pharmacyItemsList == null ? "0" : _medicineProvider.pharmacyItemsList.length.toString()) +
TranslationBase.of(context).itemsInSearch, TranslationBase.of(context).itemsInSearch,
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,
margin: 5, margin: 5,
@ -116,25 +116,22 @@ class _MedicineSearchState extends State<MedicineSearchScreen> {
: ListView.builder( : ListView.builder(
scrollDirection: Axis.vertical, scrollDirection: Axis.vertical,
shrinkWrap: true, shrinkWrap: true,
itemCount: data == null ? 0 : data.length, itemCount: _medicineProvider.pharmacyItemsList == null ? 0 : _medicineProvider.pharmacyItemsList.length,
itemBuilder: (BuildContext context, int index) { itemBuilder: (BuildContext context, int index) {
return InkWell( return InkWell(
child: MedicineItemWidget( child: MedicineItemWidget(
label: data[index]["ItemDescription"], label: _medicineProvider.pharmacyItemsList[index]["ItemDescription"],
url: data[index]["ProductImageBase64"], url: _medicineProvider.pharmacyItemsList[index]["ProductImageBase64"],
), ),
onTap: () { onTap: () {
Navigator.push( Navigator.push(
context, context,
MaterialPageRoute( MaterialPageRoute(
builder: (context) => builder: (context) =>
ChangeNotifierProvider( PharmaciesListScreen(
create: (_) => MedicineProvider(), itemID: _medicineProvider.pharmacyItemsList[index]["ItemID"],
child: PharmaciesListScreen( url: _medicineProvider.pharmacyItemsList[index]
itemID: data[index]["ItemID"], ["ProductImageBase64"]),
url: data[index]
["ProductImageBase64"]),
),
), ),
); );
}, },
@ -150,16 +147,10 @@ class _MedicineSearchState extends State<MedicineSearchScreen> {
searchMedicine(context) { searchMedicine(context) {
FocusScope.of(context).unfocus(); FocusScope.of(context).unfocus();
if (myController.text.isNullOrEmpty()) { if (myController.text.isNullOrEmpty()) {
this.setState(() { _medicineProvider.clearPharmacyItemsList();
data = null;
});
helpers.showErrorToast("Type Medicine Name"); helpers.showErrorToast("Type Medicine Name");
return; return;
} }
_medicineProvider.getMedicineItem(myController.text).then((str) { _medicineProvider.getMedicineItem(myController.text);
this.setState(() {
data = _medicineProvider.pharmacyItemsList;
});
});
} }
} }

@ -55,7 +55,17 @@ class _PharmaciesListState extends State<PharmaciesListScreen> {
projectsProvider = Provider.of(context); projectsProvider = Provider.of(context);
return AppScaffold( return AppScaffold(
appBarTitle: TranslationBase.of(context).pharmaciesList, appBarTitle: TranslationBase.of(context).pharmaciesList,
body: Container( body: !_medicineProvider.isFinished
? DrAppCircularProgressIndeicator()
: _medicineProvider.hasError
? Center(
child: Text(
_medicineProvider.errorMsg,
style: TextStyle(
color: Theme.of(context).errorColor),
),
)
:Container(
height: SizeConfig.screenHeight, height: SizeConfig.screenHeight,
child: ListView( child: ListView(
shrinkWrap: true, shrinkWrap: true,
@ -63,84 +73,70 @@ class _PharmaciesListState extends State<PharmaciesListScreen> {
physics: const AlwaysScrollableScrollPhysics(), physics: const AlwaysScrollableScrollPhysics(),
children: <Widget>[ children: <Widget>[
RoundedContainer( RoundedContainer(
child: !_medicineProvider.isFinished child: Row(
? DrAppCircularProgressIndeicator() children: <Widget>[
: _medicineProvider.hasError Expanded(
? Center( flex: 1,
child: Text( child: ClipRRect(
_medicineProvider.errorMsg, borderRadius: BorderRadius.all(
style: TextStyle( Radius.circular(7)),
color: Theme.of(context).errorColor), child: Image.memory(
), dataFromBase64String(widget.url),
) height:
: StreamBuilder<Object>( SizeConfig.imageSizeMultiplier *
stream: null, 21,
builder: (context, snapshot) { width:
return Row( SizeConfig.imageSizeMultiplier *
children: <Widget>[ 20,
Expanded( fit: BoxFit.cover,
flex: 1, ),
child: ClipRRect( ),
borderRadius: BorderRadius.all( ),
Radius.circular(7)), Expanded(
child: Image.memory( flex: 3,
dataFromBase64String(widget.url), child: Column(
height: mainAxisAlignment:
SizeConfig.imageSizeMultiplier * MainAxisAlignment.start,
21, crossAxisAlignment:
width: CrossAxisAlignment.stretch,
SizeConfig.imageSizeMultiplier * children: <Widget>[
20, AppText(
fit: BoxFit.cover, TranslationBase.of(context)
), .description,
), marginLeft: 10,
), marginTop: 0,
Expanded( marginRight: 10,
flex: 3, marginBottom: 2,
child: Column( fontWeight: FontWeight.bold,
mainAxisAlignment: ),
MainAxisAlignment.start, AppText(
crossAxisAlignment: _medicineProvider.pharmaciesList[0]["ItemDescription"],
CrossAxisAlignment.stretch, marginLeft: 10,
children: <Widget>[ marginTop: 0,
AppText( marginRight: 10,
TranslationBase.of(context) marginBottom: 10,
.description, ),
marginLeft: 10, AppText(
marginTop: 0, TranslationBase.of(context).price,
marginRight: 10, marginLeft: 10,
marginBottom: 2, marginTop: 0,
fontWeight: FontWeight.bold, marginRight: 10,
), marginBottom: 2,
AppText( fontWeight: FontWeight.bold,
_data[0]["ItemDescription"], ),
marginLeft: 10, AppText(
marginTop: 0, _medicineProvider.pharmaciesList[0]["SellingPrice"]
marginRight: 10, .toString(),
marginBottom: 10, marginLeft: 10,
), marginTop: 0,
AppText( marginRight: 10,
TranslationBase.of(context).price, marginBottom: 10,
marginLeft: 10, ),
marginTop: 0, ],
marginRight: 10, ),
marginBottom: 2, )
fontWeight: FontWeight.bold, ],
), )),
AppText(
_data[0]["SellingPrice"]
.toString(),
marginLeft: 10,
marginTop: 0,
marginRight: 10,
marginBottom: 10,
),
],
),
)
],
);
})),
Container( Container(
margin: EdgeInsets.only( margin: EdgeInsets.only(
top: SizeConfig.widthMultiplier * 2, top: SizeConfig.widthMultiplier * 2,
@ -165,7 +161,7 @@ class _PharmaciesListState extends State<PharmaciesListScreen> {
child: ListView.builder( child: ListView.builder(
shrinkWrap: true, shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(), physics: const NeverScrollableScrollPhysics(),
itemCount: _data == null ? 0 : _data.length, itemCount: _medicineProvider.pharmaciesList == null ? 0 : _medicineProvider.pharmaciesList.length,
itemBuilder: (BuildContext context, int index) { itemBuilder: (BuildContext context, int index) {
return RoundedContainer( return RoundedContainer(
child: Row( child: Row(
@ -176,7 +172,7 @@ class _PharmaciesListState extends State<PharmaciesListScreen> {
borderRadius: borderRadius:
BorderRadius.all(Radius.circular(7)), BorderRadius.all(Radius.circular(7)),
child: Image.network( child: Image.network(
_data[index]["ProjectImageURL"], _medicineProvider.pharmaciesList[index]["ProjectImageURL"],
height: height:
SizeConfig.imageSizeMultiplier * 15, SizeConfig.imageSizeMultiplier * 15,
width: width:
@ -188,7 +184,7 @@ class _PharmaciesListState extends State<PharmaciesListScreen> {
Expanded( Expanded(
flex: 4, flex: 4,
child: AppText( child: AppText(
_data[index]["LocationDescription"], _medicineProvider.pharmaciesList[index]["LocationDescription"],
margin: 10, margin: 10,
), ),
), ),
@ -207,7 +203,7 @@ class _PharmaciesListState extends State<PharmaciesListScreen> {
color: Colors.red, color: Colors.red,
), ),
onTap: () => launch("tel://" + onTap: () => launch("tel://" +
_data[index]["PhoneNumber"]), _medicineProvider.pharmaciesList[index]["PhoneNumber"]),
), ),
), ),
Padding( Padding(
@ -220,10 +216,10 @@ class _PharmaciesListState extends State<PharmaciesListScreen> {
onTap: () { onTap: () {
MapsLauncher.launchCoordinates( MapsLauncher.launchCoordinates(
double.parse( double.parse(
_data[index]["Latitude"]), _medicineProvider.pharmaciesList[index]["Latitude"]),
double.parse( double.parse(
_data[index]["Longitude"]), _medicineProvider.pharmaciesList[index]["Longitude"]),
_data[index] _medicineProvider.pharmaciesList[index]
["LocationDescription"]); ["LocationDescription"]);
}, },
), ),
@ -241,19 +237,15 @@ class _PharmaciesListState extends State<PharmaciesListScreen> {
)); ));
} }
Future<bool> pharmaciesList() async { pharmaciesList() async {
_medicineProvider.getPharmaciesList(widget.itemID).then((result) { _medicineProvider.getPharmaciesList(widget.itemID);
this.setState(() {
_data = _medicineProvider.pharmaciesList;
});
});
return true;
} }
Image imageFromBase64String(String base64String) { Image imageFromBase64String(String base64String) {
return Image.memory(base64Decode(base64String)); return Image.memory(base64Decode(base64String));
} }
//TODO CHECK THE URL IS NULL OR NOT
Uint8List dataFromBase64String(String base64String) { Uint8List dataFromBase64String(String base64String) {
return base64Decode(base64String); return base64Decode(base64String);
} }

Loading…
Cancel
Save