-
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 4, 2024
1 parent
fb68f91
commit 049529d
Showing
17 changed files
with
304 additions
and
64 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
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
47 changes: 47 additions & 0 deletions
47
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,47 @@ | ||
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" | ||
} |
29 changes: 21 additions & 8 deletions
29
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,26 @@ | ||
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> | ||
} |
22 changes: 6 additions & 16 deletions
22
app/src/main/java/com/sun/weather/data/repository/source/WeatherDataSource.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
31 changes: 31 additions & 0 deletions
31
app/src/main/java/com/sun/weather/data/repository/source/local/AppDatabase.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,31 @@ | ||
package com.sun.weather.data.repository.source.local | ||
|
||
import android.content.Context | ||
import androidx.room.Database | ||
import androidx.room.Room | ||
import androidx.room.RoomDatabase | ||
import androidx.room.TypeConverters | ||
import com.sun.weather.data.model.Weather | ||
import com.sun.weather.data.repository.source.local.converter.WeatherBasicConverter | ||
import com.sun.weather.data.repository.source.local.converter.WeatherBasicListConverter | ||
import com.sun.weather.data.repository.source.local.dao.WeatherDao | ||
|
||
@Database(entities = [Weather::class], version = 1, exportSchema = false) | ||
@TypeConverters(WeatherBasicConverter::class, WeatherBasicListConverter::class) | ||
abstract class AppDatabase : RoomDatabase() { | ||
abstract fun weatherDao(): WeatherDao | ||
companion object { | ||
private const val DB_NAME = "Weather.db" | ||
@Volatile | ||
private var INSTANCE: AppDatabase? = null | ||
fun getInstance(context: Context) = INSTANCE ?: synchronized(this) { | ||
INSTANCE ?: buildDatabase(context).also { INSTANCE = it } | ||
} | ||
private fun buildDatabase(context: Context) = | ||
Room.databaseBuilder( | ||
context.applicationContext, | ||
AppDatabase::class.java, | ||
DB_NAME | ||
).build() | ||
} | ||
} |
49 changes: 12 additions & 37 deletions
49
app/src/main/java/com/sun/weather/data/repository/source/local/WeatherLocalDataSource.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,55 +1,30 @@ | ||
package com.sun.weather.data.repository.source.local | ||
|
||
import com.sun.weather.data.model.CurrentWeather | ||
import com.sun.weather.data.model.FavouriteLocation | ||
import com.sun.weather.data.model.HourlyForecast | ||
import com.sun.weather.data.model.WeeklyForecast | ||
import com.sun.weather.data.model.Weather | ||
import com.sun.weather.data.repository.source.WeatherDataSource | ||
import com.sun.weather.data.repository.source.local.dao.WeatherDao | ||
|
||
class WeatherLocalDataSource( private val weatherDao: WeatherDao): WeatherDataSource.Local { | ||
override suspend fun getSelectedLocation(key: String): String { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override suspend fun isFavoriteLocationExists(cityName: String, countryName: String): Boolean { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override suspend fun getCurrentWeatherLocal() { | ||
TODO("Not yet implemented") | ||
} | ||
class WeatherLocalDataSource( | ||
private val weatherDao: WeatherDao | ||
) : WeatherDataSource.Local{ | ||
|
||
override suspend fun saveCurrentWeather(currentWeather: CurrentWeather) { | ||
TODO("Not yet implemented") | ||
} | ||
override suspend fun insertCurrentWeather(current: Weather, hourly: Weather, daily: Weather) { | ||
|
||
override suspend fun getWeeklyForecastLocal(city: String) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override suspend fun saveWeeklyForecastLocal(weeklyForecast: WeeklyForecast) { | ||
TODO("Not yet implemented") | ||
} | ||
override suspend fun insertCurrentWeather(weather: Weather) { | ||
|
||
override suspend fun getHourlyForecastLocal(city: String) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override suspend fun saveHourlyForecastLocal(hourlyForecast: HourlyForecast) { | ||
TODO("Not yet implemented") | ||
override suspend fun getAllLocalWeathers(): List<Weather> { | ||
return weatherDao.getAllData() | ||
} | ||
|
||
override suspend fun insertFavoriteWeather(favouriteLocation: FavouriteLocation) { | ||
TODO("Not yet implemented") | ||
override suspend fun getLocalWeather(id: String): Weather? { | ||
return weatherDao.getWeather(id) | ||
} | ||
|
||
override suspend fun getAllFavorite(): List<FavouriteLocation> { | ||
TODO("Not yet implemented") | ||
override suspend fun deleteWeather(id: String) { | ||
weatherDao.deleteWeather(id) | ||
} | ||
|
||
override suspend fun removeFavoriteItem(id: Long) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
} |
Oops, something went wrong.