diff --git a/lib/src/data/models/preferences.dart b/lib/src/data/models/preferences.dart index 0b67f3e..568362c 100644 --- a/lib/src/data/models/preferences.dart +++ b/lib/src/data/models/preferences.dart @@ -2,23 +2,32 @@ import 'package:json_annotation/json_annotation.dart'; import 'package:uuid/uuid.dart'; import 'package:uuid/uuid_util.dart'; +import 'assessments.dart'; + part 'preferences.g.dart'; @JsonSerializable() class Preferences { final String userId; + final Assessment lastAssessment; Preferences({ userId, + this.lastAssessment, }) : userId = userId ?? Uuid().v4(options: { 'grng': UuidUtil.cryptoRNG, }); - Preferences.clone(Preferences preferences) - : this( - userId: preferences.userId, - ); + Preferences cloneWith({ + Preferences preferences, + Assessment lastAssessment, + }) { + return Preferences( + userId: preferences.userId, + lastAssessment: lastAssessment ?? preferences.lastAssessment, + ); + } factory Preferences.fromJson(Map json) => _$PreferencesFromJson(json); diff --git a/lib/src/data/models/preferences.g.dart b/lib/src/data/models/preferences.g.dart index 61c850e..ef185c0 100644 --- a/lib/src/data/models/preferences.g.dart +++ b/lib/src/data/models/preferences.g.dart @@ -9,10 +9,14 @@ part of 'preferences.dart'; Preferences _$PreferencesFromJson(Map json) { return Preferences( userId: json['user_id'], + lastAssessment: json['last_assessment'] == null + ? null + : Assessment.fromJson(json['last_assessment'] as Map), ); } Map _$PreferencesToJson(Preferences instance) => { 'user_id': instance.userId, + 'last_assessment': instance.lastAssessment?.toJson(), }; diff --git a/lib/src/ui/screens/assessment/assessment.dart b/lib/src/ui/screens/assessment/assessment.dart index 37bc37f..34bff09 100644 --- a/lib/src/ui/screens/assessment/assessment.dart +++ b/lib/src/ui/screens/assessment/assessment.dart @@ -3,38 +3,53 @@ import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_hud/flutter_hud.dart'; import 'package:coronavirus_diary/src/blocs/checkup/checkup.dart'; +import 'package:coronavirus_diary/src/blocs/preferences/preferences.dart'; import 'assessments/index.dart'; -import 'share.dart'; class AssessmentScreen extends StatelessWidget { static const routeName = '/assessment'; @override Widget build(BuildContext context) { - return BlocBuilder( + return BlocBuilder( builder: (context, state) { - Widget body; - if (state is CheckupStateInProgress) { - context.bloc().add(CompleteCheckup()); - } else if (state is CheckupStateCompleted) { - switch (state.assessment.matchesPuiSymptoms) { - case true: - body = PositiveAssessment(); - break; - case false: - body = NegativeAssessment(); - break; - } - } - return WidgetHUD( - showHUD: state is CheckupStateCompleting, - hud: HUD(label: 'Loading your assessment'), - builder: (context) { - return Scaffold( - appBar: AppBar( - title: Text('Your Personalized Assessment'), - ), - body: body, + final PreferencesState preferencesState = state; + + return BlocBuilder( + builder: (context, state) { + Widget body; + if (state is CheckupStateInProgress) { + context.bloc().add(CompleteCheckup()); + } else if (state is CheckupStateCompleted) { + // Remember assessment + Preferences newPreferences = + preferencesState.preferences.cloneWith( + lastAssessment: state.assessment, + ); + context + .bloc() + .add(UpdatePreferences(newPreferences)); + + switch (state.assessment.matchesPuiSymptoms) { + case true: + body = PositiveAssessment(); + break; + case false: + body = NegativeAssessment(); + break; + } + } + return WidgetHUD( + showHUD: state is CheckupStateCompleting, + hud: HUD(label: 'Loading your assessment'), + builder: (context) { + return Scaffold( + appBar: AppBar( + title: Text('Your Personalized Assessment'), + ), + body: body, + ); + }, ); }, );