Skip to content

Commit

Permalink
added recipes sources
Browse files Browse the repository at this point in the history
  • Loading branch information
judemont committed Apr 9, 2024
1 parent c73e20d commit ad0eae5
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 47 deletions.
20 changes: 13 additions & 7 deletions lib/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class DatabaseService {
static const String databaseName = "reciperDB.sqlite";
static Database? db;

static const databaseVersion = 2;
static const databaseVersion = 3;
List<String> tables = ["Recipes"];

static const secretKey = "2023_PRIVATE_KEY_ENCRYPT_2023";
Expand Down Expand Up @@ -45,9 +45,12 @@ class DatabaseService {
print(" DB Version : $newVersion");
print(oldVersion);
if (oldVersion < newVersion) {
if (oldVersion < 3) {
if (oldVersion < 2) {
db.execute("""ALTER TABLE Recipes ADD COLUMN servings TEXT """);
}
if (oldVersion < 3) {
db.execute("""ALTER TABLE Recipes ADD COLUMN source TEXT """);
}
}
}

Expand All @@ -58,7 +61,8 @@ class DatabaseService {
steps TEXT,
title TEXT NOT NULL,
servings TEXT,
ingredients TEXT
ingredients TEXT,
source TEXT,
)
""");
}
Expand All @@ -74,7 +78,8 @@ class DatabaseService {
steps: recipe.steps,
title: recipe.title,
ingredients: recipe.ingredients,
servings: recipe.servings)
servings: recipe.servings,
source: recipe.source)
.toMap());
return id;
}
Expand All @@ -96,9 +101,10 @@ class DatabaseService {

return Recipe(
id: queryResult[0]["id"] as int,
steps: queryResult[0]["steps"] as String,
title: queryResult[0]["title"] as String,
ingredients: queryResult[0]["ingredients"] as String,
steps: queryResult[0]["steps"] as String?,
title: queryResult[0]["title"] as String?,
ingredients: queryResult[0]["ingredients"] as String?,
source: queryResult[0]["source"] as String?,
);
}

Expand Down
30 changes: 17 additions & 13 deletions lib/models/recipe.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
class Recipe {
int? id;
String steps;
String servings;
String title;
String ingredients;
String? steps;
String? servings;
String? title;
String? ingredients;
String? source;

Recipe(
{this.id,
this.steps = "",
this.servings = "",
this.title = '',
this.ingredients = ""});
this.steps,
this.servings,
this.title,
this.ingredients,
this.source});

Map<String, Object?> toMap() {
return {
Expand All @@ -19,16 +21,18 @@ class Recipe {
'steps': steps,
'title': title,
'ingredients': ingredients,
'source': source,
};
}

static Recipe fromMap(Map<String, dynamic> map) {
return Recipe(
id: map['id'] as int?,
servings: map["servings"] ?? "",
steps: map['steps'] ?? "",
title: map['title'] ?? "",
ingredients: map['ingredients'] ?? "",
id: map['id'],
servings: map["servings"],
steps: map['steps'],
title: map['title'],
ingredients: map['ingredients'],
source: map['source'],
);
}
}
1 change: 1 addition & 0 deletions lib/widgets/extract_recipe_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class _ExtractRecipeButtonState extends State<ExtractRecipeButton> {
ingredients:
(recipeData.ingredients ?? []).join("\n"),
steps: (recipeData.instructions ?? []).join("\n"),
source: recipeData.source,
),
),
),
Expand Down
36 changes: 22 additions & 14 deletions lib/widgets/recipe_editor_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class _RecipeEditorPageState extends State<RecipeEditorPage> {
String ingredients = "";
String steps = "";
String servings = "";
String source = "";

@override
Widget build(BuildContext context) {
Expand All @@ -44,14 +45,16 @@ class _RecipeEditorPageState extends State<RecipeEditorPage> {
title: title,
servings: servings,
steps: steps,
ingredients: ingredients));
ingredients: ingredients,
source: source));
} else {
DatabaseService.updateRecipe(Recipe(
id: widget.initialRecipe!.id,
servings: servings,
title: title,
steps: steps,
ingredients: ingredients));
ingredients: ingredients,
source: source));
}

Navigator.of(context).push(
Expand Down Expand Up @@ -99,12 +102,6 @@ class _RecipeEditorPageState extends State<RecipeEditorPage> {
SizedBox(height: fieldsMargin),
TextFormField(
initialValue: widget.initialRecipe?.ingredients,
validator: (value) {
if (value!.isEmpty) {
return 'Please enter something.';
}
return null;
},
onSaved: (value) {
ingredients = value!;
},
Expand All @@ -118,12 +115,6 @@ class _RecipeEditorPageState extends State<RecipeEditorPage> {
SizedBox(height: fieldsMargin),
TextFormField(
initialValue: widget.initialRecipe?.steps,
validator: (value) {
if (value!.isEmpty) {
return 'Please enter something.';
}
return null;
},
onSaved: (value) {
steps = value!;
},
Expand All @@ -134,6 +125,23 @@ class _RecipeEditorPageState extends State<RecipeEditorPage> {
hintText: 'Steps',
),
),
SizedBox(height: fieldsMargin),
TextFormField(
validator: (value) {
if ((value ?? "").isNotEmpty &&
Uri.tryParse(value ?? "") == null) {
return "Please enter a valid URL";
}
},
initialValue: widget.initialRecipe?.source,
onSaved: (value) {
source = value ?? "";
},
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Recipe source URL',
),
)
],
),
),
Expand Down
25 changes: 19 additions & 6 deletions lib/widgets/recipe_view_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:reciper/widgets/recipe_editor_page.dart';
import 'package:url_launcher/url_launcher.dart';
import '../models/recipe.dart';

class RecipeViewPage extends StatefulWidget {
Expand All @@ -17,16 +18,17 @@ class _RecipeViewPageState extends State<RecipeViewPage> {

@override
Widget build(BuildContext context) {
List<String> ingredientsList = widget.recipe.ingredients.split("\n");
List<String> ingredientsList =
(widget.recipe.ingredients ?? "").split("\n");
ingredientsList.removeWhere((element) => element == "");

if (ingredientsList.isEmpty) {
ingredientsList.add(widget.recipe.ingredients);
ingredientsList.add(widget.recipe.ingredients ?? "");
}
List<String> stepsList = widget.recipe.steps.split("\n");
List<String> stepsList = (widget.recipe.steps ?? "").split("\n");
stepsList.removeWhere((element) => element == "");
if (stepsList.isEmpty) {
stepsList.add(widget.recipe.ingredients);
stepsList.add(widget.recipe.ingredients ?? "");
}

return Scaffold(
Expand Down Expand Up @@ -57,14 +59,14 @@ class _RecipeViewPageState extends State<RecipeViewPage> {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.recipe.title,
widget.recipe.title ?? "",
style: const TextStyle(
fontSize: 30, fontWeight: FontWeight.bold),
),
const SizedBox(
height: 15,
),
Text(widget.recipe.servings.isNotEmpty
Text((widget.recipe.servings ?? "").isNotEmpty
? "Servings: ${widget.recipe.servings}"
: ""),
const SizedBox(
Expand Down Expand Up @@ -120,6 +122,17 @@ class _RecipeViewPageState extends State<RecipeViewPage> {
});
});
}),
ListTile(
title: Text(
"Source: ${widget.recipe.source}",
style: const TextStyle(color: Colors.blue),
),
onTap: () {
if (Uri.tryParse(widget.recipe.source ?? "") !=
null) {
launchUrl(Uri.parse(widget.recipe.source!));
}
})
],
))));
}
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/recipes_list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class _RecipeListViewState extends State<RecipeListView> {
itemCount: widget.recipes.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(widget.recipes[index].title),
title: Text(widget.recipes[index].title??""),
selected: selectedRecipesID.contains(widget.recipes[index].id),
selectedColor: Colors.white,
selectedTileColor: const Color.fromARGB(255, 63, 63, 63),
Expand Down
8 changes: 4 additions & 4 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,10 @@ packages:
dependency: transitive
description:
name: path_provider_android
sha256: "477184d672607c0a3bf68fbbf601805f92ef79c82b64b4d6eb318cbca4c48668"
sha256: "51f0d2c554cfbc9d6a312ab35152fc77e2f0b758ce9f1a444a3a1e5b8f3c6b7f"
url: "https://pub.dev"
source: hosted
version: "2.2.2"
version: "2.2.3"
path_provider_foundation:
dependency: transitive
description:
Expand Down Expand Up @@ -492,10 +492,10 @@ packages:
dependency: "direct main"
description:
name: recipe_extractor
sha256: "0d4f5118983416b36871cdb765dc28a8189394b065e65dbc15276e4d1ef35899"
sha256: "85aceb38e0090f28e26fdef9afb7b983db2cfa998f34ce5a6561f95d34a92829"
url: "https://pub.dev"
source: hosted
version: "2.2.1"
version: "2.3.2"
share_plus:
dependency: "direct main"
description:
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.2.0
version: 1.3.0

environment:
sdk: '>=3.3.1 <4.0.0'
Expand All @@ -33,7 +33,7 @@ dependencies:
flutter:
sdk: flutter
path_provider: ^2.1.2
recipe_extractor: ^2.2.1
recipe_extractor: ^2.3.2
share_plus: ^8.0.3
sqflite: ^2.3.3
sqflite_common: ^2.5.4
Expand Down

0 comments on commit ad0eae5

Please sign in to comment.