-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lê Chí Dũng
committed
Sep 5, 2024
1 parent
fb68f91
commit 2f1c292
Showing
36 changed files
with
532 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
app/src/main/java/com/sun/weather/data/model/WeatherBasic.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.sun.weather.data.model | ||
|
||
import android.os.Parcelable | ||
import kotlinx.parcelize.Parcelize | ||
|
||
@Parcelize | ||
data class WeatherBasic( | ||
var dateTime: Long? = 0, | ||
var currentTemperature: Double? = 0.0, | ||
var maxTemperature: Double? = 0.0, | ||
var minTemperature: Double? = 0.0, | ||
var iconWeather: String? = "", | ||
var weatherDescription: String? = "", | ||
var humidity: Int? = 0, | ||
var percentCloud: Int? = 0, | ||
var windSpeed: Double? = 0.0, | ||
) : Parcelable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
app/src/main/java/com/sun/weather/data/model/entity/WeatherEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.sun.weather.data.model.entity | ||
|
||
import android.os.Parcelable | ||
import androidx.room.ColumnInfo | ||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
import com.sun.weather.data.model.WeatherBasic | ||
import com.sun.weather.data.model.entity.WeatherEntry.CURRENTLY_OBJECT | ||
import com.sun.weather.data.model.entity.WeatherEntry.DAILY_OBJECT | ||
import com.sun.weather.data.model.entity.WeatherEntry.HOURLY_OBJECT | ||
import com.sun.weather.data.model.entity.WeatherEntry.TBL_WEATHER_NAME | ||
import kotlinx.parcelize.Parcelize | ||
|
||
@Entity(tableName = TBL_WEATHER_NAME) | ||
@Parcelize | ||
data class WeatherEntity( | ||
@PrimaryKey | ||
val id: String = "", | ||
val latitude: Double? = 0.0, | ||
val longitude: Double? = 0.0, | ||
val timeZone: Long? = 0, | ||
var city: String? = "", | ||
var country: String? = "", | ||
@ColumnInfo(name = CURRENTLY_OBJECT) | ||
val weatherCurrent: WeatherBasic?, | ||
@ColumnInfo(name = HOURLY_OBJECT) | ||
var weatherHourlyList: List<WeatherBasic>?, | ||
@ColumnInfo(name = DAILY_OBJECT) | ||
var weatherDailyList: List<WeatherBasic>?, | ||
) : Parcelable { | ||
fun getLocation(): String { | ||
return if (!city.isNullOrEmpty() && !country.isNullOrEmpty()) { | ||
"$city, $country" | ||
} else { | ||
"Unknown" | ||
} | ||
} | ||
} | ||
|
||
object WeatherEntry { | ||
// Local database entries | ||
const val TBL_WEATHER_NAME = "weather_forecasts" | ||
const val CURRENTLY_OBJECT = "currently" | ||
const val HOURLY_OBJECT = "hourly" | ||
const val DAILY_OBJECT = "daily" | ||
} |
53 changes: 45 additions & 8 deletions
53
app/src/main/java/com/sun/weather/data/repository/WeatherRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,50 @@ | ||
package com.sun.weather.data.repository.source | ||
package com.sun.weather.data.repository | ||
|
||
import com.sun.weather.data.model.CurrentWeather | ||
import com.sun.weather.data.model.HourlyForecast | ||
import com.sun.weather.data.model.WeeklyForecast | ||
import com.sun.weather.data.model.FavouriteLocation | ||
import com.sun.weather.data.model.Weather | ||
import com.sun.weather.data.model.entity.WeatherEntity | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface WeatherRepository { | ||
fun getCurrentWeather(city: String, language: String): Flow<CurrentWeather> | ||
fun getCurrentLocationWeather(lat: Double, lon: Double, language: String): Flow<CurrentWeather> | ||
fun getHourlyForecast(city: String, language: String): Flow<HourlyForecast> | ||
fun getWeeklyForecast(city: String, language: String): Flow<WeeklyForecast> | ||
fun getSelectedLocation(key: String): String | ||
|
||
fun isFavoriteLocationExists( | ||
cityName: String, | ||
countryName: String, | ||
): Boolean | ||
|
||
fun saveCurrentWeather(currentWeather: WeatherEntity) | ||
|
||
fun saveWeeklyForecastLocal(weeklyForecast: WeatherEntity) | ||
|
||
fun getLocalWeather(id: String): Weather? | ||
|
||
fun saveHourlyForecastLocal(hourlyForecast: WeatherEntity) | ||
|
||
fun insertFavoriteWeather(favouriteLocation: FavouriteLocation) | ||
|
||
fun getAllFavorite(): List<FavouriteLocation> | ||
|
||
fun removeFavoriteItem(id: Long) | ||
|
||
fun getCurrentWeather( | ||
city: String, | ||
language: String, | ||
): Flow<WeatherEntity> | ||
|
||
fun getCurrentLocationWeather( | ||
lat: Double, | ||
lon: Double, | ||
language: String, | ||
): Flow<WeatherEntity> | ||
|
||
fun getHourlyForecast( | ||
city: String, | ||
language: String, | ||
): Flow<WeatherEntity> | ||
|
||
fun getWeeklyForecast( | ||
city: String, | ||
language: String, | ||
): Flow<WeatherEntity> | ||
} |
38 changes: 24 additions & 14 deletions
38
app/src/main/java/com/sun/weather/data/repository/WeatherRepositoryImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,50 @@ | ||
package com.sun.weather.data.repository | ||
|
||
import com.sun.weather.data.model.CurrentWeather | ||
import com.sun.weather.data.model.HourlyForecast | ||
import com.sun.weather.data.model.WeeklyForecast | ||
import com.sun.weather.data.model.entity.WeatherEntity | ||
import com.sun.weather.data.model.toWeatherEntity | ||
import com.sun.weather.data.repository.source.WeatherDataSource | ||
import com.sun.weather.data.repository.source.WeatherRepository | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import org.koin.core.component.KoinComponent | ||
|
||
class WeatherRepositoryImpl( | ||
private val localDataSource: WeatherDataSource.Local, | ||
private val remoteDataSource: WeatherDataSource.Remote | ||
private val remoteDataSource: WeatherDataSource.Remote, | ||
) : KoinComponent, WeatherRepository { | ||
|
||
override fun getCurrentWeather(city: String, language: String): Flow<CurrentWeather> { | ||
override fun getCurrentWeather( | ||
city: String, | ||
language: String, | ||
): Flow<WeatherEntity> { | ||
return flow { | ||
emit(remoteDataSource.getCurrentWeather(city, language)) | ||
emit(remoteDataSource.getCurrentWeather(city, language).toWeatherEntity()) | ||
} | ||
} | ||
|
||
override fun getCurrentLocationWeather(lat: Double, lon: Double, language: String): Flow<CurrentWeather> { | ||
override fun getCurrentLocationWeather( | ||
lat: Double, | ||
lon: Double, | ||
language: String, | ||
): Flow<WeatherEntity> { | ||
return flow { | ||
emit(remoteDataSource.getCurrentLocationWeather(lat, lon, language)) | ||
emit(remoteDataSource.getCurrentLocationWeather(lat, lon, language).toWeatherEntity()) | ||
} | ||
} | ||
|
||
override fun getHourlyForecast(city: String, language: String): Flow<HourlyForecast> { | ||
override fun getHourlyForecast( | ||
city: String, | ||
language: String, | ||
): Flow<WeatherEntity> { | ||
return flow { | ||
emit(remoteDataSource.getHourlyForecast(city, language)) | ||
emit(remoteDataSource.getHourlyForecast(city, language).toWeatherEntity()) | ||
} | ||
} | ||
|
||
override fun getWeeklyForecast(city: String, language: String): Flow<WeeklyForecast> { | ||
override fun getWeeklyForecast( | ||
city: String, | ||
language: String, | ||
): Flow<WeatherEntity> { | ||
return flow { | ||
emit(remoteDataSource.getWeeklyForecast(city, language)) | ||
emit(remoteDataSource.getWeeklyForecast(city, language).toWeatherEntity()) | ||
} | ||
} | ||
} |
Oops, something went wrong.