Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature choose a book to trade before confirm #46

Merged
merged 2 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 58 additions & 27 deletions lib/components/cards/transaction_card.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:livrodin/components/button_option.dart';
import 'package:livrodin/components/dialogs/book_list_available_from_user.dart';
import 'package:livrodin/components/profile_icon.dart';
import 'package:livrodin/configs/livrodin_icons.dart';
import 'package:livrodin/configs/themes.dart';
import 'package:livrodin/controllers/book_controller.dart';
import 'package:livrodin/models/availability.dart';
import 'package:livrodin/models/book.dart';
import 'package:livrodin/models/transaction.dart';
import 'package:livrodin/models/user.dart';
Expand All @@ -14,13 +18,15 @@ class TransactionCard extends StatelessWidget {
final Function()? onMessagePressed;
final Function()? onConfirmPressed;
final Function()? onCancelPressed;
final Function(Availability)? onChooseBook;

const TransactionCard({
super.key,
required this.transaction,
this.onMessagePressed,
this.onConfirmPressed,
this.onCancelPressed,
this.onChooseBook,
});

@override
Expand Down Expand Up @@ -65,7 +71,10 @@ class TransactionCard extends StatelessWidget {
height: 5,
),
Expanded(
child: TransactionDetail(transaction: transaction),
child: TransactionDetail(
transaction: transaction,
onChooseBook: onChooseBook,
),
)
],
),
Expand Down Expand Up @@ -154,15 +163,15 @@ class TransactionCard extends StatelessWidget {
}

class TransactionDetail extends StatelessWidget {
const TransactionDetail({
Key? key,
required this.transaction,
this.color,
}) : super(key: key);
const TransactionDetail(
{Key? key, required this.transaction, this.color, this.onChooseBook})
: super(key: key);

final Transaction transaction;
final Color? color;

final Function(Availability)? onChooseBook;

@override
Widget build(BuildContext context) {
return Row(
Expand All @@ -185,6 +194,9 @@ class TransactionDetail extends StatelessWidget {
user: transaction.user2,
book: transaction.book2,
otherUser: transaction.user1,
onChooseBook: (availability) {
onChooseBook?.call(availability);
},
)
: ProfileCard(
user: transaction.user2,
Expand Down Expand Up @@ -231,11 +243,13 @@ class BookCardWithProfile extends StatelessWidget {
final Book? book;
final User user;
final User otherUser;
final Function(Availability)? onChooseBook;
const BookCardWithProfile({
Key? key,
required this.user,
required this.otherUser,
this.book,
this.onChooseBook,
}) : super(key: key);

@override
Expand All @@ -246,27 +260,44 @@ class BookCardWithProfile extends StatelessWidget {
children: [
Column(
children: [
Container(
height: 125,
width: 87,
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(5),
image: book != null
? DecorationImage(
image: NetworkImage(book!.coverUrl!),
fit: BoxFit.cover,
)
: null,
),
child: Visibility(
visible: book == null,
child: Center(
child: Text(
"Aguardando\n${otherUser.name}\nEscolher",
style: const TextStyle(
fontWeight: FontWeight.w600, fontSize: 10, color: grey),
textAlign: TextAlign.center,
GestureDetector(
onTap: book == null
? () async {
var result = await Get.dialog<Availability?>(
BookListAvailableFromUser(
user: user,
),
);

if (result != null) {
onChooseBook?.call(result);
}
}
: null,
child: Container(
height: 125,
width: 87,
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(5),
image: book != null
? DecorationImage(
image: NetworkImage(book!.coverUrl!),
fit: BoxFit.cover,
)
: null,
),
child: Visibility(
visible: book == null,
child: Center(
child: Text(
"Aguardando\n${otherUser.isMe ? "Você" : otherUser.name}\nEscolher",
style: const TextStyle(
fontWeight: FontWeight.w600,
fontSize: 10,
color: grey),
textAlign: TextAlign.center,
),
),
),
),
Expand Down
50 changes: 50 additions & 0 deletions lib/components/dialogs/book_list_available_from_user.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:livrodin/components/cards/book_card.dart';
import 'package:livrodin/controllers/user_book_available_controller.dart';
import 'package:livrodin/models/user.dart';

class BookListAvailableFromUser extends StatefulWidget {
final User user;

const BookListAvailableFromUser({super.key, required this.user});

@override
State<BookListAvailableFromUser> createState() =>
_BookListAvailableFromUserState();
}

class _BookListAvailableFromUserState extends State<BookListAvailableFromUser> {
late UserBookAvailableController _userBookAvailable;

@override
void initState() {
super.initState();
_userBookAvailable =
Get.put(UserBookAvailableController(user: widget.user));
}

@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text('Selecione um livro'),
content: Obx(
() => SizedBox(
width: 300,
child: SingleChildScrollView(
child: Column(
children: _userBookAvailable.availabilityList
.map((e) => BookCard(
book: e.book,
onTap: (_) {
Get.back(result: e);
},
))
.toList(),
),
),
),
),
);
}
}
2 changes: 1 addition & 1 deletion lib/components/dialogs/user_list_available.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class UserListAvailableDialog extends StatelessWidget {
return ListTile(
title: Text(availabilities[index].book.title!),
subtitle: Text(
availabilities[index].dateAvailable.toString(),
availabilities[index].createdAt.toString(),
),
);
},
Expand Down
9 changes: 9 additions & 0 deletions lib/components/dialogs/user_transations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ class _UserTransationsDialogState extends State<UserTransationsDialog>
_userTransactionController
.fetchTransactions();
},
onChooseBook: (availability) async {
await _bookController
.confirmTransaction(
transaction.id,
availability.id,
);
_userTransactionController
.fetchTransactions();
},
),
);
},
Expand Down
3 changes: 2 additions & 1 deletion lib/configs/constants.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const collectionUsers = 'Users';
const collectionBooks = 'Book';
const collectionRatings = 'BookRating';
const collectionAvailabe = 'BookAvailable';
const collectionAvailable = 'BookAvailable';
const collectionTransaction = 'Transaction';
const collectionDiscussions = 'Discussions';
const collectionReplies = 'Replies';
const collectionInterestList = '"InterestList"';
const collectionGenres = 'Genres';
const storageUsers = 'Users';
11 changes: 11 additions & 0 deletions lib/controllers/book_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:livrodin/models/discussion.dart';
import 'package:livrodin/models/interest.dart';
import 'package:livrodin/models/reply.dart';
import 'package:livrodin/models/transaction.dart';
import 'package:livrodin/models/user.dart';
import 'package:livrodin/services/book_service.dart';

import 'auth_controller.dart';
Expand Down Expand Up @@ -344,4 +345,14 @@ class BookController extends GetxController {
rethrow;
}
}

Future<List<Availability>> getAvailableBooksFromUser(User user) async {
try {
final result = await bookService.getAvailableBooksFromUser(user);
return result;
} catch (e) {
printError(info: e.toString());
rethrow;
}
}
}
37 changes: 37 additions & 0 deletions lib/controllers/user_book_available_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'dart:developer';

import 'package:get/get.dart';
import 'package:livrodin/controllers/book_controller.dart';
import 'package:livrodin/models/availability.dart';
import 'package:livrodin/models/user.dart';
import 'package:livrodin/utils/state_machine.dart';

class UserBookAvailableController extends GetxController {
final User user;

final BookController _bookController = Get.find<BookController>();

UserBookAvailableController({required this.user});

RxList<Availability> availabilityList = <Availability>[].obs;

Rx<FetchState> fetchState = FetchState.loading.obs;

@override
void onInit() {
super.onInit();
_getAvailableBooksFromUser();
}

_getAvailableBooksFromUser() {
_bookController.getAvailableBooksFromUser(user).then((value) {
availabilityList.value = value;
inspect(value);
}).catchError((e) {
printError(info: e.toString());
fetchState.value = FetchState.error;
}).whenComplete(() {
fetchState.value = FetchState.success;
});
}
}
4 changes: 2 additions & 2 deletions lib/models/availability.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ class Availability {
final String _id;
final Book book;
final User user;
final DateTime dateAvailable;
final DateTime createdAt;
final BookAvailableType availableType;

Availability({
required String id,
required this.book,
required this.user,
required this.dateAvailable,
required this.createdAt,
required this.availableType,
}) : _id = id;

Expand Down
Loading