Skip to content

Commit

Permalink
Chat on purchases bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
IQuinteros committed Jun 27, 2021
1 parent c814158 commit 30ec506
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 45 deletions.
14 changes: 6 additions & 8 deletions lib/bloc/chat_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@ class ChatBloc extends BaseBloc<ChatModel>{
return;
}

Future<ChatModel?> getChatFromPurchase(PurchaseModel purchase) async {
Future<List<ChatModel>> getChatsFromPurchase(PurchaseModel purchase) async {
final chats = await chatAPI.selectAll(
params: {
'purchase_id': purchase.id
}
);

if(chats.length > 0)
return chats[0];

return null;
return chats;
}

Future<List<MessageModel>> getMessagesFromChat(ChatModel chat) async => await messageAPI.selectAll(
Expand Down Expand Up @@ -56,13 +53,14 @@ class ChatBloc extends BaseBloc<ChatModel>{
required PurchaseModel purchase,
StoreModel? store,
}) async {
final chat = await getChatFromPurchase(purchase);
List<ChatModel> chats = await getChatsFromPurchase(purchase);
chats = chats.where((element) => element.store?.id == store?.id).toList();

if(chat != null){
if(chats.length > 0){
return SendMessageResult(result: (await messageAPI.insert(
item: message,
additionalParams: {
'chat_id': chat.id
'chat_id': chats[0].id
}
)).object != null, isNewChat: false);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/providers/base_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class BaseAPI<T extends BaseModel>{
Map<String, dynamic> Function(T) getJsonParams;
T Function(Map<String, dynamic>) constructor;

final bool DEBUG = false;
final bool DEBUG = true;
final bool simulateLag = false;

final Function(FailConnectReason reason)? onFailConnect;
Expand Down
4 changes: 3 additions & 1 deletion lib/views/chat_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,10 @@ class _ChatViewState extends State<ChatView> {

Future<void> updateChat(BuildContext context, ChatModel? chatToUse, [Function()? onSetState]) async {
final chatBloc = BlocProvider.of<ChatBloc>(context);
refreshChat = await chatBloc.getChatFromPurchase(chatToUse?.purchase ?? widget.purchase!);
final result = await chatBloc.getChatsFromPurchase(chatToUse?.purchase ?? widget.purchase!);

final chatLoadedList = result.where((element) => element.store?.id == (chatToUse?.store?.id ?? widget.store?.id)).toList();
refreshChat = chatLoadedList.length > 0? chatLoadedList[0] : null;
if(widget.chat != null && refreshChat != null){
widget.chat!.messages = refreshChat!.messages;
}
Expand Down
99 changes: 64 additions & 35 deletions lib/views/purchase_detail_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@ import 'package:flutter_ecoapp/views/widgets/bottom_nav_bar.dart';
import 'package:flutter_ecoapp/views/widgets/normal_button.dart';
import 'package:google_fonts/google_fonts.dart';

class PurchaseDetailView extends StatelessWidget {
class PurchaseDetailView extends StatefulWidget {
final PurchaseModel purchase;

const PurchaseDetailView({Key? key, required this.purchase}) : super(key: key);

@override
_PurchaseDetailViewState createState() => _PurchaseDetailViewState();
}

class _PurchaseDetailViewState extends State<PurchaseDetailView> {
@override
Widget build(BuildContext context) {
return Scaffold(
Expand All @@ -41,14 +46,13 @@ class PurchaseDetailView extends StatelessWidget {
}

Widget getContent(BuildContext context){
List<Widget> storeSections = [];
purchase.storeSortedArticles.forEach((key, value) => storeSections.add(_StoreList(store: key, articles: value, purchase: purchase,)));
final chatBloc = BlocProvider.of<ChatBloc>(context);

final content = Column(
children: [
//SearchBar(),
EcoTitle(
text: 'Compra #${purchase.id}',
text: 'Compra #${widget.purchase.id}',
leftButton: IconButton(
icon: Icon(Icons.keyboard_arrow_left_rounded),
color: EcoAppColors.MAIN_COLOR,
Expand Down Expand Up @@ -90,12 +94,42 @@ class PurchaseDetailView extends StatelessWidget {
),
),
Divider(thickness: 1,),
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: storeSections

FutureBuilder(
future: chatBloc.getChatsFromPurchase(widget.purchase),
builder: (context, AsyncSnapshot<List<ChatModel>> snapshot){
switch(snapshot.connectionState){
case ConnectionState.done:
List<Widget> storeSections = [];

widget.purchase.storeSortedArticles.forEach((key, value) {
final chatList = snapshot.data!.where((element) => element.store?.id == key?.id).toList();
storeSections.add(
_StoreList(
store: key,
articles: value,
purchase: widget.purchase,
chat: chatList.length > 0? chatList[0] : null,
onExitFromChatView: () => setState(() {}),
)
);
});
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: storeSections
);
default: return Container(
margin: EdgeInsets.symmetric(
horizontal: 20.0,
vertical: 20.0
),
child: CircularProgressIndicator()
);
}
}
),
Divider(thickness: 1),
_SummaryItem(purchase: purchase)
_SummaryItem(purchase: widget.purchase)
],
);

Expand Down Expand Up @@ -212,18 +246,20 @@ class _StoreList extends StatelessWidget {
Key? key,
required this.store,
required this.articles,
required this.purchase
required this.purchase,
this.chat,
required this.onExitFromChatView
}) : super(key: key);

final StoreModel? store;
final List<ArticleToPurchase> articles;
final PurchaseModel purchase;
final ChatModel? chat;
final Function()? onExitFromChatView;

@override
Widget build(BuildContext context) {

final chatBloc = BlocProvider.of<ChatBloc>(context);

List<Widget> articlesToDisplay = articles.map<Widget>((e) => ArticleCard.fromPurchase(
article: e.article,
ecoIndicator: e.form.getIndicator(),
Expand Down Expand Up @@ -286,30 +322,23 @@ class _StoreList extends StatelessWidget {
),
),
Expanded(child: Container()),
store != null? FutureBuilder(
future: chatBloc.getChatFromPurchase(purchase),
builder: (context, AsyncSnapshot<ChatModel?> snapshot){
switch(snapshot.connectionState){
case ConnectionState.done:
return IconButton(
icon: Icon(
Icons.sms_rounded,
color: EcoAppColors.MAIN_COLOR,
),
onPressed: () => Navigator.push(
context,
MaterialPageRoute(
builder: (__) => ChatView(
chat: snapshot.data,
store: snapshot.data != null? null : store,
purchase: snapshot.data != null? null : purchase,
)
)
)
);
default: return CircularProgressIndicator();
}

store != null? IconButton(
icon: Icon(
Icons.sms_rounded,
color: EcoAppColors.MAIN_COLOR,
),
onPressed: () async {
await Navigator.push(
context,
MaterialPageRoute(
builder: (__) => ChatView(
chat: chat,
store: chat != null? null : store,
purchase: chat != null? null : purchase,
)
)
);
onExitFromChatView?.call();
}
) : Container()
],
Expand Down

0 comments on commit 30ec506

Please sign in to comment.