record method
Future<bool>
record(
- Map<
DVConsentCategory, bool> answers, { - Set<
DVConsentCategory> ? asked, - DVConsentPrompt prompt = DVConsentPrompt.settingsScreen,
- String? userId,
Records a choice, and returns whether it was written.
A grant that cannot be written is not treated as consent
(DV-ANALYTICS-006). A withdrawal that cannot be written still stops
collection, because stopping loses nothing, and is reported the same
way. A category that was asked and not answered is denied.
Implementation
Future<bool> record(
Map<DVConsentCategory, bool> answers, {
Set<DVConsentCategory>? asked,
DVConsentPrompt prompt = DVConsentPrompt.settingsScreen,
String? userId,
}) async {
final Set<DVConsentCategory> presented = <DVConsentCategory>{
...?asked,
...answers.keys,
};
for (final DVConsentCategory c in presented) {
_declared(c);
}
final Map<String, bool> resolved = <String, bool>{
for (final DVConsentCategory c in presented)
if (!_declared(c).required) c.name: answers[c] ?? false,
};
final Map<DVConsentCategory, bool> before = _snapshot();
final DateTime at = _clock().toUtc();
try {
final List<DVConsentRecord> mine = await records();
final int seq = mine.isEmpty ? 1 : mine.last.seq + 1;
await database.execute(
'INSERT INTO $table (id, seq, install_id, user_id, policy_version, '
'asked, answers, prompt, recorded_at) '
'VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)',
<Object?>[
_randomId(),
seq,
installId,
userId,
policy.version,
jsonEncode(resolved.keys.toList()..sort()),
jsonEncode(resolved),
prompt.name,
at.toIso8601String(),
],
);
} on Object catch (error) {
_diagnose(
'DV-ANALYTICS-006',
'a consent choice could not be recorded ($error); it is not treated '
'as consent',
);
for (final MapEntry<String, bool> e in resolved.entries) {
if (!e.value) _unrecordedWithdrawals.add(e.key);
}
_notify(before);
return false;
}
_answers = <String, bool>{
// A partial choice under the same version keeps the earlier answers
// for the categories it did not present.
...?_answers,
...resolved,
};
_unrecordedWithdrawals.removeAll(resolved.keys);
_notify(before);
return true;
}