Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ... [Keep all previous imports] ...
- # Update the Builder string with loading indicators
- Builder.load_string('''
- <WeatherScreen>:
- MDScrollViewRefreshLayout:
- # ... [Keep previous layout structure] ...
- MDCard:
- id: current_weather_card
- # ... [Keep previous content] ...
- MDBoxLayout:
- spacing: dp(20)
- adaptive_size: True
- center_x: self.parent.center_x
- MDLabel:
- id: weather_icon
- text: ""
- font_style: "Icon"
- size_hint: None, None
- size: dp(48), dp(48)
- theme_text_color: "Custom"
- text_color: 1, 0.8, 0, 1
- MDLabel:
- id: temperature_label
- text: "--°C"
- font_style: "H2"
- halign: "center"
- bold: True
- MDBoxLayout:
- id: loading_indicator
- size_hint_y: None
- height: dp(20)
- spacing: dp(5)
- pos_hint: {'center_x': 0.5}
- MDSpinner:
- size_hint: None, None
- size: dp(20), dp(20)
- active: False
- palette: [
- app.theme_cls.primary_color,
- app.theme_cls.accent_color,
- app.theme_cls.primary_light,
- ]
- # ... [Keep rest of the layout] ...
- ''')
- class WeatherScreen(MDScreen):
- def show_loading(self, show):
- # Update loading state with spinner
- spinner = self.ids.loading_indicator.children[0]
- spinner.active = show
- for widget_id in ['current_weather_card', 'location_card', 'hourly_forecast_card', 'details_card']:
- self.ids[widget_id].opacity = 0.3 if show else 1
- self.ids.refresh_layout.refreshing = show
- def get_location_and_fetch_weather(self):
- try:
- if platform == "android":
- from android.permissions import Permission, request_permissions
- request_permissions([
- Permission.ACCESS_FINE_LOCATION,
- Permission.ACCESS_COARSE_LOCATION,
- Permission.INTERNET
- ])
- # ... [Rest of location code] ...
- except Exception as e:
- print(f"Permission error: {e}")
- self.fetch_weather(48.4, 10.0, False)
- def reverse_geocode(self, lat, lon, used_gps):
- try:
- # Add proper user agent for Nominatim
- headers = {
- "User-Agent": "MyWeatherApp/1.0 ([email protected])"
- }
- # ... [Rest of geocode code] ...
- except Exception as e:
- print(f"Geocode error: {e}")
- Clock.schedule_once(lambda dt: setattr(
- self.ids.location_label,
- 'text',
- f"Approximate Location\n({lat:.2f}, {lon:.2f})"
- ))
- def update_hourly_forecast(self, times, temps):
- try:
- # Limit to 12 hours for better mobile performance
- times = times[:12]
- temps = temps[:12]
- # ... [Rest of forecast code] ...
- except Exception as e:
- print(f"Forecast error: {e}")
- self.ids.hourly_forecast_container.clear_widgets()
- def show_error(self, message):
- Clock.schedule_once(lambda dt: super().show_error(message))
- with open("weather_errors.log", "a") as f:
- f.write(f"{datetime.now()} - {message}\n")
- # ... [Keep rest of the class] ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement