-
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
Showing
12 changed files
with
116 additions
and
70 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
16 changes: 0 additions & 16 deletions
16
app/src/main/java/com/github/raylemon/weather/ForecastApp.kt
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
app/src/main/java/com/github/raylemon/weather/data/DBServer.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.github.raylemon.weather.data | ||
|
||
import com.github.raylemon.weather.data.db.DbForecast | ||
import com.github.raylemon.weather.data.domain.ForecastList | ||
import com.github.raylemon.weather.data.mapper.DBMapper | ||
import com.orm.query.Condition | ||
import com.orm.query.Select | ||
|
||
/** | ||
* Created by big04 on 17-03-16. | ||
*/ | ||
class DBServer : ForecastSource { | ||
private val DAY_IN_MILLIS = 1000 * 24 * 60 * 60 | ||
|
||
private fun todayInMilis() = System.currentTimeMillis() * DAY_IN_MILLIS / DAY_IN_MILLIS | ||
|
||
override fun getForecast(cnt: Int, city: String): ForecastList? { | ||
val list = Select.from(DbForecast::class.java) | ||
.where("dt>= ?", arrayOf(todayInMilis().toString())) | ||
.and(Condition("city").like(city)) | ||
.limit(cnt.toString()) | ||
.list() | ||
if (list.size == 0) return null | ||
else return DBMapper.convertToDomain(list) | ||
} | ||
|
||
fun save(forecastList: ForecastList) { | ||
DBMapper.convertFromDomain(forecastList).forEach { it.save() } | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
app/src/main/java/com/github/raylemon/weather/data/ForecastProvider.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,23 @@ | ||
package com.github.raylemon.weather.data | ||
|
||
import com.github.raylemon.weather.data.domain.ForecastList | ||
import org.jetbrains.anko.AnkoLogger | ||
import org.jetbrains.anko.info | ||
import java.util.* | ||
|
||
/** | ||
* Created by big04 on 17-03-16. | ||
*/ | ||
object ForecastProvider : ForecastSource, AnkoLogger { | ||
val sources = arrayOf(DBServer(), JsonServer()) | ||
override fun getForecast(cnt: Int, city: String): ForecastList { | ||
for (source in sources) { | ||
val fore = source.getForecast(cnt, city) | ||
if (fore != null) { | ||
info("data sent from $source") | ||
return fore | ||
} | ||
} | ||
throw NoSuchElementException("No element found !") | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/com/github/raylemon/weather/data/ForecastSource.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,10 @@ | ||
package com.github.raylemon.weather.data | ||
|
||
import com.github.raylemon.weather.data.domain.ForecastList | ||
|
||
/** | ||
* Created by big04 on 17-03-16. | ||
*/ | ||
interface ForecastSource { | ||
fun getForecast(cnt: Int, city: String): ForecastList? | ||
} |
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: 0 additions & 17 deletions
17
app/src/main/java/com/github/raylemon/weather/data/db/CPOrmConfig.kt
This file was deleted.
Oops, something went wrong.
22 changes: 18 additions & 4 deletions
22
app/src/main/java/com/github/raylemon/weather/data/db/db_klasses.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,10 +1,24 @@ | ||
package com.github.raylemon.weather.data.db | ||
|
||
import za.co.cporm.model.CPDefaultRecord | ||
import com.orm.SugarRecord | ||
import com.orm.dsl.Unique | ||
|
||
|
||
/** | ||
* Created by big04 on 17-03-16. | ||
*/ | ||
class ForecastList(var city: String = "", var country: String = "", var weather: List<Forecast> = emptyList()) : CPDefaultRecord<ForecastList>() | ||
class Forecast(var dt: Long = 0L, var desc: String = "", var icon: String = "", var temp: Temperatures = Temperatures(), var pressure: Float = 0f, var humidity: Int = 0) : CPDefaultRecord<Forecast>() | ||
class Temperatures(var day: Float = 0f, var min: Float = 0f, var max: Float = 0f, var night: Float = 0f, var morn: Float = 0f, var eve: Float = 0f) : CPDefaultRecord<Temperatures>() | ||
|
||
class DbForecast( | ||
@Unique var dt: Long = 0L, | ||
var city: String = "", | ||
var country: String = "", | ||
var desc: String = "", | ||
var icon: String = "", | ||
var pressure: Float = 0f, | ||
var humidity: Int = 0, | ||
var day: Float = 0f, | ||
var min: Float = 0f, | ||
var max: Float = 0f, | ||
var night: Float = 0f, | ||
var morn: Float = 0f, | ||
var eve: Float = 0f) : SugarRecord() |
27 changes: 13 additions & 14 deletions
27
app/src/main/java/com/github/raylemon/weather/data/mapper/DBMapper.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,24 +1,23 @@ | ||
package com.github.raylemon.weather.data.mapper | ||
|
||
import com.github.raylemon.weather.data.db.Forecast | ||
import com.github.raylemon.weather.data.db.ForecastList | ||
import com.github.raylemon.weather.data.db.Temperatures | ||
import com.github.raylemon.weather.data.domain.Forecast as DFore | ||
import com.github.raylemon.weather.data.domain.ForecastList as DFList | ||
import com.github.raylemon.weather.data.domain.Temperatures as DTemp | ||
import com.github.raylemon.weather.data.db.DbForecast | ||
import com.github.raylemon.weather.data.domain.Forecast | ||
import com.github.raylemon.weather.data.domain.ForecastList | ||
import com.github.raylemon.weather.data.domain.Temperatures | ||
|
||
/** | ||
* Created by big04 on 17-03-16. | ||
*/ | ||
object DBMapper { | ||
fun convertToDomain(forecastList: ForecastList) = with(forecastList) { DFList(city, country, convertListToDomain(weather)) } | ||
private fun convertListToDomain(list: List<Forecast>) = list.map { convertItemListToDomain(it) } | ||
private fun convertItemListToDomain(forecast: Forecast) = with(forecast) { DFore(dt, desc, icon, convertTempToDomain(temp), pressure, humidity) } | ||
private fun convertTempToDomain(temperatures: Temperatures) = with(temperatures) { DTemp(day, min, max, night, eve, morn) } | ||
fun convertToDomain(weather: List<DbForecast>) = with(weather) { ForecastList(first().city, first().city, convertListToDomain(this)) } | ||
private fun convertListToDomain(list: List<DbForecast>) = list.map { convertItemListToDomain(it) } | ||
private fun convertItemListToDomain(dbForecast: DbForecast) = with(dbForecast) { Forecast(dt, desc, icon, Temperatures(day, min, max, night, eve, morn), pressure, humidity) } | ||
|
||
fun convertFromDomain(forecastList: DFList) = with(forecastList) { ForecastList(city, country, convertListFromDomain(weather)) } | ||
private fun convertListFromDomain(list: List<DFore>) = list.map { convertItemListFromDomain(it) } | ||
private fun convertItemListFromDomain(forecast: DFore) = with(forecast) { Forecast(dt, desc, icon, convertTempFromDomain(temp), pressure, humidity) } | ||
private fun convertTempFromDomain(temperatures: DTemp) = with(temperatures) { Temperatures(day, min, max, night, eve, morn) } | ||
fun convertFromDomain(forecastList: ForecastList) = with(forecastList) { | ||
convertListFromDomain(weather, city, country) | ||
} | ||
|
||
private fun convertListFromDomain(list: List<Forecast>, city: String, country: String) = list.map { convertItemListFromDomain(it, city, country) } | ||
private fun convertItemListFromDomain(forecast: Forecast, city: String, country: String) = with(forecast) { DbForecast(dt, city, country, desc, icon, pressure, humidity, temp.day, temp.min, temp.max, temp.night, temp.morn, temp.eve) } | ||
|
||
} |
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