Skip to content

Commit

Permalink
fix: type checks and matching
Browse files Browse the repository at this point in the history
  • Loading branch information
hig-dev committed Jan 17, 2024
1 parent 46f4a78 commit 92629cb
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 80 deletions.
13 changes: 6 additions & 7 deletions app/lib/screens/study/report/report_section_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@ import 'report_section_widget.dart';
typedef SectionBuilder = ReportSectionWidget Function(ReportSection section, StudySubject subject);

class ReportSectionContainer extends StatelessWidget {
static Map<Type, SectionBuilder> sectionTypes = {
AverageSection: (section, instance) => AverageSectionWidget(instance, section as AverageSection),
LinearRegressionSection: (section, instance) =>
LinearRegressionSectionWidget(instance, section as LinearRegressionSection),
};

final ReportSection section;
final StudySubject subject;
final bool primary;
final GestureTapCallback? onTap;

const ReportSectionContainer(this.section, {super.key, required this.subject, this.onTap, this.primary = false});

ReportSectionWidget buildContents(BuildContext context) => sectionTypes[section.runtimeType]!(section, subject);
ReportSectionWidget buildContents(BuildContext context) => switch (section) {
AverageSection averageSection => AverageSectionWidget(subject, averageSection),
LinearRegressionSection linearRegressionSection =>
LinearRegressionSectionWidget(subject, linearRegressionSection),
_ => throw ArgumentError('Section type ${section.type} not supported.'),
};

List<Widget> buildPrimaryHeader(BuildContext context, ThemeData theme) => [
Text(
Expand Down
10 changes: 3 additions & 7 deletions core/lib/src/models/questionnaire/answer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,11 @@ class Answer<V> {
static Answer fromJson(Map<String, dynamic> data) {
final dynamic value = data[keyResponse];
switch (value) {
case bool:
case bool():
return Answer<bool>.parseJson(data);
case num:
case num():
return Answer<num>.parseJson(data);
case int:
return Answer<num>.parseJson(data);
case double:
return Answer<num>.parseJson(data);
case String:
case String():
return Answer<String>.parseJson(data);
default:
// todo Why is value List<dynamic> instead of List<String>?
Expand Down
22 changes: 10 additions & 12 deletions core/lib/src/models/results/result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,19 @@ class Result<T> {
factory Result.fromJson(Map<String, dynamic> json) => _fromJson(json) as Result<T>;

Map<String, dynamic> toJson() {
Map<String, dynamic> resultMap;
switch (T) {
case QuestionnaireState:
resultMap = {keyResult: (result as QuestionnaireState).toJson()};
break;
case bool:
resultMap = {keyResult: result};
break;
default:
print('Unsupported question type: $T');
resultMap = {keyResult: ''};
}
final Map<String, dynamic> resultMap = switch (result) {
final QuestionnaireState questionnaireState => {keyResult: questionnaireState.toJson()},
bool() => {keyResult: result},
_ => {keyResult: _getUnsupportedResult()}
};
return mergeMaps<String, dynamic>(_$ResultToJson(this), resultMap);
}

String _getUnsupportedResult() {
print('Unsupported question type: $T');
return '';
}

static Result _fromJson(Map<String, dynamic> data) {
switch (data[keyType] as String) {
case 'QuestionnaireState':
Expand Down
20 changes: 9 additions & 11 deletions core/lib/src/models/tables/study_subject.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,16 @@ class StudySubject extends SupabaseObjectFunctions<StudySubject> {
required T result,
bool? offline,
}) async {
late final Result<T> resultObject;
switch (T) {
case QuestionnaireState:
resultObject = Result<T>.app(type: 'QuestionnaireState', periodId: periodId, result: result);
break;
case bool:
resultObject = Result<T>.app(type: 'bool', periodId: periodId, result: result);
break;
default:
print('Unsupported question type: $T');
resultObject = Result<T>.app(type: 'unknown', periodId: periodId, result: result);
final Result<T> resultObject = switch (result) {
QuestionnaireState() => Result<T>.app(type: 'QuestionnaireState', periodId: periodId, result: result),
bool() => Result<T>.app(type: 'bool', periodId: periodId, result: result),
_ => Result<T>.app(type: 'unknown', periodId: periodId, result: result),
};

if (resultObject.type == 'unknown') {
print('Unsupported question type: $T');
}

SubjectProgress p = SubjectProgress(
subjectId: id,
interventionId: getInterventionForDate(DateTime.now())!.id,
Expand Down
62 changes: 20 additions & 42 deletions core/lib/src/util/supabase_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,28 @@ abstract class SupabaseObject {
Map<String, dynamic> toJson();
}

String tableName(Type cls) {
switch (cls) {
case Study:
return Study.tableName;
case StudySubject:
return StudySubject.tableName;
case SubjectProgress:
return SubjectProgress.tableName;
case AppConfig:
return AppConfig.tableName;
case Repo:
return Repo.tableName;
case StudyInvite:
return StudyInvite.tableName;
case StudyUUser:
return StudyUUser.tableName;
default:
print('$cls is not a supported Supabase type');
throw TypeError();
}
}
String tableName(Type cls) => switch (cls) {
== Study => Study.tableName,
== StudySubject => StudySubject.tableName,
== SubjectProgress => SubjectProgress.tableName,
== AppConfig => AppConfig.tableName,
== Repo => Repo.tableName,
== StudyInvite => StudyInvite.tableName,
== StudyUUser => StudyUUser.tableName,
_ => throw ArgumentError('$cls is not a supported Supabase type'),
};

abstract class SupabaseObjectFunctions<T extends SupabaseObject> implements SupabaseObject {
static T fromJson<T extends SupabaseObject>(Map<String, dynamic> json) {
switch (T) {
case Study:
return Study.fromJson(json) as T;
case StudySubject:
return StudySubject.fromJson(json) as T;
case SubjectProgress:
return SubjectProgress.fromJson(json) as T;
case AppConfig:
return AppConfig.fromJson(json) as T;
case Repo:
return Repo.fromJson(json) as T;
case StudyInvite:
return StudyInvite.fromJson(json) as T;
case StudyUUser:
return StudyUUser.fromJson(json) as T;
default:
print('$T is not a supported Supabase type');
throw TypeError();
}
}
static T fromJson<T extends SupabaseObject>(Map<String, dynamic> json) => switch (T) {
== Study => Study.fromJson(json) as T,
== StudySubject => StudySubject.fromJson(json) as T,
== SubjectProgress => SubjectProgress.fromJson(json) as T,
== AppConfig => AppConfig.fromJson(json) as T,
== Repo => Repo.fromJson(json) as T,
== StudyInvite => StudyInvite.fromJson(json) as T,
== StudyUUser => StudyUUser.fromJson(json) as T,
_ => throw ArgumentError('$T is not a supported Supabase type'),
};

Future<T> delete() async => SupabaseQuery.extractSupabaseSingleRow<T>(
await env.client.from(tableName(T)).delete().primaryKeys(primaryKeys).select().single(),
Expand Down
2 changes: 1 addition & 1 deletion core/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ homepage: https://studyu.health
repository: https://github.com/hpi-studyu/studyu

environment:
sdk: ">=2.17.0 <4.0.0"
sdk: ">=3.0.0 <4.0.0"

dependencies:
collection: ^1.18.0
Expand Down

0 comments on commit 92629cb

Please sign in to comment.