Skip to content

Conversation

@danielramaa
Copy link
Member

@danielramaa danielramaa commented Nov 26, 2025

Closes #1714

Description

Fixed a critical bug where the app would crash or clear the user's cached data (Schedule, Exams, Profile) when refreshing without an internet connection.

Changes Implemented

  • Exam, Profile, & Lecture Providers: Implemented try-catch blocks to handle SocketException. If the network fails, the app now gracefully falls back to the local database instead of crashing or showing an empty list.
  • CachedAsyncNotifier: Fixed logic in _invalidLocalData to treat empty lists as valid cached data. This prevents the app from forcing a network refresh for students who genuinely have empty schedules/exams.

Steps to Replicate

  1. Open the app and ensure data (Schedule, Exams, Profile) is loaded.
  2. Turn off WiFi/Mobile Data (simulate offline mode).
  3. Pull-to-refresh on the Home, Schedule, and Exams pages.
  4. Result: The app now displays the cached data instead of showing an error or wiping the screen.

Review checklist

  • Terms and conditions reflect the changes

View Changes

  • Description has screenshots of the UI changes. (Logic fix only, no UI changes)
  • Tested both in light and dark mode.
  • New text is both in portuguese (PT) and english (EN). (No new text added)
  • Works in different text zoom levels.
  • Works in different screen sizes.

Performance

  • No helper functions to return widgets are added. New widgets are created instead.
  • Used ListView.builder for Long Lists.
  • Controllers (TextEditingController, ...) are beeing disposed of in dispose() method.

@codecov
Copy link

codecov bot commented Nov 26, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 60%. Comparing base (f4e5321) to head (d3eb8d7).
⚠️ Report is 12 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #1739   +/-   ##
=======================================
  Coverage       60%     60%           
=======================================
  Files            2       2           
  Lines           81      81           
=======================================
  Hits            48      48           
  Misses          33      33           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@pedroafmonteiro pedroafmonteiro requested a review from a team November 26, 2025 14:21
@HenriqueSFernandes
Copy link
Contributor

The lecture provider seems to be working, but the following ones return a ClientException when refreshing the page without internet:

  • Exames
  • Library Occupation
  • Restaurants

@danielramaa
Copy link
Member Author

Added the final offline caching fixes for:

  • Restaurants: Now loads cached menus if the network request fails.
  • Library Occupation: Now loads cached floor data if offline.

Fix for Faculty Locations (Map):
I also found and fixed a crash in faculty_locations_provider.dart. The loadFromRemote function was failing because it tried to access the cache state directly. It now correctly re-reads the location assets, solving the crash when refreshing the map view.

All offline scenarios (Exams, Profile, Schedule, Library, Restaurants, Map) are now fully covered and tested.

@DGoiana
Copy link
Collaborator

DGoiana commented Dec 2, 2025

👋 hey! why isn't this done at a higher level? it would cover every case and avoid so many lines added.

@DGoiana
Copy link
Collaborator

DGoiana commented Dec 2, 2025

Fixed logic in _invalidLocalData to treat empty lists as valid cached data. This prevents the app from forcing a network refresh for students who genuinely have empty schedules/exams

the problem here is not the empty list in the cached data. the problem is how can we differentiate a correct empty list (empty cache or empty remote response), from a list that is not loaded yet by remote

Copilot AI review requested due to automatic review settings December 3, 2025 16:57
@danielramaa
Copy link
Member Author

Refactored based on feedback to avoid code duplication (DRY):

  1. Moved Error Handling Upstream: The try-catch logic for offline fallback is now centralized in CachedAsyncNotifier inside the build() method. If loadFromRemote fails (e.g., offline), it falls back to localData if available.
  2. Cleaned Individual Providers: Removed the redundant try-catch blocks from Exam, Profile, Lecture, Restaurant, and Library providers. They now just handle fetching and saving.
  3. Reverted Logic: Reverted _invalidLocalData to the original implementation as requested. The new upstream error handling ensures that even if we attempt a fetch due to an empty list, the app won't crash if the network fails.

@danielramaa danielramaa requested review from a team and DGoiana December 3, 2025 17:03
@danielramaa danielramaa review requested due to automatic review settings December 3, 2025 17:03
Copy link
Member

@pedroafmonteiro pedroafmonteiro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice work on this, thanks!

Copilot AI review requested due to automatic review settings December 4, 2025 12:29
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request implements offline caching improvements to prevent the app from crashing when refreshing without an internet connection. The main changes introduce error handling with fallback to cached data when network requests fail.

Key Changes

  • Modified CachedAsyncNotifier to add try-catch blocks around remote data fetching, with fallback to local cached data when network requests fail
  • Removed error suppression in LectureNotifier to allow proper error handling by the base class
  • Fixed FacultyLocationsNotifier to properly fetch from assets instead of returning stale state

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
packages/uni_app/lib/model/providers/riverpod/cached_async_notifier.dart Added try-catch error handling with local data fallback for build() and refreshRemote() methods
packages/uni_app/lib/model/providers/riverpod/lecture_provider.dart Removed .catchError() to allow base class error handling
packages/uni_app/lib/model/providers/riverpod/exam_provider.dart Formatting cleanup (removed blank lines)
packages/uni_app/lib/model/providers/riverpod/profile_provider.dart Added clarifying comments for data fetching flow
packages/uni_app/lib/model/providers/riverpod/restaurant_provider.dart Added clarifying comments for data fetching flow
packages/uni_app/lib/model/providers/riverpod/library_occupation_provider.dart Added clarifying comments for data fetching flow
packages/uni_app/lib/model/providers/riverpod/faculty_locations_provider.dart Fixed to fetch from assets instead of returning cached state

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@pedroafmonteiro pedroafmonteiro requested a review from a team December 9, 2025 21:36
Copy link
Collaborator

@DGoiana DGoiana left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be aware of the empty list problem, as it can cause unexpected crashes. Come up with a good solution to that!

Copy link
Collaborator

@DGoiana DGoiana left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be aware of the empty list problem, as it can cause unexpected crashes. Come up with a good solution to that!

@danielramaa danielramaa merged commit 6c970af into develop Dec 17, 2025
6 checks passed
@danielramaa danielramaa deleted the fix/offline-caching-1714 branch December 17, 2025 14:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix caching when user does not have an Internet connection

5 participants