-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from Onestop-Agrotech/0.5.0
0.5.0
- Loading branch information
Showing
26 changed files
with
1,213 additions
and
910 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:bloc/bloc.dart'; | ||
import 'package:meta/meta.dart'; | ||
import 'package:mvp/domain/product_repository.dart'; | ||
import 'package:mvp/models/storeProducts.dart'; | ||
|
||
part 'productsapi_event.dart'; | ||
part 'productsapi_state.dart'; | ||
|
||
class ProductsapiBloc extends Bloc<ProductsapiEvent, ProductsapiState> { | ||
final ProductRepositoryImpl _productRepository; | ||
|
||
ProductsapiBloc(this._productRepository) : super(ProductsapiInitial()); | ||
|
||
@override | ||
Stream<ProductsapiState> mapEventToState( | ||
ProductsapiEvent event, | ||
) async* { | ||
if (event is GetVegetables) { | ||
try { | ||
yield ProductsapiLoading(); | ||
final products = await _productRepository.fetchVegetables(); | ||
yield ProductsapiLoaded(products); | ||
} catch (err) { | ||
print(err); | ||
yield ProductsapiError(err.toString()); | ||
} | ||
} else if (event is GetFruits) { | ||
try { | ||
yield ProductsapiLoading(); | ||
final products = await _productRepository.fetchFruits(); | ||
yield ProductsapiLoaded(products); | ||
} catch (err) { | ||
print(err); | ||
yield ProductsapiError(err.toString()); | ||
} | ||
} else if (event is GetDailyEssentials) { | ||
try { | ||
yield ProductsapiLoading(); | ||
final products = await _productRepository.fetchDailyEssentials(); | ||
yield ProductsapiLoaded(products); | ||
} catch (err) { | ||
print(err); | ||
yield ProductsapiError(err.toString()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
part of 'productsapi_bloc.dart'; | ||
|
||
@immutable | ||
abstract class ProductsapiEvent {} | ||
|
||
class GetVegetables extends ProductsapiEvent {} | ||
|
||
class GetFruits extends ProductsapiEvent {} | ||
|
||
class GetDailyEssentials extends ProductsapiEvent {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
part of 'productsapi_bloc.dart'; | ||
|
||
@immutable | ||
abstract class ProductsapiState {} | ||
|
||
class ProductsapiInitial extends ProductsapiState {} | ||
|
||
class ProductsapiLoading extends ProductsapiState {} | ||
|
||
class ProductsapiLoaded extends ProductsapiState { | ||
final List<StoreProduct> products; | ||
|
||
ProductsapiLoaded(this.products); | ||
} | ||
|
||
class ProductsapiError extends ProductsapiState { | ||
final String msg; | ||
|
||
ProductsapiError(this.msg); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class AppException implements Exception { | ||
final _message; | ||
|
||
AppException([this._message]); | ||
|
||
String toString() { | ||
return "$_message"; | ||
} | ||
} | ||
|
||
class FetchDataException extends AppException { | ||
// communication error | ||
FetchDataException([String message]) : super(message); | ||
} | ||
|
||
class UnauthorisedException extends AppException { | ||
// Unauthorised | ||
UnauthorisedException([message]) : super(message); | ||
} | ||
|
||
class InternalServerError extends AppException { | ||
InternalServerError([message]) : super("Internal Server Error"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:mvp/classes/storage_sharedPrefs.dart'; | ||
import 'package:mvp/constants/apiCalls.dart'; | ||
import 'package:mvp/domain/exceptions.dart'; | ||
import 'package:mvp/models/storeProducts.dart'; | ||
import 'package:http/http.dart' as http; | ||
|
||
abstract class ProductRepository { | ||
Future<List<StoreProduct>> fetchVegetables(); | ||
Future<List<StoreProduct>> fetchFruits(); | ||
Future<List<StoreProduct>> fetchDailyEssentials(); | ||
} | ||
|
||
class ProductRepositoryImpl implements ProductRepository { | ||
@override | ||
Future<List<StoreProduct>> fetchVegetables() async { | ||
var responseJson; | ||
try { | ||
StorageSharedPrefs p = new StorageSharedPrefs(); | ||
String token = await p.getToken(); | ||
String hub = await p.gethub(); | ||
Map<String, String> requestHeaders = {'x-auth-token': token}; | ||
String url = APIService.getCategorywiseProducts(hub, "vegetable"); | ||
final response = await http.get(url, headers: requestHeaders); | ||
responseJson = _returnResponse(response); | ||
return responseJson; | ||
} on SocketException { | ||
throw FetchDataException('No Internet connection'); | ||
} | ||
} | ||
|
||
Future<List<StoreProduct>> fetchFruits() async { | ||
var responseJson; | ||
try { | ||
StorageSharedPrefs p = new StorageSharedPrefs(); | ||
String token = await p.getToken(); | ||
String hub = await p.gethub(); | ||
Map<String, String> requestHeaders = {'x-auth-token': token}; | ||
String url = APIService.getCategorywiseProducts(hub, "fruit"); | ||
final response = await http.get(url, headers: requestHeaders); | ||
responseJson = _returnResponse(response); | ||
return responseJson; | ||
} on SocketException { | ||
throw FetchDataException('No Internet connection'); | ||
} | ||
} | ||
|
||
Future<List<StoreProduct>> fetchDailyEssentials() async { | ||
var responseJson; | ||
try { | ||
StorageSharedPrefs p = new StorageSharedPrefs(); | ||
String token = await p.getToken(); | ||
String hub = await p.gethub(); | ||
Map<String, String> requestHeaders = {'x-auth-token': token}; | ||
String url = APIService.getCategorywiseProducts(hub, "dailyEssential"); | ||
final response = await http.get(url, headers: requestHeaders); | ||
responseJson = _returnResponse(response); | ||
return responseJson; | ||
} on SocketException { | ||
throw FetchDataException('No Internet connection'); | ||
} | ||
} | ||
|
||
List<StoreProduct> _returnResponse(http.Response response) { | ||
switch (response.statusCode) { | ||
case 200: | ||
return jsonToCateogrywiseProductModel(response.body); | ||
case 401: | ||
throw UnauthorisedException(response.statusCode.toString()); | ||
case 500: | ||
throw InternalServerError(); | ||
default: | ||
throw FetchDataException( | ||
'Error occured while Communication with Server with StatusCode : ${response.statusCode}'); | ||
} | ||
} | ||
} |
Oops, something went wrong.