@@ -28,6 +28,7 @@ import androidx.navigation.fragment.findNavController
28
28
import com.github.droidworksstudio.common.capitalizeEachWord
29
29
import com.github.droidworksstudio.common.hasInternetPermission
30
30
import com.github.droidworksstudio.common.hideKeyboard
31
+ import com.github.droidworksstudio.common.showLongToast
31
32
import com.github.droidworksstudio.launcher.R
32
33
import com.github.droidworksstudio.launcher.databinding.FragmentWidgetsBinding
33
34
import com.github.droidworksstudio.launcher.helper.AppHelper
@@ -36,7 +37,9 @@ import com.github.droidworksstudio.launcher.listener.OnSwipeTouchListener
36
37
import com.github.droidworksstudio.launcher.listener.ScrollEventListener
37
38
import com.github.droidworksstudio.launcher.utils.Constants
38
39
import dagger.hilt.android.AndroidEntryPoint
40
+ import kotlinx.coroutines.DelicateCoroutinesApi
39
41
import kotlinx.coroutines.Dispatchers
42
+ import kotlinx.coroutines.GlobalScope
40
43
import kotlinx.coroutines.async
41
44
import kotlinx.coroutines.launch
42
45
import kotlinx.coroutines.withContext
@@ -120,6 +123,7 @@ class WidgetFragment : Fragment(),
120
123
121
124
}
122
125
126
+ @OptIn(DelicateCoroutinesApi ::class )
123
127
private fun setupWeatherWidget () {
124
128
val sharedPreferences =
125
129
context.getSharedPreferences(Constants .WEATHER_PREFS , Context .MODE_PRIVATE )
@@ -139,7 +143,9 @@ class WidgetFragment : Fragment(),
139
143
if (! showWeatherWidget || ! context.hasInternetPermission()) return @launch
140
144
141
145
try {
142
- val weatherDeferred = async { appHelper.fetchWeatherData(context, latitude, longitude) }
146
+ val weatherDeferred = GlobalScope .async {
147
+ appHelper.fetchWeatherData(context, latitude, longitude)
148
+ }
143
149
144
150
// Prepare UI elements concurrently
145
151
withContext(Dispatchers .Main ) {
@@ -163,40 +169,50 @@ class WidgetFragment : Fragment(),
163
169
}
164
170
}
165
171
166
- val weatherResponse = weatherDeferred.await()
167
- Log .d(" weatherResponse" , " $weatherResponse " )
172
+ val result = weatherDeferred.await()
173
+ Log .d(" weatherResponse" , " $result " )
168
174
169
- withContext(Dispatchers .Main ) {
170
- val timestamp = convertTimestampToReadableDate(weatherResponse.dt)
171
- binding.apply {
172
- weatherCity.text = getString(R .string.widget_weather_location, weatherResponse.name, weatherResponse.sys.country)
173
- weatherTemperature.text = getString(R .string.widget_weather_temp, weatherResponse.main.temp, temperatureScale)
174
- weatherDescription.text = getString(R .string.widget_weather_description, weatherResponse.weather[0 ].description).capitalizeEachWord()
175
- weatherWind.text = getString(R .string.widget_weather_wind, weatherResponse.wind.speed, speedScale)
176
- weatherHumidity.text = getString(R .string.widget_weather_humidity, weatherResponse.main.humidity)
177
- weatherLastRun.text = timestamp
178
- weatherRefresh.text = getString(R .string.widget_weather_refresh, getString(R .string.refresh_icon))
175
+ when (result) {
176
+ is AppHelper .WeatherResult .Success -> {
177
+ val weatherResponse = result.weatherResponse
179
178
180
- val weatherIconBitmap = createWeatherIcon(context, setWeatherIcon(context, weatherResponse.weather[0 ].id))
181
- weatherIcon.setImageBitmap(weatherIconBitmap) // Ensure this matches your ImageView ID
182
- weatherIcon.setColorFilter(widgetTextColor)
179
+ withContext(Dispatchers .Main ) {
180
+ val timestamp = convertTimestampToReadableDate(weatherResponse.dt)
181
+ binding.apply {
182
+ weatherCity.text = getString(R .string.widget_weather_location, weatherResponse.name, weatherResponse.sys.country)
183
+ weatherTemperature.text = getString(R .string.widget_weather_temp, weatherResponse.main.temp, temperatureScale)
184
+ weatherDescription.text = getString(R .string.widget_weather_description, weatherResponse.weather[0 ].description).capitalizeEachWord()
185
+ weatherWind.text = getString(R .string.widget_weather_wind, weatherResponse.wind.speed, speedScale)
186
+ weatherHumidity.text = getString(R .string.widget_weather_humidity, weatherResponse.main.humidity)
187
+ weatherLastRun.text = timestamp
188
+ weatherRefresh.text = getString(R .string.widget_weather_refresh, getString(R .string.refresh_icon))
183
189
184
- val sunriseIconBitmap = createSunIcon (context, getString( R .string.sunrise_icon ))
185
- sunriseIcon .setImageBitmap(sunriseIconBitmap)
186
- sunriseIcon .setColorFilter(widgetTextColor)
190
+ val weatherIconBitmap = createWeatherIcon (context, setWeatherIcon(context, weatherResponse.weather[ 0 ].id ))
191
+ weatherIcon .setImageBitmap(weatherIconBitmap) // Ensure this matches your ImageView ID
192
+ weatherIcon .setColorFilter(widgetTextColor)
187
193
188
- val sunsetIconBitmap = createSunIcon(context, getString(R .string.sunset_icon ))
189
- sunsetIcon .setImageBitmap(sunsetIconBitmap )
190
- sunsetIcon .setColorFilter(widgetTextColor)
194
+ val sunriseIconBitmap = createSunIcon(context, getString(R .string.sunrise_icon ))
195
+ sunriseIcon .setImageBitmap(sunriseIconBitmap )
196
+ sunriseIcon .setColorFilter(widgetTextColor)
191
197
192
- val sunriseTime = convertTimestampToReadableDate(weatherResponse.sys.sunrise)
193
- sunriseText.text = getString(R .string.widget_sunrise_time, sunriseTime)
198
+ val sunsetIconBitmap = createSunIcon(context, getString(R .string.sunset_icon))
199
+ sunsetIcon.setImageBitmap(sunsetIconBitmap)
200
+ sunsetIcon.setColorFilter(widgetTextColor)
194
201
195
- val sunsetTime = convertTimestampToReadableDate(weatherResponse.sys.sunset )
196
- sunsetText .text = getString(R .string.widget_sunset_time, sunsetTime )
202
+ val sunriseTime = convertTimestampToReadableDate(weatherResponse.sys.sunrise )
203
+ sunriseText .text = getString(R .string.widget_sunrise_time, sunriseTime )
197
204
205
+ val sunsetTime = convertTimestampToReadableDate(weatherResponse.sys.sunset)
206
+ sunsetText.text = getString(R .string.widget_sunset_time, sunsetTime)
207
+
208
+ weatherRoot.visibility = View .VISIBLE
209
+ }
210
+ }
211
+ }
198
212
199
- weatherRoot.visibility = View .VISIBLE
213
+ is AppHelper .WeatherResult .Failure -> {
214
+ val errorMessage = result.errorMessage
215
+ context.showLongToast(errorMessage)
200
216
}
201
217
}
202
218
} catch (e: Exception ) {
0 commit comments