Skip to content

Commit

Permalink
Clean up external functions to be class methods
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasyishak committed Jul 24, 2023
1 parent 9c73186 commit 6297a07
Showing 1 changed file with 50 additions and 80 deletions.
130 changes: 50 additions & 80 deletions pkgs/unified_analytics/lib/src/survey_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,6 @@ bool checkSurveyDate(Survey survey) {
return false;
}

/// Function to parse the contents of the persisted dismissed surveys
Map<String, PersistedSurvey> parsePersistedSurveys(File dismissedSurveyFile) {
final contents = jsonDecode(dismissedSurveyFile.readAsStringSync())
as Map<String, dynamic>;

// Initialize the list of persisted surveys and add to them
// as they are being parsed
var persistedSurveys = <String, PersistedSurvey>{};
contents.forEach((key, value) {
value as Map<String, dynamic>;

final uniqueId = key;
final snoozed = value['status'] == 'snoozed' ? true : false;
final timestamp =
DateTime.fromMillisecondsSinceEpoch(value['timestamp'] as int);

persistedSurveys[uniqueId] = PersistedSurvey(
uniqueId: uniqueId,
snoozed: snoozed,
timestamp: timestamp,
);
});

return persistedSurveys;
}

/// Function that takes in a json data structure that is in
/// the form of a list and returns a list of [Survey]s
///
Expand All @@ -68,35 +42,6 @@ List<Survey> parseSurveysFromJson(List<dynamic> body) => body
.where(checkSurveyDate)
.toList();

/// Function to persist the provided [Survey] in the file on disk
///
/// Each entry for a survey will have the following format
/// ```
/// {
/// "survey-unique-id": {
/// "status": "snoozed", // status is either snoozed or dismissed
/// "timestamp": 1690219834859
/// }
/// }
/// ```
void persistSurvey(
Survey survey,
bool permanently,
File dismissedSurveyFile,
) {
final contents = jsonDecode(dismissedSurveyFile.readAsStringSync())
as Map<String, dynamic>;

// Add the new data and write back out to the file
final status = permanently ? 'dismissed' : 'snoozed';
contents[survey.uniqueId] = {
'status': status,
'timestamp': clock.now().millisecondsSinceEpoch,
};

dismissedSurveyFile.writeAsStringSync(jsonEncode(contents));
}

class Condition {
/// How to query the log file
///
Expand Down Expand Up @@ -253,14 +198,57 @@ class SurveyHandler {
///
/// In the snoozed state, the survey will be prompted again after
/// the survey's specified snooze period
void dismiss(Survey survey, bool permanently) =>
persistSurvey(survey, permanently, _dismissedSurveyFile);
///
/// Each entry for a survey will have the following format
/// ```
/// {
/// "survey-unique-id": {
/// "status": "snoozed", // status is either snoozed or dismissed
/// "timestamp": 1690219834859
/// }
/// }
/// ```
void dismiss(Survey survey, bool permanently) {
final contents = jsonDecode(_dismissedSurveyFile.readAsStringSync())
as Map<String, dynamic>;

// Add the new data and write back out to the file
final status = permanently ? 'dismissed' : 'snoozed';
contents[survey.uniqueId] = {
'status': status,
'timestamp': clock.now().millisecondsSinceEpoch,
};

_dismissedSurveyFile.writeAsStringSync(jsonEncode(contents));
}

/// Retrieve a list of strings for each [Survey] persisted on disk
///
/// The survey may be in a snoozed or dismissed state based on user action
Map<String, PersistedSurvey> fetchPersistedSurveys() =>
parsePersistedSurveys(_dismissedSurveyFile);
Map<String, PersistedSurvey> fetchPersistedSurveys() {
final contents = jsonDecode(_dismissedSurveyFile.readAsStringSync())
as Map<String, dynamic>;

// Initialize the list of persisted surveys and add to them
// as they are being parsed
var persistedSurveys = <String, PersistedSurvey>{};
contents.forEach((key, value) {
value as Map<String, dynamic>;

final uniqueId = key;
final snoozed = value['status'] == 'snoozed' ? true : false;
final timestamp =
DateTime.fromMillisecondsSinceEpoch(value['timestamp'] as int);

persistedSurveys[uniqueId] = PersistedSurvey(
uniqueId: uniqueId,
snoozed: snoozed,
timestamp: timestamp,
);
});

return persistedSurveys;
}

/// Retrieves the survey metadata file from [kContextualSurveyUrl]
Future<List<Survey>> fetchSurveyList() async {
Expand All @@ -286,9 +274,7 @@ class SurveyHandler {
}
}

class FakeSurveyHandler implements SurveyHandler {
@override
final File _dismissedSurveyFile;
class FakeSurveyHandler extends SurveyHandler {
final List<Survey> _fakeInitializedSurveys = [];

/// Use this class in tests if you can provide the
Expand All @@ -301,11 +287,7 @@ class FakeSurveyHandler implements SurveyHandler {
required Directory homeDirectory,
required FileSystem fs,
required List<Survey> initializedSurveys,
}) : _dismissedSurveyFile = fs.file(p.join(
homeDirectory.path,
kDartToolDirectoryName,
kDismissedSurveyFileName,
)) {
}) : super(fs: fs, homeDirectory: homeDirectory) {
// We must pass the surveys from the list to the
// `checkSurveyDate` function here and not for the
// `.fromString()` constructor because the `parseSurveysFromJson`
Expand All @@ -323,25 +305,13 @@ class FakeSurveyHandler implements SurveyHandler {
required Directory homeDirectory,
required FileSystem fs,
required String content,
}) : _dismissedSurveyFile = fs.file(p.join(
homeDirectory.path,
kDartToolDirectoryName,
kDismissedSurveyFileName,
)) {
}) : super(fs: fs, homeDirectory: homeDirectory) {
final body = jsonDecode(content) as List<dynamic>;
for (final fakeSurvey in parseSurveysFromJson(body)) {
_fakeInitializedSurveys.add(fakeSurvey);
}
}

@override
void dismiss(Survey survey, bool permanently) =>
persistSurvey(survey, permanently, _dismissedSurveyFile);

@override
Map<String, PersistedSurvey> fetchPersistedSurveys() =>
parsePersistedSurveys(_dismissedSurveyFile);

@override
Future<List<Survey>> fetchSurveyList() =>
Future<List<Survey>>.value(_fakeInitializedSurveys);
Expand Down

0 comments on commit 6297a07

Please sign in to comment.