Skip to content

Commit

Permalink
fix: sqlite keywords (#1277)
Browse files Browse the repository at this point in the history
  • Loading branch information
DGoiana authored Sep 2, 2024
2 parents 418b9b2 + d460c06 commit cb95ca5
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import 'package:uni/model/entities/exam.dart';
/// See the [Exam] class to see what data is stored in this database.
class AppExamsDatabase extends AppDatabase<List<Exam>> {
AppExamsDatabase()
: super('exams.db', [_createScript], onUpgrade: migrate, version: 6);
: super('exams.db', [_createScript], onUpgrade: migrate, version: 7);

static const _createScript = '''
CREATE TABLE exams(id TEXT, subject TEXT, begin TEXT, end TEXT,
CREATE TABLE exams(id TEXT, subject TEXT, start TEXT, finish TEXT,
rooms TEXT, examType TEXT, faculty TEXT, PRIMARY KEY (id,faculty)) ''';

/// Returns a list containing all of the exams stored in this database.
Expand All @@ -25,8 +25,8 @@ CREATE TABLE exams(id TEXT, subject TEXT, begin TEXT, end TEXT,
return Exam.secConstructor(
maps[i]['id'] as String,
maps[i]['subject'] as String,
DateTime.parse(maps[i]['begin'] as String),
DateTime.parse(maps[i]['end'] as String),
DateTime.parse(maps[i]['start'] as String),
DateTime.parse(maps[i]['finish'] as String),
maps[i]['rooms'] as String,
maps[i]['examType'] as String,
maps[i]['faculty'] as String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';

import 'package:sqflite/sqlite_api.dart';
import 'package:uni/controller/local_storage/database/app_database.dart';
import 'package:uni/model/entities/course.dart';
import 'package:uni/model/entities/profile.dart';
Expand All @@ -9,15 +10,20 @@ import 'package:uni/model/entities/profile.dart';
/// This database stores information about the user's university profile.
class AppUserDataDatabase extends AppDatabase<Profile> {
AppUserDataDatabase()
: super('userdata.db', ['CREATE TABLE userdata(key TEXT, value TEXT)']);
: super(
'userdata.db',
['CREATE TABLE userdata(name TEXT, value TEXT)'],
onUpgrade: migrate,
version: 2,
);

/// Adds [data] (profile) to this database.
@override
Future<void> saveToDatabase(Profile data) async {
for (final keymap in data.keymapValues()) {
await insertInDatabase(
'userdata',
{'key': keymap.item1, 'value': keymap.item2},
{'name': keymap.item1, 'value': keymap.item2},
);
}
}
Expand All @@ -37,19 +43,19 @@ class AppUserDataDatabase extends AppDatabase<Profile> {
String? feesBalance;
DateTime? feesLimit;
for (final entry in maps) {
if (entry['key'] == 'name') {
if (entry['name'] == 'name') {
name = entry['value'] as String;
}
if (entry['key'] == 'email') {
if (entry['name'] == 'email') {
email = entry['value'] as String;
}
if (entry['key'] == 'printBalance') {
if (entry['name'] == 'printBalance') {
printBalance = entry['value'] as String;
}
if (entry['key'] == 'feesBalance') {
if (entry['name'] == 'feesBalance') {
feesBalance = entry['value'] as String;
}
if (entry['key'] == 'feesLimit') {
if (entry['name'] == 'feesLimit') {
feesLimit = DateTime.tryParse(entry['value'] as String);
}
}
Expand All @@ -71,4 +77,15 @@ class AppUserDataDatabase extends AppDatabase<Profile> {

await db.delete('userdata');
}

static FutureOr<void> migrate(
Database db,
int oldVersion,
int newVersion,
) async {
final batch = db.batch()
..execute('DROP TABLE IF EXISTS userdata')
..execute('CREATE TABLE userdata(name TEXT, value TEXT)');
await batch.commit();
}
}
8 changes: 4 additions & 4 deletions packages/uni_app/lib/generated/model/entities/exam.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions packages/uni_app/lib/model/entities/exam.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ part '../../generated/model/entities/exam.g.dart';
/// Manages a generic Exam.
///
/// The information stored is:
/// - The `begin` and `end` times of the Exam
/// - The `start` and `finish` times of the Exam
/// - The Exam `subject`
/// - A List with the `rooms` in which the Exam takes place
/// - The Exam `type`
Expand All @@ -19,8 +19,8 @@ part '../../generated/model/entities/exam.g.dart';
class Exam {
Exam(
this.id,
this.begin,
this.end,
this.start,
this.finish,
this.subject,
this.rooms,
this.examType,
Expand All @@ -32,15 +32,15 @@ class Exam {
Exam.secConstructor(
this.id,
this.subject,
this.begin,
this.end,
this.start,
this.finish,
String rooms,
this.examType,
this.faculty,
) : rooms = rooms.split(',');

final DateTime begin;
final DateTime end;
final DateTime start;
final DateTime finish;
final String id;
final String subject;
final List<String> rooms;
Expand All @@ -59,29 +59,29 @@ class Exam {
Map<String, dynamic> toJson() => _$ExamToJson(this);

/// Returns whether or not this exam has already ended.
bool hasEnded() => DateTime.now().compareTo(end) >= 0;
bool hasEnded() => DateTime.now().compareTo(finish) >= 0;

String weekDay(AppLocale locale) {
return DateFormat.EEEE(locale.localeCode.languageCode)
.dateSymbols
.WEEKDAYS[begin.weekday % 7];
.WEEKDAYS[start.weekday % 7];
}

String month(AppLocale locale) {
return DateFormat.EEEE(locale.localeCode.languageCode)
.dateSymbols
.MONTHS[begin.month - 1];
.MONTHS[start.month - 1];
}

String get beginTime => formatTime(begin);
String get startTime => formatTime(start);

String get endTime => formatTime(end);
String get finishTime => formatTime(finish);

String formatTime(DateTime time) => DateFormat('HH:mm').format(time);

@override
String toString() {
return '''$id - $subject - ${begin.year} - $month - ${begin.day} - $beginTime-$endTime - $examType - $rooms - $weekDay''';
return '''$id - $subject - ${start.year} - $month - ${start.day} - $startTime-$finishTime - $examType - $rooms - $weekDay''';
}

/// Prints the data in this exam to the [Logger] with an INFO level.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ExamProvider extends StateProviderNotifier<List<Exam>> {
final exams = await ExamFetcher(profile.courses, userUcs)
.extractExams(session, parserExams);

exams.sort((exam1, exam2) => exam1.begin.compareTo(exam2.begin));
exams.sort((exam1, exam2) => exam1.start.compareTo(exam2.start));

final db = AppExamsDatabase();
await db.saveIfPersistentSession(exams);
Expand Down
10 changes: 5 additions & 5 deletions packages/uni_app/lib/view/exams/exams.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ class ExamsPageViewState extends SecondaryPageViewState<ExamsPageView> {

for (var i = 0; i < exams.length; i++) {
if (i + 1 >= exams.length) {
if (exams[i].begin.day == exams[i - 1].begin.day &&
exams[i].begin.month == exams[i - 1].begin.month) {
if (exams[i].start.day == exams[i - 1].start.day &&
exams[i].start.month == exams[i - 1].start.month) {
currentDayExams.add(exams[i]);
} else {
if (currentDayExams.isNotEmpty) {
Expand All @@ -91,8 +91,8 @@ class ExamsPageViewState extends SecondaryPageViewState<ExamsPageView> {
columns.add(createExamCard(context, currentDayExams));
break;
}
if (exams[i].begin.day == exams[i + 1].begin.day &&
exams[i].begin.month == exams[i + 1].begin.month) {
if (exams[i].start.day == exams[i + 1].start.day &&
exams[i].start.month == exams[i + 1].start.month) {
currentDayExams.add(exams[i]);
} else {
currentDayExams.add(exams[i]);
Expand Down Expand Up @@ -122,7 +122,7 @@ class ExamsPageViewState extends SecondaryPageViewState<ExamsPageView> {
alignment: Alignment.center,
child: Text(
'${exams.first.weekDay(locale)}, '
'${exams.first.begin.formattedDate(locale)}',
'${exams.first.start.formattedDate(locale)}',
style: Theme.of(context).textTheme.titleLarge,
),
),
Expand Down
12 changes: 6 additions & 6 deletions packages/uni_app/lib/view/exams/widgets/exam_row.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class _ExamRowState extends State<ExamRow> {
@override
Widget build(BuildContext context) {
final roomsKey =
'${widget.exam.subject}-${widget.exam.rooms}-${widget.exam.beginTime}-'
'${widget.exam.endTime}';
'${widget.exam.subject}-${widget.exam.rooms}-${widget.exam.startTime}-'
'${widget.exam.finishTime}';
return Center(
child: Container(
padding: const EdgeInsets.only(left: 12, bottom: 8, right: 12),
Expand All @@ -54,10 +54,10 @@ class _ExamRowState extends State<ExamRow> {
children: <Widget>[
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: widget.exam.beginTime != '00:00'
children: widget.exam.startTime != '00:00'
? [
ExamTime(
begin: widget.exam.beginTime,
begin: widget.exam.startTime,
),
]
: [],
Expand Down Expand Up @@ -146,8 +146,8 @@ class _ExamRowState extends State<ExamRow> {
return Event(
title: '${widget.exam.examType} ${widget.exam.subject}',
location: widget.exam.rooms.toString(),
startDate: widget.exam.begin,
endDate: widget.exam.end,
startDate: widget.exam.start,
endDate: widget.exam.finish,
);
}
}
2 changes: 1 addition & 1 deletion packages/uni_app/lib/view/home/widgets/exam_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class ExamCard extends GenericCard {

List<Exam> getPrimaryExams(List<Exam> visibleExams) {
return visibleExams
.where((exam) => isSameDay(visibleExams[0].begin, exam.begin))
.where((exam) => isSameDay(visibleExams[0].start, exam.start))
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class NextExamsWidget extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
DateRectangle(
date: exams.isNotEmpty ? exams.first.begin.formattedDate(locale) : '',
date: exams.isNotEmpty ? exams.first.start.formattedDate(locale) : '',
),
Column(
children: exams.map((exam) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class RemainingExamsWidget extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
exam.begin.formattedDate(locale),
exam.start.formattedDate(locale),
style: Theme.of(context).textTheme.bodyLarge,
),
ExamTitle(
Expand Down
16 changes: 8 additions & 8 deletions packages/uni_app/test/unit/view/Widgets/exam_row_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ void main() async {

group('Exam Row', () {
const subject = 'SOPE';
final begin = DateTime(
final start = DateTime(
DateTime.now().year,
DateTime.now().month,
DateTime.now().day,
10,
);
final end = DateTime(
final finish = DateTime(
DateTime.now().year,
DateTime.now().month,
DateTime.now().day,
12,
);
final beginTime = DateFormat('HH:mm').format(begin);
final endTime = DateFormat('HH:mm').format(end);
final startTime = DateFormat('HH:mm').format(start);
final finishTime = DateFormat('HH:mm').format(finish);

testWidgets('When given a single room', (tester) async {
final rooms = ['B315'];
final exam = Exam('1230', begin, end, subject, rooms, '', 'feup');
final exam = Exam('1230', start, finish, subject, rooms, '', 'feup');
final widget = ExamRow(
exam: exam,
teacher: '',
Expand All @@ -44,7 +44,7 @@ void main() async {
await tester.pumpWidget(testableWidget(widget, providers: providers));
await tester.pump();

final roomsKey = '$subject-$rooms-$beginTime-$endTime';
final roomsKey = '$subject-$rooms-$startTime-$finishTime';

expect(
find.descendant(
Expand All @@ -57,7 +57,7 @@ void main() async {

testWidgets('When multiple rooms', (tester) async {
final rooms = ['B315', 'B316', 'B330'];
final exam = Exam('1230', begin, end, subject, rooms, '', 'feup');
final exam = Exam('1230', start, finish, subject, rooms, '', 'feup');
final widget = ExamRow(
exam: exam,
teacher: '',
Expand All @@ -72,7 +72,7 @@ void main() async {
await tester.pumpWidget(testableWidget(widget, providers: providers));
await tester.pump();

final roomsKey = '$subject-$rooms-$beginTime-$endTime';
final roomsKey = '$subject-$rooms-$startTime-$finishTime';

expect(
find.descendant(
Expand Down

0 comments on commit cb95ca5

Please sign in to comment.