rename to one trip

This commit is contained in:
Alexander Laevens
2022-11-26 00:25:22 -07:00
parent 839147e0b0
commit 25e1a42392
207 changed files with 8015 additions and 245 deletions

View File

@@ -35,7 +35,7 @@ class Homegroup {
);
}
static Future<Homegroup?> fetchHomegroup(int id) async {
static Future<Homegroup?> get(int id) async {
String requestURL = "$baseURL/api/homegroups/$id/";
String token = TokenSingleton().getToken();
@@ -52,7 +52,7 @@ class Homegroup {
}
}
static Future<Homegroup?> createHomegroup(String title) async {
static Future<Homegroup?> create(String title) async {
String requestURL = "$baseURL/api/homegroups/";
String token = TokenSingleton().getToken();

View File

@@ -37,8 +37,7 @@ class HomegroupInvite {
return null;
}
static Future<HomegroupInvite?> createInvite(
int homegroupID, int userID) async {
static Future<HomegroupInvite?> create(int homegroupID, int userID) async {
String requestURL = "$baseURL/api/groupinvites/";
String token = TokenSingleton().getToken();
final http.Response response = await http.post(
@@ -54,7 +53,7 @@ class HomegroupInvite {
return null;
}
Future<bool> deleteInvite() async {
Future<bool> delete() async {
String requestURL = "$baseURL/api/groupinvites/$id/";
String token = TokenSingleton().getToken();
final http.Response response = await http.delete(

View File

@@ -8,10 +8,15 @@ import 'package:http/http.dart' as http;
class Recipe {
int id;
int homegroup;
String name;
List<Ingredient> ingredients;
Recipe({required this.id, required this.name, required this.ingredients});
Recipe(
{required this.id,
required this.name,
required this.ingredients,
required this.homegroup});
factory Recipe.fromJson(Map<String, dynamic> json) {
List<Ingredient> ingredients = [];
@@ -20,11 +25,12 @@ class Recipe {
}
return Recipe(
id: json["id"] as int,
homegroup: json["homegroup"] as int,
name: json["name"] as String,
ingredients: ingredients);
}
static Future<Recipe?> fetch(int id) async {
static Future<Recipe?> get(int id) async {
String requestURL = "$baseURL/api/recipes/$id/";
String token = TokenSingleton().getToken();
@@ -40,15 +46,15 @@ class Recipe {
return null;
}
static Future<List<Recipe>> fetchList(int groupID) async {
Homegroup? group = await Homegroup.fetchHomegroup(groupID);
static Future<List<Recipe>> getList(int groupID) async {
Homegroup? group = await Homegroup.get(groupID);
if (group == null) {
return [];
}
List<Recipe> recipes = [];
for (int recipeID in group.recipes) {
Recipe? recipe = await Recipe.fetch(recipeID);
Recipe? recipe = await Recipe.get(recipeID);
if (recipe != null) {
recipes.add(recipe);
}
@@ -56,4 +62,20 @@ class Recipe {
return recipes;
}
static Future<Recipe?> create(String name, int group) async {
String requestURL = "$baseURL/api/recipes/";
String token = TokenSingleton().getToken();
final http.Response response = await http.post(
Uri.parse(requestURL),
headers: {"Authorization": "Token $token"},
body: {"homegroup": "$group", "name": name},
);
if (response.statusCode == 201) {
return Recipe.fromJson(jsonDecode(response.body));
}
return null;
}
}

View File

@@ -37,7 +37,7 @@ class SimpleUser {
);
}
static Future<SimpleUser?> fetchUser({int? id}) async {
static Future<SimpleUser?> get({int? id}) async {
String requestURL = "$baseURL/auth/users/${id ?? 'me'}";
String token = TokenSingleton().getToken();

View File

@@ -39,7 +39,7 @@ class User {
);
}
static Future<User?> fetchUser() async {
static Future<User?> getMe() async {
String requestURL = "$baseURL/auth/users/me";
String token = TokenSingleton().getToken();
@@ -56,7 +56,7 @@ class User {
}
}
Future<User?> patch({
Future<User?> patchMe({
String? firstName,
String? lastName,
int? homegroup,

View File

@@ -20,7 +20,7 @@ class _ProfilePageState extends State<ProfilePage> {
late Future<bool> _isLoaded;
Future<bool> _loadProfile() async {
User? userInfo = await User.fetchUser();
User? userInfo = await User.getMe();
if (userInfo != null) {
_userInfo = userInfo;
@@ -108,7 +108,7 @@ class _ProfilePageState extends State<ProfilePage> {
invites: _userInfo.homegroupInvites,
onJoin: (id) async {
User? response =
await _userInfo.patch(homegroup: id);
await _userInfo.patchMe(homegroup: id);
if (response != null) {
setState(() {
@@ -117,7 +117,7 @@ class _ProfilePageState extends State<ProfilePage> {
}
},
onCreate: () async {
String? name = await createHomegroupDialog(
String? name = await textEntryDialog(
context,
"Create Homegroup",
"Homegroup Name",
@@ -132,14 +132,13 @@ class _ProfilePageState extends State<ProfilePage> {
return;
}
Homegroup? hg =
await Homegroup.createHomegroup(name);
Homegroup? hg = await Homegroup.create(name);
if (hg == null) {
return;
}
User? response =
await _userInfo.patch(homegroup: hg.id);
await _userInfo.patchMe(homegroup: hg.id);
if (response != null) {
setState(() {

View File

@@ -28,7 +28,7 @@ class _CreateJoinHomegroupState extends State<CreateJoinHomegroup> {
Future<bool> _loadInvites() async {
for (int id in widget.invites) {
Homegroup? hg = await Homegroup.fetchHomegroup(id);
Homegroup? hg = await Homegroup.get(id);
if (hg != null) {
_invitedGroups.add(hg);
@@ -157,7 +157,7 @@ class _EditHomegroupState extends State<EditHomegroup> {
Map<HomegroupInvite, SimpleUser> _groupInviteUsers = {};
Future<bool> _loadHomegroup() async {
Homegroup? hg = await Homegroup.fetchHomegroup(widget.homegroupID);
Homegroup? hg = await Homegroup.get(widget.homegroupID);
if (hg == null) {
return false;
}
@@ -168,7 +168,7 @@ class _EditHomegroupState extends State<EditHomegroup> {
_groupInviteUsers = {};
for (int id in hg.users) {
SimpleUser? u = await SimpleUser.fetchUser(id: id);
SimpleUser? u = await SimpleUser.get(id: id);
if (u != null) {
_groupUsers.add(u);
@@ -181,7 +181,7 @@ class _EditHomegroupState extends State<EditHomegroup> {
continue;
}
SimpleUser? u = await SimpleUser.fetchUser(id: invite.userID);
SimpleUser? u = await SimpleUser.get(id: invite.userID);
if (u != null) {
_groupInvites.add(invite);
@@ -271,8 +271,8 @@ class _EditHomegroupState extends State<EditHomegroup> {
user:
_groupInviteUsers[_groupInvites[index]]!,
onButton: (id) async {
bool success = await _groupInvites[index]
.deleteInvite();
bool success =
await _groupInvites[index].delete();
if (success) {
setState(() {
_isLoaded = _loadHomegroup();
@@ -304,7 +304,7 @@ class _EditHomegroupState extends State<EditHomegroup> {
}
for (int id in selectedIDs) {
await HomegroupInvite.createInvite(_homegroup!.id, id);
await HomegroupInvite.create(_homegroup!.id, id);
}
setState(() {

View File

@@ -1,8 +1,9 @@
import 'package:flutter/material.dart';
import 'package:grocery_helper/api/models/homegroup.dart';
import 'package:grocery_helper/api/models/ingredient.dart';
import 'package:grocery_helper/api/models/recipe.dart';
import 'package:grocery_helper/api/models/user.dart';
import 'package:grocery_helper/pages/recipes_page/widgets/recipe_card_widget.dart';
import 'package:grocery_helper/widgets/text_entry_dialog.dart';
class RecipesPage extends StatefulWidget {
const RecipesPage({super.key});
@@ -14,16 +15,15 @@ class RecipesPage extends StatefulWidget {
class _RecipesPageState extends State<RecipesPage> {
late Future<List<Recipe>> _recipes;
late User _userInfo;
late Homegroup _homegroup;
Future<List<Recipe>> _fetchList() async {
User? userInfo = await User.fetchUser();
User? userInfo = await User.getMe();
if (userInfo == null || userInfo.homegroup == null) {
return [];
}
_userInfo = userInfo;
List<Recipe> recipes = await Recipe.fetchList(_userInfo.homegroup!);
List<Recipe> recipes = await Recipe.getList(_userInfo.homegroup!);
return recipes;
}
@@ -42,38 +42,87 @@ class _RecipesPageState extends State<RecipesPage> {
return Text(snapshot.error.toString());
} else if (snapshot.hasData &&
snapshot.connectionState == ConnectionState.done) {
return RecipeList(recipes: snapshot.data!);
return RecipeList(
recipes: snapshot.data!, homegroup: _userInfo.homegroup!);
} else {
return const CircularProgressIndicator();
return const Center(child: CircularProgressIndicator());
}
},
);
}
}
class RecipeList extends StatelessWidget {
class RecipeList extends StatefulWidget {
final List<Recipe> recipes;
const RecipeList({super.key, required this.recipes});
final int homegroup;
const RecipeList({super.key, required this.recipes, required this.homegroup});
@override
State<RecipeList> createState() => _RecipeListState();
}
class _RecipeListState extends State<RecipeList> {
int? _expandedCard;
void showError(String text) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
backgroundColor: Theme.of(context).colorScheme.error,
content: Text(text),
action: SnackBarAction(
textColor: Theme.of(context).colorScheme.onError,
label: "Dismiss",
onPressed: () => ScaffoldMessenger.of(context).hideCurrentSnackBar(),
),
),
);
}
@override
Widget build(BuildContext context) {
return Stack(
children: [
ListView.builder(
padding: const EdgeInsets.all(8),
itemCount: recipes.length,
itemBuilder: (context, index) =>
RecipeCard(recipe: recipes[index])),
ListView.separated(
padding: const EdgeInsets.all(8),
itemCount: widget.recipes.length,
separatorBuilder: (context, index) => const SizedBox(height: 12),
itemBuilder: (context, index) => RecipeCard(
recipe: widget.recipes[index],
isExpanded: _expandedCard == index,
onTap: () {
setState(() {
if (_expandedCard == index) {
_expandedCard = null;
} else {
_expandedCard = index;
}
});
},
),
),
Align(
alignment: Alignment.bottomRight,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: FloatingActionButton.extended(
onPressed: () {},
onPressed: () async {
String? name =
await textEntryDialog(context, "Recipe Name", "Recipe");
if (name == null) {
return;
}
if (name == "") {
showError("Recipe name cannot be empty");
return;
}
Recipe? created = await Recipe.create(name, widget.homegroup);
},
label: Row(
children: const [Icon(Icons.note_add), Text("New Recipe")],
),
// child: const Icon(Icons.note_add),
),
),
)
@@ -81,78 +130,3 @@ class RecipeList extends StatelessWidget {
);
}
}
class RecipeCard extends StatelessWidget {
final Recipe recipe;
const RecipeCard({super.key, required this.recipe});
@override
Widget build(BuildContext context) {
final TextStyle ingredientStyle = Theme.of(context)
.textTheme
.titleLarge!
.copyWith(color: Theme.of(context).colorScheme.onSurface);
return Card(
elevation: 10,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
top: Radius.circular(10), bottom: Radius.zero),
),
margin: EdgeInsets.zero,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Text(
recipe.name,
style: Theme.of(context).textTheme.titleLarge,
),
),
const Divider(),
Container(
color: Theme.of(context).colorScheme.surface,
child: ListView.separated(
padding: const EdgeInsets.all(8),
itemCount: (recipe.ingredients.length / 2).ceil(),
shrinkWrap: true,
separatorBuilder: (context, index) => Divider(
color: Theme.of(context).colorScheme.onSurface,
),
itemBuilder: (context, index) {
if (recipe.ingredients.length % 2 == 0 ||
index * 2 <= recipe.ingredients.length - 2) {
return Row(
children: [
Expanded(
child: Text(
recipe.ingredients[index * 2].name,
style: ingredientStyle,
),
),
Expanded(
child: Text(recipe.ingredients[index * 2 + 1].name,
style: ingredientStyle),
)
],
);
} else {
return Row(
children: [
Text(recipe.ingredients[index * 2].name,
style: ingredientStyle),
],
);
}
},
),
)
],
),
),
);
}
}

View File

@@ -0,0 +1,133 @@
import 'package:flutter/material.dart';
import 'package:grocery_helper/api/models/ingredient.dart';
import 'package:grocery_helper/api/models/recipe.dart';
import 'package:grocery_helper/theme.dart';
import 'package:grocery_helper/widgets/text_entry_dialog.dart';
class RecipeCard extends StatelessWidget {
final Recipe recipe;
final bool isExpanded;
final Function() onTap;
const RecipeCard({
super.key,
required this.recipe,
required this.isExpanded,
required this.onTap,
});
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Card(
elevation: 10,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
top: const Radius.circular(10),
bottom: isExpanded ? Radius.zero : const Radius.circular(10)),
),
margin: EdgeInsets.zero,
child: GestureDetector(
onTap: onTap,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Padding(
padding: const EdgeInsets.all(8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Text(
recipe.name,
style: Theme.of(context).textTheme.headlineSmall,
),
),
AnimatedRotation(
duration: const Duration(milliseconds: 200),
turns: isExpanded ? 0.5 : 0,
child: const Icon(Icons.expand_more, size: 30),
),
],
),
),
AnimatedSize(
duration: const Duration(milliseconds: 200),
child: isExpanded
? IngredientSection(ingredients: recipe.ingredients)
: const SizedBox(),
),
],
),
),
),
AnimatedSize(
duration: const Duration(milliseconds: 200),
child: isExpanded
? ElevatedButton(
style: bottomButtonStyle,
onPressed: () async {
String? name = await textEntryDialog(
context, "Ingredient Name", "Ingredient");
},
child: const Text("Add Ingredient"),
)
: const SizedBox(),
)
],
);
}
}
class IngredientSection extends StatelessWidget {
final List<Ingredient> ingredients;
const IngredientSection({super.key, required this.ingredients});
@override
Widget build(BuildContext context) {
final TextStyle ingredientStyle = Theme.of(context)
.textTheme
.bodyLarge!
.copyWith(color: Theme.of(context).colorScheme.onSurface);
return Container(
color: Theme.of(context).colorScheme.surface,
child: ListView.separated(
padding: const EdgeInsets.all(8),
itemCount: (ingredients.length / 2).ceil(),
shrinkWrap: true,
separatorBuilder: (context, index) => Divider(
color: index % 2 == 0
? Theme.of(context).colorScheme.primary
: Theme.of(context).colorScheme.error,
),
itemBuilder: (context, index) {
if (ingredients.length % 2 == 0 ||
index * 2 <= ingredients.length - 2) {
return Row(
children: [
Expanded(
child: Text(
ingredients[index * 2].name,
style: ingredientStyle,
),
),
Expanded(
child: Text(ingredients[index * 2 + 1].name,
style: ingredientStyle),
)
],
);
} else {
return Row(
children: [
Text(ingredients[index * 2].name, style: ingredientStyle),
],
);
}
},
),
);
}
}

View File

@@ -15,59 +15,69 @@ class _LoginScreenState extends State<LoginScreen> {
@override
Widget build(BuildContext context) {
final Brightness brightness = Theme.of(context).colorScheme.brightness;
return Scaffold(
body: Center(
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: [
SizedBox.square(
dimension: 200,
child: SvgPicture.asset(
brightness == Brightness.light
? "assets/images/holes.svg"
: "assets/images/holes-dark.svg",
fit: BoxFit.contain,
),
),
FractionallySizedBox(
widthFactor: 0.9,
child: Card(
elevation: 10,
child: Padding(
padding: const EdgeInsets.all(8),
child: Column(
children: [
LayoutBuilder(builder: (builder, constraints) {
return ToggleButtons(
isSelected: _isSelected,
constraints: BoxConstraints.expand(
width: constraints.maxWidth / 2 - 8,
height: 30),
selectedBorderColor:
Theme.of(context).colorScheme.primary,
onPressed: (index) {
setState(() {
for (int i = 0; i < _isSelected.length; i++) {
_isSelected[i] = (i == index);
}
});
},
children: const [Text("Log In"), Text("Sign Up")],
);
}),
_isSelected[0] ? const LoginForm() : const SignupForm()
],
body: LayoutBuilder(
builder: (BuildContext context, BoxConstraints viewportContraints) {
return SingleChildScrollView(
child: ConstrainedBox(
constraints:
BoxConstraints(minHeight: viewportContraints.maxHeight),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.min,
children: [
SizedBox.square(
dimension: 150,
child: SvgPicture.asset(
"assets/images/desktop.svg",
fit: BoxFit.contain,
),
),
),
FractionallySizedBox(
widthFactor: 0.9,
child: Card(
elevation: 10,
child: Padding(
padding: const EdgeInsets.all(8),
child: Column(
children: [
LayoutBuilder(builder: (builder, constraints) {
return ToggleButtons(
isSelected: _isSelected,
constraints: BoxConstraints.expand(
width: constraints.maxWidth / 2 - 8,
height: 30),
selectedBorderColor:
Theme.of(context).colorScheme.primary,
onPressed: (index) {
setState(() {
for (int i = 0;
i < _isSelected.length;
i++) {
_isSelected[i] = (i == index);
}
});
},
children: const [
Text("Log In"),
Text("Sign Up")
],
);
}),
_isSelected[0]
? const LoginForm()
: const SignupForm()
],
),
),
),
),
],
),
],
),
),
),
);
},
),
);
}

View File

@@ -62,7 +62,7 @@ class _TextEntryFormState extends State<TextEntryForm> {
}
}
Future<String?> createHomegroupDialog(
Future<String?> textEntryDialog(
BuildContext context, String title, String label,
{String? defaultValue}) async {
String? name = await showDialog(