Skip to content

Commit

Permalink
time is calculated using timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
SidharthMudgil committed Aug 9, 2023
1 parent 2130678 commit 304dc33
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@ import com.sidharth.mosam.util.DrawableUtils
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import java.util.TimeZone

object WeatherResponseMapper {
fun mapWeatherResponseToWeatherData(response: WeatherResponse): WeatherData {
val background = DrawableUtils.getBackgroundBasedOnTime(response.current.dt)
val currentWeather = mapCurrentWeather(response.current)
val dailyWeatherList = mapDailyWeather(response.daily)
val background = DrawableUtils.getBackgroundBasedOnTime(response.current.dt, response.timezone)
val currentWeather = mapCurrentWeather(response.current, response.timezone)
val dailyWeatherList = mapDailyWeather(response.daily, response.timezone)
return WeatherData(background, currentWeather, dailyWeatherList)
}

private fun mapCurrentWeather(current: CurrentWeather): Weather {
private fun mapCurrentWeather(current: CurrentWeather, timezone: String): Weather {
val weatherDescription = current.weather.firstOrNull()?.description.orEmpty()
return Weather(
sunrise = current.sunrise.toHMM(),
sunset = current.sunset.toHMM(),
sunrise = current.sunrise.toHMM(timezone),
sunset = current.sunset.toHMM(timezone),
temperature = current.temp,
feelsLike = current.feelsLike,
pressure = current.pressure,
Expand All @@ -36,20 +37,22 @@ object WeatherResponseMapper {
)
}

private fun Long.toHMM(): String {
private fun Long.toHMM(timezone: String): String {
val dateFormat = SimpleDateFormat("HH:mm", Locale.getDefault())
dateFormat.timeZone = TimeZone.getTimeZone(timezone)
return dateFormat.format(Date(this * 1000))
}

private fun Long.toEEE(): String {
private fun Long.toEEE(timezone: String): String {
val dateFormat = SimpleDateFormat("EEE", Locale.getDefault())
dateFormat.timeZone = TimeZone.getTimeZone(timezone)
return dateFormat.format(Date(this * 1000))
}

private fun mapDailyWeather(daily: List<DailyWeather>): List<DailyForecast> {
private fun mapDailyWeather(daily: List<DailyWeather>, timezone: String): List<DailyForecast> {
return daily.map {
DailyForecast(
day = it.dt.toEEE(),
day = it.dt.toEEE(timezone),
temp = it.temp.day,
icon = DrawableUtils.getIconForWeather(it.weather.firstOrNull()?.main.orEmpty())
)
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/sidharth/mosam/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ class MainActivity : AppCompatActivity() {
observeBindWeatherData()
}

private fun initDependencyInjection() {
BaseApplication.instance.appComponent.inject(this)
}

private fun getWeatherData() {
if (ActivityCompat.checkSelfPermission(
this,
Expand All @@ -95,10 +99,6 @@ class MainActivity : AppCompatActivity() {
}
}

private fun initDependencyInjection() {
BaseApplication.instance.appComponent.inject(this)
}

private fun setupNetworkCallback() {
NetworkUtils.startNetworkCallback(
context = applicationContext,
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/sidharth/mosam/util/DrawableUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import androidx.annotation.DrawableRes
import com.sidharth.mosam.R
import java.util.Calendar
import java.util.Locale
import java.util.TimeZone

object DrawableUtils {
@DrawableRes
fun getBackgroundBasedOnTime(timestamp: Long): Int {
fun getBackgroundBasedOnTime(timestamp: Long, timezone: String): Int {
val calendar = Calendar.getInstance()
calendar.timeInMillis = timestamp * 1000
calendar.timeZone = TimeZone.getTimeZone(timezone)
return when (calendar.get(Calendar.HOUR_OF_DAY)) {
in 5..10 -> R.drawable.bg_morning
in 11..17 -> R.drawable.bg_day
Expand Down

0 comments on commit 304dc33

Please sign in to comment.