Skip to content

Commit

Permalink
implement retrying the query, visual overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
humanova committed May 29, 2023
1 parent 20da566 commit f3f1253
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 69 deletions.
23 changes: 21 additions & 2 deletions lib/components/data_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class DataResponse {
class DataProcessor {
final String apiUrl = 'https://humanova.space/api/price';

Future<DataResponse> getPrices(String imageBase64) async {
Future<DataResponse> getPricesByImage(String imageBase64) async {
try {
final response = await http.post(
apiUrl,
Expand All @@ -30,9 +30,28 @@ class DataProcessor {
}
}

Future<DataResponse> getPricesByQuery(String query) async {
try {
final response = await http.post(
apiUrl,
headers: {
'Content-Type': 'application/json',
},
body: json.encode({'text': query}),
);

return DataResponse(
isSuccess: response.statusCode == 200,
data: json.decode(utf8.decode(response.bodyBytes)));
} catch (e) {
return DataResponse(isSuccess: false, data: e.toString());
}
}

Product parseProduct(dynamic jsonData) {
final String name = jsonData['name'];
final String image = jsonData['image'];
final String query = jsonData['query'];
final List<dynamic> pricesData = jsonData['prices'];

final List<Price> prices = pricesData
Expand All @@ -42,6 +61,6 @@ class DataProcessor {
))
.toList();

return Product(name: name, image: image, prices: prices);
return Product(name: name, image: image, query: query, prices: prices);
}
}
18 changes: 10 additions & 8 deletions lib/models/product.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
class Product {
final String name;
final String image; //image url
final String image;
final String query;
final List<Price> prices;

Product({this.name, this.image, this.prices});
Product({this.name, this.image, this.query, this.prices});

factory Product.fromJson(Map<String, dynamic> json) {
var priceList = json['prices'] as List;
List<Price> prices =
priceList.map((priceJson) => Price.fromJson(priceJson)).toList();
List<Price> prices = priceList.map((priceJson) => Price.fromJson(priceJson)).toList();

return Product(
name: json['name'] as String,
image: json['image'] as String,
query: json['query'] as String,
prices: prices,
);
}
Expand All @@ -21,6 +22,7 @@ class Product {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['name'] = this.name;
data['image'] = this.image;
data['query'] = this.query;
data['prices'] = this.prices.map((price) => price.toJson()).toList();
return data;
}
Expand All @@ -32,10 +34,10 @@ class Price {

Price({this.store, this.price});

factory Price.fromJson(Map<String, dynamic> json) {
factory Price.fromJson(Map<String, dynamic> json) {
return Price(
store: json['store'],
price: json['price'].toDouble(),
store: json['store'] as String,
price: json['price'] as double,
);
}

Expand All @@ -44,5 +46,5 @@ class Price {
'store': store,
'price': price,
};
}
}
}
100 changes: 80 additions & 20 deletions lib/screens/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,93 @@ import 'package:prc_app/screens/image_screen.dart';
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final screenHeight = MediaQuery.of(context).size.height;
final screenWidth = MediaQuery.of(context).size.width;
final buttonSize = screenHeight * 0.3;

return Scaffold(
appBar: AppBar(
title: Text('PRC'),
title: Text('PRC: Shop Smartly!'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SearchHistoryScreen()),
);
},
child: Text('View Search History'),
),
SizedBox(height: 20),
RaisedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ImageScreen()),
);
},
child: Text('Find Prices'),
Container(
width: screenWidth * 0.8,
child: Column(
children: [
GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ImageScreen()),
);
},
child: Container(
width: double.infinity,
height: buttonSize,
decoration: BoxDecoration(
color: Colors.orange,
borderRadius: BorderRadius.circular(10.0),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.2),
blurRadius: 10.0,
spreadRadius: 2.0,
),
],
),
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.search,
size: buttonSize * 0.7,
color: Colors.white,
),

],
),
),
),
),
SizedBox(height: screenHeight * 0.05),
GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SearchHistoryScreen(),
),
);
},
child: Container(
width: double.infinity,
height: buttonSize,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(10.0),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.2),
blurRadius: 10.0,
spreadRadius: 2.0,
),
],
),
child: Center(
child: Icon(
Icons.history,
size: buttonSize * 0.6,
color: Colors.white,
),
),
),
),
],
),
),
],
),
Expand Down
Loading

0 comments on commit f3f1253

Please sign in to comment.