Skip to content

Commit

Permalink
Revert "Division of advance question from main question catalog"
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbendebiene authored Jul 27, 2023
1 parent d13fd21 commit 2d4a44f
Show file tree
Hide file tree
Showing 20 changed files with 174 additions and 221 deletions.
42 changes: 0 additions & 42 deletions assets/advanced_question_catalog/definition.json

This file was deleted.

13 changes: 0 additions & 13 deletions assets/advanced_question_catalog/locales/de.arb

This file was deleted.

13 changes: 0 additions & 13 deletions assets/advanced_question_catalog/locales/en.arb

This file was deleted.

41 changes: 41 additions & 0 deletions assets/question_catalog/definition.json
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,47 @@
}
]
},
{
"question": {
"id": 1170,
"name": "@platformHeightName",
"text": "@platformHeightText",
"description": "@platformHeightDescription"
},
"answer": {
"type": "Number",
"input": {
"placeholder": "@platformHeightPlaceholder",
"decimals": 0,
"min": 0,
"max": 150,
"unit": "@unitCentimeter"
},
"constructor": {
"height": [
"REPLACE", "/\\.?0{1,2}$/", "", [
"INSERT", ".", "-2", [
"PAD", "0", "3", "$input"
]
]
]
}
},
"conditions": [
{
"osm_tags": {
"public_transport": "platform",
"height": false
},
"osm_element": [
"OpenWay",
"ClosedWay",
"Relation"
]
}
],
"isProfessional": true
},
{
"question": {
"id": 1180,
Expand Down
8 changes: 8 additions & 0 deletions assets/question_catalog/locales/de.arb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@
"@stopCurbTypeLoweredOption" : {},
"stopCurbTypeFlushOption" : "Ebenerdig (≈0cm)",
"@stopCurbTypeFlushOption" : {},
"platformHeightName" : "Plattform-Höhe",
"@platformHeightName" : {},
"platformHeightText" : "Wie groß ist der Höhenunterschied zwischen Plattform und Straße bzw. Schienenoberkante?",
"@platformHeightText" : {},
"platformHeightDescription" : "Wenn diese Höhe innerhalb eines Steiges variiert, ist der typische Wert für die übliche Einstiegsstelle von Rollstuhlfahrenden zu bestimmen.",
"@platformHeightDescription" : {},
"platformHeightPlaceholder" : "Plattform-Höhe",
"@platformHeightPlaceholder" : {},
"platformWidthName" : "Plattform-Breite",
"@platformWidthName" : {},
"platformWidthText" : "Was ist die überwiegende Breite der Plattform?",
Expand Down
8 changes: 8 additions & 0 deletions assets/question_catalog/locales/en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@
"@stopCurbTypeLoweredOption" : {},
"stopCurbTypeFlushOption" : "Flush (≈0cm)",
"@stopCurbTypeFlushOption" : {},
"platformHeightName" : "Platform height",
"@platformHeightName" : {},
"platformHeightText" : "What is the height difference between the platform and the road respectively the top edge of the rail?",
"@platformHeightText" : {},
"platformHeightDescription" : "If this height varies within a platform, the typical value for the usual entry point for wheelchair users should be determined.",
"@platformHeightDescription" : {},
"platformHeightPlaceholder" : "Platform height",
"@platformHeightPlaceholder" : {},
"platformWidthName" : "Platform width",
"@platformWidthName" : {},
"platformWidthText" : "What is the predominant width of the platform?",
Expand Down
7 changes: 6 additions & 1 deletion lib/api/app_worker/app_worker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ class AppWorker extends ServiceWorker<AppWorkerMessage>
case AppWorkerSubject.jumpToQuestion:
return jumpToQuestion(message.data);

case AppWorkerSubject.updateQuestionCatalogPreferences:
return updateQuestionCatalogPreferences(excludeProfessional: message.data);

case AppWorkerSubject.updateQuestionCatalog:
return updateQuestionCatalog(message.data);
return takeQuestionCatalogAsset(message.data);

case AppWorkerSubject.dispose:
return exit();
Expand Down Expand Up @@ -98,6 +101,8 @@ enum AppWorkerSubject {
previousQuestion,
jumpToQuestion,

updateQuestionCatalogPreferences,

updateQuestionCatalog,

dispose,
Expand Down
13 changes: 8 additions & 5 deletions lib/api/app_worker/app_worker_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,18 @@ class AppWorkerInterface extends Service implements Disposable {
));
}

Future<void> updateQuestionCatalog({required QuestionCatalog questionCatalog, required bool onlyLanguageChange}) {
Future<void> updateQuestionCatalogPreferences({required bool excludeProfessional}) {
return _worker.send<void>(AppWorkerMessage(
AppWorkerSubject.updateQuestionCatalog, (
questionCatalog: questionCatalog,
onlyLanguageChange: onlyLanguageChange,
)
AppWorkerSubject.updateQuestionCatalogPreferences,
excludeProfessional,
));
}

Future<void> updateQuestionCatalog({required QuestionCatalog questionCatalog}) {
return _worker
.send<void>(AppWorkerMessage(AppWorkerSubject.updateQuestionCatalog, questionCatalog));
}

/// Close the service worker when un-registering this service.
@override
Expand Down
21 changes: 12 additions & 9 deletions lib/api/app_worker/question_catalog_handler.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import 'dart:async';
import 'package:flutter/material.dart';

import '/utils/service_worker.dart';
import '/models/question_catalog/question_catalog.dart';

mixin QuestionCatalogHandler<M> on ServiceWorker<M> {
static final _completer = Completer<QuestionCatalog>();

@mustCallSuper
void updateQuestionCatalog(({QuestionCatalog questionCatalog, bool onlyLanguageChange}) questionCatalogChangeData) {
if (_completer.isCompleted) {
_questionCatalog = Future.value(questionCatalogChangeData.questionCatalog);
} else {
_completer.complete(questionCatalogChangeData.questionCatalog);
}
void takeQuestionCatalogAsset(QuestionCatalog questionCatalog) {
_completer.complete(questionCatalog);
}

var _questionCatalog = _completer.future;

Future<QuestionCatalog> get questionCatalog => _questionCatalog;

/// Note: currently this won't update any existing questionnaires.
/// Only the creation of subsequent questionnaires will be affected by this.
Future<void> updateQuestionCatalogPreferences({required bool excludeProfessional}) async {
final qc = await _questionCatalog;
_questionCatalog = Future.value(
qc.copyWith(excludeProfessional: excludeProfessional),
);
}
}
39 changes: 8 additions & 31 deletions lib/api/app_worker/questionnaire_handler.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:async';
import 'dart:ui';

import '/models/question_catalog/question_catalog.dart';
import '/models/authenticated_user.dart';
import '/models/element_variants/element_identifier.dart';
import '/models/answer.dart';
Expand All @@ -15,10 +14,12 @@ import 'question_catalog_handler.dart';
/// Handles questions to element matching and allows uploading the made changes.
mixin QuestionnaireHandler<M> on ServiceWorker<M>, QuestionCatalogHandler<M>, ElementHandler<M> {

final _questionnaireStore = QuestionnaireStore();

Questionnaire? _activeQuestionnaire;


final _activeQuestionnaireStreamController = StreamController<QuestionnaireRepresentation?>();

/// A MultiStream that returns the current questionnaire state if any on initial subscription.
Expand All @@ -36,6 +37,7 @@ mixin QuestionnaireHandler<M> on ServiceWorker<M>, QuestionCatalogHandler<M>, El
}
});


/// This either reopens an existing questionnaire or creates a new one.
void openQuestionnaire(ElementIdentifier element) async {
Expand Down Expand Up @@ -132,7 +134,8 @@ mixin QuestionnaireHandler<M> on ServiceWorker<M>, QuestionCatalogHandler<M>, El
if (_activeQuestionnaire != null) {
if (_questionnaireStore.isFinished(_activeQuestionnaire!)) {
_questionnaireStore.markAsUnfinished(_activeQuestionnaire!);
} else {
}
else {
_activeQuestionnaire!.previous();
}
// notify change
Expand All @@ -150,7 +153,7 @@ mixin QuestionnaireHandler<M> on ServiceWorker<M>, QuestionCatalogHandler<M>, El
_activeQuestionnaireStreamController.add(
QuestionnaireRepresentation.derive(_activeQuestionnaire!),
);
}
}
else if (_questionnaireStore.isUnfinished(_activeQuestionnaire!)) {
_questionnaireStore.markAsFinished(_activeQuestionnaire!);
// notify change
Expand All @@ -177,33 +180,6 @@ mixin QuestionnaireHandler<M> on ServiceWorker<M>, QuestionCatalogHandler<M>, El
_activeQuestionnaireStreamController.close();
super.exit();
}

@override
void updateQuestionCatalog(({QuestionCatalog questionCatalog, bool onlyLanguageChange}) questionCatalogChangeData) {
super.updateQuestionCatalog(questionCatalogChangeData);

if (questionCatalogChangeData.onlyLanguageChange) {
// Update all QuestionDefinition of all the stored questionnares
final questionnaireList = _questionnaireStore.items;

for (final Questionnaire questionnaire in questionnaireList) {
questionnaire.updateQuestionCatalogLanguage(questionCatalogChangeData.questionCatalog);
}
// Update current _activeQuestionnaire
if (_activeQuestionnaire != null) {
// notify change
_activeQuestionnaireStreamController.add(
QuestionnaireRepresentation.derive(
_activeQuestionnaire!,
isCompleted: _questionnaireStore.isFinished(_activeQuestionnaire!),
),
);
}
} else {
_questionnaireStore.clear();
closeQuestionnaire();
}
}
}

/// An immutable representation of a Questionnaire used to present a snapshot of its state.
Expand All @@ -221,11 +197,12 @@ class QuestionnaireRepresentation {
this.isCompleted = false,
});

QuestionnaireRepresentation.derive(Questionnaire questionnaire, {this.isCompleted = false}) :
QuestionnaireRepresentation.derive(Questionnaire questionnaire, {this.isCompleted = false }) :
entries = questionnaire.entries,
activeIndex = questionnaire.activeIndex;
}


class ElementUploadData {
final AuthenticatedUser user;

Expand Down
36 changes: 12 additions & 24 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:get_it/get_it.dart';
import 'package:mobx/mobx.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'dart:ui';
import 'dart:ui' as ui;

import '/commons/themes.dart';
import '/commons/app_config.dart' as app_config;
Expand All @@ -28,42 +28,30 @@ Future <void> main() async {

final QuestionCatalogReader questionCatalogReader = QuestionCatalogReader();

const mainCatalogDirectory = 'assets/question_catalog';
const professionalCatalogDirectory = 'assets/advanced_question_catalog';
ui.PlatformDispatcher.instance.onLocaleChanged = () async {
await questionCatalogReader.read();
};



GetIt.I
.registerSingleton<AppWorkerInterface>(futures[0] as AppWorkerInterface);
GetIt.I.registerSingleton<PreferencesService>(
PreferencesService(preferences: futures[1] as SharedPreferences),
);

PlatformDispatcher.instance.onLocaleChanged = () async {
final isProfessional = GetIt.I.get<PreferencesService>().isProfessional;
final assetPaths = [
mainCatalogDirectory,
if (isProfessional) professionalCatalogDirectory
];

GetIt.I.get<AppWorkerInterface>().updateQuestionCatalog(
questionCatalog: await questionCatalogReader.readAll(assetPaths),
onlyLanguageChange: true);
};

GetIt.I.get<AppWorkerInterface>().updateQuestionCatalog(questionCatalog: await questionCatalogReader.read(),);

// required because isolate cannot read assets
// https://github.com/flutter/flutter/issues/96895
Future.wait([rootBundle.load('assets/datasets/map_feature_collection.json')])
.then(GetIt.I.get<AppWorkerInterface>().passAssets);

// This will clear all pending questionnaires
reaction((p0) => GetIt.I.get<PreferencesService>().isProfessional, (value) async {
final assetPaths = [
mainCatalogDirectory,
if (value) professionalCatalogDirectory
];

GetIt.I.get<AppWorkerInterface>().updateQuestionCatalog(
questionCatalog: await questionCatalogReader.readAll(assetPaths),
onlyLanguageChange: false);
reaction((p0) => GetIt.I.get<PreferencesService>().isProfessional, (value) {
GetIt.I.get<AppWorkerInterface>().updateQuestionCatalogPreferences(
excludeProfessional: !value,
);
}, fireImmediately: true);

runApp(const MyApp());
Expand Down
Loading

0 comments on commit 2d4a44f

Please sign in to comment.