diff --git a/example/lib/main.dart b/example/lib/main.dart index a31dfb7..944b74c 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -2,6 +2,8 @@ import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:cognitive_data/databases/firebase_db/firebase_db.dart'; import 'package:cognitive_data/models/device.dart'; import 'package:cognitive_data/models/session.dart'; +import 'package:cognitive_data/models/trial.dart'; +import 'package:cognitive_data/models/trial_type.dart'; import 'package:flutter/material.dart'; import 'package:firebase_core/firebase_core.dart'; @@ -34,7 +36,7 @@ class MyHomePage extends StatelessWidget { final String title; final _db = FirebaseDB( FirebaseFirestore.instance, - participantID: '101,', + participantID: '101', sessionID: '001', taskName: 'dsb', ); @@ -74,7 +76,17 @@ class MyHomePage extends StatelessWidget { child: const Text("Save Device metadata to firebase"), ), ElevatedButton( - onPressed: () {}, + onPressed: () async { + final Trial trial = Trial( + participantID: _db.participantID, + sessionID: _db.sessionID, + trialType: TrialType.practice, + stim: '123', + response: '321', + ); + + await _db.addTrial(trial: trial); + }, child: const Text("Save trial to firebase"), ), ElevatedButton( diff --git a/lib/databases/firebase_db/firebase_db.dart b/lib/databases/firebase_db/firebase_db.dart index 811a144..8575bf1 100644 --- a/lib/databases/firebase_db/firebase_db.dart +++ b/lib/databases/firebase_db/firebase_db.dart @@ -80,9 +80,23 @@ class FirebaseDB implements DB { await sessionRef.doc('sessionMetadata').set(sessionData); } + /// Adds a single [trial] to [FirebaseFirestore]. + /// Stores each [trial] data in an independent doc inside a collection + /// named `trials`. @override - void addTrial({required Trial trial}) { - // TODO: implement addTrial + Future addTrial({required Trial trial}) async { + final Map trialMap = { + 'participantID': trial.participantID, + 'sessionID': trial.sessionID, + 'trialType': trial.trialType, + 'stim': trial.stim, + 'response': trial.response, + }; + + final CollectionReference trialsRef = _db.collection( + 'participants/$participantID/cognitive_tasks/$taskName/sessions/$sessionID/trials'); + + await trialsRef.add(trialMap); } @override