Skip to content

Commit

Permalink
Merge pull request #153 from myofficework000/mvi_design
Browse files Browse the repository at this point in the history
Clean code MVI completed
  • Loading branch information
myofficework000 authored Jun 30, 2023
2 parents 953d082 + 8f7c31f commit bb2e044
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -13,38 +12,42 @@ private data class IndexedWeatherData(
val data: WeatherData
)

fun WeatherDataDto.toWeatherDataMap(): Map<Int, List<WeatherData>> {
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<Int, List<WeatherData>> = 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(
time = LocalDateTime.parse(time, DateTimeFormatter.ISO_DATE_TIME),
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
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<Int>,
val temperature_2m: List<Double>,
@Json(name = "pressure_msl")
val pressureMsl: List<Double>,
@Json(name = "relativehumidity_2m")
val relativehumidity2m: List<Int>,
@Json(name = "temperature_2m")
val temperature2m: List<Double>,
@Json(name = "time")
val time: List<String>,
val windspeed_10m: List<Double>
@Json(name = "weathercode")
val weathercode: List<Int>,
@Json(name = "windspeed_10m")
val windspeed10m: List<Double>
)
Original file line number Diff line number Diff line change
@@ -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
)

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit bb2e044

Please sign in to comment.