Skip to content
This repository has been archived by the owner on Oct 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #41 from phantomate/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
phantomate authored Jul 30, 2023
2 parents b08db83 + 0aded12 commit 0ed124c
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 32 deletions.
3 changes: 3 additions & 0 deletions lib/blocs/login/login_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:bloc/bloc.dart';
import 'package:hive/hive.dart';
import 'package:untare/blocs/authentication/authentication_bloc.dart';
import 'package:untare/blocs/authentication/authentication_event.dart';
import 'package:untare/exceptions/api_connection_exception.dart';
import 'package:untare/exceptions/api_exception.dart';
import 'package:untare/models/user.dart';
import 'package:untare/models/userToken.dart';
Expand Down Expand Up @@ -50,6 +51,8 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {
} else {
emit(LoginFailure(error: e.message ?? e.toString()));
}
} on ApiConnectionException catch(e) {
emit(LoginFailure(error: e.message));
} catch (err) {
emit(LoginFailure(error: err.toString()));
}
Expand Down
47 changes: 32 additions & 15 deletions lib/components/widgets/recipe_detail_tabbar_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class RecipeDetailTabBarWidgetState extends State<RecipeDetailTabBarWidget> {
return TabBarView(
children: [
ingredientTabView(),
directionsTabView()
directionsTabView(widget.recipe)
],
);
}
Expand Down Expand Up @@ -114,24 +114,24 @@ class RecipeDetailTabBarWidgetState extends State<RecipeDetailTabBarWidget> {
}
}

Widget directionsTabView() {
Widget directionsTabView(Recipe recipe) {
List<Widget> directionsSteps = [];

if (widget.recipe.steps.isNotEmpty) {
if (widget.recipe.steps.length > 1) {
for (int i = 0; i < widget.recipe.steps.length; i++) {
if (recipe.steps.isNotEmpty) {
if (recipe.steps.length > 1) {
for (int i = 0; i < recipe.steps.length; i++) {
List<Widget> stepList = [];

stepList.addAll(widget.recipe.steps[i].ingredients.map((item) => ingredientComponent(item, servings, newServings, true, context)).toList());
stepList.addAll(recipe.steps[i].ingredients.map((item) => ingredientComponent(item, servings, newServings, true, context)).toList());

stepList.add(Padding(padding: const EdgeInsets.fromLTRB(20, 12, 15, 10), child: Text(widget.recipe.steps[i].instruction ?? '', style: const TextStyle(fontSize: 15))));
stepList.add(Padding(padding: const EdgeInsets.fromLTRB(20, 12, 15, 10), child: Text(recipe.steps[i].instruction ?? '', style: const TextStyle(fontSize: 15))));

directionsSteps.add(directionStepLayout(context, Column(crossAxisAlignment: CrossAxisAlignment.start, children: stepList), i+1, widget.recipe.steps[i].time, widget.recipe.steps[i].name));
directionsSteps.add(directionStepLayout(context, Column(crossAxisAlignment: CrossAxisAlignment.start, children: stepList), i+1, recipe.steps[i].time, recipe.steps[i].name));
}

} else if (widget.recipe.steps.length == 1) {
List<String> splitDirectionsStrings = (widget.recipe.steps.first.instruction != null && widget.recipe.steps.first.instruction != '')
? widget.recipe.steps.first.instruction!.split("\n\n")
} else if (recipe.steps.length == 1) {
List<String> splitDirectionsStrings = (recipe.steps.first.instruction != null && recipe.steps.first.instruction != '')
? recipe.steps.first.instruction!.split("\n\n")
: [];

if (splitDirectionsStrings.length <= 2) {
Expand All @@ -151,8 +151,8 @@ class RecipeDetailTabBarWidgetState extends State<RecipeDetailTabBarWidget> {
context,
Padding(padding: const EdgeInsets.fromLTRB(20, 12, 15, 10), child: Text(splitInstruction, style: const TextStyle(fontSize: 15))),
i+1,
widget.recipe.steps.first.time,
widget.recipe.steps.first.name
recipe.steps.first.time,
recipe.steps.first.name
)
);
}
Expand Down Expand Up @@ -262,11 +262,28 @@ class RecipeDetailTabBarWidgetState extends State<RecipeDetailTabBarWidget> {
SettingsCubit settingsCubit = context.read<SettingsCubit>();
bool? useFractions = (settingsCubit.state.userServerSetting!.useFractions == true);

double rawAmount = (ingredient.amount * (((newServing/initServing))*100).ceil()/100);
double rawAmount = ingredient.amount * newServing / initServing;
String amount = (ingredient.amount > 0) ? ('${rawAmount.toFormattedString()} ') : '';
if (amount != '' && useFractions == true && (rawAmount % 1) != 0) {
amount = '${rawAmount.toMixedFraction()} ';
// If we have a complex decimal we build a "simple" fraction. Otherwise we do the normal one
if ((((rawAmount - rawAmount.toInt()) * 100) % 5) != 0) {
// Use this crap because we can't change precision programmatically
if (rawAmount.toInt() < 1) {
amount = '${MixedFraction.fromDouble(rawAmount, precision: 1.0e-1).reduce()} ';
} else if (rawAmount.toInt() < 10) {
amount = '${MixedFraction.fromDouble(rawAmount, precision: 1.0e-2).reduce()} ';
} else if (rawAmount.toInt() < 100) {
amount = '${MixedFraction.fromDouble(rawAmount, precision: 1.0e-3).reduce()} ';
} else if(rawAmount.toInt() < 1000) {
amount = '${MixedFraction.fromDouble(rawAmount, precision: 1.0e-4).reduce()} ';
} else {
amount = '${MixedFraction.fromDouble(rawAmount, precision: 1.0e-5).reduce()} ';
}
} else {
amount = '${MixedFraction.fromDouble(rawAmount)} ';
}
}

String unit = (ingredient.amount > 0 && ingredient.unit != null) ? ('${ingredient.unit!.getUnitName(rawAmount)} ') : '';
String food = (ingredient.food != null) ? ('${ingredient.food!.getFoodName(rawAmount)} ') : '';
String note = (ingredient.note != null && ingredient.note != '') ? ('(${ingredient.note!})') : '';
Expand Down
21 changes: 19 additions & 2 deletions lib/components/widgets/recipe_shopping_list_stateful_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,28 @@ class RecipeShoppingListWidgetState extends State<RecipeShoppingListWidget> {
SettingsCubit settingsCubit = context.read<SettingsCubit>();
bool? useFractions = (settingsCubit.state.userServerSetting!.useFractions == true);

double rawAmount = (ingredient.amount * (((newServing/initServing))*100).ceil()/100);
double rawAmount = ingredient.amount * newServing / initServing;
String amount = (ingredient.amount > 0) ? ('${rawAmount.toFormattedString()} ') : '';
if (amount != '' && useFractions == true && (rawAmount % 1) != 0) {
amount = '${rawAmount.toMixedFraction()} ';
// If we have a complex decimal we build a "simple" fraction. Otherwise we do the normal one
if ((((rawAmount - rawAmount.toInt()) * 100) % 5) != 0) {
// Use this crap because we can't change precision programmatically
if (rawAmount.toInt() < 1) {
amount = '${MixedFraction.fromDouble(rawAmount, precision: 1.0e-1).reduce()} ';
} else if (rawAmount.toInt() < 10) {
amount = '${MixedFraction.fromDouble(rawAmount, precision: 1.0e-2).reduce()} ';
} else if (rawAmount.toInt() < 100) {
amount = '${MixedFraction.fromDouble(rawAmount, precision: 1.0e-3).reduce()} ';
} else if(rawAmount.toInt() < 1000) {
amount = '${MixedFraction.fromDouble(rawAmount, precision: 1.0e-4).reduce()} ';
} else {
amount = '${MixedFraction.fromDouble(rawAmount, precision: 1.0e-5).reduce()} ';
}
} else {
amount = '${MixedFraction.fromDouble(rawAmount)} ';
}
}

String unit = (ingredient.unit != null && ingredient.amount > 0) ? ('${ingredient.unit!.getUnitName(rawAmount)} ') : '';
String food = (ingredient.food != null) ? ('${ingredient.food!.getFoodName(rawAmount)} ') : '';
bool? checkBoxValue = !(ingredient.food != null && ingredient.food!.onHand!);
Expand Down
3 changes: 3 additions & 0 deletions lib/exceptions/api_connection_exception.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
class ApiConnectionException implements Exception{
final String message;

ApiConnectionException({this.message = 'Api error'});
}
3 changes: 1 addition & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_phoenix/flutter_phoenix.dart';
Expand Down Expand Up @@ -61,7 +60,7 @@ void main() async{

await Workmanager().initialize(_callbackDispatcher);

SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]).then((_){
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp, DeviceOrientation.portraitDown, DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]).then((_){
runApp(
Phoenix(
child: const Tare()
Expand Down
6 changes: 3 additions & 3 deletions lib/models/recipe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Recipe {
@HiveField(10)
final String? updatedAt;
@HiveField(11)
final bool internal;
final bool? internal;
@HiveField(12)
final int? servings;
@HiveField(13)
Expand All @@ -56,7 +56,7 @@ class Recipe {
this.createdBy,
this.createdAt,
this.updatedAt,
required this.internal,
this.internal,
this.servings,
this.servingsText,
this.rating,
Expand Down Expand Up @@ -158,7 +158,7 @@ class Recipe {
createdBy: json['created_by'] as int?,
createdAt: json['created_at'] as String?,
updatedAt: json['updated_at'] as String?,
internal: json['internal'] as bool,
internal: json['internal'] as bool?,
servings: (json['servings'] is int) ? json['servings'] : ((json['servings'] is double) ? json['servings'].toInt() : null),
servingsText: json['servings_text'] as String?,
rating: (json['rating'] is int) ? json['rating'] : ((json['rating'] is double) ? json['rating'].toInt() : null),
Expand Down
2 changes: 1 addition & 1 deletion lib/models/recipe.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 16 additions & 3 deletions lib/models/step.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:untare/models/ingredient.dart';
import 'package:hive/hive.dart';
import 'package:untare/models/recipe.dart';

part 'step.g.dart';

Expand All @@ -22,6 +23,10 @@ class StepModel {
final int? time;
@HiveField(7)
final int? order;
@HiveField(8)
final int? stepRecipe;
@HiveField(9)
final Recipe? stepRecipeData;

StepModel({
this.id,
Expand All @@ -31,7 +36,9 @@ class StepModel {
this.ingredientsMarkdown,
this.ingredientsVue,
this.time,
this.order
this.order,
this.stepRecipe,
this.stepRecipeData
});

StepModel copyWith({
Expand All @@ -43,6 +50,8 @@ class StepModel {
String? ingredientsVue,
int? time,
int? order,
int? stepRecipe,
Recipe? stepRecipeData
}) {
return StepModel(
id: id ?? this.id,
Expand All @@ -52,7 +61,8 @@ class StepModel {
ingredientsMarkdown: ingredientsMarkdown ?? this.ingredientsMarkdown,
ingredientsVue: ingredientsVue ?? this.ingredientsVue,
time: time ?? this.time,
order: order ?? this.order
order: order ?? this.order,
stepRecipe: stepRecipe ?? this.stepRecipe
);
}

Expand All @@ -71,7 +81,8 @@ class StepModel {
'instruction': instruction ?? '',
'ingredients': ingredients,
'time': time ?? 0,
'order': order ?? 0
'order': order ?? 0,
'step_recipe': stepRecipe
};
}

Expand All @@ -85,6 +96,8 @@ class StepModel {
ingredientsVue: json['ingredients_vue'] as String?,
time: json['time'] as int?,
order: json['order'] as int?,
stepRecipe: json['step_recipe'] as int?,
stepRecipeData: (json['step_recipe_data'] != null) ? Recipe.fromJson(json['step_recipe_data']) : null
);
}
}
10 changes: 8 additions & 2 deletions lib/models/step.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions lib/pages/recipes_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ class RecipesPageState extends State<RecipesPage> {
}
isLoading = false;
recipes.addAll(state.recipes);
} else if (state is RecipeListFetchedFromCache) {
if (state.recipes.isEmpty || state.recipes.length < pageSize) {
isLastPage = true;
}
isLoading = false;
recipes.addAll(state.recipes);
}

if (state is RecipeError) {
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/starting_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class __SignInFormState extends State<_SignInForm> {
hintText: 'https://recipes.excample.com'
),
controller: _urlController,
keyboardType: TextInputType.name,
keyboardType: TextInputType.url,
autocorrect: false,
validator: (value){
if (value == null){
Expand Down
4 changes: 2 additions & 2 deletions lib/services/api/api_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ApiService {

Future sendRequest(BaseRequest request) async {
try {
Response response = await Response.fromStream(await request.send());
Response response = await Response.fromStream(await request.send().timeout(const Duration(seconds: 10)));

if (response.statusCode == 401) {
box.clear();
Expand All @@ -111,7 +111,7 @@ class ApiService {
);
}

throw ApiConnectionException();
throw ApiConnectionException(message: e.toString());
}
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.1.1+3
version: 1.1.2+4

environment:
sdk: ">=2.17.0 <3.0.0"
Expand Down

0 comments on commit 0ed124c

Please sign in to comment.