From 33ca183b30335de9154c3f083ce6175514c53271 Mon Sep 17 00:00:00 2001 From: Aman Date: Tue, 3 Jan 2023 02:04:31 +0530 Subject: [PATCH] fix: checkin/checkout state (#214) * ptch: change print commands to original versions * bfix: show checkedout comp just after checkout * bfix: add/remove checkedout comp on toggling in leaves page * ptch: format home_view.dart * bfix: update all check-in/out components correctly * refactor: modularize the checkout logic Signed-off-by: Aman Signed-off-by: Aman Co-authored-by: Manan Garg --- lib/services/api/user_api.dart | 1 - lib/ui/home_view.dart | 6 +- .../leaves/components/leave_status_card.dart | 176 ++++++++++-------- lib/ui/leaves/my_leaves_view.dart | 34 +--- lib/viewmodels/home_viewmodel.dart | 17 +- 5 files changed, 121 insertions(+), 113 deletions(-) diff --git a/lib/services/api/user_api.dart b/lib/services/api/user_api.dart index f3970319..d25d86be 100644 --- a/lib/services/api/user_api.dart +++ b/lib/services/api/user_api.dart @@ -23,7 +23,6 @@ class UserApi { try { var jsonResponse = await ApiUtils.post(uri, headers: headers, body: json); - print(jsonResponse); var user = User.fromJson(jsonResponse); return user; } on ForbiddenException catch (e) { diff --git a/lib/ui/home_view.dart b/lib/ui/home_view.dart index cdc03b87..18f7c581 100644 --- a/lib/ui/home_view.dart +++ b/lib/ui/home_view.dart @@ -82,7 +82,8 @@ class _HomeViewState extends State { ), ), GestureDetector( - onTap: () => Get.toNamed(MyLeavesView.id), + onTap: () => Get.toNamed(MyLeavesView.id)! + .then((_) => _model.checkedOut = isCheckedOut), child: Padding( padding: EdgeInsets.symmetric( vertical: 4.r, @@ -314,7 +315,8 @@ class _HomeViewState extends State { ), if (_model.isLeaveEnabled) _buildDrawerComponent( - onTap: () => Get.toNamed(MyLeavesView.id), + onTap: () => Get.toNamed(MyLeavesView.id)! + .then((_) => _model.checkedOut = isCheckedOut), iconPath: 'assets/icons/leaves.png', title: 'Leaves', ), diff --git a/lib/ui/leaves/components/leave_status_card.dart b/lib/ui/leaves/components/leave_status_card.dart index f7339ef4..25f075d3 100644 --- a/lib/ui/leaves/components/leave_status_card.dart +++ b/lib/ui/leaves/components/leave_status_card.dart @@ -5,99 +5,127 @@ import 'package:appetizer/viewmodels/leaves/leave_status_card_viewmodel.dart'; import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; -class LeaveStatusCard extends StatefulWidget { +class LeaveStatusCard extends StatelessWidget { final int? remainingLeaves; + const LeaveStatusCard(this.remainingLeaves); - LeaveStatusCard(this.remainingLeaves); - - @override - _LeaveStatusCardState createState() => _LeaveStatusCardState(); -} - -class _LeaveStatusCardState extends State { @override Widget build(BuildContext context) { return BaseView( - builder: (context, model, child) => Card( - elevation: 5.0, - margin: EdgeInsets.all(6.r), - shape: RoundedRectangleBorder( - borderRadius: BorderRadius.circular(6.r), - ), - child: Column( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, - children: [ - Padding( - padding: EdgeInsets.all(12.r), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - Column( - crossAxisAlignment: CrossAxisAlignment.start, + builder: (context, model, child) => Column( + children: [ + Card( + elevation: 5.0, + margin: EdgeInsets.all(6.r), + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(6.r), + ), + child: Column( + mainAxisAlignment: MainAxisAlignment.spaceEvenly, + children: [ + Padding( + padding: EdgeInsets.all(12.r), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - Text( - 'Your Status', - style: AppTheme.headline4, - ), - Row( - mainAxisAlignment: MainAxisAlignment.start, + Column( + crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( - 'Remaining Leaves : ', - style: AppTheme.bodyText1, + 'Your Status', + style: AppTheme.headline4, ), - Text( - '${widget.remainingLeaves ?? '-'}', - style: AppTheme.subtitle1, + Row( + mainAxisAlignment: MainAxisAlignment.start, + children: [ + Text( + 'Remaining Leaves : ', + style: AppTheme.bodyText1, + ), + Text( + '${remainingLeaves ?? '-'}', + style: AppTheme.subtitle1, + ), + ], ), - ], - ), - Row( - mainAxisAlignment: MainAxisAlignment.start, - children: [ - Text( - 'Currently : ', - style: AppTheme.bodyText1, - ), - Text( - (isCheckedOut) ? 'CHECKED-OUT' : 'CHECKED-IN', - style: AppTheme.subtitle1.copyWith( - color: - isCheckedOut ? AppTheme.red : AppTheme.green, - ), + Row( + mainAxisAlignment: MainAxisAlignment.start, + children: [ + Text( + 'Currently : ', + style: AppTheme.bodyText1, + ), + Text( + (isCheckedOut) ? 'CHECKED-OUT' : 'CHECKED-IN', + style: AppTheme.subtitle1.copyWith( + color: isCheckedOut + ? AppTheme.red + : AppTheme.green, + ), + ), + ], ), ], ), + Icon( + Icons.account_circle, + color: AppTheme.secondary, + size: 70.r, + ), ], ), - Icon( - Icons.account_circle, - color: AppTheme.secondary, - size: 70.r, - ), - ], - ), - ), - Divider(color: AppTheme.grey, height: 0), - Container( - width: MediaQuery.of(context).size.width / 2, - padding: EdgeInsets.all(4.r), - child: ElevatedButton( - style: ElevatedButton.styleFrom( - backgroundColor: isCheckedOut ? AppTheme.green : AppTheme.red, - padding: EdgeInsets.all(8.r), ), - onPressed: model.onCheckTapped, - child: Text( - (isCheckedOut) ? 'CHECK IN' : 'CHECK OUT', - style: AppTheme.headline5.copyWith( - color: AppTheme.white, + Divider(color: AppTheme.grey, height: 0), + Container( + width: MediaQuery.of(context).size.width / 2, + padding: EdgeInsets.all(4.r), + child: ElevatedButton( + style: ElevatedButton.styleFrom( + backgroundColor: + isCheckedOut ? AppTheme.green : AppTheme.red, + padding: EdgeInsets.all(8.r), + ), + onPressed: () { + model.onCheckTapped(); + }, + child: Text( + (isCheckedOut) ? 'CHECK IN' : 'CHECK OUT', + style: AppTheme.headline5.copyWith( + color: AppTheme.white, + ), + ), ), ), - ), + ], ), - ], - ), + ), + (isCheckedOut) + ? _buildInfoComponent('Check-in to start taking meals again') + : _buildInfoComponent( + 'Check-out to leave upcoming meals in sequence'), + ], + ), + ); + } + + Widget _buildInfoComponent(String info) { + return Container( + padding: const EdgeInsets.symmetric(vertical: 4, horizontal: 8), + color: AppTheme.lightGrey, + child: Row( + mainAxisAlignment: MainAxisAlignment.start, + children: [ + Icon( + Icons.info, + size: 16.r, + color: AppTheme.blackSecondary, + ), + SizedBox(width: 8.r), + Text( + info, + style: AppTheme.bodyText2, + ), + ], ), ); } diff --git a/lib/ui/leaves/my_leaves_view.dart b/lib/ui/leaves/my_leaves_view.dart index 83a7cccb..a85fb040 100644 --- a/lib/ui/leaves/my_leaves_view.dart +++ b/lib/ui/leaves/my_leaves_view.dart @@ -1,5 +1,4 @@ import 'package:appetizer/app_theme.dart'; -import 'package:appetizer/globals.dart'; import 'package:appetizer/ui/base_view.dart'; import 'package:appetizer/ui/components/appetizer_app_bar.dart'; import 'package:appetizer/ui/leaves/leave_history_view.dart'; @@ -12,28 +11,6 @@ import 'package:get/get.dart'; class MyLeavesView extends StatelessWidget { static const String id = 'my_leaves_view'; - Widget _buildInfoComponent(String info) { - return Container( - padding: const EdgeInsets.symmetric(vertical: 4, horizontal: 8), - color: AppTheme.lightGrey, - child: Row( - mainAxisAlignment: MainAxisAlignment.start, - children: [ - Icon( - Icons.info, - size: 16.r, - color: AppTheme.blackSecondary, - ), - SizedBox(width: 8.r), - Text( - info, - style: AppTheme.bodyText2, - ), - ], - ), - ); - } - Widget _buildLeaveHistoryComponent() { return SafeArea( child: Container( @@ -72,16 +49,7 @@ class MyLeavesView extends StatelessWidget { body: Column( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - Column( - children: [ - LeaveStatusCard(model.leaveCount), - (isCheckedOut) - ? _buildInfoComponent( - 'Check-in to start taking meals again') - : _buildInfoComponent( - 'Check-out to leave upcoming meals in sequence'), - ], - ), + LeaveStatusCard(model.leaveCount), _buildLeaveHistoryComponent() ], ), diff --git a/lib/viewmodels/home_viewmodel.dart b/lib/viewmodels/home_viewmodel.dart index f80934a9..8541a5db 100644 --- a/lib/viewmodels/home_viewmodel.dart +++ b/lib/viewmodels/home_viewmodel.dart @@ -26,6 +26,16 @@ class HomeViewModel extends BaseModel { locator(); final DialogService _dialogService = locator(); + bool _checkedOut = isCheckedOut; + + bool get checkedOut => _checkedOut; + + set checkedOut(bool state) { + _checkedOut = state; + isCheckedOut = state; + notifyListeners(); + } + String _selectedHostel = 'Your Meals'; String get selectedHostel => _selectedHostel; @@ -56,7 +66,7 @@ class HomeViewModel extends BaseModel { Future fetchInitialCheckedStatus() async { try { var userDetails = await _userApi.getCurrentUser(); - isCheckedOut = userDetails.isCheckedOut; + checkedOut = userDetails.isCheckedOut; notifyListeners(); } on Failure catch (f) { if (f.message == Constants.UNAUTHORIZED_EXCEPTION) { @@ -122,7 +132,8 @@ class HomeViewModel extends BaseModel { Future checkout() async { try { - isCheckedOut = await _leaveApi.checkout(); + checkedOut = await _leaveApi.checkout(); + notifyListeners(); } on Failure catch (f) { setState(ViewState.Error); setErrorMessage(f.message); @@ -139,7 +150,7 @@ class HomeViewModel extends BaseModel { if (dialogResponse.confirmed) { await checkout(); - if (isCheckedOut) { + if (checkedOut) { SnackBarUtils.showDark('Info', 'You have checked out'); } }