-
Notifications
You must be signed in to change notification settings - Fork 1
Fix: Duplicate weather API call during startup and refresh #182
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,25 +2,38 @@ package bose.ankush.network.repository | |||||||||||||||||||
|
|
||||||||||||||||||||
| import bose.ankush.network.api.WeatherApiService | ||||||||||||||||||||
| import bose.ankush.network.common.NetworkConnectivity | ||||||||||||||||||||
| import bose.ankush.network.common.NetworkException | ||||||||||||||||||||
| import bose.ankush.network.model.WeatherForecast | ||||||||||||||||||||
| import bose.ankush.network.utils.NetworkUtils | ||||||||||||||||||||
|
|
||||||||||||||||||||
| class WeatherRepositoryImpl( | ||||||||||||||||||||
| private val apiService: WeatherApiService, | ||||||||||||||||||||
| private val networkConnectivity: NetworkConnectivity, | ||||||||||||||||||||
| ) : WeatherRepository { | ||||||||||||||||||||
| override suspend fun refreshWeatherData(coordinates: Pair<Double, Double>): WeatherForecast? { | ||||||||||||||||||||
| if (!networkConnectivity.isNetworkAvailable()) return null | ||||||||||||||||||||
| override suspend fun refreshWeatherData( | ||||||||||||||||||||
| coordinates: Pair<Double, Double>, | ||||||||||||||||||||
| ): Result<WeatherForecast?> { | ||||||||||||||||||||
| if (!networkConnectivity.isNetworkAvailable()) { | ||||||||||||||||||||
| return Result.failure( | ||||||||||||||||||||
| NetworkException( | ||||||||||||||||||||
| NetworkException.NETWORK_UNAVAILABLE, | ||||||||||||||||||||
| "No internet connection available", | ||||||||||||||||||||
| ), | ||||||||||||||||||||
| ) | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return try { | ||||||||||||||||||||
| NetworkUtils.retryWithExponentialBackoff { | ||||||||||||||||||||
| apiService.getOneCallWeather( | ||||||||||||||||||||
| coordinates.first.toString(), | ||||||||||||||||||||
| coordinates.second.toString(), | ||||||||||||||||||||
| ) | ||||||||||||||||||||
| } | ||||||||||||||||||||
| Result.success( | ||||||||||||||||||||
| NetworkUtils.retryWithExponentialBackoff { | ||||||||||||||||||||
| apiService.getOneCallWeather( | ||||||||||||||||||||
| coordinates.first.toString(), | ||||||||||||||||||||
| coordinates.second.toString(), | ||||||||||||||||||||
| ) | ||||||||||||||||||||
| }, | ||||||||||||||||||||
| ) | ||||||||||||||||||||
| } catch (e: Exception) { | ||||||||||||||||||||
| throw Exception("Network | Failed to refresh weather data: ${e.message}", e) | ||||||||||||||||||||
| // retryWithExponentialBackoff already normalizes failures to NetworkException. | ||||||||||||||||||||
| Result.failure(e) | ||||||||||||||||||||
|
Comment on lines
34
to
+36
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Description: Check what exceptions retryWithExponentialBackoff throws and whether it handles CancellationException
# Find the implementation of retryWithExponentialBackoff
ast-grep --pattern $'suspend fun retryWithExponentialBackoff($$$) {
$$$
}'
# Also search for any CancellationException handling
rg -n -C5 'CancellationException' --type=kotlinRepository: bosankus/Compose-Weatherify Length of output: 25115 🏁 Script executed: # Find retryWithExponentialBackoff function definition
rg -n "retryWithExponentialBackoff" --type=kotlin -B2 -A10Repository: bosankus/Compose-Weatherify Length of output: 10782 🏁 Script executed: # View the full WeatherRepositoryImpl.kt file to see lines 16-37
cat -n network/src/commonMain/kotlin/bose/ankush/network/repository/WeatherRepositoryImpl.ktRepository: bosankus/Compose-Weatherify Length of output: 1693 🏁 Script executed: # View the complete NetworkUtils.kt file
cat -n network/src/commonMain/kotlin/bose/ankush/network/utils/NetworkUtils.ktRepository: bosankus/Compose-Weatherify Length of output: 1552 Catching The broad 🔧 Proposed fix } catch (e: Exception) {
+ if (e is kotlinx.coroutines.CancellationException) throw e
// retryWithExponentialBackoff already normalizes failures to NetworkException.
Result.failure(e)
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty
onFailuresilently swallows errors.The failure branch discards the exception without logging or propagating it. This means:
At minimum, log the error. Consider also propagating to the UI if the method signature allows.
🔧 Proposed fix to add logging
onFailure = { - // Log error or pass it to UI + // TODO: Consider propagating to UI for user feedback + Log.e("WeatherRepository", "Failed to refresh weather data", it) }📝 Committable suggestion
🤖 Prompt for AI Agents