Skip to content

Commit

Permalink
Fix dio log issue, add notification clearing on school change
Browse files Browse the repository at this point in the history
  • Loading branch information
adisve committed Oct 7, 2022
1 parent 60694aa commit 1e04cff
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
12 changes: 9 additions & 3 deletions lib/core/api/repository/backend_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@ class BackendRepository implements IBackendService {
ApiEndPoints.baseUrl,
'${ApiEndPoints.getOneSchedule}$scheduleId',
{
ApiEndPoints.school: school,
ApiEndPoints.school: school.toString(),
}.map((key, value) => MapEntry(key, value.toString())));

return await dioHandle
.getUri(uri, options: Options(validateStatus: (_) => true))
.then((response) {
return response.parseSchedule();
}).onError((error, stackTrace) {
log(name: 'backend_repository', error: error, '$error.message');
log(
name: 'backend_repository',
error: error,
'$error.message in [getRequestSchedule]');
return ApiScheduleOrProgrammeResponse.error('Timed out');
});
}
Expand All @@ -65,7 +68,10 @@ class BackendRepository implements IBackendService {
.then((response) {
return response.parsePrograms();
}).onError((error, stackTrace) {
log(name: 'backend_repository', error: error, '$error.message');
log(
name: 'backend_repository',
error: error,
'$error.message in [getPrograms]');
return ApiScheduleOrProgrammeResponse.error('Timed out');
});
}
Expand Down
37 changes: 25 additions & 12 deletions lib/core/api/repository/cache_and_interaction_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,55 @@ class CacheAndInteractionRepository implements ICacheAndInteractionService {
final _databaseService = getIt<DatabaseRepository>();

@override
Future<ApiScheduleOrProgrammeResponse> programSearchDispatcher(String searchQuery) async {
String defaultSchool = _preferenceService.getString(PreferenceTypes.school)!;
ApiScheduleOrProgrammeResponse response = await _backendService.getPrograms(searchQuery, defaultSchool);
Future<ApiScheduleOrProgrammeResponse> programSearchDispatcher(
String searchQuery) async {
String defaultSchool =
_preferenceService.getString(PreferenceTypes.school)!;
ApiScheduleOrProgrammeResponse response =
await _backendService.getPrograms(searchQuery, defaultSchool);
return response;
}

@override
Future<ApiScheduleOrProgrammeResponse> scheduleFetchDispatcher(scheduleId) async {
String defaultSchool = _preferenceService.getString(PreferenceTypes.school)!;
ApiScheduleOrProgrammeResponse response = await _backendService.getRequestSchedule(scheduleId, defaultSchool);
Future<ApiScheduleOrProgrammeResponse> scheduleFetchDispatcher(
scheduleId) async {
String defaultSchool =
_preferenceService.getString(PreferenceTypes.school)!;
ApiScheduleOrProgrammeResponse response =
await _backendService.getRequestSchedule(scheduleId, defaultSchool);
return response;
}

@override
Future<ApiScheduleOrProgrammeResponse> getCachedOrNewSchedule(String scheduleId) async {
Future<ApiScheduleOrProgrammeResponse> getCachedOrNewSchedule(
String scheduleId) async {
final bool bookmarksContainsThisScheduleId = _preferenceService
.getStringList(PreferenceTypes.bookmarks)!
.map((json) => bookmarkedScheduleModelFromJson(json).scheduleId)
.contains(scheduleId);

if (bookmarksContainsThisScheduleId) {
final ScheduleModel? userCachedSchedule = await _getCachedSchedule(scheduleId);
final ScheduleModel? userCachedSchedule =
await _getCachedSchedule(scheduleId);

/// If the schedule for some reason is not found in the database,
/// or if the schedule is more than 30 minutes old
if (userCachedSchedule == null ||
DateTime.now().subtract(TimeConstants.updateOffset).isAfter(userCachedSchedule.cachedAt.toLocal())) {
DateTime.now()
.subtract(TimeConstants.updateOffset)
.isAfter(userCachedSchedule.cachedAt.toLocal())) {
/// Make sure that only if the user has an internet connection and the
/// schedule is 'outdated', the app will display the new schedule.
/// Otherwise it returns [ApiResponse.cached(userCachedSchedule)]
ApiScheduleOrProgrammeResponse apiResponse = await scheduleFetchDispatcher(scheduleId);
ApiScheduleOrProgrammeResponse apiResponse =
await scheduleFetchDispatcher(scheduleId);
if (apiResponse.data != null) {
return apiResponse;
} else if (userCachedSchedule != null) {
return ApiScheduleOrProgrammeResponse.cached(userCachedSchedule);
}
return ApiScheduleOrProgrammeResponse.error(RuntimeErrorType.scheduleFetchError());
return ApiScheduleOrProgrammeResponse.error(
RuntimeErrorType.scheduleFetchError());
}

return ApiScheduleOrProgrammeResponse.cached(userCachedSchedule);
Expand All @@ -69,7 +81,8 @@ class CacheAndInteractionRepository implements ICacheAndInteractionService {
/// and a default schedule)
@override
Future<SharedPreferenceResponse> verifyDefaultSchoolExists() async {
final String? defaultSchool = _preferenceService.getString(PreferenceTypes.school);
final String? defaultSchool =
_preferenceService.getString(PreferenceTypes.school);

if (defaultSchool != null) {
return SharedPreferenceResponse.schoolAvailable(defaultSchool);
Expand Down
19 changes: 14 additions & 5 deletions lib/core/ui/init_cubit/init_cubit.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:tumble/core/api/repository/cache_and_interaction_repository.dart';
import 'package:tumble/core/api/repository/notification_repository.dart';
import 'package:tumble/core/database/database_response.dart';
import 'package:tumble/core/database/repository/database_repository.dart';
import 'package:tumble/core/shared/app_dependencies.dart';
Expand All @@ -9,23 +10,29 @@ import 'package:tumble/core/dependency_injection/get_it_instances.dart';
part 'init_state.dart';

class InitCubit extends Cubit<InitState> {
InitCubit() : super(const InitState(defaultSchool: null, status: InitStatus.NO_SCHOOL)) {
InitCubit()
: super(const InitState(
defaultSchool: null, status: InitStatus.NO_SCHOOL)) {
_init();
}

final _cacheAndInteractionService = getIt<CacheAndInteractionRepository>();
final _databaseService = getIt<DatabaseRepository>();
final _appDependencies = getIt<AppDependencies>();
final _notificationService = getIt<NotificationRepository>();

Future<void> _init() async {
SharedPreferenceResponse sharedPreferenceResponse = await _cacheAndInteractionService.verifyDefaultSchoolExists();
SharedPreferenceResponse sharedPreferenceResponse =
await _cacheAndInteractionService.verifyDefaultSchoolExists();
switch (sharedPreferenceResponse.status) {
case InitialStatus.NO_SCHOOL:
emit(const InitState(defaultSchool: null, status: InitStatus.NO_SCHOOL));
emit(
const InitState(defaultSchool: null, status: InitStatus.NO_SCHOOL));
break;
case InitialStatus.SCHOOL_AVAILABLE:
String defaultSchool = sharedPreferenceResponse.data;
emit(InitState(defaultSchool: defaultSchool, status: InitStatus.SCHOOL_AVAILABLE));
emit(InitState(
defaultSchool: defaultSchool, status: InitStatus.SCHOOL_AVAILABLE));
break;
}
}
Expand All @@ -38,11 +45,13 @@ class InitCubit extends Cubit<InitState> {
/// Clear local db
_databaseService.removeAll();
_databaseService.removeAllCachedCourseColors();
_notificationService.cancelAllNotifications();

emit(state.copyWith(defaultSchool: schoolName));
return;
} else if (state.status == InitStatus.NO_SCHOOL) {
emit(state.copyWith(defaultSchool: schoolName, status: InitStatus.SCHOOL_AVAILABLE));
emit(state.copyWith(
defaultSchool: schoolName, status: InitStatus.SCHOOL_AVAILABLE));
}
}
}

0 comments on commit 1e04cff

Please sign in to comment.