remove clean code

This commit is contained in:
Henry Hiles 2023-04-01 14:12:11 -04:00
parent d221968137
commit 7e8f92c446
18 changed files with 123 additions and 116 deletions

View file

@ -1,51 +0,0 @@
package com.henryhiles.qweather.data.location
import android.Manifest
import android.app.Application
import android.content.Context
import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationManager
import androidx.core.content.ContextCompat
import com.henryhiles.qweather.domain.location.LocationTracker
class DefaultLocationTracker constructor(
private val application: Application
) : LocationTracker {
override suspend fun getCurrentLocation(): Location? {
val hasAccessFineLocationPermission = ContextCompat.checkSelfPermission(
application,
Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED
val locationManager =
application.getSystemService(Context.LOCATION_SERVICE) as LocationManager
val isGpsEnabled = locationManager.isProviderEnabled(
LocationManager.GPS_PROVIDER
)
if (!hasAccessFineLocationPermission || !isGpsEnabled) return null
return locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
// return suspendCancellableCoroutine { cont ->
// locationManager.getLastKnownLocation.apply {
// if (isComplete) {
// if (isSuccessful) {
// cont.resume(result)
// } else {
// cont.resume(null)
// }
// return@suspendCancellableCoroutine
// }
// addOnSuccessListener {
// cont.resume(it)
// }
// addOnFailureListener {
// cont.resume(null)
// }
// addOnCanceledListener {
// cont.cancel()
// }
// }
// }
}
}

View file

@ -1,12 +0,0 @@
package com.henryhiles.qweather.data.remote
import retrofit2.http.GET
import retrofit2.http.Query
interface WeatherApi {
@GET("v1/forecast?hourly=temperature_2m,weathercode,relativehumidity_2m,windspeed_10m,pressure_msl")
suspend fun getWeatherData(
@Query("latitude") lat: Double,
@Query("longitude") long: Double
): WeatherDto
}

View file

@ -1,18 +0,0 @@
package com.henryhiles.qweather.data.repository
import com.henryhiles.qweather.data.mappers.toWeatherInfo
import com.henryhiles.qweather.data.remote.WeatherApi
import com.henryhiles.qweather.domain.repository.WeatherRepository
import com.henryhiles.qweather.domain.util.Resource
import com.henryhiles.qweather.domain.weather.WeatherInfo
class WeatherRepositoryImpl constructor(private val api: WeatherApi) : WeatherRepository {
override suspend fun getWeatherData(lat: Double, long: Double): Resource<WeatherInfo> {
return try {
Resource.Success(data = api.getWeatherData(lat = lat, long = long).toWeatherInfo())
} catch (e: Exception) {
e.printStackTrace()
Resource.Error(e.message ?: "An unknown error occurred.")
}
}
}

View file

@ -1,6 +1,6 @@
package com.henryhiles.qweather.di
import com.henryhiles.qweather.data.remote.WeatherApi
import com.henryhiles.qweather.domain.remote.WeatherApi
import com.henryhiles.qweather.presentation.screenmodel.AppearanceSettingsScreenModel
import com.henryhiles.qweather.presentation.screenmodel.PreferenceManager
import com.henryhiles.qweather.presentation.screenmodel.WeatherScreenModel

View file

@ -1,9 +1,7 @@
package com.henryhiles.qweather.di
import com.henryhiles.qweather.data.location.DefaultLocationTracker
import com.henryhiles.qweather.domain.location.LocationTracker
import org.koin.core.module.dsl.singleOf
import org.koin.dsl.bind
import org.koin.dsl.module
//@Module
@ -16,5 +14,5 @@ import org.koin.dsl.module
val locationModule = module {
singleOf(::DefaultLocationTracker) bind LocationTracker::class
singleOf(::LocationTracker)
}

View file

@ -1,9 +1,7 @@
package com.henryhiles.qweather.di
import com.henryhiles.qweather.data.repository.WeatherRepositoryImpl
import com.henryhiles.qweather.domain.repository.WeatherRepository
import org.koin.core.module.dsl.singleOf
import org.koin.dsl.bind
import org.koin.dsl.module
//@Module
@ -15,5 +13,5 @@ import org.koin.dsl.module
//}
val repositoryModule = module {
singleOf(::WeatherRepositoryImpl) bind WeatherRepository::class
singleOf(::WeatherRepository)
}

View file

@ -1,7 +1,50 @@
package com.henryhiles.qweather.domain.location
import android.Manifest
import android.app.Application
import android.content.Context
import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationManager
import androidx.core.content.ContextCompat
interface LocationTracker {
suspend fun getCurrentLocation(): Location?
class LocationTracker constructor(
private val application: Application
) {
fun getCurrentLocation(): Location? {
val hasAccessFineLocationPermission = ContextCompat.checkSelfPermission(
application,
Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED
val locationManager =
application.getSystemService(Context.LOCATION_SERVICE) as LocationManager
val isGpsEnabled = locationManager.isProviderEnabled(
LocationManager.GPS_PROVIDER
)
if (!hasAccessFineLocationPermission || !isGpsEnabled) return null
return locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
// return suspendCancellableCoroutine { cont ->
// locationManager.getLastKnownLocation.apply {
// if (isComplete) {
// if (isSuccessful) {
// cont.resume(result)
// } else {
// cont.resume(null)
// }
// return@suspendCancellableCoroutine
// }
// addOnSuccessListener {
// cont.resume(it)
// }
// addOnFailureListener {
// cont.resume(null)
// }
// addOnCanceledListener {
// cont.cancel()
// }
// }
// }
}
}

View file

@ -1,7 +1,7 @@
package com.henryhiles.qweather.data.mappers
package com.henryhiles.qweather.domain.mappers
import com.henryhiles.qweather.data.remote.WeatherDataDto
import com.henryhiles.qweather.data.remote.WeatherDto
import com.henryhiles.qweather.domain.remote.WeatherDataDto
import com.henryhiles.qweather.domain.remote.WeatherDto
import com.henryhiles.qweather.domain.weather.WeatherData
import com.henryhiles.qweather.domain.weather.WeatherInfo
import com.henryhiles.qweather.domain.weather.WeatherType

View file

@ -0,0 +1,12 @@
package com.henryhiles.qweather.domain.remote
import retrofit2.http.GET
import retrofit2.http.Query
interface WeatherApi {
@GET("v1/forecast?latitude=43.72&longitude=-79.35&hourly=temperature_2m&daily=weathercode,temperature_2m_max,temperature_2m_min,apparent_temperature_max,apparent_temperature_min&timezone=auto")
suspend fun getWeatherData(
@Query("latitude") lat: Double,
@Query("longitude") long: Double
): WeatherDto
}

View file

@ -1,4 +1,4 @@
package com.henryhiles.qweather.data.remote
package com.henryhiles.qweather.domain.remote
import com.squareup.moshi.Json

View file

@ -1,4 +1,4 @@
package com.henryhiles.qweather.data.remote
package com.henryhiles.qweather.domain.remote
import com.squareup.moshi.Json

View file

@ -1,8 +1,17 @@
package com.henryhiles.qweather.domain.repository
import com.henryhiles.qweather.domain.mappers.toWeatherInfo
import com.henryhiles.qweather.domain.remote.WeatherApi
import com.henryhiles.qweather.domain.util.Resource
import com.henryhiles.qweather.domain.weather.WeatherInfo
interface WeatherRepository {
suspend fun getWeatherData(lat: Double, long: Double): Resource<WeatherInfo>
class WeatherRepository constructor(private val api: WeatherApi) {
suspend fun getWeatherData(lat: Double, long: Double): Resource<WeatherInfo> {
return try {
Resource.Success(data = api.getWeatherData(lat = lat, long = long).toWeatherInfo())
} catch (e: Exception) {
e.printStackTrace()
Resource.Error(e.message ?: "An unknown error occurred.")
}
}
}

View file

@ -1,6 +1,6 @@
package com.henryhiles.qweather.domain.util
sealed class Resource<T>(val data: T? = null, val message: String? = null) {
class Success<T>(data: T?): Resource<T>(data)
class Error<T>(message: String, data: T? = null): Resource<T>(data, message)
class Success<T>(data: T?) : Resource<T>(data)
class Error<T>(message: String, data: T? = null) : Resource<T>(data, message)
}

View file

@ -11,114 +11,140 @@ sealed class WeatherType(
weatherDesc = "Clear sky",
iconRes = R.drawable.ic_sunny
)
object MainlyClear : WeatherType(
weatherDesc = "Mainly clear",
iconRes = R.drawable.ic_cloudy
)
object PartlyCloudy : WeatherType(
weatherDesc = "Partly cloudy",
iconRes = R.drawable.ic_cloudy
)
object Overcast : WeatherType(
weatherDesc = "Overcast",
iconRes = R.drawable.ic_cloudy
)
object Foggy : WeatherType(
weatherDesc = "Foggy",
iconRes = R.drawable.ic_very_cloudy
)
object DepositingRimeFog : WeatherType(
weatherDesc = "Depositing rime fog",
iconRes = R.drawable.ic_very_cloudy
)
object LightDrizzle : WeatherType(
weatherDesc = "Light drizzle",
iconRes = R.drawable.ic_rainshower
)
object ModerateDrizzle : WeatherType(
weatherDesc = "Moderate drizzle",
iconRes = R.drawable.ic_rainshower
)
object DenseDrizzle : WeatherType(
weatherDesc = "Dense drizzle",
iconRes = R.drawable.ic_rainshower
)
object LightFreezingDrizzle : WeatherType(
weatherDesc = "Slight freezing drizzle",
iconRes = R.drawable.ic_snowyrainy
)
object DenseFreezingDrizzle : WeatherType(
weatherDesc = "Dense freezing drizzle",
iconRes = R.drawable.ic_snowyrainy
)
object SlightRain : WeatherType(
weatherDesc = "Slight rain",
iconRes = R.drawable.ic_rainy
)
object ModerateRain : WeatherType(
weatherDesc = "Rainy",
iconRes = R.drawable.ic_rainy
)
object HeavyRain : WeatherType(
weatherDesc = "Heavy rain",
iconRes = R.drawable.ic_rainy
)
object HeavyFreezingRain: WeatherType(
object HeavyFreezingRain : WeatherType(
weatherDesc = "Heavy freezing rain",
iconRes = R.drawable.ic_snowyrainy
)
object SlightSnowFall: WeatherType(
object SlightSnowFall : WeatherType(
weatherDesc = "Slight snow fall",
iconRes = R.drawable.ic_snowy
)
object ModerateSnowFall: WeatherType(
object ModerateSnowFall : WeatherType(
weatherDesc = "Moderate snow fall",
iconRes = R.drawable.ic_heavysnow
)
object HeavySnowFall: WeatherType(
object HeavySnowFall : WeatherType(
weatherDesc = "Heavy snow fall",
iconRes = R.drawable.ic_heavysnow
)
object SnowGrains: WeatherType(
object SnowGrains : WeatherType(
weatherDesc = "Snow grains",
iconRes = R.drawable.ic_heavysnow
)
object SlightRainShowers: WeatherType(
object SlightRainShowers : WeatherType(
weatherDesc = "Slight rain showers",
iconRes = R.drawable.ic_rainshower
)
object ModerateRainShowers: WeatherType(
object ModerateRainShowers : WeatherType(
weatherDesc = "Moderate rain showers",
iconRes = R.drawable.ic_rainshower
)
object ViolentRainShowers: WeatherType(
object ViolentRainShowers : WeatherType(
weatherDesc = "Violent rain showers",
iconRes = R.drawable.ic_rainshower
)
object SlightSnowShowers: WeatherType(
object SlightSnowShowers : WeatherType(
weatherDesc = "Light snow showers",
iconRes = R.drawable.ic_snowy
)
object HeavySnowShowers: WeatherType(
object HeavySnowShowers : WeatherType(
weatherDesc = "Heavy snow showers",
iconRes = R.drawable.ic_snowy
)
object ModerateThunderstorm: WeatherType(
object ModerateThunderstorm : WeatherType(
weatherDesc = "Moderate thunderstorm",
iconRes = R.drawable.ic_thunder
)
object SlightHailThunderstorm: WeatherType(
object SlightHailThunderstorm : WeatherType(
weatherDesc = "Thunderstorm with slight hail",
iconRes = R.drawable.ic_rainythunder
)
object HeavyHailThunderstorm: WeatherType(
object HeavyHailThunderstorm : WeatherType(
weatherDesc = "Thunderstorm with heavy hail",
iconRes = R.drawable.ic_rainythunder
)
companion object {
fun fromWMO(code: Int): WeatherType {
return when(code) {
return when (code) {
0 -> ClearSky
1 -> MainlyClear
2 -> PartlyCloudy

View file

@ -1,4 +1,4 @@
package com.henryhiles.qweather.presentation.components
package com.henryhiles.qweather.presentation.components.weather
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
@ -16,6 +16,7 @@ import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.henryhiles.qweather.R
import com.henryhiles.qweather.presentation.components.WeatherDataDisplay
import com.henryhiles.qweather.presentation.screenmodel.WeatherState
import java.time.format.DateTimeFormatter
import kotlin.math.roundToInt

View file

@ -10,6 +10,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.henryhiles.qweather.presentation.components.weather.WeatherHour
import com.henryhiles.qweather.presentation.screenmodel.WeatherState
import java.time.LocalDateTime

View file

@ -1,4 +1,4 @@
package com.henryhiles.qweather.presentation.components
package com.henryhiles.qweather.presentation.components.weather
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement

View file

@ -20,7 +20,7 @@ import cafe.adriel.voyager.navigator.tab.TabOptions
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.rememberPermissionState
import com.henryhiles.qweather.R
import com.henryhiles.qweather.presentation.components.WeatherCard
import com.henryhiles.qweather.presentation.components.weather.WeatherCard
import com.henryhiles.qweather.presentation.components.WeatherForecast
import com.henryhiles.qweather.presentation.screenmodel.WeatherScreenModel