Skip to content

Commit

Permalink
exams seperate from classes
Browse files Browse the repository at this point in the history
  • Loading branch information
31b4 committed Aug 27, 2024
1 parent d5c6d38 commit a18ae97
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 38 deletions.
16 changes: 15 additions & 1 deletion lib/models/class.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:intl/intl.dart';
import 'package:univerx/models/exam.dart';

class Class {
final int id;
Expand Down Expand Up @@ -41,7 +42,7 @@ class Class {
}

// ------------------ Helper functions ------------------
factory Class.fromICS(String icsData) {
static Object fromICS(String icsData) {
final lines = icsData.split('\n');
DateTime? startTime;
DateTime? endTime;
Expand All @@ -59,6 +60,19 @@ class Class {
location = line.split(':')[1];
}
}
//if title starts with vizsga return with Exam()
if (title!.contains('Vizsga')) {
return Exam(
id: -1,
classId: -1,
title: title!,
description: "",
startTime: startTime!,
endTime: endTime!,
location: location!,
isUserCreated: false,
);
}

return Class(
id: -1,
Expand Down
38 changes: 24 additions & 14 deletions lib/models/exam.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,7 @@ class Exam {
return formatter.format(startTime);
}

static String getFormattedTitle(String title) {
if (title.startsWith('[Óra]')) {
title = title.substring(title.indexOf(']') + 2, title.length);
var sv = title.split(" ");
title = sv[0] + " " + sv[1];
}
else if (title.startsWith('[Vizsga]')) {
var sv = title.split(" ");
title = sv[0] + " " + sv[1] + " " +sv[2];
}


return title;
}

Class convertExamToClass() {
return Class(
id: classId,
Expand All @@ -71,4 +58,27 @@ class Exam {
isUserCreated: isUserCreated,
);
}
static String formatText(int limit, String text) {
var words = text.split(' ');
var result = '';

if (words[0].length > limit){
return words[0].substring(0,limit-3) + "...";
}


for (var word in words) {
if (result.length + word.length <= limit) {
result += word + ' ';
} else {
//result += '...';
break;
}
}
//if last character is a comma, remove it
if (result[result.length - 2] == ',') {
result = result.substring(0, result.length - 2);
}
return result;
}
}
79 changes: 56 additions & 23 deletions lib/services/neptun_ICS_fetching.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import 'dart:convert';
import 'dart:math';
import 'package:http/http.dart' as http;
import 'package:univerx/database/database_helper.dart';
import 'package:univerx/models/class.dart';
import 'package:univerx/models/exam.dart';
import 'package:univerx/services/neptun_API_fetching.dart';

class ParsedData {
final List<Class> classes;
final List<Exam> exams;

ParsedData({required this.classes, required this.exams});
}

class EventService {
final String url;
Expand All @@ -11,50 +21,70 @@ class EventService {

Future<void> fetchAndUpdateIcs() async {

// Fetch events from .ics file
List<Class> newEvents = await fetchEvents();
if (newEvents.isEmpty) {
return;
}
// Fetch events from .ics file
ParsedData newEvents = await fetchEvents();
if (newEvents.classes.isEmpty && newEvents.exams.isEmpty) {
return;
}

//classes and exams
List<Class> newClasses = newEvents.classes;
List<Exam> newExams = newEvents.exams;
print(newExams);
print(newClasses);

// Fetch existing events from database
DatabaseHelper dbHelper = DatabaseHelper.instance;
// Fetch existing events from database
DatabaseHelper dbHelper = DatabaseHelper.instance;


// Clear the existing events in the database
await dbHelper.deleteNeptunClasses();
// Clear the existing events in the database
await dbHelper.deleteNeptunClasses();
await dbHelper.deleteNeptunExams();

// Save new events to the database
for (Class newEvent in newEvents) {
await dbHelper.insertClass(newEvent);
// Save new events to the database
for (Class newEvent in newClasses) {
await dbHelper.insertClass(newEvent);
}
for (Exam newExam in newExams) {
await dbHelper.insertExam(newExam);
}
}
}

Future<List<Class>> fetchEvents() async {
Future<ParsedData> fetchEvents() async {
try {
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
final icsData = response.body;
final events = parseICS(icsData);
events.sort((a, b) => a.startTime.compareTo(b.startTime)); // Sort events by start time
return events;
ParsedData parsed = parseICS(icsData);
parsed.exams.sort((a, b) => a.startTime.compareTo(b.startTime)); // Sort events by start time
parsed.classes.sort((a, b) => a.startTime.compareTo(b.startTime)); // Sort events by start time


return parsed;
} else {
throw Exception('Failed to load events');
}
} catch (e) {
print('Error fetching events: $e');
return [];
return null!;
}
}

List<Class> parseICS(String icsData) {
ParsedData parseICS(String icsData) {
try {
final events = <Class>[];
ParsedData parsedData = ParsedData(classes: [], exams: []);
final eventStrings = icsData.split('BEGIN:VEVENT');
for (var eventString in eventStrings.skip(1)) {
events.add(Class.fromICS(eventString));
// if object type exam add to exam else add to event
final event = Class.fromICS(eventString);
if (event.runtimeType == Exam) {
parsedData.exams.add(event as Exam);
}
else if (event.runtimeType == Class) {
parsedData.classes.add(event as Class);
}
}
return events;
return parsedData;
} catch (e) {
print('Error parsing ICS data: $e');
rethrow;
Expand Down Expand Up @@ -100,7 +130,10 @@ class EventService {
break;
}
}

//if last character is a comma, remove it
if (result[result.length - 2] == ',') {
result = result.substring(0, result.length - 2);
}
return result;
}

Expand Down

0 comments on commit a18ae97

Please sign in to comment.