@@ -14,6 +14,7 @@ import java.text.SimpleDateFormat
14
14
import java.util.Date
15
15
import java.util.Locale
16
16
import java.util.concurrent.locks.Condition
17
+ import java.util.Calendar
17
18
18
19
//
19
20
@@ -54,23 +55,28 @@ class MainActivity : AppCompatActivity() {
54
55
.baseUrl(" https://api.openweathermap.org/data/2.5/" )
55
56
.build()
56
57
.create(ApiInterface ::class .java)
57
- val response = retrofit.getWeatherData(cityName , " 3be41e2a359cb318dc3b06d237a9e1ee" , " metric" )
58
- response.enqueue(object : Callback <WeatherApp >{
58
+ val response = retrofit.getWeatherData(cityName, " 3be41e2a359cb318dc3b06d237a9e1ee" , " metric" )
59
+ response.enqueue(object : Callback <WeatherApp > {
59
60
override fun onResponse (call : Call <WeatherApp >, response : Response <WeatherApp >) {
60
61
if (response.isSuccessful) {
61
62
val responseBody = response.body()
62
63
if (responseBody != null ) {
64
+
65
+ val sunRise = responseBody.sys.sunrise.toLong()
66
+ val sunSet = responseBody.sys.sunset.toLong()
67
+ val condition = responseBody.weather.firstOrNull()?.main ? : " unknown"
68
+ changesImagesAcoddingToWearther(condition, sunRise, sunSet)
69
+
70
+ // Now, you can use these variables throughout the function
63
71
val temperature = responseBody.main.temp.toString()
64
72
val humidity = responseBody.main.humidity
65
73
val windSpeed = responseBody.wind.speed
66
- val sunRise = responseBody.sys.sunrise.toLong()
67
74
val seaLevel = responseBody.main.pressure
68
- val condition = responseBody.weather.firstOrNull()?.main? : " unknown"
69
- val sunSet = responseBody.sys.sunset.toLong()
70
75
val maxTem = responseBody.main.temp_max
71
76
val minTem = responseBody.main.temp_min
72
77
73
78
79
+
74
80
Log .d(" WeatherApp" , " Temperature: $temperature " )
75
81
binding.temperature.text = " $temperature °C"
76
82
binding.weather.text = condition
@@ -86,7 +92,7 @@ class MainActivity : AppCompatActivity() {
86
92
binding.date.text= date()
87
93
binding.cityname.text = " $cityName "
88
94
89
- changesImagesAcoddingToWearther(condition)
95
+ changesImagesAcoddingToWearther(condition ? : " unknown " , sunRise, sunSet )
90
96
91
97
} else {
92
98
Log .e(" WeatherApp" , " Response body is null" )
@@ -104,51 +110,58 @@ class MainActivity : AppCompatActivity() {
104
110
})
105
111
}
106
112
107
- private fun changesImagesAcoddingToWearther (condition : String ) {
108
- when (condition){
113
+ private fun changesImagesAcoddingToWearther (condition : String , sunrise : Long , sunset : Long ) {
114
+ val currentTime = System .currentTimeMillis() / 1000 // Convert to seconds
115
+ val isNight = currentTime < sunrise || currentTime > sunset
109
116
110
- " Clear Sky" , " Sunny" , " Clear" -> {
117
+ when {
118
+ isNight -> {
119
+ binding.root.setBackgroundResource(R .drawable.night_scr)
120
+ binding.lottieAnimationView.setAnimation(R .raw.moon)
121
+ }
122
+ condition in arrayOf(" Clear Sky" , " Sunny" , " Clear" ) -> {
111
123
binding.root.setBackgroundResource(R .drawable.sunny_background)
112
124
binding.lottieAnimationView.setAnimation(R .raw.sun)
113
125
}
114
- // all are type of cloude , i cAN also use all in one
115
- " Clouds" -> {
126
+ condition == " Clouds" -> {
116
127
binding.root.setBackgroundResource(R .drawable.colud_background)
117
128
binding.lottieAnimationView.setAnimation(R .raw.cloud)
118
129
}
119
- " Haze" , " Mist" -> {
130
+ condition in arrayOf( " Haze" , " Mist" ) -> {
120
131
binding.root.setBackgroundResource(R .drawable.haze_scr)
121
132
binding.lottieAnimationView.setAnimation(R .raw.cloud)
122
133
}
123
- " Pertly Clouds" -> {
134
+ condition == " Pertly Clouds" -> {
124
135
binding.root.setBackgroundResource(R .drawable.pertly_clouds)
125
136
binding.lottieAnimationView.setAnimation(R .raw.cloud)
126
137
}
127
- " Overcast" -> {
138
+ condition == " Thunderstorm" -> {
139
+ binding.root.setBackgroundResource(R .drawable.thunderstormphoto)
140
+ binding.lottieAnimationView.setAnimation(R .raw.thunderstorm)
141
+ }
142
+ condition == " Overcast" -> {
128
143
binding.root.setBackgroundResource(R .drawable.overcast_clouds)
129
144
binding.lottieAnimationView.setAnimation(R .raw.cloud)
130
145
}
131
- " Foggy" -> {
146
+ condition == " Foggy" -> {
132
147
binding.root.setBackgroundResource(R .drawable.foggy_scr)
133
148
binding.lottieAnimationView.setAnimation(R .raw.cloud)
134
149
}
135
- // after that rain will start
136
-
137
-
138
- " Light Rain " , " Drizzle " , " Moderate Rain " , " Showers " , " Heavy Rain " , " Thunderstorm " -> {
150
+ condition in arrayOf(
151
+ " Light Rain " , " Drizzle " , " Moderate Rain " ,
152
+ " Showers " , " Heavy Rain " , " Thunderstorm "
153
+ ) -> {
139
154
binding.root.setBackgroundResource(R .drawable.rain_background)
140
155
binding.lottieAnimationView.setAnimation(R .raw.rain)
141
156
}
142
- " Light Snow" , " Moderate Snow" ," Heavy Snow" , " Blizzard" -> {
143
- binding.root.setBackgroundResource(R .drawable.rain_background)
144
- binding.lottieAnimationView.setAnimation(R .raw.rain)
145
-
157
+ condition in arrayOf(" Light Snow" , " Moderate Snow" , " Heavy Snow" , " Blizzard" ) -> {
158
+ binding.root.setBackgroundResource(R .drawable.snow_background)
159
+ binding.lottieAnimationView.setAnimation(R .raw.snow)
146
160
}
147
- else -> {
161
+ else -> {
148
162
binding.root.setBackgroundResource(R .drawable.sunny_background)
149
163
binding.lottieAnimationView.setAnimation(R .raw.sun)
150
164
}
151
-
152
165
}
153
166
binding.lottieAnimationView.playAnimation()
154
167
}
0 commit comments