Added sqlite database for answers
This commit is contained in:
parent
e2e9c314af
commit
9fb029da20
4 changed files with 72 additions and 1 deletions
|
@ -1,3 +1,5 @@
|
|||
import 'package:intl/intl.dart';
|
||||
import 'package:ohthatsa/pages/practice/PracticeDatabase.dart';
|
||||
import 'package:ohthatsa/pages/practice/PracticeType.dart';
|
||||
import 'package:ohthatsa/util/Months.dart';
|
||||
|
||||
|
@ -5,6 +7,20 @@ class PracticeAnswer {
|
|||
final int question;
|
||||
final int answer;
|
||||
final PracticeType practiceType;
|
||||
DateTime dateTime;
|
||||
|
||||
PracticeAnswer(this.question, this.answer, this.practiceType);
|
||||
PracticeAnswer(this.question, this.answer, this.practiceType){
|
||||
dateTime = DateTime.now();
|
||||
}
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
// Should match the SQLite format YYYY-MM-DD HH:MM:SS.SSS
|
||||
String timeFormat = DateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(dateTime);
|
||||
return <String, dynamic>{
|
||||
"question": question,
|
||||
"answer": answer,
|
||||
"type": practiceType.toString().split(".").last,
|
||||
"time": timeFormat
|
||||
};
|
||||
}
|
||||
}
|
50
lib/pages/practice/PracticeDatabase.dart
Normal file
50
lib/pages/practice/PracticeDatabase.dart
Normal file
|
@ -0,0 +1,50 @@
|
|||
import 'package:ohthatsa/pages/practice/PracticeAnswer.dart';
|
||||
import 'package:ohthatsa/pages/practice/PracticeType.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
import 'package:path/path.dart';
|
||||
|
||||
class PracticeDatabase {
|
||||
Database _database;
|
||||
|
||||
static PracticeDatabase getDatabase() {
|
||||
PracticeDatabase practiceDatabase = PracticeDatabase();
|
||||
practiceDatabase.open();
|
||||
return practiceDatabase;
|
||||
}
|
||||
|
||||
_onCreate(Database database, int version) async {
|
||||
String types = PracticeType.values.map((type) => "'${type.toString().split(".").last}'").join(", ");
|
||||
await database.execute('''
|
||||
CREATE TABLE PracticeAnswer (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
question INTEGER NOT NULL,
|
||||
answer INTEGER NOT NULL,
|
||||
type TEXT CHECK( type IN ($types) ) NOT NULL,
|
||||
time DATETIME NOT NULL
|
||||
);
|
||||
''');
|
||||
}
|
||||
|
||||
Future open() async{
|
||||
var databasesPath = await getDatabasesPath();
|
||||
final databaseName = "answers.db";
|
||||
var path = join(databasesPath, databaseName);
|
||||
this._database = await openDatabase(
|
||||
path,
|
||||
version: 1,
|
||||
onCreate: _onCreate
|
||||
);
|
||||
}
|
||||
|
||||
Future insertAnswer(PracticeAnswer answer) async {
|
||||
this._database.insert("PracticeAnswer", answer.toMap());
|
||||
}
|
||||
|
||||
static insertAnswers(List<PracticeAnswer> answers) async {
|
||||
var practiceDatabase = PracticeDatabase();
|
||||
await practiceDatabase.open();
|
||||
answers.forEach((answer) {
|
||||
practiceDatabase.insertAnswer(answer);
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:ohthatsa/pages/practice/PracticeDatabase.dart';
|
||||
import 'package:ohthatsa/pages/practice/PracticeSetup.dart';
|
||||
import 'package:ohthatsa/pages/practice/PracticeThing.dart';
|
||||
import 'package:ohthatsa/pages/practice/PracticeThingCentury.dart';
|
||||
|
@ -8,6 +9,7 @@ import 'package:ohthatsa/pages/practice/PracticeThingMonth.dart';
|
|||
import 'package:ohthatsa/AppDrawer.dart';
|
||||
import 'package:ohthatsa/pages/practice/PracticeType.dart';
|
||||
import 'package:ohthatsa/util/Extensions.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
|
||||
import 'PracticeAnswer.dart';
|
||||
|
||||
|
@ -211,6 +213,7 @@ class _PracticeState extends State<PracticePage> {
|
|||
practiceThing.next()
|
||||
});
|
||||
if((_startCount - _count) == 0) {
|
||||
PracticeDatabase.insertAnswers(practiceThing.answers);
|
||||
showDialog(
|
||||
context: context,
|
||||
child: finishedPracticeDialog()
|
||||
|
|
|
@ -24,6 +24,8 @@ dependencies:
|
|||
flutter:
|
||||
sdk: flutter
|
||||
numberpicker: ^1.2.1
|
||||
sqflite: ^1.3.0
|
||||
intl: ^0.16.1
|
||||
|
||||
|
||||
# The following adds the Cupertino Icons font to your application.
|
||||
|
|
Loading…
Add table
Reference in a new issue