Skip to content

Commit

Permalink
fix: checkin/checkout state (#214)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

Signed-off-by: Aman <[email protected]>
Co-authored-by: Manan Garg <[email protected]>
  • Loading branch information
aman-singh7 and Coder-Manan authored Jan 2, 2023
1 parent d943931 commit 33ca183
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 113 deletions.
1 change: 0 additions & 1 deletion lib/services/api/user_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions lib/ui/home_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ class _HomeViewState extends State<HomeView> {
),
),
GestureDetector(
onTap: () => Get.toNamed(MyLeavesView.id),
onTap: () => Get.toNamed(MyLeavesView.id)!
.then((_) => _model.checkedOut = isCheckedOut),
child: Padding(
padding: EdgeInsets.symmetric(
vertical: 4.r,
Expand Down Expand Up @@ -314,7 +315,8 @@ class _HomeViewState extends State<HomeView> {
),
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',
),
Expand Down
176 changes: 102 additions & 74 deletions lib/ui/leaves/components/leave_status_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<LeaveStatusCard> {
@override
Widget build(BuildContext context) {
return BaseView<LeaveStatusCardViewModel>(
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: <Widget>[
Padding(
padding: EdgeInsets.all(12.r),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
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: <Widget>[
Padding(
padding: EdgeInsets.all(12.r),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
'Your Status',
style: AppTheme.headline4,
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Remaining Leaves : ',
style: AppTheme.bodyText1,
'Your Status',
style: AppTheme.headline4,
),
Text(
'${widget.remainingLeaves ?? '-'}',
style: AppTheme.subtitle1,
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Text(
'Remaining Leaves : ',
style: AppTheme.bodyText1,
),
Text(
'${remainingLeaves ?? '-'}',
style: AppTheme.subtitle1,
),
],
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
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: <Widget>[
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: <Widget>[
Icon(
Icons.info,
size: 16.r,
color: AppTheme.blackSecondary,
),
SizedBox(width: 8.r),
Text(
info,
style: AppTheme.bodyText2,
),
],
),
);
}
Expand Down
34 changes: 1 addition & 33 deletions lib/ui/leaves/my_leaves_view.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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: <Widget>[
Icon(
Icons.info,
size: 16.r,
color: AppTheme.blackSecondary,
),
SizedBox(width: 8.r),
Text(
info,
style: AppTheme.bodyText2,
),
],
),
);
}

Widget _buildLeaveHistoryComponent() {
return SafeArea(
child: Container(
Expand Down Expand Up @@ -72,16 +49,7 @@ class MyLeavesView extends StatelessWidget {
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
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()
],
),
Expand Down
17 changes: 14 additions & 3 deletions lib/viewmodels/home_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ class HomeViewModel extends BaseModel {
locator<PushNotificationService>();
final DialogService _dialogService = locator<DialogService>();

bool _checkedOut = isCheckedOut;

bool get checkedOut => _checkedOut;

set checkedOut(bool state) {
_checkedOut = state;
isCheckedOut = state;
notifyListeners();
}

String _selectedHostel = 'Your Meals';

String get selectedHostel => _selectedHostel;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -139,7 +150,7 @@ class HomeViewModel extends BaseModel {

if (dialogResponse.confirmed) {
await checkout();
if (isCheckedOut) {
if (checkedOut) {
SnackBarUtils.showDark('Info', 'You have checked out');
}
}
Expand Down

0 comments on commit 33ca183

Please sign in to comment.