Fix race condition widget refactor 6966862986207730827#116
Fix race condition widget refactor 6966862986207730827#116harrydbarnes wants to merge 16 commits intomainfrom
Conversation
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
- Implemented permission result handling in MainActivity to refresh data or show feedback. - Replaced hardcoded SimpleDateFormat with DateUtils in CalendarCard to respect user time format preference. - Added TODO in MainViewModel regarding hardcoded news category. - Replaced e.printStackTrace() with Log.e() in repositories for better error tracking. - Added TAG constants to repositories.
- Implemented permission result handling in MainActivity. - Replaced hardcoded date format with DateUtils in CalendarCard. - Re-implemented dynamic news category selection in MainViewModel. - Added isLoading state to MainViewModel. - Replaced e.printStackTrace() with Log.e() in repositories. - Updated repositories to return Boolean success/failure status. - Implemented transactional replaceAll in ArticleDao/NewsRepository to prevent data loss. - Implemented retry logic in WidgetUpdateWorker based on refresh success. - Created missing colors.xml to fix build failure. - Implemented settings navigation in HomeScreen.
- Implemented permission result handling in MainActivity. - Replaced hardcoded date format with DateUtils in CalendarCard. - Re-implemented dynamic news category selection in MainViewModel. - Added isLoading state to MainViewModel. - Fixed combine function signature in MainViewModel to handle 8 flows. - Replaced e.printStackTrace() with Log.e() in repositories. - Updated repositories to return Boolean success/failure status. - Implemented transactional replaceAll in ArticleDao/NewsRepository to prevent data loss. - Implemented retry logic in WidgetUpdateWorker based on refresh success. - Created missing colors.xml to fix build failure. - Implemented settings navigation in HomeScreen.
…Repository.kt Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
- Implemented permission result handling in MainActivity. - Replaced hardcoded date format with DateUtils in CalendarCard. - Re-implemented dynamic news category selection in MainViewModel. - Added isLoading state to MainViewModel. - Fixed combine function signature in MainViewModel to handle 8 flows. - Replaced e.printStackTrace() with Log.e() in repositories. - Updated repositories to return Boolean success/failure status. - Implemented transactional replaceAll in ArticleDao/NewsRepository to prevent data loss. - Implemented retry logic in WidgetUpdateWorker based on refresh success. - Created missing colors.xml to fix build failure. - Implemented settings navigation in HomeScreen. - Fixed syntax error in CalendarRepository.
- Implemented permission result handling in MainActivity. - Replaced hardcoded date format with DateUtils in CalendarCard. - Re-implemented dynamic news category selection in MainViewModel. - Added isLoading state to MainViewModel. - Refactored MainViewModel combine logic to use chained calls for type safety. - Replaced e.printStackTrace() with Log.e() in repositories and DayAheadWidget. - Updated repositories to return Boolean success/failure status. - Implemented transactional replaceAll in ArticleDao/NewsRepository. - Implemented retry logic in WidgetUpdateWorker. - Created missing colors.xml to fix build failure. - Implemented settings navigation in HomeScreen. - Fixed CalendarRepository syntax error and extracted magic number. - Added @JvmStatic to AppUtils for Java interop. - Extracted Hilt versions in build.gradle. - Added null/blank title filtering in NewsRepository.
- Implemented permission result handling in MainActivity. - Replaced hardcoded date format with DateUtils in CalendarCard. - Re-implemented dynamic news category selection in MainViewModel. - Added isLoading state to MainViewModel. - Refactored MainViewModel combine logic to use chained calls for type safety. - Replaced e.printStackTrace() with Log.e() in repositories and DayAheadWidget. - Updated repositories to return Boolean success/failure status. - Implemented transactional replaceAll in ArticleDao/NewsRepository. - Implemented retry logic in WidgetUpdateWorker. - Created missing colors.xml to fix build failure. - Implemented settings navigation in HomeScreen. - Fixed CalendarRepository syntax error and extracted magic number. - Added @JvmStatic to AppUtils for Java interop. - Extracted Hilt versions in build.gradle. - Added null/blank title filtering in NewsRepository. - Implemented location fetching in MainActivity and updated MainViewModel. - Updated Geocoder usage for API 33+.
- Updated `fetchLocationName` in `MainViewModel` to use `suspendCancellableCoroutine` for the Tiramisu `Geocoder` API, preventing a race condition where the coroutine scope would close before the callback fired. - Refactored `DayAheadWidget` to inject `WeatherRepository` instead of `WeatherDao` and `Gson`. - Simplified `DayAheadWidget.onUpdate` to fetch parsed weather data directly from the repository flow, removing data access and parsing logic from the UI component.
Summary of ChangesHello @harrydbarnes, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request undertakes a comprehensive modernization of the application's architecture and user interface. It transitions the UI from legacy Android Views to the declarative Jetpack Compose framework, establishes a robust and reactive data layer using Room for persistence and Kotlin Flow for data streams, and integrates Dagger Hilt for efficient dependency management. A key aspect of this refactor is addressing a race condition in the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This is an impressive and extensive refactoring of the application from a traditional View-based architecture to a modern one using Jetpack Compose, Hilt, Room, and Kotlin Coroutines/Flows. The new architecture is much cleaner, more maintainable, and follows current Android best practices. The use of the repository pattern, a reactive UI layer with StateFlow, and dependency injection is excellent.
I've identified a potential bug in the geocoder implementation that could cause a coroutine to hang, and I've also provided a couple of suggestions for architectural and UX improvements. Overall, this is a fantastic update that significantly improves the quality of the codebase.
| suspendCancellableCoroutine { cont -> | ||
| geocoder.getFromLocation(lat, lon, 1) { addresses -> | ||
| if (addresses.isNotEmpty()) { | ||
| val address = addresses[0] | ||
| val name = address.locality ?: address.subAdminArea ?: "Unknown Location" | ||
| _locationName.value = name | ||
| } | ||
| if (cont.isActive) { | ||
| cont.resume(Unit) | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
The suspendCancellableCoroutine block for fetching the location name on Android Tiramisu (API 33+) and above does not handle the error case for the geocoder.getFromLocation callback. If the geocoding service returns an error, the onError part of the listener is not implemented, and the coroutine may never resume, causing it to hang indefinitely. You should implement the onError callback to handle failures gracefully by using the full GeocodeListener interface.
suspendCancellableCoroutine { cont ->
val listener = object : Geocoder.GeocodeListener {
override fun onGeocode(addresses: MutableList<Address>) {
if (addresses.isNotEmpty()) {
val address = addresses[0]
val name = address.locality ?: address.subAdminArea ?: "Unknown Location"
_locationName.value = name
}
if (cont.isActive) cont.resume(Unit)
}
override fun onError(errorMessage: String?) {
if (cont.isActive) cont.resume(Unit) // Resume even on error
}
}
geocoder.getFromLocation(lat, lon, 1, listener)
}| private val prefs = context.getSharedPreferences(AppConstants.PREFS_NAME, Context.MODE_PRIVATE) | ||
| private val _tempUnit = MutableStateFlow(prefs.getString(AppConstants.KEY_TEMP_UNIT, AppConstants.DEFAULT_TEMP_UNIT) ?: "celsius") | ||
| private val _userName = MutableStateFlow(prefs.getString(AppConstants.KEY_USER_NAME, "User") ?: "User") |
There was a problem hiding this comment.
The ViewModel is directly accessing SharedPreferences. To improve separation of concerns, testability, and align with the repository pattern used elsewhere in the app, consider abstracting SharedPreferences access into a dedicated repository (e.g., UserPreferencesRepository). This repository could expose user preferences like temperature unit and user name as a Flow, which can then be combined with other data sources in the ViewModel.
| onClick = { | ||
| val intent = Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply { | ||
| data = Uri.fromParts("package", context.packageName, null) | ||
| } | ||
| context.startActivity(intent) | ||
| } |
There was a problem hiding this comment.
The onClick handler for the WeatherCard currently opens the application's details page in the system settings. This is an unconventional user experience. Typically, a user might expect this action to either refresh the weather data or navigate to a more detailed forecast screen. The previous version of the app navigated to a dedicated SettingsActivity. Please consider changing this to a more intuitive action, such as navigating to the app's settings screen.
onClick = {
// For example, navigate to the app's settings screen.
// context.startActivity(Intent(context, SettingsActivity::class.java))
}
No description provided.