Skip to content

Commit 35d381b

Browse files
committed
completed showing weather data of hardcoded city
1 parent 49bc9ce commit 35d381b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1268
-198
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,14 @@ android {
4848

4949
dependencies {
5050

51-
implementation project(':backend:weather-api')
51+
implementation project(':backend:weather-repository')
5252

5353
implementation libs.androidx.core.ktx
5454
implementation libs.androidx.appcompat
5555
implementation libs.material
5656
implementation libs.androidx.activity
5757
implementation libs.androidx.constraintlayout
58+
implementation libs.androidx.lifecycle.viewmode.ktx
5859
testImplementation libs.junit
5960
androidTestImplementation libs.androidx.junit
6061
androidTestImplementation libs.androidx.espresso.core

app/src/main/java/rahulstech/android/weatherapp/activity/HomeActivity.kt

Lines changed: 102 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,132 @@ package rahulstech.android.weatherapp.activity
22

33
import android.os.Bundle
44
import android.util.Log
5+
import android.widget.ImageView
56
import android.widget.TextView
6-
import android.widget.Toast
77
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
119
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
1517

1618
class HomeActivity : AppCompatActivity() {
1719

1820
private val TAG = HomeActivity::class.java.simpleName
1921

20-
private var labelCity: TextView? = null
22+
private val WEATHER_DATETIME_FORMATTER = DateTimeFormatter.ofPattern("EEE, dd MMMM hh:mm a")
2123

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
2351

2452
override fun onCreate(savedInstanceState: Bundle?) {
2553
super.onCreate(savedInstanceState)
2654
setContentView(R.layout.activity_home)
2755

2856
labelCity = findViewById(R.id.label_city)
2957
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)
3070

71+
// Delhi: 1112321
72+
// Kandi: 1118644
3173

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")
5276
}
5377

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()
5785

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 }
5990

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+
}
62106
}
63107

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+
64131
private fun onRequestFail(resCode: Int, body: String?) {
65132
Log.e(TAG, "request failed with res-code=$resCode err-body: $body")
66133
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package rahulstech.android.weatherapp.viewmodel
2+
3+
import android.app.Application
4+
import androidx.lifecycle.AndroidViewModel
5+
import androidx.lifecycle.LiveData
6+
import androidx.lifecycle.MutableLiveData
7+
import androidx.lifecycle.switchMap
8+
import rahulstech.android.weatherapp.BuildConfig
9+
import rahulstech.weather.repository.WeatherForecast
10+
import rahulstech.weather.repository.WeatherRepository
11+
12+
class HomeViewModel(application: Application) : AndroidViewModel(application) {
13+
14+
private val repository by lazy { WeatherRepository(BuildConfig.WEATHER_API_KEY) }
15+
16+
private val locationIdLiveData = MutableLiveData<String>()
17+
18+
val weatherToday: LiveData<WeatherForecast?> = locationIdLiveData.switchMap {
19+
repository.getWeatherToday(it)
20+
}
21+
22+
fun setLocationId(locationId: String) {
23+
locationIdLiveData.value = locationId
24+
}
25+
}

0 commit comments

Comments
 (0)