Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #1722 robotoff resend anonymous #2117

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ class UserManagementProvider with ChangeNotifier {

/// Checks if any credentials exist in storage
Future<bool> credentialsInStorage() async {
final bool userId = await DaoSecuredString.contains(key: _USER_ID);
final bool password = await DaoSecuredString.contains(key: _PASSWORD);
final String? userId = await DaoSecuredString.get(_USER_ID);
final String? password = await DaoSecuredString.get(_PASSWORD);

return userId && password;
return userId != null && password != null;
}

/// Saves user to storage
Expand Down
33 changes: 30 additions & 3 deletions packages/smooth_app/lib/pages/question_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class QuestionPage extends StatefulWidget {
class _QuestionPageState extends State<QuestionPage>
with SingleTickerProviderStateMixin, TraceableClientMixin {
int _currentQuestionIndex = 0;
final Map<String, InsightAnnotation> _anonymousAnnotationList =
<String, InsightAnnotation>{};
InsightAnnotation? _lastAnswer;

static const Color _noBackground = Colors.redAccent;
Expand Down Expand Up @@ -121,7 +123,7 @@ class _QuestionPageState extends State<QuestionPage>
Widget _buildWidget(BuildContext context, int currentQuestionIndex) {
final List<RobotoffQuestion> questions = widget.questions;
if (questions.length == currentQuestionIndex) {
return const CongratsWidget();
return CongratsWidget(_anonymousAnnotationList);
}
return Column(
children: <Widget>[
Expand Down Expand Up @@ -346,6 +348,9 @@ class _QuestionPageState extends State<QuestionPage>
}) async {
final AppLocalizations appLocalizations = AppLocalizations.of(context);

if (OpenFoodAPIConfiguration.globalUser == null && insightId != null) {
_anonymousAnnotationList.putIfAbsent(insightId, () => insightAnnotation);
}
await LoadingDialog.run<Status>(
context: context,
title: appLocalizations.saving_answer,
Expand All @@ -371,7 +376,9 @@ class _QuestionPageState extends State<QuestionPage>
}

class CongratsWidget extends StatelessWidget {
const CongratsWidget({Key? key}) : super(key: key);
const CongratsWidget(this._anonymousAnnotationList, {Key? key})
: super(key: key);
final Map<String, InsightAnnotation> _anonymousAnnotationList;

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -409,13 +416,20 @@ class CongratsWidget extends StatelessWidget {
action: SmoothActionButton(
text: appLocalizations.sign_in,
onPressed: () async {
Navigator.maybePop<Widget>(context);
await Navigator.push<Widget>(
context,
MaterialPageRoute<Widget>(
builder: (_) => const LoginPage(),
),
);
if (OpenFoodAPIConfiguration.globalUser != null) {
await LoadingDialog.run<void>(
context: context,
title: appLocalizations.saving_answer,
future: _postInsightAnnotations(
_anonymousAnnotationList),
);
}
},
),
),
Expand All @@ -442,4 +456,17 @@ class CongratsWidget extends StatelessWidget {
),
);
}

Future<void> _postInsightAnnotations(
Map<String, InsightAnnotation> annotationList) async {
annotationList
.forEach((String insightId, InsightAnnotation insightAnnotation) async {
await OpenFoodAPIClient.postInsightAnnotation(
insightId,
insightAnnotation,
deviceId: OpenFoodAPIConfiguration.uuid,
user: OpenFoodAPIConfiguration.globalUser,
);
});
}
}