-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: user feedback when loading state gets halted on submit button, w…
…hen network state is offline
- Loading branch information
Showing
4 changed files
with
221 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,73 @@ | ||
import 'package:bloc/bloc.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:smart_link/config/strings/app_strings.dart'; | ||
import 'package:smart_link/services/auth_service.dart'; | ||
import 'package:smart_link/services/feedback_service.dart'; | ||
|
||
import '../../../models/user_feedback_model.dart'; | ||
import 'package:smart_link/config/config.dart'; | ||
import 'package:smart_link/models/models.dart'; | ||
import 'package:smart_link/services/services.dart'; | ||
|
||
part 'feedback_state.dart'; | ||
|
||
class FeedbackCubit extends Cubit<FeedbackState> { | ||
final FeedbackService feedbackService; | ||
final AuthenticationService service; | ||
final GoogleAuthService authService; | ||
|
||
FeedbackCubit({ | ||
required this.feedbackService, | ||
required this.service, | ||
required this.authService, | ||
}) : super(const FeedbackInitial(color: Colors.grey)); | ||
|
||
Future<void> submitFeedback(String subject, String body) async { | ||
if (isRequiredFeedbackEmpty(subject, body)) return; | ||
|
||
emit(const Loading(color: Colors.blue)); | ||
|
||
final DateTime(:day, :month, :year) = DateTime.now(); | ||
|
||
final currentDate = DateTime(day, month, year); | ||
|
||
final feedback = UserFeedback( | ||
id: service.getCurrentUser!.uid, | ||
email: service.getCurrentUser!.email!, | ||
username: service.getCurrentUser!.displayName!, | ||
final feedback = _createUserFeedback( | ||
subject: subject, | ||
body: body, | ||
submittedDate: currentDate, | ||
service: authService, | ||
); | ||
|
||
await feedbackService.post(feedback); | ||
|
||
emit(const Submitted(message: AppStrings.feedbackPosted)); | ||
emit(const FeedbackInitial(color: Colors.grey)); | ||
try { | ||
await feedbackService.post(feedback); | ||
emit(const Submitted(message: AppStrings.feedbackPosted)); | ||
} on NetworkException { | ||
emit(const Error(message: AppStrings.noInternet)); | ||
} catch (e) { | ||
emit(const Error(message: 'Something went wrong!')); | ||
} finally { | ||
emit(const FeedbackInitial(color: Colors.grey)); | ||
} | ||
} | ||
|
||
bool isRequiredFeedbackEmpty(String subject, String body) { | ||
if (subject.isNotEmpty && body.isNotEmpty) { | ||
emit(const NonEmptyState(color: Colors.blue)); | ||
emit(const EmptyFieldsState(color: Colors.blue)); | ||
return false; | ||
} | ||
|
||
emit(const FeedbackInitial(color: Colors.grey)); | ||
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(); | ||
|
||
return UserFeedback( | ||
id: service.getCurrentUser!.uid, | ||
email: service.getCurrentUser!.email!, | ||
username: service.getCurrentUser!.displayName!, | ||
subject: subject, | ||
body: body, | ||
submittedDate: currentDate, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters