Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exception when there are no more tuitions to pay #829

Merged
merged 2 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ class TuitionNotification extends Notification {
!(await AppSharedPreferences.getTuitionNotificationToggle());
if (notificationsAreDisabled) return false;
final FeesFetcher feesFetcher = FeesFetcher();
final String nextDueDate = await parseFeesNextLimit(
final DateTime? dueDate = await parseFeesNextLimit(
await feesFetcher.getUserFeesResponse(session));
_dueDate = DateTime.parse(nextDueDate);

if(dueDate == null) return false;

_dueDate = dueDate;
return DateTime.now().difference(_dueDate).inDays >= -3;
}

Expand Down
20 changes: 10 additions & 10 deletions uni/lib/controller/local_storage/app_user_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ class AppUserDataDatabase extends AppDatabase {
final List<Map<String, dynamic>> maps = await db.query('userdata');

// Convert the List<Map<String, dynamic> into a Profile.
String? name, email, printBalance, feesBalance, feesLimit;
String? name, email, printBalance, feesBalance;
DateTime? feesLimit;
for (Map<String, dynamic> entry in maps) {
if (entry['key'] == 'name') name = entry['value'];
if (entry['key'] == 'email') email = entry['value'];
if (entry['key'] == 'printBalance') printBalance = entry['value'];
if (entry['key'] == 'feesBalance') feesBalance = entry['value'];
if (entry['key'] == 'feesLimit') feesLimit = entry['value'];
if (entry['key'] == 'feesLimit') feesLimit = DateTime.tryParse(entry['value']);
}

return Profile(
Expand All @@ -46,7 +47,7 @@ class AppUserDataDatabase extends AppDatabase {
courses: <Course>[],
printBalance: printBalance ?? '?',
feesBalance: feesBalance ?? '?',
feesLimit: feesLimit ?? '?');
feesLimit: feesLimit);
}

/// Deletes all of the data stored in this database.
Expand All @@ -65,13 +66,12 @@ class AppUserDataDatabase extends AppDatabase {

/// Saves the user's balance and payment due date to the database.
///
/// *Note:*
/// * the first value in [feesInfo] is the user's balance.
/// * the second value in [feesInfo] is the user's payment due date.
void saveUserFees(Tuple2<String, String> feesInfo) async {
void saveUserFees(String feesBalance, DateTime? feesLimit) async {
await insertInDatabase(
'userdata', {'key': 'feesBalance', 'value': feesInfo.item1});
await insertInDatabase(
'userdata', {'key': 'feesLimit', 'value': feesInfo.item2});
'userdata', {'key': 'feesBalance', 'value': feesBalance});
await insertInDatabase('userdata', {
'key': 'feesLimit',
'value': feesLimit != null ? feesLimit.toIso8601String() : ''
});
}
}
8 changes: 5 additions & 3 deletions uni/lib/controller/parsers/parser_fees.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ Future<String> parseFeesBalance(http.Response response) async {
/// Extracts the user's payment due date from an HTTP [response].
///
/// If there are no due payments, `Sem data` is returned.
Future<String> parseFeesNextLimit(http.Response response) async {
Future<DateTime?> parseFeesNextLimit(http.Response response) async {
final document = parse(response.body);

final lines = document.querySelectorAll('#tab0 .tabela tr');

if (lines.length < 2) {
return 'Sem data';
return null;
}
final String limit = lines[1].querySelectorAll('.data')[1].text;

return limit;
//it's completly fine to throw an exeception if it fails, in this case,
//since probably sigarra is returning something we don't except
return DateTime.parse(limit);
}
4 changes: 2 additions & 2 deletions uni/lib/model/entities/profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ class Profile {
late List<Course> courses;
final String printBalance;
final String feesBalance;
final String feesLimit;
final DateTime? feesLimit;

Profile(
{this.name = '',
this.email = '',
courses,
this.printBalance = '',
this.feesBalance = '',
this.feesLimit = ''})
this.feesLimit})
: courses = courses ?? [];

/// Creates a new instance from a JSON object.
Expand Down
4 changes: 2 additions & 2 deletions uni/lib/model/providers/profile_state_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ProfileStateProvider extends StateProviderNotifier {
final response = await FeesFetcher().getUserFeesResponse(session);

final String feesBalance = await parseFeesBalance(response);
final String feesLimit = await parseFeesNextLimit(response);
final DateTime? feesLimit = await parseFeesNextLimit(response);

final DateTime currentTime = DateTime.now();
final Tuple2<String, String> userPersistentInfo =
Expand All @@ -73,7 +73,7 @@ class ProfileStateProvider extends StateProviderNotifier {

// Store fees info
final profileDb = AppUserDataDatabase();
profileDb.saveUserFees(Tuple2<String, String>(feesBalance, feesLimit));
profileDb.saveUserFees(feesBalance, feesLimit);
}

final Profile newProfile = Profile(
Expand Down
65 changes: 31 additions & 34 deletions uni/lib/view/profile/widgets/account_info_card.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import 'package:uni/model/entities/reference.dart';
import 'package:uni/model/providers/profile_state_provider.dart';
Expand Down Expand Up @@ -50,37 +51,34 @@ class AccountInfoCard extends GenericCard {
Container(
margin: const EdgeInsets.only(
top: 8.0, bottom: 20.0, right: 30.0),
child: getInfoText(profile.feesLimit, context))
child: getInfoText(
profile.feesLimit != null
? DateFormat('yyyy-MM-dd')
.format(profile.feesLimit!)
: 'Sem data',
context))
]),
TableRow(children: [
Container(
margin:
const EdgeInsets.only(top: 8.0, bottom: 20.0, left: 20.0),
child: Text("Notificar próxima data limite: ",
style: Theme.of(context).textTheme.titleSmall)
),
margin: const EdgeInsets.only(
top: 8.0, bottom: 20.0, left: 20.0),
child: Text("Notificar próxima data limite: ",
style: Theme.of(context).textTheme.titleSmall)),
Container(
margin:
const EdgeInsets.only(top: 8.0, bottom: 20.0, left: 20.0),
child:
const TuitionNotificationSwitch()
)
margin: const EdgeInsets.only(
top: 8.0, bottom: 20.0, left: 20.0),
child: const TuitionNotificationSwitch())
])
]),
Container(
padding: const EdgeInsets.all(10),
child: Row(
children: <Widget>[
Text('Referências pendentes',
style: Theme.of(context).textTheme.titleLarge
?.apply(color: Theme.of(context).colorScheme.secondary)),
]
)
),
child: Row(children: <Widget>[
Text('Referências pendentes',
style: Theme.of(context).textTheme.titleLarge?.apply(
color: Theme.of(context).colorScheme.secondary)),
])),
ReferenceWidgets(references: references),
const SizedBox(
height: 10
),
const SizedBox(height: 10),
showLastRefreshedTime(profileStateProvider.feesRefreshTime, context)
]);
},
Expand All @@ -97,7 +95,8 @@ class AccountInfoCard extends GenericCard {
class ReferenceWidgets extends StatelessWidget {
final List<Reference> references;

const ReferenceWidgets({Key? key, required this.references}): super(key: key);
const ReferenceWidgets({Key? key, required this.references})
: super(key: key);

@override
Widget build(BuildContext context) {
Expand All @@ -114,16 +113,14 @@ class ReferenceWidgets extends StatelessWidget {
if (references.length == 1) {
return ReferenceSection(reference: references[0]);
}
return Column(
children: [
ReferenceSection(reference: references[0]),
const Divider(
thickness: 1,
indent: 30,
endIndent: 30,
),
ReferenceSection(reference: references[1]),
]
);
return Column(children: [
ReferenceSection(reference: references[0]),
const Divider(
thickness: 1,
indent: 30,
endIndent: 30,
),
ReferenceSection(reference: references[1]),
]);
}
}