@@ -2,65 +2,132 @@ package rahulstech.android.weatherapp.activity
2
2
3
3
import android.os.Bundle
4
4
import android.util.Log
5
+ import android.widget.ImageView
5
6
import android.widget.TextView
6
- import android.widget.Toast
7
7
import androidx.appcompat.app.AppCompatActivity
8
- import com.weather.api.WeatherClient
9
- import com.weather.api.model.CurrentWeatherReport
10
- import rahulstech.android.weatherapp.BuildConfig
8
+ import androidx.lifecycle.ViewModelProvider
11
9
import rahulstech.android.weatherapp.R
12
- import retrofit2.Call
13
- import retrofit2.Callback
14
- import retrofit2.Response
10
+ import rahulstech.android.weatherapp.viewmodel.HomeViewModel
11
+ import rahulstech.weather.repository.WeatherCondition
12
+ import rahulstech.weather.repository.WeatherForecast
13
+ import java.time.LocalDateTime
14
+ import java.time.LocalTime
15
+ import java.time.format.DateTimeFormatter
16
+ import java.util.Locale
15
17
16
18
class HomeActivity : AppCompatActivity () {
17
19
18
20
private val TAG = HomeActivity ::class .java.simpleName
19
21
20
- private var labelCity : TextView ? = null
22
+ private val WEATHER_DATETIME_FORMATTER = DateTimeFormatter .ofPattern( " EEE, dd MMMM hh:mm a " )
21
23
22
- private var labelTemperature: TextView ? = null
24
+ private val WEATHER_TIME_FORMATTER = DateTimeFormatter .ofPattern(" hh:mm a" )
25
+
26
+ private lateinit var viewModel: HomeViewModel
27
+
28
+ private lateinit var labelCity: TextView
29
+
30
+ private lateinit var labelTemperature: TextView
31
+
32
+ private lateinit var labelTempUnit: TextView
33
+
34
+ private lateinit var iconWeatherCondition: ImageView
35
+
36
+ private lateinit var uvIndex: TextView
37
+
38
+ private lateinit var precipitation: TextView
39
+
40
+ private lateinit var humidity: TextView
41
+
42
+ private lateinit var sunrise: TextView
43
+
44
+ private lateinit var sunset: TextView
45
+
46
+ private lateinit var weatherCondition: TextView
47
+
48
+ private lateinit var otherTemp: TextView
49
+
50
+ private lateinit var dateTime: TextView
23
51
24
52
override fun onCreate (savedInstanceState : Bundle ? ) {
25
53
super .onCreate(savedInstanceState)
26
54
setContentView(R .layout.activity_home)
27
55
28
56
labelCity = findViewById(R .id.label_city)
29
57
labelTemperature = findViewById(R .id.label_temperature)
58
+ labelTempUnit = findViewById(R .id.label_temp_unit)
59
+ iconWeatherCondition = findViewById(R .id.icon_weather_condition)
60
+ uvIndex = findViewById(R .id.uv_index)
61
+ precipitation = findViewById(R .id.precipitation)
62
+ humidity = findViewById(R .id.humidity)
63
+ sunrise = findViewById(R .id.sunrise)
64
+ sunset = findViewById(R .id.sunset)
65
+ weatherCondition = findViewById(R .id.weather_condition)
66
+ otherTemp = findViewById(R .id.other_temp)
67
+ dateTime = findViewById(R .id.dateTime)
68
+
69
+ viewModel = ViewModelProvider (this ).get(HomeViewModel ::class .java)
30
70
71
+ // Delhi: 1112321
72
+ // Kandi: 1118644
31
73
32
- WeatherClient (BuildConfig .WEATHER_API_KEY )
33
- .getWeatherService()?.getCurrentWeatherAsync(" London" )
34
- ?.enqueue(object : Callback <CurrentWeatherReport >{
35
- override fun onResponse (
36
- call : Call <CurrentWeatherReport >,
37
- res : Response <CurrentWeatherReport >
38
- ) {
39
- if (res.isSuccessful) {
40
- onCurrentWeatherReportFetched(res.body())
41
- }
42
- else {
43
- onRequestFail(res.code(), res.errorBody()?.string())
44
- }
45
- }
46
-
47
- override fun onFailure (call : Call <CurrentWeatherReport >, err : Throwable ) {
48
- Log .e(TAG , " current weather fetch error " , err)
49
- Toast .makeText(this @HomeActivity, " can not fetch current weather" , Toast .LENGTH_SHORT ).show()
50
- }
51
- })
74
+ viewModel.weatherToday.observe(this ) { onCurrentWeatherReportFetched(it) }
75
+ viewModel.setLocationId(" 1112321" )
52
76
}
53
77
54
- private fun onCurrentWeatherReportFetched (report : CurrentWeatherReport ? ) {
55
- val city = report?.location?.name
56
- val tempC = report?.current?.temp_c
78
+ private fun onCurrentWeatherReportFetched (report : WeatherForecast ? ) {
79
+
80
+ if (null == report) {
81
+ return
82
+ }
83
+
84
+ val now = LocalTime .now()
57
85
58
- Log .i(TAG ," city=$city tempC=$tempC " )
86
+ val city = report.city
87
+ val day = report.day
88
+ val hours = report.hours
89
+ val current = hours.find { it.datetime.hour == now.hour }
59
90
60
- labelCity?.text = city
61
- labelTemperature?.text = tempC.toString()
91
+ labelCity.text = " ${city.name} , ${city.region} "
92
+ dateTime.text = LocalDateTime .now().format(WEATHER_DATETIME_FORMATTER )
93
+ sunrise.text = day.sunrise.format(WEATHER_TIME_FORMATTER )
94
+ sunset.text = day.sunset.format(WEATHER_TIME_FORMATTER )
95
+ otherTemp.text = resources.getString(R .string.text_other_temp_c, day.maxTemp, day.minTemp, current?.feelsLike)
96
+
97
+ current?.let {
98
+ labelTemperature.text = String .format(Locale .ENGLISH , " %.2f" , it.temp)
99
+ labelTempUnit.text = " °C"
100
+ uvIndex.text = getUvLabel(it.uv)
101
+ precipitation.text = String .format(Locale .ENGLISH , " %.2f%%" , it.precipitation * 100 )
102
+ humidity.text = String .format(Locale .ENGLISH , " %.2f%%" , it.humidity)
103
+
104
+ updateWeatherCondition(current.condition, current.isDay)
105
+ }
62
106
}
63
107
108
+ private fun updateWeatherCondition (wc : WeatherCondition , isDay : Boolean ) {
109
+ weatherCondition.text = wc.name
110
+ iconWeatherCondition.setImageResource(when (wc) {
111
+ WeatherCondition .Fine -> if (isDay) R .drawable.sun else R .drawable.moon
112
+ WeatherCondition .Fog -> R .drawable.fog
113
+ WeatherCondition .Mist -> R .drawable.mist
114
+ WeatherCondition .Cloudy -> R .drawable.cloud
115
+ WeatherCondition .Rainy -> R .drawable.rain
116
+ WeatherCondition .Thunder -> R .drawable.thunder
117
+ WeatherCondition .Snow -> R .drawable.snowfall
118
+ WeatherCondition .Sleet -> R .drawable.sleet
119
+ WeatherCondition .Blizzard -> R .drawable.blizzard
120
+ })
121
+ }
122
+
123
+ private fun getUvLabel (uv : Float ): String = resources.getString( when {
124
+ uv <= 2 -> R .string.text_uv_low
125
+ uv <= 5 -> R .string.text_uv_moderate
126
+ uv <= 7 -> R .string.text_uv_high
127
+ uv <= 10 -> R .string.text_uv_very_high
128
+ else -> R .string.text_uv_extream
129
+ })
130
+
64
131
private fun onRequestFail (resCode : Int , body : String? ) {
65
132
Log .e(TAG , " request failed with res-code=$resCode err-body: $body " )
66
133
}
0 commit comments