diff --git a/app/src/main/java/com/henryhiles/qweather/data/location/DefaultLocationTracker.kt b/app/src/main/java/com/henryhiles/qweather/data/location/DefaultLocationTracker.kt deleted file mode 100644 index 2787021..0000000 --- a/app/src/main/java/com/henryhiles/qweather/data/location/DefaultLocationTracker.kt +++ /dev/null @@ -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() -// } -// } -// } - } -} \ No newline at end of file diff --git a/app/src/main/java/com/henryhiles/qweather/data/remote/WeatherApi.kt b/app/src/main/java/com/henryhiles/qweather/data/remote/WeatherApi.kt deleted file mode 100644 index 112c766..0000000 --- a/app/src/main/java/com/henryhiles/qweather/data/remote/WeatherApi.kt +++ /dev/null @@ -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 -} \ No newline at end of file diff --git a/app/src/main/java/com/henryhiles/qweather/data/repository/WeatherRepositoryImpl.kt b/app/src/main/java/com/henryhiles/qweather/data/repository/WeatherRepositoryImpl.kt deleted file mode 100644 index ee36300..0000000 --- a/app/src/main/java/com/henryhiles/qweather/data/repository/WeatherRepositoryImpl.kt +++ /dev/null @@ -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 { - 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.") - } - } -} \ No newline at end of file diff --git a/app/src/main/java/com/henryhiles/qweather/di/AppModule.kt b/app/src/main/java/com/henryhiles/qweather/di/AppModule.kt index 186fa84..fe4b88d 100644 --- a/app/src/main/java/com/henryhiles/qweather/di/AppModule.kt +++ b/app/src/main/java/com/henryhiles/qweather/di/AppModule.kt @@ -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 diff --git a/app/src/main/java/com/henryhiles/qweather/di/LocationModule.kt b/app/src/main/java/com/henryhiles/qweather/di/LocationModule.kt index eaafa10..9ee1b87 100644 --- a/app/src/main/java/com/henryhiles/qweather/di/LocationModule.kt +++ b/app/src/main/java/com/henryhiles/qweather/di/LocationModule.kt @@ -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) } \ No newline at end of file diff --git a/app/src/main/java/com/henryhiles/qweather/di/RepositoryModule.kt b/app/src/main/java/com/henryhiles/qweather/di/RepositoryModule.kt index ee5e862..7e19a4e 100644 --- a/app/src/main/java/com/henryhiles/qweather/di/RepositoryModule.kt +++ b/app/src/main/java/com/henryhiles/qweather/di/RepositoryModule.kt @@ -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) } \ No newline at end of file diff --git a/app/src/main/java/com/henryhiles/qweather/domain/location/LocationTracker.kt b/app/src/main/java/com/henryhiles/qweather/domain/location/LocationTracker.kt index 5f98184..8aee9fb 100644 --- a/app/src/main/java/com/henryhiles/qweather/domain/location/LocationTracker.kt +++ b/app/src/main/java/com/henryhiles/qweather/domain/location/LocationTracker.kt @@ -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() +// } +// } +// } + } } \ No newline at end of file diff --git a/app/src/main/java/com/henryhiles/qweather/data/mappers/WeatherMappers.kt b/app/src/main/java/com/henryhiles/qweather/domain/mappers/WeatherMappers.kt similarity index 88% rename from app/src/main/java/com/henryhiles/qweather/data/mappers/WeatherMappers.kt rename to app/src/main/java/com/henryhiles/qweather/domain/mappers/WeatherMappers.kt index b7f6aa8..da2d649 100644 --- a/app/src/main/java/com/henryhiles/qweather/data/mappers/WeatherMappers.kt +++ b/app/src/main/java/com/henryhiles/qweather/domain/mappers/WeatherMappers.kt @@ -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 diff --git a/app/src/main/java/com/henryhiles/qweather/domain/remote/WeatherApi.kt b/app/src/main/java/com/henryhiles/qweather/domain/remote/WeatherApi.kt new file mode 100644 index 0000000..4c8b913 --- /dev/null +++ b/app/src/main/java/com/henryhiles/qweather/domain/remote/WeatherApi.kt @@ -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 +} \ No newline at end of file diff --git a/app/src/main/java/com/henryhiles/qweather/data/remote/WeatherDataDto.kt b/app/src/main/java/com/henryhiles/qweather/domain/remote/WeatherDataDto.kt similarity index 90% rename from app/src/main/java/com/henryhiles/qweather/data/remote/WeatherDataDto.kt rename to app/src/main/java/com/henryhiles/qweather/domain/remote/WeatherDataDto.kt index 82982ba..bf1ac31 100644 --- a/app/src/main/java/com/henryhiles/qweather/data/remote/WeatherDataDto.kt +++ b/app/src/main/java/com/henryhiles/qweather/domain/remote/WeatherDataDto.kt @@ -1,4 +1,4 @@ -package com.henryhiles.qweather.data.remote +package com.henryhiles.qweather.domain.remote import com.squareup.moshi.Json diff --git a/app/src/main/java/com/henryhiles/qweather/data/remote/WeatherDto.kt b/app/src/main/java/com/henryhiles/qweather/domain/remote/WeatherDto.kt similarity index 73% rename from app/src/main/java/com/henryhiles/qweather/data/remote/WeatherDto.kt rename to app/src/main/java/com/henryhiles/qweather/domain/remote/WeatherDto.kt index cc977ae..09c1adc 100644 --- a/app/src/main/java/com/henryhiles/qweather/data/remote/WeatherDto.kt +++ b/app/src/main/java/com/henryhiles/qweather/domain/remote/WeatherDto.kt @@ -1,4 +1,4 @@ -package com.henryhiles.qweather.data.remote +package com.henryhiles.qweather.domain.remote import com.squareup.moshi.Json diff --git a/app/src/main/java/com/henryhiles/qweather/domain/repository/WeatherRepository.kt b/app/src/main/java/com/henryhiles/qweather/domain/repository/WeatherRepository.kt index 30cc457..cd19b31 100644 --- a/app/src/main/java/com/henryhiles/qweather/domain/repository/WeatherRepository.kt +++ b/app/src/main/java/com/henryhiles/qweather/domain/repository/WeatherRepository.kt @@ -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 +class WeatherRepository constructor(private val api: WeatherApi) { + suspend fun getWeatherData(lat: Double, long: Double): Resource { + 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.") + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/henryhiles/qweather/domain/util/Resource.kt b/app/src/main/java/com/henryhiles/qweather/domain/util/Resource.kt index adea1b8..a6ade37 100644 --- a/app/src/main/java/com/henryhiles/qweather/domain/util/Resource.kt +++ b/app/src/main/java/com/henryhiles/qweather/domain/util/Resource.kt @@ -1,6 +1,6 @@ package com.henryhiles.qweather.domain.util sealed class Resource(val data: T? = null, val message: String? = null) { - class Success(data: T?): Resource(data) - class Error(message: String, data: T? = null): Resource(data, message) + class Success(data: T?) : Resource(data) + class Error(message: String, data: T? = null) : Resource(data, message) } diff --git a/app/src/main/java/com/henryhiles/qweather/domain/weather/WeatherType.kt b/app/src/main/java/com/henryhiles/qweather/domain/weather/WeatherType.kt index 0e700e2..1a09032 100644 --- a/app/src/main/java/com/henryhiles/qweather/domain/weather/WeatherType.kt +++ b/app/src/main/java/com/henryhiles/qweather/domain/weather/WeatherType.kt @@ -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 diff --git a/app/src/main/java/com/henryhiles/qweather/presentation/components/weather/WeatherCard.kt b/app/src/main/java/com/henryhiles/qweather/presentation/components/weather/WeatherCard.kt index 5d9a277..719697c 100644 --- a/app/src/main/java/com/henryhiles/qweather/presentation/components/weather/WeatherCard.kt +++ b/app/src/main/java/com/henryhiles/qweather/presentation/components/weather/WeatherCard.kt @@ -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 diff --git a/app/src/main/java/com/henryhiles/qweather/presentation/components/weather/WeatherForecast.kt b/app/src/main/java/com/henryhiles/qweather/presentation/components/weather/WeatherForecast.kt index dc0b561..ba1177b 100644 --- a/app/src/main/java/com/henryhiles/qweather/presentation/components/weather/WeatherForecast.kt +++ b/app/src/main/java/com/henryhiles/qweather/presentation/components/weather/WeatherForecast.kt @@ -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 diff --git a/app/src/main/java/com/henryhiles/qweather/presentation/components/weather/WeatherHour.kt b/app/src/main/java/com/henryhiles/qweather/presentation/components/weather/WeatherHour.kt index aa4655b..6c93c81 100644 --- a/app/src/main/java/com/henryhiles/qweather/presentation/components/weather/WeatherHour.kt +++ b/app/src/main/java/com/henryhiles/qweather/presentation/components/weather/WeatherHour.kt @@ -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 diff --git a/app/src/main/java/com/henryhiles/qweather/presentation/tabs/TodayTab.kt b/app/src/main/java/com/henryhiles/qweather/presentation/tabs/TodayTab.kt index bf0b2a9..b029407 100644 --- a/app/src/main/java/com/henryhiles/qweather/presentation/tabs/TodayTab.kt +++ b/app/src/main/java/com/henryhiles/qweather/presentation/tabs/TodayTab.kt @@ -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