Skip to content

Commit

Permalink
fix #105 incorrect usage of submitted date in feedback module
Browse files Browse the repository at this point in the history
  • Loading branch information
mediocre9 committed Jan 16, 2024
1 parent 79e22ca commit 9f545ad
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 44 deletions.
3 changes: 2 additions & 1 deletion lib/models/user_feedback_model.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:smart_link/models/model.dart';

final class UserFeedback extends Model {
final String email;
final String username;
final String subject;
final String body;
final DateTime submittedDate;
final Timestamp submittedDate;

UserFeedback({
required super.id,
Expand Down
32 changes: 15 additions & 17 deletions lib/screens/feedback_screen/cubit/feedback_cubit.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:bloc/bloc.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:equatable/equatable.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:smart_link/config/config.dart';
import 'package:smart_link/models/models.dart';
Expand All @@ -14,12 +16,12 @@ class FeedbackCubit extends Cubit<FeedbackState> {
FeedbackCubit({
required this.feedbackService,
required this.authService,
}) : super(const FeedbackInitial(color: Colors.grey));
}) : super(FeedbackInitial());

Future<void> submitFeedback(String subject, String body) async {
if (isRequiredFeedbackEmpty(subject, body)) return;

emit(const Loading(color: Colors.blue));
emit(Loading());

final feedback = _createUserFeedback(
subject: subject,
Expand All @@ -29,42 +31,38 @@ class FeedbackCubit extends Cubit<FeedbackState> {

try {
await feedbackService.post(feedback);
emit(const Submitted(message: AppStrings.feedbackPosted));
emit(const SubmittedState(message: AppStrings.feedbackPosted));
} on NetworkException {
emit(const Error(message: AppStrings.noInternet));
emit(const ErrorState(message: AppStrings.noInternet));
} catch (e) {
emit(const Error(message: 'Something went wrong!'));
emit(const ErrorState(message: 'Something went wrong!'));
} finally {
emit(const FeedbackInitial(color: Colors.grey));
emit(FeedbackInitial());
}
}

bool isRequiredFeedbackEmpty(String subject, String body) {
if (subject.isNotEmpty && body.isNotEmpty) {
emit(const EmptyFieldsState(color: Colors.blue));
emit(const SubmitButtonState(color: Colors.blue));
return false;
}

emit(const FeedbackInitial(color: Colors.grey));
emit(FeedbackInitial());
return true;
}

DateTime _getCurrentDate() {
final DateTime(:day, :month, :year) = DateTime.now();
return DateTime(day, month, year);
}

UserFeedback _createUserFeedback({
required String subject,
required String body,
required GoogleAuthService service,
}) {
final currentDate = _getCurrentDate();
final currentDate = Timestamp.fromDate(DateTime.timestamp());
final User(:uid, :email, :displayName) = service.getCurrentUser!;

return UserFeedback(
id: service.getCurrentUser!.uid,
email: service.getCurrentUser!.email!,
username: service.getCurrentUser!.displayName!,
id: uid,
email: email!,
username: displayName!,
subject: subject,
body: body,
submittedDate: currentDate,
Expand Down
24 changes: 8 additions & 16 deletions lib/screens/feedback_screen/cubit/feedback_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,24 @@ sealed class FeedbackState extends Equatable {
List<Object> get props => [];
}

final class FeedbackInitial extends FeedbackState {
final Color color;

const FeedbackInitial({required this.color});
}
final class FeedbackInitial extends FeedbackState {}

final class Loading extends FeedbackState {
final Color color;

const Loading({required this.color});
}
final class Loading extends FeedbackState {}

final class Error extends FeedbackState {
final class ErrorState extends FeedbackState {
final String message;

const Error({required this.message});
const ErrorState({required this.message});
}

final class EmptyFieldsState extends FeedbackState {
final class SubmitButtonState extends FeedbackState {
final Color color;

const EmptyFieldsState({required this.color});
const SubmitButtonState({required this.color});
}

final class Submitted extends FeedbackState {
final class SubmittedState extends FeedbackState {
final String message;

const Submitted({required this.message});
const SubmittedState({required this.message});
}
40 changes: 30 additions & 10 deletions lib/screens/feedback_screen/feedback_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,22 @@ class _FeedbackScreenState extends State<FeedbackScreen>
actions: [
BlocConsumer<FeedbackCubit, FeedbackState>(
listener: (context, state) {
if (state is Submitted) {
if (state is SubmittedState) {
showSnackBarWidget(context, state.message);
}

if (state is Error) {
if (state is ErrorState) {
showSnackBarWidget(context, state.message);
}
},
builder: (context, state) {
switch (state) {
case FeedbackInitial():
return IconButton(
onPressed: null,
icon: Icon(Icons.send_rounded, color: state.color),
);
return const _SubmitButtonWidget();

case EmptyFieldsState():
return IconButton(
icon: Icon(Icons.send_rounded, color: state.color),
case SubmitButtonState():
return _SubmitButtonWidget(
color: state.color,
onPressed: () async {
FocusManager.instance.primaryFocus?.unfocus();
await context.read<FeedbackCubit>().submitFeedback(
Expand All @@ -57,7 +54,8 @@ class _FeedbackScreenState extends State<FeedbackScreen>
case Loading():
return Transform.scale(
scale: 0.7,
child: CircularProgressIndicator(color: state.color),
child:
const CircularProgressIndicator(color: Colors.blue),
);

default:
Expand Down Expand Up @@ -104,6 +102,11 @@ class _FeedbackScreenState extends State<FeedbackScreen>
);
}

DateTime _getCurrentDate() {
final DateTime(:year, :month, :day) = DateTime.now();
return DateTime(year, month, day);
}

@override
void dispose() {
super.dispose();
Expand All @@ -116,3 +119,20 @@ class _FeedbackScreenState extends State<FeedbackScreen>
_bodyController.clear();
}
}

class _SubmitButtonWidget extends StatelessWidget {
final Color? color;
final Function()? onPressed;
const _SubmitButtonWidget({this.color, this.onPressed});

@override
Widget build(BuildContext context) {
return IconButton(
icon: Icon(
Icons.send_rounded,
color: color ?? Colors.grey,
),
onPressed: onPressed,
);
}
}

0 comments on commit 9f545ad

Please sign in to comment.