From 8f7c31f5359ad70405dfc9810fde084a2b7ba148 Mon Sep 17 00:00:00 2001 From: 1+1=2 Date: Fri, 30 Jun 2023 09:48:02 -0700 Subject: [PATCH] Clean code MVI completed Bugs fixed and data is showing. That should be all for this feature. --- .../data/mappers/WeatherMappers.kt | 29 ++++++++++--------- .../data/remote/CurrentWeather.kt | 10 ------- .../data/remote/Hourly.kt | 18 ++++++++++-- .../data/remote/HourlyUnits.kt | 18 ++++++++++-- .../data/remote/WeatherDataDto.kt | 17 ----------- .../data/remote/WeatherDto.kt | 8 ----- .../data/remote/WeatherResponse.kt | 22 ++++++++++---- .../clean_code_with_mvi_mvvm/di/AppModule.kt | 8 ++++- .../presentation/WeatherCard.kt | 1 + 9 files changed, 71 insertions(+), 60 deletions(-) delete mode 100644 app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/remote/CurrentWeather.kt delete mode 100644 app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/remote/WeatherDataDto.kt delete mode 100644 app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/remote/WeatherDto.kt diff --git a/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/mappers/WeatherMappers.kt b/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/mappers/WeatherMappers.kt index 0ba62c3f..bff0741a 100644 --- a/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/mappers/WeatherMappers.kt +++ b/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/mappers/WeatherMappers.kt @@ -1,10 +1,9 @@ package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.mappers +import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.remote.WeatherResponse import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.domain.weather.WeatherData import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.domain.weather.WeatherInfo import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.domain.weather.WeatherType -import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.remote.WeatherDataDto -import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.remote.WeatherDto import java.time.LocalDateTime import java.time.format.DateTimeFormatter @@ -13,13 +12,14 @@ private data class IndexedWeatherData( val data: WeatherData ) -fun WeatherDataDto.toWeatherDataMap(): Map> { - return time.mapIndexed { index, time -> - val temperature = temperatures[index] - val weatherCode = weatherCodes[index] - val windSpeed = windSpeeds[index] - val pressure = pressures[index] - val humidity = humidities[index] +fun WeatherResponse.toWeatherDataMap(): Map> = hourly.run { + time.mapIndexed { index, time -> + val temperature = temperature2m[index] + val weatherCode = weathercode[index] + val windSpeed = windspeed10m[index] + val pressure = pressureMsl[index] + val humidity = relativehumidity2m[index] + IndexedWeatherData( index = index, data = WeatherData( @@ -27,24 +27,27 @@ fun WeatherDataDto.toWeatherDataMap(): Map> { temperatureCelsius = temperature, pressure = pressure, windSpeed = windSpeed, - humidity = humidity, + humidity = humidity.toDouble(), weatherType = WeatherType.fromWMO(weatherCode) ) ) }.groupBy { it.index / 24 }.mapValues { - it.value.map { it.data } + it.value.map { x -> x.data } } } -fun WeatherDto.toWeatherInfo(): WeatherInfo { - val weatherDataMap = weatherData.toWeatherDataMap() +fun WeatherResponse.toWeatherInfo(): WeatherInfo { + val weatherDataMap = this.toWeatherDataMap() val now = LocalDateTime.now() + + // This is basically rounding the current time to nearest hour val currentWeatherData = weatherDataMap[0]?.find { val hour = if (now.minute < 30) now.hour else now.hour + 1 it.time.hour == hour } + return WeatherInfo( weatherDataPerDay = weatherDataMap, currentWeatherData = currentWeatherData diff --git a/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/remote/CurrentWeather.kt b/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/remote/CurrentWeather.kt deleted file mode 100644 index 589d5677..00000000 --- a/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/remote/CurrentWeather.kt +++ /dev/null @@ -1,10 +0,0 @@ -package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.remote - -data class CurrentWeather( - val is_day: Int, - val temperature: Double, - val time: String, - val weathercode: Int, - val winddirection: Double, - val windspeed: Double -) \ No newline at end of file diff --git a/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/remote/Hourly.kt b/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/remote/Hourly.kt index c6396402..9beb84b9 100644 --- a/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/remote/Hourly.kt +++ b/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/remote/Hourly.kt @@ -1,8 +1,20 @@ package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.remote +import com.squareup.moshi.Json +import com.squareup.moshi.JsonClass + +@JsonClass(generateAdapter = true) data class Hourly( - val relativehumidity_2m: List, - val temperature_2m: List, + @Json(name = "pressure_msl") + val pressureMsl: List, + @Json(name = "relativehumidity_2m") + val relativehumidity2m: List, + @Json(name = "temperature_2m") + val temperature2m: List, + @Json(name = "time") val time: List, - val windspeed_10m: List + @Json(name = "weathercode") + val weathercode: List, + @Json(name = "windspeed_10m") + val windspeed10m: List ) \ No newline at end of file diff --git a/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/remote/HourlyUnits.kt b/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/remote/HourlyUnits.kt index 86e4bc61..718e798a 100644 --- a/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/remote/HourlyUnits.kt +++ b/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/remote/HourlyUnits.kt @@ -1,8 +1,20 @@ package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.remote +import com.squareup.moshi.Json +import com.squareup.moshi.JsonClass + +@JsonClass(generateAdapter = true) data class HourlyUnits( - val relativehumidity_2m: String, - val temperature_2m: String, + @Json(name = "pressure_msl") + val pressureMsl: String, + @Json(name = "relativehumidity_2m") + val relativehumidity2m: String, + @Json(name = "temperature_2m") + val temperature2m: String, + @Json(name = "time") val time: String, - val windspeed_10m: String + @Json(name = "weathercode") + val weathercode: String, + @Json(name = "windspeed_10m") + val windspeed10m: String ) \ No newline at end of file diff --git a/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/remote/WeatherDataDto.kt b/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/remote/WeatherDataDto.kt deleted file mode 100644 index 9d6345bb..00000000 --- a/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/remote/WeatherDataDto.kt +++ /dev/null @@ -1,17 +0,0 @@ -package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.remote - -import com.squareup.moshi.Json - -data class WeatherDataDto( - val time: List, - @field:Json(name = "temperature_2m") - val temperatures: List, - @field:Json(name = "weathercode") - val weatherCodes: List, - @field:Json(name = "pressure_msl") - val pressures: List, - @field:Json(name = "windspeed_10m") - val windSpeeds: List, - @field:Json(name = "relativehumidity_2m") - val humidities: List -) \ No newline at end of file diff --git a/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/remote/WeatherDto.kt b/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/remote/WeatherDto.kt deleted file mode 100644 index 0eccd227..00000000 --- a/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/remote/WeatherDto.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.remote - -import com.squareup.moshi.Json - -data class WeatherDto( - @field:Json(name = "hourly") - val weatherData: WeatherDataDto -) \ No newline at end of file diff --git a/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/remote/WeatherResponse.kt b/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/remote/WeatherResponse.kt index 150d8001..dcd13bd0 100644 --- a/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/remote/WeatherResponse.kt +++ b/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/data/remote/WeatherResponse.kt @@ -1,14 +1,26 @@ package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.remote +import com.squareup.moshi.Json +import com.squareup.moshi.JsonClass + +@JsonClass(generateAdapter = true) data class WeatherResponse( - val current_weather: CurrentWeather, + @Json(name = "elevation") val elevation: Double, - val generationtime_ms: Double, + @Json(name = "generationtime_ms") + val generationtimeMs: Double, + @Json(name = "hourly") val hourly: Hourly, - val hourly_units: HourlyUnits, + @Json(name = "hourly_units") + val hourlyUnits: HourlyUnits, + @Json(name = "latitude") val latitude: Double, + @Json(name = "longitude") val longitude: Double, + @Json(name = "timezone") val timezone: String, - val timezone_abbreviation: String, - val utc_offset_seconds: Int + @Json(name = "timezone_abbreviation") + val timezoneAbbreviation: String, + @Json(name = "utc_offset_seconds") + val utcOffsetSeconds: Int ) \ No newline at end of file diff --git a/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/di/AppModule.kt b/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/di/AppModule.kt index aec949da..77501af0 100644 --- a/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/di/AppModule.kt +++ b/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/di/AppModule.kt @@ -5,6 +5,8 @@ import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_w import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.remote.WeatherApi import com.google.android.gms.location.FusedLocationProviderClient import com.google.android.gms.location.LocationServices +import com.squareup.moshi.Moshi +import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory import dagger.Module import dagger.Provides import dagger.hilt.InstallIn @@ -23,7 +25,11 @@ object AppModule { fun provideWeatherApi(): WeatherApi { return Retrofit.Builder() .baseUrl(BASE_URL_WEATHER_API) - .addConverterFactory(MoshiConverterFactory.create()) + .addConverterFactory(MoshiConverterFactory.create( + Moshi.Builder() + .add(KotlinJsonAdapterFactory()) + .build() + )) .build() .create() } diff --git a/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/presentation/WeatherCard.kt b/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/presentation/WeatherCard.kt index b86297ff..1ba59fb2 100644 --- a/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/presentation/WeatherCard.kt +++ b/app/src/main/java/com/example/jetpack_compose_all_in_one/android_architectures/clean_code_with_mvi_mvvm/presentation/WeatherCard.kt @@ -38,6 +38,7 @@ fun WeatherCard( Card( modifier = modifier.then(Modifier.padding(dp_15)), shape = RoundedCornerShape(dp_15), + colors = CardDefaults.cardColors(containerColor = backgroundColor), elevation = CardDefaults.cardElevation(dp_15), ) { Column(