-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from mario-bermonti:data-manager
Create a data manager
- Loading branch information
Showing
13 changed files
with
166 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import 'package:cognitive_data/data.dart'; | ||
import 'package:path_provider/path_provider.dart'; | ||
|
||
class DataManager { | ||
final String participantID; | ||
final String sessionID; | ||
late final DataBase _dataBase; | ||
|
||
/// TODO add type | ||
final data; | ||
|
||
DataManager({ | ||
required this.participantID, | ||
required this.sessionID, | ||
required this.data, | ||
}); | ||
|
||
Future<void> initDB({required String name}) async { | ||
final dir = await getApplicationDocumentsDirectory(); | ||
final String dbPath = "${dir.path}/$name.sqlite3"; | ||
|
||
_dataBase = DataBase(path: dbPath); | ||
} | ||
|
||
/// Helper method that adds the necessary device data to db | ||
void addDeviceData() { | ||
_dataBase.addDeviceData( | ||
participantId: participantID, | ||
sessionID: sessionID, | ||
); | ||
} | ||
|
||
/// Helper method that adds the necessary session metadata to db | ||
void addSessionMetaData() { | ||
_dataBase.addSessionMetaData( | ||
participantId: participantID, | ||
sessionID: sessionID, | ||
timeStart: data.sessionData.startTime, | ||
timeEnd: data.sessionData.endTime, | ||
); | ||
} | ||
|
||
/// Adds data from practice trials to db | ||
void addPracticeTrialData() { | ||
_addTrialData( | ||
trialType: TrialType.practice, | ||
trialData: data.trialData, | ||
); | ||
} | ||
|
||
/// Adds data from experimental trials to db | ||
/// Requires the experimental trials [trialData] | ||
void addExperimentalTrialData({required dynamic trialData}) { | ||
_addTrialData( | ||
trialType: TrialType.experimental, | ||
trialData: trialData, | ||
); | ||
} | ||
|
||
/// Helper method to add the data from trials to db | ||
/// TODO specify type | ||
void _addTrialData({ | ||
required TrialType trialType, | ||
required dynamic trialData, | ||
}) { | ||
for (var trial in trialData) { | ||
_dataBase.addTrialData( | ||
participantId: participantID, | ||
stim: trial.stim, | ||
resp: trial.response, | ||
trialType: trialType, | ||
sessionID: sessionID, | ||
); | ||
} | ||
} | ||
|
||
/// Save session metadata, device and trial data to db | ||
Future<void> saveDataDB() async { | ||
await _dataBase.saveData(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
String createSessionID( | ||
{required String participantID, required String startTime}) { | ||
final String sessionID = participantID + startTime; | ||
|
||
return sessionID; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import 'package:cognitive_data/data.dart'; | ||
|
||
import '../data_manager/data_manager.dart'; | ||
|
||
/// Process data from a single session and save it to the db | ||
Future<void> processData({ | ||
required String participantID, | ||
required String sessionID, | ||
required dynamic practiceData, | ||
required dynamic experimentalData, | ||
required String dbName, | ||
required TrialType trialType, | ||
}) async { | ||
DataManager dataManager = DataManager( | ||
participantID: participantID, | ||
sessionID: sessionID, | ||
data: practiceData, | ||
); | ||
|
||
await dataManager.initDB(name: dbName); | ||
dataManager.addDeviceData(); | ||
dataManager.addSessionMetaData(); | ||
dataManager.addPracticeTrialData(); | ||
dataManager.addExperimentalTrialData(trialData: experimentalData.trialData); | ||
await dataManager.saveDataDB(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,33 @@ | ||
import 'package:cognitive_data/trial_type.dart'; | ||
import 'package:digit_span_tasks/digit_span_tasks.dart'; | ||
import 'package:mdigit_span_tasks/src/data_manager/data_manager.dart'; | ||
import 'package:mdigit_span_tasks/src/data_manager/session_id_creator.dart'; | ||
import 'package:mdigit_span_tasks/src/services/data_processor.dart'; | ||
|
||
import '../participant_info/participant_info_dialog.dart'; | ||
|
||
/// Run a data collection session | ||
/// Running a session includes configuring everything needed and running a | ||
/// cognitive task specificed with [taskRunner]. | ||
void runSession({required Function taskRunner}) async { | ||
void runSession({required Function taskRunner, required String dbName}) async { | ||
final String participantID = await showParticipantInfoDialog(); | ||
taskRunner(); | ||
DigitSpanTasksData data = await taskRunner(); | ||
|
||
/// We use the startTime for the practice session to create a single | ||
/// session id for both practice and experimental data. | ||
final String sessionID = createSessionID( | ||
participantID: participantID, | ||
startTime: data.practiceData.sessionData.startTime.toString(), | ||
); | ||
|
||
/// TODO Specify type | ||
final dataPractice = data.practiceData; | ||
await processData( | ||
participantID: participantID, | ||
sessionID: sessionID, | ||
practiceData: dataPractice, | ||
experimentalData: data.experimentalData, | ||
dbName: dbName, | ||
trialType: TrialType.practice, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
# | ||
|
||
list(APPEND FLUTTER_PLUGIN_LIST | ||
sqlite3_flutter_libs | ||
) | ||
|
||
list(APPEND FLUTTER_FFI_PLUGIN_LIST | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters